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

274 lines
8.8 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,
2019-07-19 13:38:09 -04:00
[int]$OperatorMin = -1,
[int]$OperatorMax = -1,
[array]$Checks = @(),
[int]$Verbose = 0
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;
2019-07-19 13:38:09 -04:00
$Check | Add-Member -membertype NoteProperty -name 'verbose' -value $Verbose;
2019-07-18 11:54:39 -04:00
$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;
2019-07-19 13:38:09 -04:00
$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 ScriptMethod -name 'Initialise' -value {
foreach ($check in $this.checks) {
$check.verbose = $this.verbose;
$check.AddSpacing();
$check.SilentCompile();
}
}
$Check | Add-Member -membertype ScriptMethod -name 'AddSpacing' -value {
$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 'Compile' -value {
2019-07-19 13:38:09 -04:00
param([bool]$Silent);
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
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-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 ($Silent -eq $FALSE -And [int]$this.exitcode -ne $IcingaEnums.IcingaExitCode.Unknown) {
2019-07-19 13:38:09 -04:00
$this.PrintOutputMessages();
}
$this.compiled = $TRUE;
2019-07-18 11:54:39 -04:00
return $this.exitcode;
}
$Check | Add-Member -membertype ScriptMethod -name 'SilentCompile' -value {
2019-07-19 13:38:09 -04:00
$this.Compile($TRUE) | Out-Null;
2019-07-18 11:54:39 -04:00
}
$Check | Add-Member -membertype ScriptMethod -name 'GetOkCount' -value {
[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-Host ([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-Host ([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 {
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 {
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 {
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 'WriteAllOutput' -value {
foreach ($check in $this.checks) {
2019-07-19 13:38:09 -04:00
$check.PrintAllMessages();
}
}
$Check | Add-Member -membertype ScriptMethod -name 'PrintAllMessages' -value {
$this.WritePackageOutputStatus();
$this.WriteAllOutput();
}
$Check | Add-Member -membertype ScriptMethod -name 'WriteCheckErrors' -value {
foreach ($check in $this.checks) {
if ([int]$check.exitcode -ne $IcingaEnums.IcingaExitCode.Ok) {
$check.PrintOutputMessages();
2019-07-18 11:54:39 -04:00
}
}
2019-07-19 13:38:09 -04:00
}
$Check | Add-Member -membertype ScriptMethod -name 'WritePackageOutputStatus' -value {
[string]$outputMessage = '{0}{1}: Check package "{2}" is {1}';
if ($this.verbose -ne 0) {
$outputMessage += ' ({3})';
}
Write-Host (
[string]::Format(
$outputMessage,
(New-StringTree $this.spacing),
$IcingaEnums.IcingaExitCodeText.($this.exitcode),
$this.name,
$this.GetPackageConfigMessage()
)
);
}
$Check | Add-Member -membertype ScriptMethod -name 'PrintOutputMessages' -value {
[bool]$printDetails = $FALSE;
[bool]$printAll = $FALSE;
switch ($this.verbose) {
0 { break; };
1 { break; };
2 {
$printDetails = $TRUE;
break;
};
Default {
$printAll = $TRUE;
break;
}
}
$this.WritePackageOutputStatus();
if ($printAll) {
$this.WriteAllOutput();
} elseif ($printDetails) {
# Now print Non-Ok Check outputs in case our package is not Ok
if ([int]$this.exitcode -ne $IcingaEnums.IcingaExitCode.Ok) {
$this.WriteCheckErrors();
}
2019-07-18 11:54:39 -04:00
}
}
$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
$worstCheck = $null;
foreach ($check in $this.checks) {
if ([int]$this.exitcode -lt $check.exitcode) {
$this.exitcode = $check.exitcode;
$worstCheck = $check;
}
}
2019-07-19 13:38:09 -04:00
}
2019-07-18 11:54:39 -04:00
2019-07-19 13:38:09 -04:00
$Check | Add-Member -membertype ScriptMethod -name 'GetPerfData' -value {
[string]$perfData = '';
foreach ($check in $this.checks) {
$perfData += $check.GetPerfData();
2019-07-18 11:54:39 -04:00
}
2019-07-19 13:38:09 -04:00
return $perfData;
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;
}