Skip to content

Tree Manipulation

lordmilko edited this page Sep 11, 2021 · 11 revisions

Contents

C#

PrtgAPI provides several mechanisms to access the children of a node. Children can be accessed via the Children member, dynamic or an indexer.

//Get the first child of a tree
var child = deviceNode.Children[0];
//Get the "Ping" child the device with ID 1001
dynamic deviceNode = client.GetTree(1001);
var ping = deviceNode.Ping;
var ping = deviceNode["Ping"];

When indexing into nodes, by default PrtgAPI will perform a case-sensitive lookup for the specified value. If you wish to ignore case, the ignoreCase parameter can be specified

var ping = deviceNode["ping", true];

If no matches are found when indexing into a value, null will be returned. If you are trying to index several levels deep without running into a NullReferenceException you can utilize null-conditional indexers.

var ping = probeNode["dc-1"]?["ping", true];

Nodes contain a number of helper methods that can be used for performing analytics across multiple nodes. The following table illustrates the methods that are available to PrtgNode objects

Method Description Example
Ancestors Gets all ancestor nodes node.Ancestors()
AncestorsAndSelf Gets all ancestor nodes (including this node) node.AncestorsAndSelf()
Contains Gets whether a node is a descendant of another node parent.Contains(descendant)
CompareTo Describes the differences between two trees first.CompareTo(second)
DescendantNodes Gets all descendant nodes node.DescendantNodes()
DescendantNodesAndSelf Gets all descendant nodes (including this node) node.DescendantNodesAndSelf()
FindNode(s) Finds nodes that match a specified predicate node.FindNodes(n => n.Name == "Ping")
InsertNode(s)After Inserts new nodes after another node in a tree node.InsertNodesAfter(descendant, newNodes)
InsertNode(s)Before Inserts new nodes before another node in a tree node.InsertNodesBefore(descendant, newNodes)
PrettyPrint Pretty prints a tree node.PrettyPrint(writer)
RemoveNode(s) Removes nodes from a tree node.RemoveNode(otherNode)
ReplaceNode(s) Replaces nodes in a tree node.ReplaceNode(oldNode, newNode)
WithChildren Updates the children of a node node.WithChildren(newChildren)
With{Object} Updates the object a node encapsulates sensorNode.WithSensor(newSensor)

For information on performing custom analytics on tree nodes, please see Extensibility

PowerShell

Advanced