mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-21 07:10:15 -05:00
parent
10c9add973
commit
d205989007
2 changed files with 111 additions and 3 deletions
65
lib/core/tools/ConvertTo-Integer.psm1
Normal file
65
lib/core/tools/ConvertTo-Integer.psm1
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -147,6 +147,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.warning = $warning;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -161,6 +163,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.warning = $warning;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -175,6 +179,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.warning = $warning;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -189,6 +195,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.warning = $warning;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -203,6 +211,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.warning = [string]::Format('{0}:{1}', $min, $max);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -217,6 +227,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.warning = [string]::Format('{0}:{1}', $min, $max);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -231,6 +243,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.warning = $warning;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -245,6 +259,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.warning = $warning;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -259,6 +275,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.warning = $warning;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -273,6 +291,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.warning = $warning;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -329,6 +349,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.critical = $critical;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -343,6 +365,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.critical = $critical;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -357,6 +381,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.critical = $critical;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -371,6 +397,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.critical = $critical;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -385,6 +413,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.critical = [string]::Format('{0}:{1}', $min, $max);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -399,6 +429,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.critical = [string]::Format('{0}:{1}', $min, $max);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -413,6 +445,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.critical = $critical;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -427,6 +461,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.critical = $critical;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -441,6 +477,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.critical = $critical;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -455,6 +493,8 @@ function New-IcingaCheck()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this.critical = $critical;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -721,14 +761,17 @@ function New-IcingaCheck()
|
||||||
|
|
||||||
$this.completed = $TRUE;
|
$this.completed = $TRUE;
|
||||||
[string]$LabelName = (Format-IcingaPerfDataLabel $this.name);
|
[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 = @{
|
$perfdata = @{
|
||||||
'label' = $LabelName;
|
'label' = $LabelName;
|
||||||
'perfdata' = '';
|
'perfdata' = '';
|
||||||
'unit' = $this.unit;
|
'unit' = $this.unit;
|
||||||
'value' = (Format-IcingaPerfDataValue $this.value);
|
'value' = (Format-IcingaPerfDataValue $value);
|
||||||
'warning' = (Format-IcingaPerfDataValue $this.warning);
|
'warning' = (Format-IcingaPerfDataValue $warning);
|
||||||
'critical' = (Format-IcingaPerfDataValue $this.critical);
|
'critical' = (Format-IcingaPerfDataValue $critical);
|
||||||
'minimum' = (Format-IcingaPerfDataValue $this.minimum);
|
'minimum' = (Format-IcingaPerfDataValue $this.minimum);
|
||||||
'maximum' = (Format-IcingaPerfDataValue $this.maximum);
|
'maximum' = (Format-IcingaPerfDataValue $this.maximum);
|
||||||
'package' = $FALSE;
|
'package' = $FALSE;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue