2020-11-03 07:35:56 -05:00
<#
. SYNOPSIS
Converts any kind of Icinga threshold with provided units
to the lowest base of the unit which makes sense . It does
support the Icinga plugin language , like ~ : 30 , @10 : 40 , 15 : 30 ,
. . .
The conversion does currently support the following units :
Size : B , KB , MB , GB , TB , PT , KiB , MiB , GiB , TiB , PiB
Time : ms , s , m , h , d w , M , y
. DESCRIPTION
Converts any kind of Icinga threshold with provided units
to the lowest base of the unit . It does support the Icinga
plugin language , like ~ : 30 , @10 : 40 , 15 : 30 , . . .
2024-08-19 10:13:24 -04:00
You can also provide date time values in the format of " yyyy/MM/dd HH:mm:ss "
and use Icinga for Windows plugin thresholds in combination . You have to escape
the ':' inside the date time value with a '`' to ensure the correct conversion .
Example :
2024 / 08 / 19 12 ` : 42 ` : 00
2020-11-03 07:35:56 -05:00
The conversion does currently support the following units :
Size : B , KB , MB , GB , TB , PT , KiB , MiB , GiB , TiB , PiB
Time : ms , s , m , h , d w , M , y
. FUNCTIONALITY
Converts values with units to the lowest unit of this category .
Accepts Icinga Thresholds .
. EXAMPLE
PS > Convert-IcingaPluginThresholds -Threshold '20d' ;
Name Value
- - - - - - - - -
2024-08-19 10:13:24 -04:00
EndRange
2020-11-03 07:35:56 -05:00
Unit s
2024-08-19 10:13:24 -04:00
StartRange
Threshold 1728000
Mode 0
Raw 20d
IsDateTime False
Value 1728000
2020-11-03 07:35:56 -05:00
. EXAMPLE
PS > Convert-IcingaPluginThresholds -Threshold '5GB' ;
Name Value
- - - - - - - - -
2024-08-19 10:13:24 -04:00
EndRange
2020-11-03 07:35:56 -05:00
Unit B
2024-08-19 10:13:24 -04:00
StartRange
Threshold 5000000000
Mode 0
Raw 5 GB
IsDateTime False
Value 5000000000
2020-11-03 07:35:56 -05:00
. EXAMPLE
PS > Convert-IcingaPluginThresholds -Threshold '10MB:20MB' ;
Name Value
- - - - - - - - -
2024-08-19 10:13:24 -04:00
EndRange 20000000
2020-11-03 07:35:56 -05:00
Unit B
2024-08-19 10:13:24 -04:00
StartRange 10000000
Threshold 10000000 : 20000000
Mode 3
Raw 10 MB : 20 MB
IsDateTime False
Value
2020-11-03 07:35:56 -05:00
. EXAMPLE
PS > Convert-IcingaPluginThresholds -Threshold '10m:1h' ;
Name Value
- - - - - - - - -
2024-08-19 10:13:24 -04:00
EndRange 3600
2020-11-03 07:35:56 -05:00
Unit s
2024-08-19 10:13:24 -04:00
StartRange 600
Threshold 600 : 3600
Mode 3
Raw 10m : 1h
Value
2020-11-03 07:35:56 -05:00
. EXAMPLE
PS > Convert-IcingaPluginThresholds -Threshold '@10m:1h' ;
Name Value
- - - - - - - - -
2024-08-19 10:13:24 -04:00
EndRange 3600
2020-11-03 07:35:56 -05:00
Unit s
2024-08-19 10:13:24 -04:00
StartRange 600
Threshold @600 : 3600
Mode 4
Raw @10m : 1h
IsDateTime False
Value
2020-11-03 07:35:56 -05:00
. EXAMPLE
2024-08-19 10:13:24 -04:00
PS > Convert-IcingaPluginThresholds -Threshold '~:1M' ;
2020-11-03 07:35:56 -05:00
Name Value
- - - - - - - - -
2024-08-19 10:13:24 -04:00
EndRange
Unit s
StartRange
Threshold ~ : 2592000
Mode 2
Raw ~ : 1M
IsDateTime False
Value 2592000
. EXAMPLE
PS > Convert-IcingaPluginThresholds -Threshold '@2024/08/19 12`:42`:00:2024/08/19 12`:42`:00' ;
Name Value
- - - - - - - - -
EndRange 133685377200000000
2020-11-03 07:35:56 -05:00
Unit s
2024-08-19 10:13:24 -04:00
StartRange 133685377200000000
Threshold @133685377200000000 : 133685377200000000
Mode 4
Raw @2024 / 08 / 19 12 ` : 42 ` : 00 : 2024 / 08 / 19 12 ` : 42 ` : 00
IsDateTime True
Value
2020-11-03 07:35:56 -05:00
. INPUTS
System . String
. OUTPUTS
System . Hashtable
. LINK
https : / / github . com / Icinga / icinga-powershell -framework
#>
function Convert-IcingaPluginThresholds ( )
{
param (
[ string ] $Threshold = $null
) ;
[ hashtable ] $RetValue = @ {
2024-08-19 10:13:24 -04:00
'Raw' = $Threshold ;
'Unit' = '' ;
'Threshold' = $null ;
'Value' = $null ;
'StartRange' = $null ;
'EndRange' = $null ;
'Mode' = $IcingaEnums . IcingaThresholdMethod . Default ;
'IsDateTime' = $FALSE ;
2020-11-03 07:35:56 -05:00
} ;
2024-08-19 10:13:24 -04:00
if ( [ string ] :: IsNullOrEmpty ( $Threshold ) ) {
2020-11-03 07:35:56 -05:00
return $RetValue ;
}
# Always ensure we are using correct digits
2024-08-19 10:13:24 -04:00
$Threshold = $Threshold . Replace ( ',' , '.' ) ;
[ array ] $Content = @ ( ) ;
2021-02-23 11:23:24 -05:00
2020-11-03 07:35:56 -05:00
if ( $Threshold . Contains ( ':' ) ) {
2021-07-15 09:37:53 -04:00
# If we have more than one ':' inside our string, lets check if this is a date time value
# In case it is convert it properly to a FileTime we can work with later on
2024-08-19 10:13:24 -04:00
if ( [ Regex ] :: Matches ( $Threshold , '`:' ) . Count -gt 1 ) {
[ bool ] $HasTilde = $FALSE ;
[ bool ] $HasAt = $FALSE ;
if ( $Threshold . Contains ( '@' ) ) {
$HasAt = $TRUE ;
} elseif ( $Threshold . Contains ( '~' ) ) {
$HasTilde = $TRUE ;
}
$Threshold = $Threshold . Replace ( '`:' , '!' ) . Replace ( '~' , '' ) . Replace ( '@' , '' ) ;
[ array ] $DatimeValueArray = $Threshold . Split ( ':' ) ;
2021-07-15 09:37:53 -04:00
try {
2024-08-19 10:13:24 -04:00
[ array ] $DateTimeValue = @ ( ) ;
if ( [ string ] :: IsNullOrEmpty ( $DatimeValueArray [ 0 ] ) -eq $FALSE ) {
[ array ] $DateTimeValue + = ( [ DateTime ] :: ParseExact ( $DatimeValueArray [ 0 ] . Replace ( '!' , ':' ) , 'yyyy\/MM\/dd HH:mm:ss' , $null ) ) . ToFileTime ( ) ;
}
if ( [ string ] :: IsNullOrEmpty ( $DatimeValueArray [ 1 ] ) -eq $FALSE ) {
[ array ] $DateTimeValue + = ( [ DateTime ] :: ParseExact ( $DatimeValueArray [ 1 ] . Replace ( '!' , ':' ) , 'yyyy\/MM\/dd HH:mm:ss' , $null ) ) . ToFileTime ( ) ;
}
if ( $DateTimeValue . Count -gt 1 ) {
$Threshold = [ string ] :: Join ( ':' , $DateTimeValue ) ;
$RetValue . Mode = $IcingaEnums . IcingaThresholdMethod . Between ;
if ( $HasAt ) {
$Threshold = [ string ] :: Format ( '@{0}' , $Threshold ) ;
}
} elseif ( $DatimeValueArray . Count -gt 1 ) {
if ( $HasTilde ) {
$Threshold = [ string ] :: Format ( '~:{0}' , $DateTimeValue [ 0 ] ) ;
} else {
$Threshold = [ string ] :: Format ( '{0}:' , $DateTimeValue [ 0 ] ) ;
}
} else {
$Threshold = $DateTimeValue [ 0 ] ;
}
$RetValue . Unit = 's' ;
$RetValue . IsDateTime = $TRUE ;
2021-07-15 09:37:53 -04:00
} catch {
2024-08-19 10:13:24 -04:00
$RetValue . Threshold = $Threshold . Replace ( '!' , '`:' ) ;
Exit-IcingaThrowException -CustomMessage ( [ string ] :: Format ( 'Could not convert the provided threshold value {0} to a valid Icinga for Windows range' , $RetValue . Raw ) ) -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions . Inputs . InvalidThresholdValue -Force ;
return $RetValue ;
2021-07-15 09:37:53 -04:00
}
2024-08-19 10:13:24 -04:00
} else {
$RetValue . Mode = $IcingaEnums . IcingaThresholdMethod . Between ;
2021-07-15 09:37:53 -04:00
}
2020-11-03 07:35:56 -05:00
$Content = $Threshold . Split ( ':' ) ;
2024-08-19 10:13:24 -04:00
if ( $Content . Count -eq 2 -And ( [ string ] :: IsNullOrEmpty ( $Content [ 1 ] ) ) ) {
$RetValue . Mode = $IcingaEnums . IcingaThresholdMethod . Lower ;
}
2020-11-03 07:35:56 -05:00
} else {
$Content + = $Threshold ;
}
[ array ] $ConvertedValue = @ ( ) ;
foreach ( $ThresholdValue in $Content ) {
[ bool ] $HasTilde = $FALSE ;
[ bool ] $HasAt = $FALSE ;
2021-07-27 08:56:37 -04:00
[ bool ] $Negate = $FALSE ;
2020-11-03 07:35:56 -05:00
$Value = '' ;
$WorkUnit = '' ;
if ( $ThresholdValue . Contains ( '~' ) ) {
$ThresholdValue = $ThresholdValue . Replace ( '~' , '' ) ;
2024-08-19 10:13:24 -04:00
$HasTilde = $TRUE ;
$RetValue . Mode = $IcingaEnums . IcingaThresholdMethod . Greater ;
2020-11-03 07:35:56 -05:00
} elseif ( $ThresholdValue . Contains ( '@' ) ) {
2024-08-19 10:13:24 -04:00
$HasAt = $TRUE ;
2020-11-03 07:35:56 -05:00
$ThresholdValue = $ThresholdValue . Replace ( '@' , '' ) ;
2024-08-19 10:13:24 -04:00
$RetValue . Mode = $IcingaEnums . IcingaThresholdMethod . Outside ;
2020-11-03 07:35:56 -05:00
}
2021-07-27 08:56:37 -04:00
if ( $ThresholdValue [ 0 ] -eq '-' -And $ThresholdValue . Length -ge 1 ) {
$Negate = $TRUE ;
$ThresholdValue = $ThresholdValue . Substring ( 1 , $ThresholdValue . Length - 1 ) ;
}
2022-10-18 04:01:22 -04:00
if ( ( $ThresholdValue -Match " (^-?[0-9]+)((\.|\,)[0-9]+)?(B|KB|MB|GB|TB|PT|KiB|MiB|GiB|TiB|PiB) $ " ) ) {
2020-11-03 07:35:56 -05:00
$WorkUnit = 'B' ;
if ( [ string ] :: IsNullOrEmpty ( $RetValue . Unit ) -eq $FALSE -And $RetValue . Unit -ne $WorkUnit ) {
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions . Inputs . MultipleUnitUsage -Force ;
}
$Value = ( Convert-Bytes -Value $ThresholdValue -Unit $WorkUnit ) . Value ;
$RetValue . Unit = $WorkUnit ;
2022-10-18 04:01:22 -04:00
} elseif ( ( $ThresholdValue -Match " (^-?[0-9]+)((\.|\,)[0-9]+)?(ms|s|m|h|d|w|M|y) $ " ) ) {
2020-11-03 07:35:56 -05:00
$WorkUnit = 's' ;
if ( [ string ] :: IsNullOrEmpty ( $RetValue . Unit ) -eq $FALSE -And $RetValue . Unit -ne $WorkUnit ) {
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions . Inputs . MultipleUnitUsage -Force ;
}
$Value = ( ConvertTo-Seconds -Value $ThresholdValue ) ;
$RetValue . Unit = $WorkUnit ;
2021-02-23 11:23:24 -05:00
} elseif ( ( $ThresholdValue -Match " (^[\d\.]*) ?(%) " ) ) {
$WorkUnit = '%' ;
$Value = ( [ string ] $ThresholdValue ) . Replace ( ' ' , '' ) . Replace ( '%' , '' ) ;
$RetValue . Unit = $WorkUnit ;
2022-10-18 04:01:22 -04:00
} elseif ( ( $ThresholdValue -Match " (^-?[0-9]+)((\.|\,)[0-9]+)?(Kbit|Mbit|Gbit|Tbit|Pbit|Ebit|Zbit|Ybit) $ " ) ) {
$Value = $Matches [ 1 ] ;
$RetValue . Unit = $Matches [ 4 ] ;
2020-11-03 07:35:56 -05:00
} else {
2021-07-27 08:56:37 -04:00
# Load all other units/values generically
2021-05-28 17:36:57 -04:00
[ string ] $StrNumeric = '' ;
[ bool ] $FirstChar = $TRUE ;
2021-05-31 03:50:13 -04:00
[ bool ] $Delimiter = $FALSE ;
2021-05-28 17:36:57 -04:00
foreach ( $entry in ( [ string ] ( $ThresholdValue ) ) . ToCharArray ( ) ) {
2021-05-31 03:50:13 -04:00
if ( ( Test-Numeric $entry ) -Or ( $entry -eq '.' -And $Delimiter -eq $FALSE ) ) {
2021-05-28 17:36:57 -04:00
$StrNumeric + = $entry ;
$FirstChar = $FALSE ;
2021-05-31 03:50:13 -04:00
if ( $entry -eq '.' ) {
$Delimiter = $TRUE ;
}
2021-05-28 17:36:57 -04:00
} else {
if ( [ string ] :: IsNullOrEmpty ( $RetValue . Unit ) -And $FirstChar -eq $FALSE ) {
$RetValue . Unit = $entry ;
} else {
$StrNumeric = '' ;
$RetValue . Unit = '' ;
break ;
}
}
}
if ( [ string ] :: IsNullOrEmpty ( $StrNumeric ) ) {
$Value = $ThresholdValue ;
} else {
$Value = [ decimal ] $StrNumeric ;
}
2020-11-03 07:35:56 -05:00
}
2021-07-27 08:56:37 -04:00
if ( ( Test-Numeric $Value ) -And $Negate ) {
$Value = $Value * -1 ;
} elseif ( $Negate ) {
$Value = [ string ] :: Format ( '-{0}' , $Value ) ;
}
2020-11-03 07:35:56 -05:00
if ( $HasTilde ) {
$ConvertedValue + = [ string ] :: Format ( '~{0}' , $Value ) ;
} elseif ( $HasAt ) {
$ConvertedValue + = [ string ] :: Format ( '@{0}' , $Value ) ;
} else {
$ConvertedValue + = $Value ;
}
}
[ string ] $Value = [ string ] :: Join ( ':' , $ConvertedValue ) ;
2024-08-19 10:13:24 -04:00
switch ( $RetValue . Mode ) {
$IcingaEnums . IcingaThresholdMethod . Default {
$RetValue . Value = $ConvertedValue [ 0 ] ;
if ( [ string ] :: IsNullOrEmpty ( $RetValue . Value ) ) {
Exit-IcingaThrowException -CustomMessage ( [ string ] :: Format ( 'Could not convert the provided threshold value {0} to a valid Icinga for Windows range' , $RetValue . Raw ) ) -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions . Inputs . InvalidThresholdValue -Force ;
return $RetValue ;
}
break ;
} ;
$IcingaEnums . IcingaThresholdMethod . Lower {
$RetValue . Value = $ConvertedValue [ 0 ] ;
if ( [ string ] :: IsNullOrEmpty ( $RetValue . Value ) ) {
Exit-IcingaThrowException -CustomMessage ( [ string ] :: Format ( 'Could not convert the provided threshold value {0} to a valid Icinga for Windows range' , $RetValue . Raw ) ) -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions . Inputs . InvalidThresholdValue -Force ;
return $RetValue ;
}
break ;
} ;
$IcingaEnums . IcingaThresholdMethod . Greater {
$RetValue . Value = $ConvertedValue [ 1 ] ;
if ( [ string ] :: IsNullOrEmpty ( $RetValue . Value ) ) {
Exit-IcingaThrowException -CustomMessage ( [ string ] :: Format ( 'Could not convert the provided threshold value {0} to a valid Icinga for Windows range' , $RetValue . Raw ) ) -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions . Inputs . InvalidThresholdValue -Force ;
return $RetValue ;
}
break ;
} ;
$IcingaEnums . IcingaThresholdMethod . Between {
$RetValue . StartRange = [ decimal ] $ConvertedValue [ 0 ] ;
$RetValue . EndRange = [ decimal ] $ConvertedValue [ 1 ] ;
if ( [ string ] :: IsNullOrEmpty ( $RetValue . StartRange ) -Or [ string ] :: IsNullOrEmpty ( $RetValue . EndRange ) ) {
Exit-IcingaThrowException -CustomMessage ( [ string ] :: Format ( 'Could not convert the provided threshold value {0} to a valid Icinga for Windows range' , $RetValue . Raw ) ) -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions . Inputs . InvalidThresholdValue -Force ;
return $RetValue ;
}
break ;
} ;
$IcingaEnums . IcingaThresholdMethod . Outside {
$RetValue . StartRange = [ decimal ] ( $ConvertedValue [ 0 ] . Replace ( '@' , '' ) ) ;
$RetValue . EndRange = [ decimal ] $ConvertedValue [ 1 ] ;
if ( [ string ] :: IsNullOrEmpty ( $RetValue . StartRange ) -Or [ string ] :: IsNullOrEmpty ( $RetValue . EndRange ) ) {
Exit-IcingaThrowException -CustomMessage ( [ string ] :: Format ( 'Could not convert the provided threshold value {0} to a valid Icinga for Windows range' , ( $RetValue . Raw ) ) ) -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions . Inputs . InvalidThresholdValue -Force ;
return $RetValue ;
}
break ;
} ;
}
2020-11-03 07:35:56 -05:00
if ( [ string ] :: IsNullOrEmpty ( $Value ) -eq $FALSE -And $Value . Contains ( ':' ) -eq $FALSE ) {
if ( ( Test-Numeric $Value ) ) {
2024-08-19 10:13:24 -04:00
$RetValue . Threshold = [ decimal ] $Value ;
$RetValue . Value = [ decimal ] $Value ;
2020-11-03 07:35:56 -05:00
return $RetValue ;
}
}
2021-02-23 11:23:24 -05:00
# Always ensure we are using correct digits
2024-08-19 10:13:24 -04:00
$Value = ( [ string ] $Value ) . Replace ( ',' , '.' ) ;
$RetValue . Threshold = $Value ;
2020-11-03 07:35:56 -05:00
return $RetValue ;
}