lordmilko / PrtgAPI

C#/PowerShell interface for PRTG Network Monitor
MIT License
301 stars 37 forks source link

How to retrieve objects with "Only use the triggers defined above" enabled and no overriding triggers defined #292

Closed masterautomation closed 2 years ago

masterautomation commented 2 years ago

What's going on?

During maintenance we routinely switch to "Only use the triggers defined above" on Probes/Groups/Devices/Sensors as way to continue recording data but prevent notifications from going out (nearly all of our objects have no overriding triggers defined "above"). I'm looking for a way to audit our PRTG instance to look for situations where we switched to "Only use the triggers defined above" and forgot to switch it back. I tried using the below PowerShell command to find what I'm looking for, but it returns only instances where a trigger is defined and the action is "...perform no notification". It also returns instances where "Only use the triggers defined above" is NOT selected, since it's querying trigger objects only.

Get-Group | Get-Trigger -Inherited $false -OnNotificationAction None

Is what I'm trying to do achievable with PrtgAPI?

Due Dilligance

lordmilko commented 2 years ago

The property you're looking for is InheritTriggers

Get-Group | Get-ObjectProperty | select -expand InheritTriggers

This property is also found under sensor/device/group/probe object's NotificationTypes.InheritTriggers property (but only on English versions of PRTG)

masterautomation commented 2 years ago

When I run that command I get an error: "select : Property "InheritTriggers" cannot be found." Also, how can I access 'NotificationTypes.InheritTriggers'? Thank you very much for your help with this!

lordmilko commented 2 years ago

Apologies, it looks like you can only set this property via Set-ObjectProperty; the only way to retrieve it is via NotificationTypes.InheritTriggers

$notInherited = Get-Group | where { !$_.NotificationTypes.InheritTriggers }
masterautomation commented 2 years ago

Sweet! That looks like it returns groups with the "Only use the triggers defined above" option enabled, so that part is correct. How can I further filter that result to groups that also do not have any non-inherited triggers applied to them?

lordmilko commented 2 years ago

I'm a bit confused by the double negative in your query however it sounds like you then want to do Get-Trigger against each Group object, possibly also specifying -Inherited:$false

$groupsThatDontInherit = Get-Group | where { !$_.NotificationTypes.InheritTriggers }

# Specifying -Inherited:$false here is probably redundant, but it doesn't hurt to be explicit
$directlyAppliedTriggers = $groupsThatDontInherit | Get-Trigger -Inherited:$false
masterautomation commented 2 years ago

When I use Get-Trigger, it only returns instances of triggers when they exist and does not return anything when they are NULL. The Get-Group | where { !$_.NotificationTypes.InheritTriggers } is correct in that it returns Groups as its results. I want to return Groups that have the "Only use the triggers defined above" option enabled, but ONLY if they do not have any explicit triggers defined. I've attached a screenshot as an example. prtg_example

lordmilko commented 2 years ago

So it sounds like you want

$groupsThatDontInheritAndDontHaveDirectTriggers = Get-Group | where { !$_.NotificationTypes.InheritTriggers -and $_.NotificationTypes.TotalTriggers -eq 0 }

Another (less performant) way of doing the same thing is to do

# Again, you probably don't need to specify -Inherited:$false
$groupsThatDontInheritAndDontHaveDirectTriggers = Get-Group | where { !$_.NotificationTypes.InheritTriggers -and !($_ | Get-Trigger -Inherited:$false) }
masterautomation commented 2 years ago

That did it! You are a genius! Thank you very much for your help and for PrtgAPI. It is an extremely useful tool!