From a6622d3dcd46585ab73dfa388d66d84a9bd20b6a Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Wed, 31 May 2023 10:07:15 +0200 Subject: [PATCH] Adds feature to summarize perf counter errors for all failed instances --- doc/31-Changelog.md | 4 +++ plugins/Invoke-IcingaCheckPerfcounter.psm1 | 32 +++++++++++++++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/doc/31-Changelog.md b/doc/31-Changelog.md index 084611d1..10f02bed 100644 --- a/doc/31-Changelog.md +++ b/doc/31-Changelog.md @@ -13,6 +13,10 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic * [#342](https://github.com/Icinga/icinga-powershell-plugins/issues/342) Fixes an issue for disk health plugin, which can fail in some cases due to an invalid conversion from the MSFT output from int to string +### Enhancements + +* [#348](https://github.com/Icinga/icinga-powershell-plugins/issues/348) Adds feature to `Invoke-IcingaCheckPerfCounter` to summarize an entire performance counter category, in case all instances of the check fail to prevent large outputs being written into the database + ## 1.10.1 (2022-12-20) ### Bugfixes diff --git a/plugins/Invoke-IcingaCheckPerfcounter.psm1 b/plugins/Invoke-IcingaCheckPerfcounter.psm1 index 33650062..9266833a 100644 --- a/plugins/Invoke-IcingaCheckPerfcounter.psm1 +++ b/plugins/Invoke-IcingaCheckPerfcounter.psm1 @@ -80,11 +80,15 @@ function Invoke-IcingaCheckPerfCounter() $CheckPackage.AddCheck( ( New-IcingaCheck -Name $counter -NoPerfData - ).SetUnknown($Counters[$counter].error, $TRUE) - ) + ).SetUnknown([string]::Format('Internal Counter Error: Failed to fetch performance counter. Error message: {1}', $counter, $Counters[$counter].error), $TRUE) + ); continue; } + # Set this to true, which means that by default we always fail + [bool]$CounterFailed = $TRUE; + [string]$FirstError = ''; + foreach ($instanceName in $Counters[$counter].Keys) { if ((Test-IcingaArrayFilter -InputObject $instanceName -Include $IncludeCounter -Exclude $ExcludeCounter) -eq $FALSE) { continue; @@ -93,14 +97,15 @@ function Invoke-IcingaCheckPerfCounter() $instance = $Counters[$counter][$instanceName]; if ([string]::IsNullOrEmpty($instance.error) -eq $FALSE) { - $CounterPackage.AddCheck( - ( - New-IcingaCheck -Name $instanceName -NoPerfData - ).SetUnknown($instance.error, $TRUE) - ) + if ([string]::IsNullOrEmpty($FirstError)) { + $FirstError = [string]($instance.error); + } continue; } + # If we found atleast one working counter in this category, proceed + $CounterFailed = $FALSE; + if ($instance -IsNot [hashtable]) { $CounterInfo = Get-IcingaPerformanceCounterDetails -Counter $counter; $IcingaCheck = New-IcingaCheck -Name $counter -Value $Counters[$counter].Value -MetricIndex $CounterInfo.Category -MetricName $CounterInfo.Counter; @@ -114,6 +119,19 @@ function Invoke-IcingaCheckPerfCounter() $IcingaCheck.WarnOutOfRange($Warning).CritOutOfRange($Critical) | Out-Null; $CounterPackage.AddCheck($IcingaCheck); } + + # If all of over counters failed for some reason, only print one combined error message. + if ($CounterFailed) { + if ([string]::IsNullOrEmpty($FirstError)) { + $FirstError = 'No counter instances could be found'; + } + $CounterPackage.AddCheck( + ( + New-IcingaCheck -Name 'Internal Counter Error' -NoPerfData + ).SetUnknown([string]::Format('Failed to fetch all instances and objects for this performance counter. First error message: {0}', $FirstError), $TRUE) + ); + } + $CheckPackage.AddCheck($CounterPackage); }