diff --git a/lib/core/tools/ConvertTo-Integer.psm1 b/lib/core/tools/ConvertTo-Integer.psm1 new file mode 100644 index 0000000..8c195b8 --- /dev/null +++ b/lib/core/tools/ConvertTo-Integer.psm1 @@ -0,0 +1,65 @@ +<# +.SYNOPSIS + Helper function to convert values to integer if possible +.DESCRIPTION + Converts an input value to integer if possible in any way. Otherwise it will return the object unmodified + + More Information on https://github.com/Icinga/icinga-powershell-framework +.FUNCTIONALITY + Converts an input value to integer if possible in any way. Otherwise it will return the object unmodified +.PARAMETER Value + Any value/object is analysed and if possible converted to an integer +.INPUTS + System.Object +.OUTPUTS + System.Integer + +.LINK + https://github.com/Icinga/icinga-powershell-framework +.NOTES +#> + +function ConvertTo-Integer() +{ + param ( + $Value, + [switch]$NullAsEmpty + ); + + if ($null -eq $Value) { + if ($NullAsEmpty) { + return ''; + } + + return 0; + } + + if ([string]::IsNullOrEmpty($Value)) { + if ($NullAsEmpty) { + return ''; + } + + return 0; + } + + if ((Test-Numeric $Value)) { + return $Value; + } + + $Type = $value.GetType().Name; + + if ($Type -eq 'GpoBoolean' -Or $Type -eq 'Boolean' -Or $Type -eq 'SwitchParameter') { + return [int]$Value; + } + + if ($Type -eq 'String') { + if ($Value.ToLower() -eq 'true' -Or $Value.ToLower() -eq 'yes' -Or $Value.ToLower() -eq 'y') { + return 1; + } + if ($Value.ToLower() -eq 'false' -Or $Value.ToLower() -eq 'no' -Or $Value.ToLower() -eq 'n') { + return 0; + } + } + + return $Value; +} diff --git a/lib/icinga/plugin/New-IcingaCheck.psm1 b/lib/icinga/plugin/New-IcingaCheck.psm1 index 89d8bf7..b7be7a9 100644 --- a/lib/icinga/plugin/New-IcingaCheck.psm1 +++ b/lib/icinga/plugin/New-IcingaCheck.psm1 @@ -147,6 +147,8 @@ function New-IcingaCheck() ); } + $this.warning = $warning; + return $this; } @@ -161,6 +163,8 @@ function New-IcingaCheck() ); } + $this.warning = $warning; + return $this; } @@ -175,6 +179,8 @@ function New-IcingaCheck() ); } + $this.warning = $warning; + return $this; } @@ -189,6 +195,8 @@ function New-IcingaCheck() ); } + $this.warning = $warning; + return $this; } @@ -203,6 +211,8 @@ function New-IcingaCheck() ); } + $this.warning = [string]::Format('{0}:{1}', $min, $max); + return $this; } @@ -217,6 +227,8 @@ function New-IcingaCheck() ); } + $this.warning = [string]::Format('{0}:{1}', $min, $max); + return $this; } @@ -231,6 +243,8 @@ function New-IcingaCheck() ); } + $this.warning = $warning; + return $this; } @@ -245,6 +259,8 @@ function New-IcingaCheck() ); } + $this.warning = $warning; + return $this; } @@ -259,6 +275,8 @@ function New-IcingaCheck() ); } + $this.warning = $warning; + return $this; } @@ -273,6 +291,8 @@ function New-IcingaCheck() ); } + $this.warning = $warning; + return $this; } @@ -329,6 +349,8 @@ function New-IcingaCheck() ); } + $this.critical = $critical; + return $this; } @@ -343,6 +365,8 @@ function New-IcingaCheck() ); } + $this.critical = $critical; + return $this; } @@ -357,6 +381,8 @@ function New-IcingaCheck() ); } + $this.critical = $critical; + return $this; } @@ -371,6 +397,8 @@ function New-IcingaCheck() ); } + $this.critical = $critical; + return $this; } @@ -385,6 +413,8 @@ function New-IcingaCheck() ); } + $this.critical = [string]::Format('{0}:{1}', $min, $max); + return $this; } @@ -399,6 +429,8 @@ function New-IcingaCheck() ); } + $this.critical = [string]::Format('{0}:{1}', $min, $max); + return $this; } @@ -413,6 +445,8 @@ function New-IcingaCheck() ); } + $this.critical = $critical; + return $this; } @@ -427,6 +461,8 @@ function New-IcingaCheck() ); } + $this.critical = $critical; + return $this; } @@ -441,6 +477,8 @@ function New-IcingaCheck() ); } + $this.critical = $critical; + return $this; } @@ -455,6 +493,8 @@ function New-IcingaCheck() ); } + $this.critical = $critical; + return $this; } @@ -721,14 +761,17 @@ function New-IcingaCheck() $this.completed = $TRUE; [string]$LabelName = (Format-IcingaPerfDataLabel $this.name); + $value = ConvertTo-Integer -Value $this.value -NullAsEmpty; + $warning = ConvertTo-Integer -Value $this.warning -NullAsEmpty; + $critical = ConvertTo-Integer -Value $this.critical -NullAsEmpty; $perfdata = @{ 'label' = $LabelName; 'perfdata' = ''; 'unit' = $this.unit; - 'value' = (Format-IcingaPerfDataValue $this.value); - 'warning' = (Format-IcingaPerfDataValue $this.warning); - 'critical' = (Format-IcingaPerfDataValue $this.critical); + 'value' = (Format-IcingaPerfDataValue $value); + 'warning' = (Format-IcingaPerfDataValue $warning); + 'critical' = (Format-IcingaPerfDataValue $critical); 'minimum' = (Format-IcingaPerfDataValue $this.minimum); 'maximum' = (Format-IcingaPerfDataValue $this.maximum); 'package' = $FALSE;