From 13a54bd117379508793c04ded181cbe863cc9fd9 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Sat, 28 Sep 2019 21:45:59 +0200 Subject: [PATCH] Prepared check plugin and perf-data output for daemon integration --- lib/icinga/plugin/New-IcingaCheck.psm1 | 20 ++++++-- lib/icinga/plugin/New-IcingaCheckPackage.psm1 | 50 ++++++++++++------- lib/icinga/plugin/New-IcingaCheckResult.psm1 | 5 +- .../plugin/Write-IcingaPluginOutput.psm1 | 8 +++ .../plugin/Write-IcingaPluginPerfData.psm1 | 32 ++++++++++++ 5 files changed, 92 insertions(+), 23 deletions(-) create mode 100644 lib/icinga/plugin/Write-IcingaPluginOutput.psm1 create mode 100644 lib/icinga/plugin/Write-IcingaPluginPerfData.psm1 diff --git a/lib/icinga/plugin/New-IcingaCheck.psm1 b/lib/icinga/plugin/New-IcingaCheck.psm1 index 590ca32..35be1e5 100644 --- a/lib/icinga/plugin/New-IcingaCheck.psm1 +++ b/lib/icinga/plugin/New-IcingaCheck.psm1 @@ -36,11 +36,18 @@ function New-IcingaCheck() $Check | Add-Member -membertype NoteProperty -name 'translation' -value $Translation; $Check | Add-Member -membertype NoteProperty -name 'checks' -value $null; $Check | Add-Member -membertype NoteProperty -name 'completed' -value $FALSE; + $Check | Add-Member -membertype NoteProperty -name 'checkcommand' -value ''; $Check | Add-Member -membertype ScriptMethod -name 'AddSpacing' -value { $this.spacing += 1; } + $Check | Add-Member -membertype ScriptMethod -name 'AssignCheckCommand' -value { + param($CheckCommand); + + $this.checkcommand = $CheckCommand; + } + $Check | Add-Member -membertype ScriptMethod -name 'WarnOutOfRange' -value { param($warning); @@ -525,7 +532,7 @@ function New-IcingaCheck() param($msgArray, [string]$spaces); foreach ($msg in $msgArray) { - Write-Host ([string]::Format('{0}{1}', $spaces, $msg)); + Write-IcingaPluginOutput ([string]::Format('{0}{1}', $spaces, $msg)); } } @@ -651,8 +658,8 @@ function New-IcingaCheck() [string]$LabelName = (Format-IcingaPerfDataLabel $this.name); return @{ - 'label' = $LabelName; - 'perfdata' = [string]::Format( + 'label' = $LabelName; + 'perfdata' = [string]::Format( "'{0}'={1}{2};{3};{4}{5}{6} ", $LabelName, (Format-IcingaPerfDataValue $this.value), @@ -662,6 +669,13 @@ function New-IcingaCheck() (Format-IcingaPerfDataValue $this.minimum), (Format-IcingaPerfDataValue $this.maximum) ); + '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(';', '')); + 'package' = $FALSE; }; } diff --git a/lib/icinga/plugin/New-IcingaCheckPackage.psm1 b/lib/icinga/plugin/New-IcingaCheckPackage.psm1 index 8cf4a08..9c2b33e 100644 --- a/lib/icinga/plugin/New-IcingaCheckPackage.psm1 +++ b/lib/icinga/plugin/New-IcingaCheckPackage.psm1 @@ -16,19 +16,20 @@ function New-IcingaCheckPackage() ); $Check = New-Object -TypeName PSObject; - $Check | Add-Member -membertype NoteProperty -name 'name' -value $Name; - $Check | Add-Member -membertype NoteProperty -name 'exitcode' -value -1; - $Check | Add-Member -membertype NoteProperty -name 'verbose' -value $Verbose; - $Check | Add-Member -membertype NoteProperty -name 'hidden' -value $Hidden; - $Check | Add-Member -membertype NoteProperty -name 'checks' -value $Checks; - $Check | Add-Member -membertype NoteProperty -name 'opand' -value $OperatorAnd; - $Check | Add-Member -membertype NoteProperty -name 'opor' -value $OperatorOr; - $Check | Add-Member -membertype NoteProperty -name 'opnone' -value $OperatorNone; - $Check | Add-Member -membertype NoteProperty -name 'opmin' -value $OperatorMin; - $Check | Add-Member -membertype NoteProperty -name 'opmax' -value $OperatorMax; - $Check | Add-Member -membertype NoteProperty -name 'spacing' -value 0; - $Check | Add-Member -membertype NoteProperty -name 'compiled' -value $FALSE; - $Check | Add-Member -membertype NoteProperty -name 'perfdata' -value $FALSE; + $Check | Add-Member -membertype NoteProperty -name 'name' -value $Name; + $Check | Add-Member -membertype NoteProperty -name 'exitcode' -value -1; + $Check | Add-Member -membertype NoteProperty -name 'verbose' -value $Verbose; + $Check | Add-Member -membertype NoteProperty -name 'hidden' -value $Hidden; + $Check | Add-Member -membertype NoteProperty -name 'checks' -value $Checks; + $Check | Add-Member -membertype NoteProperty -name 'opand' -value $OperatorAnd; + $Check | Add-Member -membertype NoteProperty -name 'opor' -value $OperatorOr; + $Check | Add-Member -membertype NoteProperty -name 'opnone' -value $OperatorNone; + $Check | Add-Member -membertype NoteProperty -name 'opmin' -value $OperatorMin; + $Check | Add-Member -membertype NoteProperty -name 'opmax' -value $OperatorMax; + $Check | Add-Member -membertype NoteProperty -name 'spacing' -value 0; + $Check | Add-Member -membertype NoteProperty -name 'compiled' -value $FALSE; + $Check | Add-Member -membertype NoteProperty -name 'perfdata' -value $FALSE; + $Check | Add-Member -membertype NoteProperty -name 'checkcommand' -value ''; $Check | Add-Member -membertype ScriptMethod -name 'Initialise' -value { foreach ($check in $this.checks) { @@ -67,6 +68,16 @@ function New-IcingaCheckPackage() $this.checks += $check; } + $Check | Add-Member -membertype ScriptMethod -name 'AssignCheckCommand' -value { + param($CheckCommand); + + $this.checkcommand = $CheckCommand; + + foreach ($check in $this.checks) { + $check.AssignCheckCommand($CheckCommand); + } + } + $Check | Add-Member -membertype ScriptMethod -name 'Compile' -value { param([bool]$Verbose); @@ -137,7 +148,7 @@ function New-IcingaCheckPackage() $Check | Add-Member -membertype ScriptMethod -name 'CheckMinimumOk' -value { if ($this.opmin -gt $this.checks.Count) { - Write-Host ([string]::Format( + Write-IcingaPluginOutput ([string]::Format( 'Unknown: The minimum argument ({0}) is exceeding the amount of assigned checks ({1}) to this package "{2}"', $this.opmin, $this.checks.Count, $this.name )); @@ -156,7 +167,7 @@ function New-IcingaCheckPackage() $Check | Add-Member -membertype ScriptMethod -name 'CheckMaximumOk' -value { if ($this.opmax -gt $this.checks.Count) { - Write-Host ([string]::Format( + Write-IcingaPluginOutput ([string]::Format( 'Unknown: The maximum argument ({0}) is exceeding the amount of assigned checks ({1}) to this package "{2}"', $this.opmax, $this.checks.Count, $this.name )); @@ -259,7 +270,7 @@ function New-IcingaCheckPackage() $Check | Add-Member -membertype ScriptMethod -name 'PrintNoChecksConfigured' -value { if ($this.checks.Count -eq 0) { - Write-Host ( + Write-IcingaPluginOutput ( [string]::Format( '{0}{1}: No checks configured for package "{2}"', (New-StringTree ($this.spacing + 1)), @@ -281,7 +292,7 @@ function New-IcingaCheckPackage() $outputMessage += ' ({3})'; } - Write-Host ( + Write-IcingaPluginOutput ( [string]::Format( $outputMessage, (New-StringTree $this.spacing), @@ -350,7 +361,7 @@ function New-IcingaCheckPackage() continue; } - $CollectedPerfData.Add($data.label, $data.perfdata); + $CollectedPerfData.Add($data.label, $data); } # Now sort the label output by name @@ -363,7 +374,8 @@ function New-IcingaCheckPackage() return @{ 'label' = $this.name; - 'perfdata' = $perfData; + 'perfdata' = $CollectedPerfData; + 'package' = $TRUE; } } diff --git a/lib/icinga/plugin/New-IcingaCheckResult.psm1 b/lib/icinga/plugin/New-IcingaCheckResult.psm1 index 5e07112..e7bc075 100644 --- a/lib/icinga/plugin/New-IcingaCheckResult.psm1 +++ b/lib/icinga/plugin/New-IcingaCheckResult.psm1 @@ -17,11 +17,14 @@ function New-IcingaCheckresult() return $IcingaEnums.IcingaExitCode.Unknown; } + $CheckCommand = (Get-PSCallStack)[1].Command; + # Compile the check / package if not already done + $this.check.AssignCheckCommand($CheckCommand); $this.check.Compile($TRUE) | Out-Null; if ([int]$this.check.exitcode -ne [int]$IcingaEnums.IcingaExitCode.Unknown -And -Not $this.noperfdata) { - Write-Host ([string]::Format('| {0}', $this.check.GetPerfData().perfdata)); + Write-IcingaPluginPerfData ($this.check.GetPerfData().perfdata); } return $this.check.exitcode; diff --git a/lib/icinga/plugin/Write-IcingaPluginOutput.psm1 b/lib/icinga/plugin/Write-IcingaPluginOutput.psm1 new file mode 100644 index 0000000..e698671 --- /dev/null +++ b/lib/icinga/plugin/Write-IcingaPluginOutput.psm1 @@ -0,0 +1,8 @@ +function Write-IcingaPluginOutput() +{ + param( + $Output + ); + + Write-Host $Output; +} diff --git a/lib/icinga/plugin/Write-IcingaPluginPerfData.psm1 b/lib/icinga/plugin/Write-IcingaPluginPerfData.psm1 new file mode 100644 index 0000000..caab482 --- /dev/null +++ b/lib/icinga/plugin/Write-IcingaPluginPerfData.psm1 @@ -0,0 +1,32 @@ +function Write-IcingaPluginPerfData() +{ + param( + $PerformanceData + ); + + [string]$PerfDataOutput = (Get-IcingaPluginPerfDataContent -PerfData $PerformanceData); + Write-Host ([string]::Format('| {0}', $PerfDataOutput)); +} + +function Get-IcingaPluginPerfDataContent() +{ + param( + $PerfData, + [bool]$AsObject = $FALSE + ); + + [string]$PerfDataOutput = ''; + + foreach ($package in $PerfData.Keys) { + $data = $PerfData[$package]; + if ($data.package) { + $PerfDataOutput += (Get-IcingaPluginPerfDataContent -PerfData $data.perfdata -AsObject $AsObject); + } else { + $PerfDataOutput += $data.perfdata; + } + } + + return $PerfDataOutput; +} + +Export-ModuleMember -Function @( 'Write-IcingaPluginPerfData' );