From 0e3e84c941a8cdd6ef500109f8d2401f1a9e29e3 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Mon, 5 Jul 2021 17:48:40 +0200 Subject: [PATCH] Fixes null exception on GetBaseThresholdArguments --- doc/31-Changelog.md | 1 + .../plugin/Get-IcingaThresholdCache.psm1 | 24 +++++++++++++++++++ lib/icinga/plugin/New-IcingaCheck.psm1 | 2 +- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 lib/icinga/plugin/Get-IcingaThresholdCache.psm1 diff --git a/doc/31-Changelog.md b/doc/31-Changelog.md index 97fc7d3c..bb41fdac 100644 --- a/doc/31-Changelog.md +++ b/doc/31-Changelog.md @@ -19,6 +19,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic * [#285](https://github.com/Icinga/icinga-powershell-framework/issues/285) Fixes plain Icinga 2 conf generation for commands, which was caused by a new exception output for additional output * [#293](https://github.com/Icinga/icinga-powershell-framework/pull/293) Fixes crash on REST-Api for NULL values while parsing the REST message * [#295](https://github.com/Icinga/icinga-powershell-framework/issues/295) Fixes background service check daemon not working with arguments for plugins +* [#297](https://github.com/Icinga/icinga-powershell-framework/pull/297) Fixes null exception error which can occur in certain edge cases, caused by testing `New-IcingaCheck` directly without function wrapper ## 1.5.0 (2021-06-02) diff --git a/lib/icinga/plugin/Get-IcingaThresholdCache.psm1 b/lib/icinga/plugin/Get-IcingaThresholdCache.psm1 new file mode 100644 index 00000000..3cb0898a --- /dev/null +++ b/lib/icinga/plugin/Get-IcingaThresholdCache.psm1 @@ -0,0 +1,24 @@ +function Get-IcingaThresholdCache() +{ + param ( + [string]$CheckCommand = $null + ); + + if ([string]::IsNullOrEmpty($CheckCommand)) { + return $null; + } + + if ($null -eq $Global:Icinga) { + return $null; + } + + if ($Global:Icinga.ContainsKey('ThresholdCache') -eq $FALSE) { + return $null; + } + + if ($Global:Icinga.ThresholdCache.ContainsKey($CheckCommand) -eq $FALSE) { + return $null; + } + + return $Global:Icinga.ThresholdCache[$CheckCommand]; +} diff --git a/lib/icinga/plugin/New-IcingaCheck.psm1 b/lib/icinga/plugin/New-IcingaCheck.psm1 index 1ffcfc2d..3e8d2e70 100644 --- a/lib/icinga/plugin/New-IcingaCheck.psm1 +++ b/lib/icinga/plugin/New-IcingaCheck.psm1 @@ -387,7 +387,7 @@ function New-IcingaCheck() '-BaseValue' = $this.BaseValue; '-Unit' = $this.Unit; '-CheckName' = $this.__GetName(); - '-ThresholdCache' = $Global:Icinga.ThresholdCache[$this.__CheckCommand]; + '-ThresholdCache' = (Get-IcingaThresholdCache -CheckCommand $this.__CheckCommand); '-Translation' = $this.Translation; '-TimeInterval' = $this.__TimeInterval; };