Fixed and improved Perf Data handling

* Added proper sorting for checks, packages and check results by name
* Fixed perf data formating to be supported by Icinga 2
* Fixed label naming by removing all invalid characters
* Fixed displaying of values by rounding to 2 digits on floats
This commit is contained in:
Lord Hepipud 2019-09-13 20:44:15 +02:00
parent 7d5a1b786e
commit 2c96a60e6d
5 changed files with 86 additions and 19 deletions

View file

@ -0,0 +1,9 @@
function Format-IcingaPerfDataLabel()
{
param(
$PerfData
);
# Remove all special characters and spaces on label names
return ((($PerfData) -Replace ' ', '_') -Replace '[\W]', '');
}

View file

@ -0,0 +1,14 @@
function Format-IcingaPerfDataValue()
{
param(
$PerfValue
);
if ((Test-Numeric $PerfValue) -eq $FALSE) {
return $PerfValue;
}
# Convert our value to a string and replace ',' with a '.' to allow Icinga to parse the output
# In addition, round every output to 2 digits
return (([string]([math]::round($PerfValue, 2))).Replace(',', '.'));
}

View file

@ -408,6 +408,8 @@ function New-IcingaCheck()
$Check | Add-Member -membertype ScriptMethod -name 'TranslateValue' -value { $Check | Add-Member -membertype ScriptMethod -name 'TranslateValue' -value {
param($value); param($value);
$value = Format-IcingaPerfDataValue $value;
if ($null -eq $this.translation -Or $null -eq $value) { if ($null -eq $this.translation -Or $null -eq $value) {
return $value; return $value;
} }
@ -633,7 +635,7 @@ function New-IcingaCheck()
$Check | Add-Member -membertype ScriptMethod -name 'GetPerfData' -value { $Check | Add-Member -membertype ScriptMethod -name 'GetPerfData' -value {
if ($this.completed -Or -Not $this.perfdata) { if ($this.completed -Or -Not $this.perfdata) {
return ''; return $null;
} }
$this.AutodiscoverMinMax(); $this.AutodiscoverMinMax();
@ -646,17 +648,21 @@ function New-IcingaCheck()
} }
$this.completed = $TRUE; $this.completed = $TRUE;
[string]$LabelName = (Format-IcingaPerfDataLabel $this.name);
return [string]::Format( return @{
'label' = $LabelName;
'perfdata' = [string]::Format(
"'{0}'={1}{2};{3};{4}{5}{6} ", "'{0}'={1}{2};{3};{4}{5}{6} ",
$this.name, $LabelName,
$this.value, (Format-IcingaPerfDataValue $this.value),
$this.unit, $this.unit,
$this.warning, (Format-IcingaPerfDataValue $this.warning),
$this.critical, (Format-IcingaPerfDataValue $this.critical),
$this.minimum, (Format-IcingaPerfDataValue $this.minimum),
$this.maximum (Format-IcingaPerfDataValue $this.maximum)
); );
};
} }
$Check | Add-Member -membertype ScriptMethod -name 'AutodiscoverMinMax' -value { $Check | Add-Member -membertype ScriptMethod -name 'AutodiscoverMinMax' -value {

View file

@ -207,8 +207,15 @@ function New-IcingaCheckPackage()
} }
$Check | Add-Member -membertype ScriptMethod -name 'WriteAllOutput' -value { $Check | Add-Member -membertype ScriptMethod -name 'WriteAllOutput' -value {
[hashtable]$MessageOrdering = @{};
foreach ($check in $this.checks) { foreach ($check in $this.checks) {
$check.PrintAllMessages(); $MessageOrdering.Add($check.name, $check);
}
$SortedArray = $MessageOrdering.GetEnumerator() | Sort-Object name;
foreach ($entry in $SortedArray) {
$entry.Value.PrintAllMessages();
} }
} }
@ -218,11 +225,18 @@ function New-IcingaCheckPackage()
} }
$Check | Add-Member -membertype ScriptMethod -name 'WriteCheckErrors' -value { $Check | Add-Member -membertype ScriptMethod -name 'WriteCheckErrors' -value {
[hashtable]$MessageOrdering = @{};
foreach ($check in $this.checks) { foreach ($check in $this.checks) {
if ([int]$check.exitcode -ne $IcingaEnums.IcingaExitCode.Ok) { if ([int]$check.exitcode -ne $IcingaEnums.IcingaExitCode.Ok) {
$check.PrintOutputMessages(); $MessageOrdering.Add($check.name, $check);
} }
} }
$SortedArray = $MessageOrdering.GetEnumerator() | Sort-Object name;
foreach ($entry in $SortedArray) {
$entry.Value.PrintAllMessages();
}
} }
$Check | Add-Member -membertype ScriptMethod -name 'PrintNoChecksConfigured' -value { $Check | Add-Member -membertype ScriptMethod -name 'PrintNoChecksConfigured' -value {
@ -300,11 +314,35 @@ function New-IcingaCheckPackage()
$Check | Add-Member -membertype ScriptMethod -name 'GetPerfData' -value { $Check | Add-Member -membertype ScriptMethod -name 'GetPerfData' -value {
[string]$perfData = ''; [string]$perfData = '';
[hashtable]$CollectedPerfData = @{};
# At first lets collect all perf data, but ensure we only add possible label duplication only once
foreach ($check in $this.checks) { foreach ($check in $this.checks) {
$perfData += $check.GetPerfData(); $data = $check.GetPerfData();
if ($null -eq $data -Or $null -eq $data.label) {
continue;
} }
return $perfData; if ($CollectedPerfData.ContainsKey($data.label)) {
continue;
}
$CollectedPerfData.Add($data.label, $data.perfdata);
}
# Now sort the label output by name
$SortedArray = $CollectedPerfData.GetEnumerator() | Sort-Object name;
# Buold the performance data output based on the sorted result
foreach ($entry in $SortedArray) {
$perfData += $entry.Value;
}
return @{
'label' = $this.name;
'perfdata' = $perfData;
}
} }
$Check.Initialise(); $Check.Initialise();

View file

@ -21,7 +21,7 @@ function New-IcingaCheckresult()
$this.check.Compile($TRUE) | Out-Null; $this.check.Compile($TRUE) | Out-Null;
if ([int]$this.check.exitcode -ne [int]$IcingaEnums.IcingaExitCode.Unknown -And -Not $this.noperfdata) { if ([int]$this.check.exitcode -ne [int]$IcingaEnums.IcingaExitCode.Unknown -And -Not $this.noperfdata) {
Write-Host ([string]::Format('| {0}', $this.check.GetPerfData())); Write-Host ([string]::Format('| {0}', $this.check.GetPerfData().perfdata));
} }
return $this.check.exitcode; return $this.check.exitcode;