icinga-powershell-framework/lib/icinga/plugin/New-IcingaCheckPackage.psm1

485 lines
16 KiB
PowerShell
Raw Normal View History

2019-07-18 11:54:39 -04:00
Import-IcingaLib icinga\enums;
2019-07-19 13:38:09 -04:00
Import-IcingaLib core\tools;
2019-07-18 11:54:39 -04:00
function New-IcingaCheckPackage()
{
param(
[string]$Name,
[switch]$OperatorAnd,
[switch]$OperatorOr,
[switch]$OperatorNone,
[int]$OperatorMin = -1,
[int]$OperatorMax = -1,
[array]$Checks = @(),
[int]$Verbose = 0,
[switch]$IgnoreEmptyPackage = $FALSE,
[switch]$Hidden = $FALSE
2019-07-18 11:54:39 -04:00
);
$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 'ignoreemptypackage' -Value $IgnoreEmptyPackage;
$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 NoteProperty -Name 'headermsg' -Value '';
$Check | Add-Member -MemberType NoteProperty -Name 'checkpackage' -Value $TRUE;
$Check | Add-Member -MemberType NoteProperty -Name 'warningchecks' -Value @();
$Check | Add-Member -MemberType NoteProperty -Name 'criticalchecks' -Value @();
$Check | Add-Member -MemberType NoteProperty -Name 'unknownchecks' -Value @();
$Check | Add-Member -MemberType ScriptMethod -Name 'HasChecks' -Value {
if ($this.checks -ne 0) {
return $TRUE
}
return $FALSE;
}
$Check | Add-Member -MemberType ScriptMethod -Name 'Initialise' -Value {
2019-07-19 13:38:09 -04:00
foreach ($check in $this.checks) {
$this.InitCheck($check);
2019-07-19 13:38:09 -04:00
}
}
$Check | Add-Member -MemberType ScriptMethod -Name 'InitCheck' -Value {
param($check);
if ($null -eq $check) {
return;
}
$check.verbose = $this.verbose;
$check.AddSpacing();
$check.SilentCompile();
}
$Check | Add-Member -MemberType ScriptMethod -Name 'AddSpacing' -Value {
2019-07-19 13:38:09 -04:00
$this.spacing += 1;
foreach ($check in $this.checks) {
$check.spacing = $this.spacing;
$check.AddSpacing();
}
2019-07-19 13:38:09 -04:00
}
2019-07-18 11:54:39 -04:00
$Check | Add-Member -MemberType ScriptMethod -Name 'AddCheck' -Value {
param($check);
if ($null -eq $check) {
return;
}
$this.InitCheck($check);
$this.checks += $check;
}
$Check | Add-Member -MemberType ScriptMethod -Name 'GetWarnings' -Value {
foreach ($check in $this.checks) {
$this.warningchecks += $check.GetWarnings();
}
return $this.warningchecks;
}
$Check | Add-Member -MemberType ScriptMethod -Name 'GetCriticals' -Value {
foreach ($check in $this.checks) {
$this.criticalchecks += $check.GetCriticals();
}
return $this.criticalchecks;
}
$Check | Add-Member -MemberType ScriptMethod -Name 'GetUnknowns' -Value {
foreach ($check in $this.checks) {
$this.unknownchecks += $check.GetUnknowns();
}
return $this.unknownchecks;
}
$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);
2019-07-18 11:54:39 -04:00
2019-07-19 13:38:09 -04:00
if ($this.compiled) {
return;
}
2019-07-18 11:54:39 -04:00
$this.compiled = $TRUE;
if ($this.checks.Count -ne 0) {
if ($this.opand) {
if ($this.CheckAllOk() -eq $FALSE) {
$this.GetWorstExitCode();
}
} elseif ($this.opor) {
if ($this.CheckOneOk() -eq $FALSE) {
$this.GetWorstExitCode();
}
} elseif ($this.opnone) {
if ($this.CheckOneOk() -eq $TRUE) {
$this.GetWorstExitCode();
$this.exitcode = $IcingaEnums.IcingaExitCode.Critical;
} else {
$this.exitcode = $IcingaEnums.IcingaExitCode.Ok;
}
} elseif ([int]$this.opmin -ne -1) {
if ($this.CheckMinimumOk() -eq $FALSE) {
$this.GetWorstExitCode();
} else {
$this.exitcode = $IcingaEnums.IcingaExitCode.Ok;
}
} elseif ([int]$this.opmax -ne -1) {
if ($this.CheckMaximumOk() -eq $FALSE) {
$this.GetWorstExitCode();
} else {
$this.exitcode = $IcingaEnums.IcingaExitCode.Ok;
}
2019-07-18 11:54:39 -04:00
}
} else {
if ($this.ignoreemptypackage) {
$this.exitcode = $IcingaEnums.IcingaExitCode.Ok;
} else {
$this.exitcode = $IcingaEnums.IcingaExitCode.Unknown;
}
2019-07-18 11:54:39 -04:00
}
2019-07-19 13:38:09 -04:00
if ([int]$this.exitcode -eq -1) {
$this.exitcode = $IcingaEnums.IcingaExitCode.Ok;
2019-07-18 11:54:39 -04:00
}
if ($Verbose -eq $TRUE) {
2019-07-19 13:38:09 -04:00
$this.PrintOutputMessages();
}
2019-07-18 11:54:39 -04:00
return $this.exitcode;
}
$Check | Add-Member -MemberType ScriptMethod -Name 'SilentCompile' -Value {
$this.Compile($FALSE) | Out-Null;
2019-07-18 11:54:39 -04:00
}
$Check | Add-Member -MemberType ScriptMethod -Name 'GetOkCount' -Value {
2019-07-18 11:54:39 -04:00
[int]$okCount = 0;
foreach ($check in $this.checks) {
if ([int]$check.exitcode -eq [int]$IcingaEnums.IcingaExitCode.Ok) {
$okCount += 1;
}
}
return $okCount;
}
$Check | Add-Member -MemberType ScriptMethod -Name 'CheckMinimumOk' -Value {
if ($this.opmin -gt $this.checks.Count) {
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
)
);
$this.exitcode = $IcingaEnums.IcingaExitCode.Unknown;
return $FALSE;
}
2019-07-18 11:54:39 -04:00
[int]$okCount = $this.GetOkCount();
if ($this.opmin -le $okCount) {
return $TRUE;
}
return $FALSE;
}
$Check | Add-Member -MemberType ScriptMethod -Name 'CheckMaximumOk' -Value {
if ($this.opmax -gt $this.checks.Count) {
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
)
);
$this.exitcode = $IcingaEnums.IcingaExitCode.Unknown;
return $FALSE;
}
2019-07-18 11:54:39 -04:00
[int]$okCount = $this.GetOkCount();
if ($this.opmax -ge $okCount) {
return $TRUE;
}
return $FALSE;
}
$Check | Add-Member -MemberType ScriptMethod -Name 'CheckAllOk' -Value {
2019-07-18 11:54:39 -04:00
foreach ($check in $this.checks) {
if ([int]$check.exitcode -ne [int]$IcingaEnums.IcingaExitCode.Ok) {
return $FALSE;
}
}
return $TRUE;
}
$Check | Add-Member -MemberType ScriptMethod -Name 'CheckOneOk' -Value {
2019-07-18 11:54:39 -04:00
foreach ($check in $this.checks) {
if ([int]$check.exitcode -eq [int]$IcingaEnums.IcingaExitCode.Ok) {
$this.exitcode = $check.exitcode;
return $TRUE;
}
}
return $FALSE;
}
$Check | Add-Member -MemberType ScriptMethod -Name 'GetPackageConfigMessage' -Value {
2019-07-18 11:54:39 -04:00
if ($this.opand) {
return 'Match All';
} elseif ($this.opor) {
return 'Match Any';
} elseif ($this.opnone) {
return 'Match None';
} elseif ([int]$this.opmin -ne -1) {
return [string]::Format('Minimum {0}', $this.opmin)
} elseif ([int]$this.opmax -ne -1) {
return [string]::Format('Maximum {0}', $this.opmax)
}
}
$Check | Add-Member -MemberType ScriptMethod -Name 'PrintOutputMessageSorted' -Value {
param($skipHidden, $skipExitCode);
if ($this.hidden -And $skipHidden) {
return;
}
[hashtable]$MessageOrdering = @{};
2019-07-18 11:54:39 -04:00
foreach ($check in $this.checks) {
if ($this.verbose -eq 0) {
if ([int]$check.exitcode -eq $skipExitCode) {
continue;
}
} elseif ($this.verbose -eq 1) {
if ([int]$check.exitcode -eq $skipExitCode -And $check.checkpackage) {
continue;
}
}
if ($MessageOrdering.ContainsKey($check.Name) -eq $FALSE) {
$MessageOrdering.Add($check.name, $check);
} else {
[int]$DuplicateKeyIndex = 1;
while ($TRUE) {
$newCheckName = [string]::Format('{0}[{1}]', $check.Name, $DuplicateKeyIndex);
if ($MessageOrdering.ContainsKey($newCheckName) -eq $FALSE) {
$MessageOrdering.Add($newCheckName, $check);
break;
}
$DuplicateKeyIndex += 1;
}
}
}
$SortedArray = $MessageOrdering.GetEnumerator() | Sort-Object name;
foreach ($entry in $SortedArray) {
$entry.Value.PrintAllMessages();
2019-07-19 13:38:09 -04:00
}
}
$Check | Add-Member -MemberType ScriptMethod -Name 'WriteAllOutput' -Value {
$this.PrintOutputMessageSorted($TRUE, $IcingaEnums.IcingaExitCode.Ok);
}
$Check | Add-Member -MemberType ScriptMethod -Name 'PrintAllMessages' -Value {
2019-07-19 13:38:09 -04:00
$this.WritePackageOutputStatus();
$this.WriteAllOutput();
}
$Check | Add-Member -MemberType ScriptMethod -Name 'WriteCheckErrors' -Value {
$this.PrintOutputMessageSorted($FALSE, $IcingaEnums.IcingaExitCode.Ok);
2019-07-19 13:38:09 -04:00
}
$Check | Add-Member -MemberType ScriptMethod -Name 'PrintNoChecksConfigured' -Value {
if ($this.checks.Count -eq 0) {
Write-IcingaPluginOutput (
[string]::Format(
'{0}{1} No checks configured for package "{2}"',
(New-StringTree ($this.spacing + 1)),
$IcingaEnums.IcingaExitCodeText.($this.exitcode),
$this.name
)
)
return;
}
}
$Check | Add-Member -MemberType ScriptMethod -Name 'WritePackageOutputStatus' -Value {
if ($this.hidden) {
return;
}
[string]$outputMessage = '{0}{1} Check package "{2}"';
2019-07-19 13:38:09 -04:00
if ($this.verbose -ne 0) {
$outputMessage += ' ({3})';
}
if ($this.exitcode -ne 0 -And $this.spacing -eq 0) {
$outputMessage += ' - {4}';
}
Write-IcingaPluginOutput (
2019-07-19 13:38:09 -04:00
[string]::Format(
$outputMessage,
(New-StringTree $this.spacing),
$IcingaEnums.IcingaExitCodeText.($this.exitcode),
$this.name,
$this.GetPackageConfigMessage(),
$this.headermsg
2019-07-19 13:38:09 -04:00
)
);
}
$Check | Add-Member -MemberType ScriptMethod -Name 'PrintOutputMessages' -Value {
2019-07-19 13:38:09 -04:00
[bool]$printAll = $FALSE;
2019-07-19 13:38:09 -04:00
switch ($this.verbose) {
0 {
# Default value. Only print a package but not the services include
break;
};
1 {
# Include the Operator into the check package result and OK checks of package
2019-07-19 13:38:09 -04:00
break;
};
Default {
$printAll = $TRUE;
break;
}
}
2019-07-19 13:38:09 -04:00
$this.WritePackageOutputStatus();
if ($printAll) {
$this.WriteAllOutput();
$this.PrintNoChecksConfigured();
} else {
2019-07-19 13:38:09 -04:00
if ([int]$this.exitcode -ne $IcingaEnums.IcingaExitCode.Ok) {
$this.WriteCheckErrors();
$this.PrintNoChecksConfigured();
2019-07-19 13:38:09 -04:00
}
2019-07-18 11:54:39 -04:00
}
}
$Check | Add-Member -MemberType ScriptMethod -Name 'AddUniqueSortedChecksToHeader' -Value {
param($checkarray, $state);
[hashtable]$CheckHash = @{};
foreach ($entry in $checkarray) {
if ($CheckHash.ContainsKey($entry) -eq $FALSE) {
$CheckHash.Add($entry, $TRUE);
}
}
[array]$SortedCheckArray = $CheckHash.GetEnumerator() | Sort-Object name;
if ($SortedCheckArray.Count -ne 0) {
$this.headermsg += [string]::Format(
'{0} {1} ',
$IcingaEnums.IcingaExitCodeText[$state],
[string]::Join(', ', $SortedCheckArray.Key)
);
}
}
$Check | Add-Member -MemberType ScriptMethod -Name 'GetWorstExitCode' -Value {
if ([int]$this.exitcode -eq [int]$IcingaEnums.IcingaExitCode.Unknown) {
return;
}
2019-07-18 11:54:39 -04:00
foreach ($check in $this.checks) {
if ([int]$this.exitcode -lt $check.exitcode) {
$this.exitcode = $check.exitcode;
}
$this.criticalchecks += $check.GetCriticals();
$this.warningchecks += $check.GetWarnings();
$this.unknownchecks += $check.GetUnknowns();
2019-07-18 11:54:39 -04:00
}
# Only apply this to our top package
if ($this.spacing -ne 0) {
return;
}
$this.AddUniqueSortedChecksToHeader(
$this.criticalchecks, $IcingaEnums.IcingaExitCode.Critical
);
$this.AddUniqueSortedChecksToHeader(
$this.warningchecks, $IcingaEnums.IcingaExitCode.Warning
);
$this.AddUniqueSortedChecksToHeader(
$this.unknownchecks, $IcingaEnums.IcingaExitCode.Unknown
);
2019-07-19 13:38:09 -04:00
}
2019-07-18 11:54:39 -04:00
$Check | Add-Member -MemberType ScriptMethod -Name 'GetPerfData' -Value {
[string]$perfData = '';
[hashtable]$CollectedPerfData = @{};
# At first lets collect all perf data, but ensure we only add possible label duplication only once
2019-07-19 13:38:09 -04:00
foreach ($check in $this.checks) {
$data = $check.GetPerfData();
if ($null -eq $data -Or $null -eq $data.label) {
continue;
}
if ($CollectedPerfData.ContainsKey($data.label)) {
continue;
}
$CollectedPerfData.Add($data.label, $data);
}
# 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' = $CollectedPerfData;
'package' = $TRUE;
2019-07-18 11:54:39 -04:00
}
}
2019-07-19 13:38:09 -04:00
$Check.Initialise();
2019-07-18 11:54:39 -04:00
return $Check;
}