From 60e7a5298680e788e44836c39b234a3f45ed1dae Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Mon, 16 Sep 2019 17:32:25 +0200 Subject: [PATCH] Added tool function to increase digit count * Added new function Format-IcingaDigitCount to fill digits with leading 0 depending on missing values * Added this function to CPU check --- lib/core/tools/Format-IcingaDigitCount.psm1 | 22 +++++++++++++++++++++ lib/plugins/Invoke-IcingaCheckCPU.psm1 | 5 +++-- 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 lib/core/tools/Format-IcingaDigitCount.psm1 diff --git a/lib/core/tools/Format-IcingaDigitCount.psm1 b/lib/core/tools/Format-IcingaDigitCount.psm1 new file mode 100644 index 0000000..8e6d012 --- /dev/null +++ b/lib/core/tools/Format-IcingaDigitCount.psm1 @@ -0,0 +1,22 @@ +function Format-IcingaDigitCount() +{ + param( + [string]$Value, + [int]$Digits + ); + + if ([string]::IsNullOrEmpty($Value)) { + return $Value; + } + + $CurrentLength = $Value.Length; + if ($CurrentLength -ge $Digits) { + return $Value; + } + + while ($Value.Length -lt $Digits) { + $Value = [string]::Format('0{0}', $Value); + } + + return $Value; +} diff --git a/lib/plugins/Invoke-IcingaCheckCPU.psm1 b/lib/plugins/Invoke-IcingaCheckCPU.psm1 index 0df4075..8fc2034 100644 --- a/lib/plugins/Invoke-IcingaCheckCPU.psm1 +++ b/lib/plugins/Invoke-IcingaCheckCPU.psm1 @@ -1,4 +1,5 @@ Import-IcingaLib core\perfcounter; +Import-IcingaLib core\tools; Import-IcingaLib icinga\plugin; function Invoke-IcingaCheckCPU() @@ -16,12 +17,12 @@ function Invoke-IcingaCheckCPU() if ($CpuCounter.Counters.Count -ne 0) { foreach ($counter in $CpuCounter.Counters) { - $IcingaCheck = New-IcingaCheck -Name ([string]::Format('Core #{0}', $counter.Instance)) -Value $counter.Value().Value -Unit '%'; + $IcingaCheck = New-IcingaCheck -Name ([string]::Format('Core #{0}', (Format-IcingaDigitCount $counter.Instance -Digits 3))) -Value $counter.Value().Value -Unit '%'; $IcingaCheck.WarnOutOfRange($Warning).CritOutOfRange($Critical) | Out-Null; $CpuPackage.AddCheck($IcingaCheck); } } else { - $IcingaCheck = New-IcingaCheck -Name ([string]::Format('Core #{0}',$Core)) -Value $CpuCounter.Value().Value -Unit '%'; + $IcingaCheck = New-IcingaCheck -Name ([string]::Format('Core #{0}', (Format-IcingaDigitCount $Core -Digits 3))) -Value $CpuCounter.Value().Value -Unit '%'; $IcingaCheck.WarnOutOfRange($Warning).CritOutOfRange($Critical) | Out-Null; $CpuPackage.AddCheck($IcingaCheck); }