From d722ae9932dc3caca31c7e7d3e5493bbcf848b57 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Sat, 5 Oct 2019 21:50:13 +0200 Subject: [PATCH] Improved constructing of Performance Data --- lib/icinga/plugin/New-IcingaCheck.psm1 | 28 +++--------- lib/icinga/plugin/New-IcingaCheckResult.psm1 | 2 +- .../New-IcingaPerformanceDataEntry.psm1 | 43 +++++++++++++++++++ .../plugin/Write-IcingaPluginPerfData.psm1 | 25 +++++++++-- 4 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 lib/icinga/plugin/New-IcingaPerformanceDataEntry.psm1 diff --git a/lib/icinga/plugin/New-IcingaCheck.psm1 b/lib/icinga/plugin/New-IcingaCheck.psm1 index 35be1e5..d949508 100644 --- a/lib/icinga/plugin/New-IcingaCheck.psm1 +++ b/lib/icinga/plugin/New-IcingaCheck.psm1 @@ -647,36 +647,22 @@ function New-IcingaCheck() $this.AutodiscoverMinMax(); - if ([string]::IsNullOrEmpty($this.minimum) -eq $FALSE) { - $this.minimum = [string]::Format(';{0}', $this.minimum); - } - if ([string]::IsNullOrEmpty($this.maximum) -eq $FALSE) { - $this.maximum = [string]::Format(';{0}', $this.maximum); - } - $this.completed = $TRUE; [string]$LabelName = (Format-IcingaPerfDataLabel $this.name); - return @{ - 'label' = $LabelName; - 'perfdata' = [string]::Format( - "'{0}'={1}{2};{3};{4}{5}{6} ", - $LabelName, - (Format-IcingaPerfDataValue $this.value), - $this.unit, - (Format-IcingaPerfDataValue $this.warning), - (Format-IcingaPerfDataValue $this.critical), - (Format-IcingaPerfDataValue $this.minimum), - (Format-IcingaPerfDataValue $this.maximum) - ); + $perfdata = @{ + 'label' = $LabelName; + 'perfdata' = ''; 'unit' = $this.unit; 'value' = (Format-IcingaPerfDataValue $this.value); 'warning' = (Format-IcingaPerfDataValue $this.warning); 'critical' = (Format-IcingaPerfDataValue $this.critical); - 'minimum' = (Format-IcingaPerfDataValue ($this.minimum).Replace(';', '')); - 'maximum' = (Format-IcingaPerfDataValue ($this.maximum).Replace(';', '')); + 'minimum' = (Format-IcingaPerfDataValue $this.minimum); + 'maximum' = (Format-IcingaPerfDataValue $this.maximum); 'package' = $FALSE; }; + + return $perfdata; } $Check | Add-Member -membertype ScriptMethod -name 'AutodiscoverMinMax' -value { diff --git a/lib/icinga/plugin/New-IcingaCheckResult.psm1 b/lib/icinga/plugin/New-IcingaCheckResult.psm1 index e7bc075..9db2518 100644 --- a/lib/icinga/plugin/New-IcingaCheckResult.psm1 +++ b/lib/icinga/plugin/New-IcingaCheckResult.psm1 @@ -24,7 +24,7 @@ function New-IcingaCheckresult() $this.check.Compile($TRUE) | Out-Null; if ([int]$this.check.exitcode -ne [int]$IcingaEnums.IcingaExitCode.Unknown -And -Not $this.noperfdata) { - Write-IcingaPluginPerfData ($this.check.GetPerfData().perfdata); + Write-IcingaPluginPerfData -PerformanceData ($this.check.GetPerfData().perfdata) -CheckCommand $CheckCommand; } return $this.check.exitcode; diff --git a/lib/icinga/plugin/New-IcingaPerformanceDataEntry.psm1 b/lib/icinga/plugin/New-IcingaPerformanceDataEntry.psm1 new file mode 100644 index 0000000..f0eeb74 --- /dev/null +++ b/lib/icinga/plugin/New-IcingaPerformanceDataEntry.psm1 @@ -0,0 +1,43 @@ +function New-IcingaPerformanceDataEntry() +{ + param ( + $PerfDataObject, + $Label = $null, + $Value = $null + ); + + if ($null -eq $PerfDataObject) { + return ''; + } + + [string]$LabelName = $PerfDataObject.label; + [string]$PerfValue = $PerfDataObject.value; + + if ([string]::IsNullOrEmpty($Label) -eq $FALSE) { + $LabelName = $Label; + } + if ([string]::IsNullOrEmpty($Value) -eq $FALSE) { + $PerfValue = $Value; + } + + $minimum = ''; + $maximum = ''; + + if ([string]::IsNullOrEmpty($PerfDataObject.minimum) -eq $FALSE) { + $minimum = [string]::Format(';{0}', $PerfDataObject.minimum); + } + if ([string]::IsNullOrEmpty($PerfDataObject.maximum) -eq $FALSE) { + $maximum = [string]::Format(';{0}', $PerfDataObject.maximum); + } + + return ([string]::Format( + "'{0}'={1}{2};{3};{4}{5}{6} ", + $LabelName, + (Format-IcingaPerfDataValue $PerfValue), + $PerfDataObject.unit, + (Format-IcingaPerfDataValue $PerfDataObject.warning), + (Format-IcingaPerfDataValue $PerfDataObject.critical), + (Format-IcingaPerfDataValue $minimum), + (Format-IcingaPerfDataValue $maximum) + )); +} diff --git a/lib/icinga/plugin/Write-IcingaPluginPerfData.psm1 b/lib/icinga/plugin/Write-IcingaPluginPerfData.psm1 index caab482..2199803 100644 --- a/lib/icinga/plugin/Write-IcingaPluginPerfData.psm1 +++ b/lib/icinga/plugin/Write-IcingaPluginPerfData.psm1 @@ -1,10 +1,12 @@ function Write-IcingaPluginPerfData() { param( - $PerformanceData + $PerformanceData, + $CheckCommand ); - [string]$PerfDataOutput = (Get-IcingaPluginPerfDataContent -PerfData $PerformanceData); + $CheckResultCache = Get-IcingaCacheData -Space 'sc_daemon' -CacheStore 'checkresult' -KeyName $CheckCommand; + [string]$PerfDataOutput = (Get-IcingaPluginPerfDataContent -PerfData $PerformanceData -CheckResultCache $CheckResultCache); Write-Host ([string]::Format('| {0}', $PerfDataOutput)); } @@ -12,6 +14,7 @@ function Get-IcingaPluginPerfDataContent() { param( $PerfData, + $CheckResultCache, [bool]$AsObject = $FALSE ); @@ -21,8 +24,24 @@ function Get-IcingaPluginPerfDataContent() $data = $PerfData[$package]; if ($data.package) { $PerfDataOutput += (Get-IcingaPluginPerfDataContent -PerfData $data.perfdata -AsObject $AsObject); + $PerfDataOutput += (Get-IcingaPluginPerfDataContent -PerfData $data.perfdata -CheckResultCache $CheckResultCache -AsObject $AsObject); } else { - $PerfDataOutput += $data.perfdata; + foreach ($checkresult in $CheckResultCache.PSobject.Properties) { + $SearchPattern = [string]::Format('{0}_', $data.label); + $SearchEntry = $checkresult.Name; + if ($SearchEntry -like "$SearchPattern*") { + $cachedresult = (New-IcingaPerformanceDataEntry -PerfDataObject $data -Label $SearchEntry -Value $checkresult.Value); + + if ($AsObject) { + $global:IcingaThreadContent['Scheduler']['PluginPerfData'] += $cachedresult; + } + $PerfDataOutput += $cachedresult; + } + } + + $compiledPerfData = (New-IcingaPerformanceDataEntry $data); + + $PerfDataOutput += $compiledPerfData; } }