lordmilko / PrtgAPI

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

Set-ChannelProperty - Problem with decimal channel limits #48

Closed fabiozanella closed 5 years ago

fabiozanella commented 5 years ago

Hi LordMilko, I’m not able to set decimal/double value in channel limits: Here an example:

$SensorId = 12345

$channelName = "ChannelName"
$upper_error_limit = [double] 0.02
$upper_warning_limit = [double] 0.01
$error_limit_message = "Test error message"
$warning_limit_message = "Test warning message"

$CreatedSensor = Get-Sensor -Id $SensorId

$CreatedSensor | Get-Channel -Name $channelName | Set-ChannelProperty -LimitsEnabled 1 -UpperErrorLimit $upper_error_limit -UpperWarningLimit $upper_warning_limit -ErrorLimitMessage $error_limit_message -WarningLimitMessage $warning_limit_message

The result is that the limit message is set properly, but the limit value is set to 0.

Thanks in advance, Fabio.

lordmilko commented 5 years ago

Hi @fabiozanella,

Can you please invoke Set-ChannelProperty with -Verbose and advise what the output is? (after stripping out your server address, username and passhash)

When I do that I get the following

PS C:\> Get-Sensor *disk* | Get-Channel -Id 1 | Set-ChannelProperty -UpperErrorLimit 0.1 -verbose

VERBOSE: Performing the operation "Set-ChannelProperty Unit = ''" on target "'Disk Time %' (Channel ID: 1, Sensor ID: 2065)".
VERBOSE: Set-ChannelProperty: Synchronously executing request
http://prtg.example.com/editsettings?id=2065&limitmaxerror_1=0.1&limitmode_1=1&username=prtgadmin&passhash=12345678
PS C:\> Get-Sensor *disk* | Get-Channel -Id 1

Name                      SensorId     Id     LastValue LimitsEnabled UpperErrorLimit LowerErrorLimit ErrorLimitMessage
----                      --------     --     --------- ------------- --------------- --------------- -----------------
Disk Time %               2065          1          <1 %          True             0.1           -2000 Channel is in ...
lordmilko commented 5 years ago

Does your computer's number format differ from that of your PRTG server?

The wiki includes the following note

When entering decimal numbers with Set-ChannelProperty it is important the number format matches that of the PRTG Server (determined by your PRTG server language); i.e. if PRTG uses commas to represent decimal places, you too must use commas to represent decimal places. If the correct number format is not used, PRTG will likely truncate the specified value, leading to undesirable results. As PowerShell treats commas as arrays, it is important to indicate to PowerShell your value is not an array, such as by representing it as a string.

PrtgAPI does not know anything about your PRTG server's number format. Instead, it relies on .NET to serialize decimal values appropriately based on your current culture. i.e.

If there is a mismatch between the number format of your operating system's language and the number format used by PRTG (e.g. a German copy of Windows connecting to an English PRTG server) PRTG won't accept the number format you specify (you've basically given it a "string" rather than a number), resulting in the value failing to be set correctly.

I don't believe there is any good solution to fix this for Set-ChannelProperty other than recommending users ensure their languages is consistent across both Windows and PRTG. For users that do need to interface with PRTG servers that utilize different languages than that of their operating system, you can potentially work around this issue by using Set-ObjectProperty instead and specifying a set of raw string parameters

# Set-ChannelProperty not working from German Windows -> US PRTG
PS C:\> Get-Sensor *disk* | Get-Channel -Id 1 | Set-ChannelProperty -UpperErrorLimit 0.2 -verbose
VERBOSE: Performing the operation "Set-ChannelProperty Unit = ''" on target "'Disk Time %' (Channel ID: 1, Sensor ID:
2065)".
VERBOSE: Set-ChannelProperty: Synchronously executing request
http://prtg.example.com/editsettings?id=2065&limitmaxerror_1=0%2C2&limitmode_1=1&username=prtgadmin&passhash=12345678
# Emulating Set-ChannelProperty via Set-ObjectProperty + raw parameters
PS C:\> Get-Sensor *disk* | Set-ObjectProperty -RawParameters @{limitmaxerror_1="0.1";limitmode_1=1}
 -verbose -force
VERBOSE: Performing the operation "Set-ObjectProperty limitmode_1 = '1', limitmaxerror_1 = '0.1'" on target "Disk IO 0
C: (Channel Sensor) (ID: 2065)".
VERBOSE: Set-ObjectProperty: Synchronously executing request
http://prtg.example.com/editsettings?id=2065&limitmode_1=1&limitmaxerror_1=0.1&username=prtgadmin&passhash=12345678

Because raw parameters allows us to specify a string, we can totally bypass the number formatting issues and insert the value we know is correct

fabiozanella commented 5 years ago

Thank you very much! If I change local regional setting it works! Otherwise works calling directly the command:

$CreatedSensor | Set-ObjectProperty -RawParameters @{limitmode_2=1;limitmaxerror_2="0.02";limitmaxwarning_2="0.01";limiterrormsg_2="Test error message 4";limitwarningmsg_2="Test warning message 4"} -Force

Or


$SensorId = 12345

$channelName = "ChannelName"
$upper_error_limit = [string]"2.02"
$upper_warning_limit = [string]"1.01"
$error_limit_message = "Test error message"
$warning_limit_message = "Test warning message"

$CreatedSensor = Get-Sensor -Id $SensorId

$CreatedSensor | Set-ObjectProperty -RawParameters @{limitmode_2=1;limitmaxerror_2=[string]$upper_error_limit;limitmaxwarning_2=[string]$upper_warning_limit;limiterrormsg_2=$error_limit_message;limitwarningmsg_2=$warning_limit_message} -Force

Best regards, Fabio.

fabiozanella commented 5 years ago

Or


$SensorId = 12345

$channelName = "ChannelName"
$upper_error_limit = [string]"2.02"
$upper_warning_limit = [string]"1.01"
$error_limit_message = "Test error message"
$warning_limit_message = "Test warning message"

$CreatedSensor = Get-Sensor -Id $SensorId

$ChannelId = 2

$CreatedSensor | Set-ObjectProperty -RawParameters @{"limitmode_$ChannelId"=1;"limitmaxerror_$ChannelId"=[string]$upper_error_limit;"limitmaxwarning_$ChannelId"=[string]$upper_warning_limit;"limiterrormsg_$ChannelId"=$error_limit_message;"limitwarningmsg_$ChannelId"=$warning_limit_message} -Force