From b2f6f254ec2af1660b8fdbac412edc589f19cf8f Mon Sep 17 00:00:00 2001 From: Alexander Stoll Date: Wed, 24 Jul 2019 15:34:49 +0200 Subject: [PATCH 1/4] Added further options in ProcessCount-Check --- lib/plugins/Invoke-IcingaCheckProcessCount.psm1 | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/plugins/Invoke-IcingaCheckProcessCount.psm1 b/lib/plugins/Invoke-IcingaCheckProcessCount.psm1 index 5b5b5ab..0efd0f0 100644 --- a/lib/plugins/Invoke-IcingaCheckProcessCount.psm1 +++ b/lib/plugins/Invoke-IcingaCheckProcessCount.psm1 @@ -15,13 +15,20 @@ function Invoke-IcingaCheckProcessCount() $ProcessPackage = New-icingaCheckPackage -Name "Process Check" -OperatorAnd -Verbose $Verbose -NoPerfData $NoPerfData; - foreach ($proc in $process) { - $ProcessCount = $ProcessInformation."Processes".$proc.processlist.Count; - $IcingaCheck = New-IcingaCheck -Name ([string]::Format('Process Count "{0}"', $proc)) -Value $ProcessCount; + if ($Process.Count -eq 0) { + $ProcessCount = $ProcessInformation['Process Count']; + $IcingaCheck = New-IcingaCheck -Name ([string]::Format('Process Count')) -Value $ProcessCount; $IcingaCheck.WarnOutOfRange($Warning).CritOutOfRange($Critical) | Out-Null; $ProcessPackage.AddCheck($IcingaCheck); + } else { + foreach ($proc in $process) { + $ProcessCount = $ProcessInformation."Processes".$proc.processlist.Count; + $IcingaCheck = New-IcingaCheck -Name ([string]::Format('Process Count "{0}"', $proc)) -Value $ProcessCount; + $IcingaCheck.WarnOutOfRange($Warning).CritOutOfRange($Critical) | Out-Null; + $ProcessPackage.AddCheck($IcingaCheck); + } } - exit (New-IcingaCheckResult -Check $ProcessPackage -NoPerfData $TRUE -Compile); + exit (New-IcingaCheckResult -Check $ProcessPackage -NoPerfData $NoPerfData -Compile); } From d3862eccdbc960e7afed05cba67a318bbd3d48ee Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Wed, 24 Jul 2019 17:10:16 +0200 Subject: [PATCH 2/4] Added support to check if objects exist on icinga checks --- lib/icinga/plugin/New-IcingaCheck.psm1 | 48 ++++++++++++++++---------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/lib/icinga/plugin/New-IcingaCheck.psm1 b/lib/icinga/plugin/New-IcingaCheck.psm1 index d65d78d..160f047 100644 --- a/lib/icinga/plugin/New-IcingaCheck.psm1 +++ b/lib/icinga/plugin/New-IcingaCheck.psm1 @@ -9,29 +9,31 @@ function New-IcingaCheck() $Unit = $null, [string]$Minimum = '', [string]$Maximum = '', + $ObjectExists = -1, [switch]$NoPerfData ); $Check = New-Object -TypeName PSObject; - $Check | Add-Member -membertype NoteProperty -name 'name' -value $Name; - $Check | Add-Member -membertype NoteProperty -name 'verbose' -value 0; - $Check | Add-Member -membertype NoteProperty -name 'messages' -value @(); - $Check | Add-Member -membertype NoteProperty -name 'oks' -value @(); - $Check | Add-Member -membertype NoteProperty -name 'warnings' -value @(); - $Check | Add-Member -membertype NoteProperty -name 'criticals' -value @(); - $Check | Add-Member -membertype NoteProperty -name 'unknowns' -value @(); - $Check | Add-Member -membertype NoteProperty -name 'value' -value $Value; - $Check | Add-Member -membertype NoteProperty -name 'exitcode' -value -1; - $Check | Add-Member -membertype NoteProperty -name 'unit' -value $Unit; - $Check | Add-Member -membertype NoteProperty -name 'spacing' -value 0; - $Check | Add-Member -membertype NoteProperty -name 'compiled' -value $FALSE; - $Check | Add-Member -membertype NoteProperty -name 'perfdata' -value (-Not $NoPerfData); - $Check | Add-Member -membertype NoteProperty -name 'warning' -value ''; - $Check | Add-Member -membertype NoteProperty -name 'critical' -value ''; - $Check | Add-Member -membertype NoteProperty -name 'minimum' -value $Minimum; - $Check | Add-Member -membertype NoteProperty -name 'maximum' -value $Maximum; - $Check | Add-Member -membertype NoteProperty -name 'checks' -value $null; - $Check | Add-Member -membertype NoteProperty -name 'completed' -value $FALSE; + $Check | Add-Member -membertype NoteProperty -name 'name' -value $Name; + $Check | Add-Member -membertype NoteProperty -name 'verbose' -value 0; + $Check | Add-Member -membertype NoteProperty -name 'messages' -value @(); + $Check | Add-Member -membertype NoteProperty -name 'oks' -value @(); + $Check | Add-Member -membertype NoteProperty -name 'warnings' -value @(); + $Check | Add-Member -membertype NoteProperty -name 'criticals' -value @(); + $Check | Add-Member -membertype NoteProperty -name 'unknowns' -value @(); + $Check | Add-Member -membertype NoteProperty -name 'value' -value $Value; + $Check | Add-Member -membertype NoteProperty -name 'exitcode' -value -1; + $Check | Add-Member -membertype NoteProperty -name 'unit' -value $Unit; + $Check | Add-Member -membertype NoteProperty -name 'spacing' -value 0; + $Check | Add-Member -membertype NoteProperty -name 'compiled' -value $FALSE; + $Check | Add-Member -membertype NoteProperty -name 'perfdata' -value (-Not $NoPerfData); + $Check | Add-Member -membertype NoteProperty -name 'warning' -value ''; + $Check | Add-Member -membertype NoteProperty -name 'critical' -value ''; + $Check | Add-Member -membertype NoteProperty -name 'minimum' -value $Minimum; + $Check | Add-Member -membertype NoteProperty -name 'maximum' -value $Maximum; + $Check | Add-Member -membertype NoteProperty -name 'objectexists' -value $ObjectExists; + $Check | Add-Member -membertype NoteProperty -name 'checks' -value $null; + $Check | Add-Member -membertype NoteProperty -name 'completed' -value $FALSE; $Check | Add-Member -membertype ScriptMethod -name 'AddSpacing' -value { $this.spacing += 1; @@ -404,6 +406,14 @@ function New-IcingaCheck() $Check | Add-Member -membertype ScriptMethod -name 'AddInternalCheckMessage' -value { param($state, $value, $type); + if ($this.objectexists -ne -1 -And $null -eq $this.objectexists) { + $this.SetExitCode($IcingaEnums.IcingaExitCode.Unknown); + $this.AddMessage([string]::Format( + '{0} does not exist', $this.name + ), $IcingaEnums.IcingaExitCode.Unknown); + return; + } + $this.SetExitCode($state); $this.AddMessage([string]::Format( '{0} {1}{4} is {2} {3}{4}', $this.name, $this.value, $type, $value, $this.unit From f03980cebf3caf1ab7aa5a5becada778b6e3df90 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Wed, 24 Jul 2019 17:19:56 +0200 Subject: [PATCH 3/4] Added support to translate check values to speaking values on output --- lib/icinga/plugin/New-IcingaCheck.psm1 | 46 +++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/lib/icinga/plugin/New-IcingaCheck.psm1 b/lib/icinga/plugin/New-IcingaCheck.psm1 index 160f047..09a1155 100644 --- a/lib/icinga/plugin/New-IcingaCheck.psm1 +++ b/lib/icinga/plugin/New-IcingaCheck.psm1 @@ -10,6 +10,7 @@ function New-IcingaCheck() [string]$Minimum = '', [string]$Maximum = '', $ObjectExists = -1, + $Translation = $null, [switch]$NoPerfData ); @@ -32,6 +33,7 @@ function New-IcingaCheck() $Check | Add-Member -membertype NoteProperty -name 'minimum' -value $Minimum; $Check | Add-Member -membertype NoteProperty -name 'maximum' -value $Maximum; $Check | Add-Member -membertype NoteProperty -name 'objectexists' -value $ObjectExists; + $Check | Add-Member -membertype NoteProperty -name 'translation' -value $Translation; $Check | Add-Member -membertype NoteProperty -name 'checks' -value $null; $Check | Add-Member -membertype NoteProperty -name 'completed' -value $FALSE; @@ -403,6 +405,26 @@ function New-IcingaCheck() return $this; } + $Check | Add-Member -membertype ScriptMethod -name 'TranslateValue' -value { + param($value); + + if ($null -eq $this.translation -Or $null -eq $value) { + return $value; + } + + $checkValue = $value; + + if ((Test-Numeric $checkValue)) { + $checkValue = [int]$checkValue; + } + + if ($this.translation.ContainsKey($checkValue)) { + return $this.translation[$checkValue]; + } + + return $value; + } + $Check | Add-Member -membertype ScriptMethod -name 'AddInternalCheckMessage' -value { param($state, $value, $type); @@ -415,9 +437,17 @@ function New-IcingaCheck() } $this.SetExitCode($state); - $this.AddMessage([string]::Format( - '{0} {1}{4} is {2} {3}{4}', $this.name, $this.value, $type, $value, $this.unit - ), $state); + $this.AddMessage( + [string]::Format( + '{0} {1}{4} is {2} {3}{4}', + $this.name, + $this.TranslateValue($this.value), + $type, + $this.TranslateValue($value), + $this.unit + ), + $state + ); switch ($state) { $IcingaEnums.IcingaExitCode.Warning { @@ -562,7 +592,15 @@ function New-IcingaCheck() $Check | Add-Member -membertype ScriptMethod -name 'AddOkOutput' -value { if ([int]$this.exitcode -eq -1) { $this.exitcode = $IcingaEnums.IcingaExitCode.Ok; - $this.AddMessage([string]::Format('{0} is {1}{2}', $this.name, $this.value, $this.unit), $IcingaEnums.IcingaExitCode.Ok); + $this.AddMessage( + [string]::Format( + '{0} is {1}{2}', + $this.name, + $this.TranslateValue($this.value), + $this.unit + ), + $IcingaEnums.IcingaExitCode.Ok + ); } } From 029ee6fbbb6b5a3a652e802b764fc7382620d0d3 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Wed, 24 Jul 2019 17:21:10 +0200 Subject: [PATCH 4/4] Fixed $null code styling comparison --- lib/icinga/plugin/New-IcingaCheckResult.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/icinga/plugin/New-IcingaCheckResult.psm1 b/lib/icinga/plugin/New-IcingaCheckResult.psm1 index c7126ff..b7b07c6 100644 --- a/lib/icinga/plugin/New-IcingaCheckResult.psm1 +++ b/lib/icinga/plugin/New-IcingaCheckResult.psm1 @@ -13,7 +13,7 @@ function New-IcingaCheckresult() $CheckResult | Add-Member -membertype NoteProperty -name 'noperfdata' -value $NoPerfData; $CheckResult | Add-Member -membertype ScriptMethod -name 'Compile' -value { - if ($this.check -eq $null) { + if ($null -eq $this.check) { return $IcingaEnums.IcingaExitCode.Unknown; }