From 5791dbedddcaf4578e30ed12c1dfe0845cedd832 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Tue, 20 Aug 2019 16:31:07 +0200 Subject: [PATCH] Extended Second conversion with Icinga threshold handling --- lib/core/tools/ConvertTo-Seconds.psm1 | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/lib/core/tools/ConvertTo-Seconds.psm1 b/lib/core/tools/ConvertTo-Seconds.psm1 index 68c1e65..194f3ed 100644 --- a/lib/core/tools/ConvertTo-Seconds.psm1 +++ b/lib/core/tools/ConvertTo-Seconds.psm1 @@ -7,9 +7,14 @@ function ConvertTo-Seconds() [string]$Value ); + if ([string]::IsNullOrEmpty($Value)) { + return $Value; + } + [string]$NumberPart = ''; [string]$UnitPart = ''; [bool]$Negate = $FALSE; + [bool]$hasUnit = $FALSE; foreach($char in $Value.ToCharArray()) { if ((Test-Numeric $char)) { @@ -21,10 +26,15 @@ function ConvertTo-Seconds() $NumberPart += '.'; } else { $UnitPart += $char; + $hasUnit = $TRUE; } } } + if (-Not $hasUnit) { + return $Value; + } + [single]$ValueSplitted = $NumberPart; $result = 0; @@ -60,3 +70,38 @@ function ConvertTo-Seconds() return $result; } + +function ConvertTo-SecondsFromIcingaThresholds() +{ + param( + [string]$Threshold + ); + + [array]$Content = $Threshold.Split(':'); + [array]$NewContent = @(); + + foreach ($entry in $Content) { + $NewContent += (Get-IcingaThresholdsAsSeconds -Value $entry) + } + + return [string]::Join(':', $NewContent); +} + +function Get-IcingaThresholdsAsSeconds() +{ + param( + [string]$Value + ); + + if ($Value.Contains('~')) { + $Value = $Value.Replace('~', ''); + return [string]::Format('~{0}', (ConvertTo-Seconds $Value)); + } elseif ($Value.Contains('@')) { + $Value = $Value.Replace('@', ''); + return [string]::Format('@{0}', (ConvertTo-Seconds $Value)); + } + + return (ConvertTo-Seconds $Value); +} + +Export-ModuleMember -Function @( 'ConvertTo-Seconds', 'ConvertTo-SecondsFromIcingaThresholds' );