mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-21 07:10:15 -05:00
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:
parent
580b99b444
commit
d1b30c77ae
5 changed files with 86 additions and 19 deletions
9
lib/core/tools/Format-IcingaPerfDataLabel.psm1
Normal file
9
lib/core/tools/Format-IcingaPerfDataLabel.psm1
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
function Format-IcingaPerfDataLabel()
|
||||||
|
{
|
||||||
|
param(
|
||||||
|
$PerfData
|
||||||
|
);
|
||||||
|
|
||||||
|
# Remove all special characters and spaces on label names
|
||||||
|
return ((($PerfData) -Replace ' ', '_') -Replace '[\W]', '');
|
||||||
|
}
|
||||||
14
lib/core/tools/Format-IcingaPerfDataValue.psm1
Normal file
14
lib/core/tools/Format-IcingaPerfDataValue.psm1
Normal 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(',', '.'));
|
||||||
|
}
|
||||||
|
|
@ -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();
|
||||||
|
|
@ -645,18 +647,22 @@ function New-IcingaCheck()
|
||||||
$this.maximum = [string]::Format(';{0}', $this.maximum);
|
$this.maximum = [string]::Format(';{0}', $this.maximum);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this.completed = $TRUE;
|
$this.completed = $TRUE;
|
||||||
|
[string]$LabelName = (Format-IcingaPerfDataLabel $this.name);
|
||||||
|
|
||||||
return [string]::Format(
|
return @{
|
||||||
"'{0}'={1}{2};{3};{4}{5}{6} ",
|
'label' = $LabelName;
|
||||||
$this.name,
|
'perfdata' = [string]::Format(
|
||||||
$this.value,
|
"'{0}'={1}{2};{3};{4}{5}{6} ",
|
||||||
$this.unit,
|
$LabelName,
|
||||||
$this.warning,
|
(Format-IcingaPerfDataValue $this.value),
|
||||||
$this.critical,
|
$this.unit,
|
||||||
$this.minimum,
|
(Format-IcingaPerfDataValue $this.warning),
|
||||||
$this.maximum
|
(Format-IcingaPerfDataValue $this.critical),
|
||||||
);
|
(Format-IcingaPerfDataValue $this.minimum),
|
||||||
|
(Format-IcingaPerfDataValue $this.maximum)
|
||||||
|
);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
$Check | Add-Member -membertype ScriptMethod -name 'AutodiscoverMinMax' -value {
|
$Check | Add-Member -membertype ScriptMethod -name 'AutodiscoverMinMax' -value {
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
|
@ -299,12 +313,36 @@ 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($CollectedPerfData.ContainsKey($data.label)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$CollectedPerfData.Add($data.label, $data.perfdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $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();
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue