lordmilko / PrtgAPI

C#/PowerShell interface for PRTG Network Monitor
MIT License
305 stars 38 forks source link

Attempting to add new threshold trigger fails #22

Closed Gladiator10864 closed 6 years ago

Gladiator10864 commented 6 years ago

Good afternoon Lordmilko!

So far I'm loving this. I've been trying to audit and better utilize a PRTG installation with around 10k sensors that had very little organization before hand, this tool has been a huge time saver.

Anyways, I'm trying to add a new threshold notification trigger to all "Disk Free" sensors. I've been using code similar to the below,

$sensers = Get-Device servertest* | Get-Sensor "Disk Free*"

foreach ($s in $sensers) {
    $params = New-TriggerParameters -Id $s.Id -Type Threshold
    $params.Threshold = 10
    $params.Condition = "Below"
    $params.OnNotificationAction = Get-NotificationAction "Email Me test"
    $params | Add-Trigger
}

This does build the $params variable as desired but it never actually adds the trigger,

PS H:\> $params

OffNotificationAction : None
Latency               : 60
Threshold             : 10
Condition             : Below
Channel               : Primary
ObjectId              : 42612
SubId                 : 
Action                : Add
Type                  : Threshold
OnNotificationAction  : Email Me test

I can add state triggers just fine but I've been having issues with adding a threshold. Hopefully you might be able to provide some insight on what I'm doing wrong here?

Thanks.

Edit: Markdown codeblock formatting

lordmilko commented 6 years ago

Hi @Gladiator10864,

The issue is you have not set a Channel for your ThresholdTriggerParameters object, however I'm concerned you're not getting the proper exception

Can you please advise what the output of $ErrorActionPreference is and also advise what version of PrtgAPI you're running

When you execute your script above you should be getting the following exception

Add-Trigger : Channel 'Primary' is not a valid value for sensor with ID 1005. Triggers assigned directly to sensors must refer to a specific Channel or Channel ID.

Can you please run the following and advise what the output is?

get-sensor -tags wmidiskspace* -count 1|foreach { New-TriggerParameters $_.id threshold | Add-Trigger }
lordmilko commented 6 years ago

In addition, can you please advise what the output is when you specify -Verbose to Add-Trigger?

$params | Add-Trigger -Verbose

This will show the exact web requests that are being executed by Add-Trigger, and show how far the request is going. You may want to hide your server name, username and passhash from the outputted response

Gladiator10864 commented 6 years ago

Thanks for the response. I forgot that I had changed my error action preference to SilentlyContinue which is why I was not seeing the exception you posted above. After changing it to "Continue", I now produce the same exception.

PRTG version 18.1.38.11934+ (will be updating to 18.2.41.1652+this weekend)

Running your first command produces the following (notice I changed the tag parameter to snmpdiskfreesesor* which is what we are using instead of wmi sensor):

PS H:\> get-sensor -tags snmpdiskfreesensor* -count 1|foreach { New-TriggerParameters $_.id threshold | Add-Trigger }
Add-Trigger : Channel 'Primary' is not a valid value for sensor with ID 34915. Triggers assigned directly to sensors must refer to a specific Channel or Channel ID.
At line:1 char:97
+ ... unt 1|foreach { New-TriggerParameters $_.id threshold | Add-Trigger }
+                                                             ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-NotificationTrigger], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,PrtgAPI.PowerShell.Cmdlets.AddNotificationTrigger

Verbose of Add-Trigger:

VERBOSE: Performing the operation "Add-NotificationTrigger" on target "Object ID: 42611 (Type: Threshold, Action: Email Me test)".
VERBOSE: Add-NotificationTrigger: Synchronously executing request https://servername.edu/api/table.xml?id=42611&content=triggers&columns=content,objid&username=myUser&passha
sh=myPassHash
VERBOSE: Add-NotificationTrigger: Synchronously executing request https://servername.edu/api/triggers.json?id=42611&username=myUser&passhash=myPassHash
VERBOSE: Add-NotificationTrigger: Synchronously executing request https://servername.edu/api/table.xml?content=sensors&columns=probe,group,favorite,lastvalue,device,downtime,dow
ntimetime,downtimesince,uptime,uptimetime,uptimesince,knowntime,cumsince,lastcheck,lastup,lastdown,minigraph,schedule,basetype,baselink,parentid,notifiesx,interval,intervalx,access,dependency
,position,status,comments,priority,message,type,tags,active,objid,name&count=*&filter_objid=42611&username=myUser&passhash=myPassHash

I now see that it is something with the channel or channel id but I really can't find how to change it. I've tried adding $params.Channel = "Free Space" but get the following exception:

Exception setting "Channel": "Cannot convert value "Free Space" to type "PrtgAPI.TriggerChannel". Error: "Cannot convert 'Free Space' of type 'System.String' to type 'TriggerChannel'. Value 
type must be convertable to one of PrtgAPI.StandardTriggerChannel, PrtgAPI.Channel or System.Int32.""

I know I tried a few format variations to set the channel yesterday as well but no matter what I tried, it would never change from "Primary".

Gladiator10864 commented 6 years ago

Took another stab at this after lunch and finally figured it out using Get-Sensor -Id | Get-Channel to get the channel ID and used that for $params.Channel value.

So my new, working, script looks like this:

$sensers = Get-Device servertest* | Get-Sensor "Disk Free*"

foreach ($s in $sensers) {
    $params = New-TriggerParameters -Id $s.Id -Type Threshold
    $channel = Get-Channel -SensorId $s.Id -Name "Free Space"
    $params.Channel = $channel.Id
    $params.Threshold = 10
    $params.Condition = "Below"
    $params.OnNotificationAction = Get-NotificationAction "Email Me test"
    $params | Add-Trigger
}

I can now run this in across the entire probe and add email notifications to all sensors at once.

Thanks again for helping me work through this!

lordmilko commented 6 years ago

Hi @Gladiator10864,

Good to hear it's all working!

Also note that you can simply do

$params.Channel = $channel

instead of

$params.Channel = $channel.Id

In addition, for performance you may want to consider moving the Get-Channel and Get-NotificationAction calls outside the foreach loop, or otherwise caching them in a variable that only updates if not null; that way you execute two fewer API requests on each iteration, which will greatly help when batching over hundreds of sensors.

Regards, lordmilko