From c4179338a1db1a0b244934e67300ae9d15187189 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Tue, 19 May 2020 17:30:50 +0200 Subject: [PATCH] Adds support to set check state by function call --- lib/icinga/plugin/New-IcingaCheck.psm1 | 52 +++++++++++++++++++++----- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/lib/icinga/plugin/New-IcingaCheck.psm1 b/lib/icinga/plugin/New-IcingaCheck.psm1 index b7be7a9..78f071b 100644 --- a/lib/icinga/plugin/New-IcingaCheck.psm1 +++ b/lib/icinga/plugin/New-IcingaCheck.psm1 @@ -94,6 +94,22 @@ function New-IcingaCheck() return $this.unknownchecks; } + $Check | Add-Member -membertype ScriptMethod -name 'SetUnknown' -value { + $this.AddInternalCheckMessage( + $IcingaEnums.IcingaExitCode.Unknown, + $null, + $null + ); + } + + $Check | Add-Member -membertype ScriptMethod -name 'SetWarning' -value { + $this.AddInternalCheckMessage( + $IcingaEnums.IcingaExitCode.Warning, + $null, + $null + ); + } + $Check | Add-Member -membertype ScriptMethod -name 'WarnOutOfRange' -value { param($warning); @@ -296,6 +312,14 @@ function New-IcingaCheck() return $this; } + $Check | Add-Member -membertype ScriptMethod -name 'SetCritical' -value { + $this.AddInternalCheckMessage( + $IcingaEnums.IcingaExitCode.Critical, + $null, + $null + ); + } + $Check | Add-Member -membertype ScriptMethod -name 'CritOutOfRange' -value { param($critical); @@ -534,17 +558,25 @@ function New-IcingaCheck() } $this.SetExitCode($state); - $this.AddMessage( - [string]::Format( - '{0}: Value "{1}{4}" is {2} threshold "{3}{4}"', + + if ($null -eq $value -And $null -eq $type) { + $this.AddMessage( $this.name, - $this.TranslateValue($this.value), - $type, - $this.TranslateValue($value), - $this.unit - ), - $state - ); + $state + ); + } else { + $this.AddMessage( + [string]::Format( + '{0}: Value "{1}{4}" is {2} threshold "{3}{4}"', + $this.name, + $this.TranslateValue($this.value), + $type, + $this.TranslateValue($value), + $this.unit + ), + $state + ); + } switch ($state) { $IcingaEnums.IcingaExitCode.Warning {