Hi everyone,
I need to modify a business process sensor through API. In that business process sensor we have a business process specific settings section and we don't manage to change object id value for a specific channel name :
In this case, we would like to change #3938 by #4561 for example.
We tried a lot of things such as :
/api/setobjectproperty.htm?id=28596&subtype=channel&subid=1&name=XXX&value=4561
Thanks for your help
Article Comments
Thank you lordmilko, very useful :)
api/getobjectproperty.htm?id=2101&name=businessprocessdefinitions
I didn't succeed to replace values with URL Encode :
editsettings?id=2101&businessprocessdefinitions_=%5B%7B%22objects%22%3A%5B%5D%2C%22warningthreshhold%2
2%3A75%2C%22errorthreshhold%22%3A50%2C%22channelname%22%3A%22channel1%22%7D%2C%7B%22objects%22%3A%5B%5D%2C%22warningthres
hhold%22%3A75%2C%22errorthreshhold%22%3A50%2C%22channelname%22%3A%22newchannel2%22%7D%5D
I use this decoded url :
editsettings?id=4454&businessprocessdefinitions_=[{"objects":[4455],"warningthreshhold":75,"errorthreshhold":50,"channelname":"aa"},{"objects":[4456],"warningthreshhold":75,"errorthreshhold":50,"channelname":"bb"}]
Encoded in this way :
editsettings%3Fid%3D4454%26businessprocessdefinitions_%3D%5B%7B%22objects%22%3A%5B4455%5D%2C%22warningthreshhold%22%3A75%2C%22errorthreshhold%22%3A50%2C%22channelname%22%3A%22aa%22%7D%2C%7B%22objects%22%3A%5B4456%5D%2C%22warningthreshhold%22%3A75%2C%22errorthreshhold%22%3A50%2C%22channelname%22%3A%22bb%22%7D%5D%0A
To use in this way (i'm on PowerShell 6.2) :
Invoke-WebRequest "https://$($properties.server)/api/editsettings%3Fid%3D4454%26businessprocessdefinitions_%3D%5B%7B%22objects%22%3A%5B4455%5D%2C%22warningthreshhold%22%3A75%2C%22errorthreshhold%22%3A50%2C%22channelname%22%3A%22aa%22%7D%2C%7B%22objects%22%3A%5B4456%5D%2C%22warningthreshhold%22%3A75%2C%22errorthreshhold%22%3A50%2C%22channelname%22%3A%22bb%22%7D%5D%0A&username=$($properties.username)&passhash=$token" -SkipCertificateCheck
Always I get an error "Invoke-WebRequest : Unauthorized".
I'm on PowerShell so the module is really interesting :
$sensor = Get-Sensor -Id 2101
$original = $sensor | Get-ObjectProperty -RawProperty businessprocessdefinitions_
$new = $original -replace "someChannelName","newChannelName"
$sensor | Set-ObjectProperty -RawProperty businessprocessdefinitions_ -RawValue $new -Force
But we use a self-signed certificate with HTTP disabled. The module works with a valid SSL certificate or with HTTP but not in HTTPS with a self-signed certificate. Even if I use -IgnoreSSL parameter I get this error :
Connect-PrtgServer : The remote certificate is invalid according to the validation procedure.
I'm going to glance at the module code but I'm not very skilled in C#.
Aug, 2019 - Permalink
If you're using a self-signed certificate you need to specify -IgnoreSSL
You also need to make sure you're connecting to the hostname of the server, not the IP Address
If you've messed with the TLS settings of your PRTG server, you'll also need to change the TLS mode to 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Ultimately, both PrtgAPI and Invoke-WebRequest are using .NET to execute their requests, so once you get it working for one you'll have an idea as to what needs to occur for it to be working for the other, however it's worth noting that PrtgAPI does not yet fully support PowerShell Core, so I would recommend using Windows PowerShell for now
Aug, 2019 - Permalink
"PrtgAPI does not yet fully support PowerShell Core, so I would recommend using Windows PowerShell for now"
- Fair enough, it works on PowerShell 5.1 ! Thanks
On the other hand I can't change business process settings :(
$sensor = Get-Sensor -Id 4454
ID 4454 refers to the business process sensor.
$original = $sensor | Get-ObjectProperty -RawProperty businessprocessdefinitions_
$new = $original -replace '[{"objects":[4455],"warningthreshhold":75,"errorthreshhold":50,"channelname":"aa"},{"objects":[3939],"warningthreshhold":75,"errorthreshhold":50,"channelname":"bb"},{"objects":[3940],"warningthreshhold":75,"errorthreshhold":50,"channelname":"cc"}]'
IDs 4455, 3939, 3940 refer to objects ID (sensors that are used in channels).
$sensor | Set-ObjectProperty -RawProperty businessprocessdefinitions_ -RawValue $new -Force
I've no errors in PowerShell but refreshing PRTG I don't have what I expect, objects ID for each channel name in business process specific settings aren't changed :s any idea ?
Thanks a lot
Aug, 2019 - Permalink
There are two issues with this line
$new = $original -replace '[{"objects":[4455],"warningthreshhold":75,"errorthreshhold":50,"channelname":"aa"},{"objects":[3939],"warningthreshhold":75,"errorthreshhold":50,"channelname":"bb"},{"objects":[3940],"warningthreshhold":75,"errorthreshhold":50,"channelname":"cc"}]'
1. The idea of using -replace is that you simply specify a single known value in the unknown JSON string you want to replace (such as an object ID, channel name, etc). Specifying the whole entire string defeats the purpose
2. Your syntax to the -replace parameter is incorrect. The syntax for using -replace is -replace <original>,<new>, however it appears to me in your line you simply wrote -replace <original> - replace it with what!
I successfully changed the ID of a Business Processes sensor channel from 2058 to 2059 by performing the following
# Get the sensor, and get the original definition C:\> $sensor = get-sensor -id 2101 C:\> $original = $sensor | get-objectproperty -RawProperty businessprocessdefinitions_ # Inspect the original definition for good measure C:\> $original [{"objects":[2058],"warningthreshhold":75,"errorthreshhold":50,"channelname":"test"}] # Update the definition somehow and inspect our changes for good measure C:\> $new = $original -replace 2058,2059 C:\> $new [{"objects":[2059],"warningthreshhold":75,"errorthreshhold":50,"channelname":"test"}] # Update the sensor C:\> $sensor | set-objectproperty -rawproperty businessprocessdefinitions_ -rawvalue $new -force # Inspect the result for good measure C:\> $sensor | get-objectproperty -RawProperty businessprocessdefinitions_ [{"objects":[2059],"warningthreshhold":75,"errorthreshhold":50,"channelname":"test"}]
Regards,
lordmilko
Aug, 2019 - Permalink
Business Process channel definitions are stored by PRTG using JSON under the businessprocessdefinitions property. As such, if you want to modify any part of these definitions the easiest way to do this is to retrieve the existing definition, modify it as required and then resubmit it back to PRTG.
The following API call retrieves the businessprocessdefinitions property of the object with ID 2101
Example output:
You can then modify this as desired, URL encode this and resubmit it back to PRTG via the editsettings endpoint
It's not exactly a smooth process, however it can be relatively straight forward using PowerShell with PrtgAPI
Regards,
lordmilko
Aug, 2019 - Permalink