Do you have a sample of how to return all the child groups belonging to a parent group (knowing the parent group object id), using the API directly, in powershell?
I have been through the entire API documentation, and trawled the KB and can't find an example. Some seem close, but I haven't been able to alter them to work.
I have a fairly large and deep group hierarchy i need to manage from ps.
Thank you.
Article Comments
Hi lordmilko,
Thanks for responding. Yes, I saw that when researching the KB. Thats why I said 'using the API directly'. I work in a secure air gapped network and are blocked from installing anything without multiple mgt authorisations, triplicate signoffs, etc, etc.
I think I can see everything I need to do directly with the API, except for walking up and down the groups. If there is a way to do that in powershell then i can build this process, without having to get through all the red tape, to try and convince the client to use prtg.
Thank you.
Aug, 2020 - Permalink
PRTG provides the /api/table.xml?content=sensortree endpoint for retrieving a basic outline of the structure of PRTG. If you want the full information on objects that is returned from API calls like content=groups however you basically need to recurse the tree yourself. In theory, it's simply a matter of recursively iterating over each group that's returned and retrieving any child groups that may be a member of it.
e.g. you first retrieve the child groups of the group 1001, returning groups, 2001 and 2002. You then retrieve the child groups of groups 2001, 2002, and then the child groups of those child groups, and so on. In order to make the recursion easier you would simply wrap your recursion logic up in a function that keeps iteratively calling itself to retrieve more records.
You can kind of get an idea as to how PrtgAPI does it by looking a the source code here.
You can see that we recurse between the methods
1. GetAdditionalGroupRecordsInternal, which then calls
2. ProcessChilrenAndMaybeRecurseGroupRecords ,which then calls
3. MaybeRecurseGroupRecordsInternal, which then descends even deeper into
1. GetAdditionalGroupRecordsInternal again, and it just keeps going deeper until we stop finding child groups
Obviously, in PrtgAPI's case this is wayyy more complicated than it will need to be for you (PrtgAPI's code needs to be modular, and needs to support traversing a group hierarchy for finding sensors, devices or groups).
While PrtgAPI makes writing this sort of logic fairly easy by having deserialized the XML or JSON response to a structured type, you can still achieve the same thing by using the raw API and writing a whole bunch of logic to execute additional API calls as required.
As to why PRTG doesn't support retrieving the entire hierarchy in one go, the answer they gave in a KB post was they think calculating the object tree would have "poor performance" (whether or not that is true or not is up for debate, but either way that's the current situation)
Aug, 2020 - Permalink
Thank you, I understand. I had pulled PrtgAPI and been through the code and already found the functions you discuss. I was also sure there should be a straightforward way in powershell to do the same just for groups, directly with the API. I found the sensortree and got that working for the top level group.
The issue i ran into was, as you mention, 'You then retrieve the child groups..', and hence my original question.
I couldn't work out: 1. how to get all the child groups of a specific group, directly with the API 2. how to then get the id of each of the child groups
Aug, 2020 - Permalink
Utilizing the Invoke-WebRequest example above (but using JSON instead of XML), the following is a working example that shows how to recurse a group hierarchy using PowerShell
function GetGroups($parentId) { $response = Invoke-WebRequest "https://prtg.example.com/api/table.json?content=groups&columns=objid,name,probe,condition,fold,groupnum,devicenum,upsens,downsens,downacksens,partialdownsens,warnsens,pausedsens,unusualsens,undefinedsens,totalsens,schedule,basetype,baselink,notifiesx,intervalx,access,dependency,position,status,comments,priority,message,parentid,tags,type,active&count=*&filter_parentid=$parentId&username=prtgadmin&password=password" ($response.Content | ConvertFrom-Json).groups } function RecurseGroups($parentId) { $groups = GetGroups $parentId foreach($group in $groups) { $group RecurseGroups $group.objid } } RecurseGroups 1001
This will return all descendant groups of the group or probe with ID 1001
Aug, 2020 - Permalink
Thanks lordmilko. I also found a way of accessing PrtgAPI, so are considering switching to that if I run into more issues.
Aug, 2020 - Permalink
Hi lordmilko
Is there anyway to do this is reverse in the PRTGAPI module? Like from a device or a sensor I want to get the hierarchy (parent groups). I see the "group" property on the device object (get-device), but I would then have to get the groups one by one and wondering if there's an easier/better way.
Thanks!
/Splint
Aug, 2022 - Permalink
You would need to recursively retrieve the ParentId of the device and its parents
$parent = Get-Device -Id 1001 while($parent -ne $null) { $parent = Get-Group -Id $parent.ParentId }
To recurse up parents of arbitrary types you would do Get-Object -Id $parent.ParentId -Resolve
Sep, 2022 - Permalink
Hi madaw,
You can easily return the descendants of a group in PowerShell by using PrtgAPI
To retrieve only direct children of a group, you can additionally specify -Recurse:$false
To retrieve the direct children of a group using the raw HTTP API, you would execute a request for groups, specifying a filter_parentid with the ID of the parent group to retrieve children from
Regards,
lordmilko
Aug, 2020 - Permalink