mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-21 07:10:15 -05:00
Reworked Icinga Plugin Framework
This commit is contained in:
parent
89f7507809
commit
7fd9f35de0
3 changed files with 468 additions and 171 deletions
|
|
@ -1,19 +1,83 @@
|
|||
Import-IcingaLib icinga\enums;
|
||||
Import-IcingaLib core\tools;
|
||||
|
||||
function New-IcingaCheck()
|
||||
{
|
||||
param(
|
||||
[string]$Name = '',
|
||||
$Value = $null,
|
||||
$Unit = $null
|
||||
[string]$Name = '',
|
||||
$Value = $null,
|
||||
$Unit = $null,
|
||||
[string]$Minimum = '',
|
||||
[string]$Maximum = ''
|
||||
);
|
||||
|
||||
$Check = New-Object -TypeName PSObject;
|
||||
$Check | Add-Member -membertype NoteProperty -name 'name' -value $Name;
|
||||
$Check | Add-Member -membertype NoteProperty -name 'messages' -value @();
|
||||
$Check | Add-Member -membertype NoteProperty -name 'value' -value $Value;
|
||||
$Check | Add-Member -membertype NoteProperty -name 'exitcode' -value -1;
|
||||
$Check | Add-Member -membertype NoteProperty -name 'unit' -value $Unit;
|
||||
$Check | Add-Member -membertype NoteProperty -name 'name' -value $Name;
|
||||
$Check | Add-Member -membertype NoteProperty -name 'verbose' -value 0;
|
||||
$Check | Add-Member -membertype NoteProperty -name 'messages' -value @();
|
||||
$Check | Add-Member -membertype NoteProperty -name 'oks' -value @();
|
||||
$Check | Add-Member -membertype NoteProperty -name 'warnings' -value @();
|
||||
$Check | Add-Member -membertype NoteProperty -name 'criticals' -value @();
|
||||
$Check | Add-Member -membertype NoteProperty -name 'unknowns' -value @();
|
||||
$Check | Add-Member -membertype NoteProperty -name 'value' -value $Value;
|
||||
$Check | Add-Member -membertype NoteProperty -name 'exitcode' -value -1;
|
||||
$Check | Add-Member -membertype NoteProperty -name 'unit' -value $Unit;
|
||||
$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 $TRUE;
|
||||
$Check | Add-Member -membertype NoteProperty -name 'warning' -value '';
|
||||
$Check | Add-Member -membertype NoteProperty -name 'critical' -value '';
|
||||
$Check | Add-Member -membertype NoteProperty -name 'minimum' -value $Minimum;
|
||||
$Check | Add-Member -membertype NoteProperty -name 'maximum' -value $Maximum;
|
||||
$Check | Add-Member -membertype NoteProperty -name 'checks' -value $null;
|
||||
$Check | Add-Member -membertype NoteProperty -name 'completed' -value $FALSE;
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'AddSpacing' -value {
|
||||
$this.spacing += 1;
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'WarnOutOfRange' -value {
|
||||
param($warning);
|
||||
|
||||
if ([string]::IsNullOrEmpty($warning)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((Test-Numeric $warning)) {
|
||||
$this.WarnIfGreaterThan($warning).WarnIfLowerThan(0) | Out-Null;
|
||||
} else {
|
||||
[array]$thresholds = $warning.Split(':');
|
||||
[string]$rangeMin = $thresholds[0];
|
||||
[string]$rangeMax = $thresholds[1];
|
||||
$negate = $rangeMin.Contains('@');
|
||||
$rangeMin = $rangeMin.Replace('@', '');
|
||||
if (-Not $negate -And (Test-Numeric $rangeMin) -And (Test-Numeric $rangeMax)) {
|
||||
$this.WarnIfLowerThan($rangeMin).WarnIfGreaterThan($rangeMax) | Out-Null;
|
||||
} elseif ((Test-Numeric $rangeMin) -And [string]::IsNullOrEmpty($rangeMax) -eq $TRUE) {
|
||||
Write-Host 'lal'
|
||||
$this.WarnIfLowerThan($rangeMin) | Out-Null;
|
||||
} elseif ($rangeMin -eq '~' -And (Test-Numeric $rangeMax)) {
|
||||
$this.WarnIfGreaterThan($rangeMax) | Out-Null;
|
||||
} elseif ($negate -And (Test-Numeric $rangeMin) -And (Test-Numeric $rangeMax)) {
|
||||
$this.WarnIfBetweenAndEqual($rangeMin, $rangeMax) | Out-Null;
|
||||
} else {
|
||||
$this.AddMessage(
|
||||
[string]::Format(
|
||||
'Invalid range specified for Warning argument: "{0}" of check {1}',
|
||||
$warning,
|
||||
$this.name
|
||||
),
|
||||
$IcingaEnums.IcingaExitCode.Unknown
|
||||
)
|
||||
$this.exitcode = $IcingaEnums.IcingaExitCode.Unknown;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
$this.warning = $warning;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'WarnIfLike' -value {
|
||||
param($warning);
|
||||
|
|
@ -71,6 +135,34 @@ function New-IcingaCheck()
|
|||
return $this;
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'WarnIfBetweenAndEqual' -value {
|
||||
param($min, $max);
|
||||
|
||||
if ($this.value -ge $min -And $this.value -le $max) {
|
||||
$this.AddInternalCheckMessage(
|
||||
$IcingaEnums.IcingaExitCode.Warning,
|
||||
[string]::Format('{0}:{1}', $min, $max),
|
||||
'between'
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'WarnIfBetween' -value {
|
||||
param($min, $max);
|
||||
|
||||
if ($this.value -gt $min -And $this.value -lt $max) {
|
||||
$this.AddInternalCheckMessage(
|
||||
$IcingaEnums.IcingaExitCode.Warning,
|
||||
[string]::Format('{0}:{1}', $min, $max),
|
||||
'between'
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'WarnIfLowerThan' -value {
|
||||
param($warning);
|
||||
|
||||
|
|
@ -127,6 +219,44 @@ function New-IcingaCheck()
|
|||
return $this;
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'CritOutOfRange' -value {
|
||||
param($critical);
|
||||
|
||||
if ((Test-Numeric $critical)) {
|
||||
$this.CritIfGreaterThan($critical).CritIfLowerThan(0) | Out-Null;
|
||||
} else {
|
||||
[array]$thresholds = $critical.Split(':');
|
||||
[string]$rangeMin = $thresholds[0];
|
||||
[string]$rangeMax = $thresholds[1];
|
||||
$negate = $rangeMin.Contains('@');
|
||||
$rangeMin = $rangeMin.Replace('@', '');
|
||||
if (-Not $negate -And (Test-Numeric $rangeMin) -And (Test-Numeric $rangeMax)) {
|
||||
$this.CritIfLowerThan($rangeMin).CritIfGreaterThan($rangeMax) | Out-Null;
|
||||
} elseif ((Test-Numeric $rangeMin) -And [string]::IsNullOrEmpty($rangeMax) -eq $TRUE) {
|
||||
$this.CritIfLowerThan($rangeMin) | Out-Null;
|
||||
} elseif ($rangeMin -eq '~' -And (Test-Numeric $rangeMax)) {
|
||||
$this.CritIfGreaterThan($rangeMax) | Out-Null;
|
||||
} elseif ($negate -And (Test-Numeric $rangeMin) -And (Test-Numeric $rangeMax)) {
|
||||
$this.CritIfBetweenAndEqual($rangeMin, $rangeMax) | Out-Null;
|
||||
} else {
|
||||
$this.AddMessage(
|
||||
[string]::Format(
|
||||
'Invalid range specified for Critical argument: "{0}" of check {1}',
|
||||
$critical,
|
||||
$this.name
|
||||
),
|
||||
$IcingaEnums.IcingaExitCode.Unknown
|
||||
)
|
||||
$this.exitcode = $IcingaEnums.IcingaExitCode.Unknown;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
$this.critical = $critical;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'CritIfLike' -value {
|
||||
param($critical);
|
||||
|
||||
|
|
@ -183,6 +313,34 @@ function New-IcingaCheck()
|
|||
return $this;
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'CritIfBetweenAndEqual' -value {
|
||||
param($min, $max);
|
||||
|
||||
if ($this.value -ge $min -And $this.value -le $max) {
|
||||
$this.AddInternalCheckMessage(
|
||||
$IcingaEnums.IcingaExitCode.Critical,
|
||||
[string]::Format('{0}:{1}', $min, $max),
|
||||
'between'
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'CritIfBetween' -value {
|
||||
param($min, $max);
|
||||
|
||||
if ($this.value -gt $min -And $this.value -lt $max) {
|
||||
$this.AddInternalCheckMessage(
|
||||
$IcingaEnums.IcingaExitCode.Critical,
|
||||
[string]::Format('{0}:{1}', $min, $max),
|
||||
'between'
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'CritIfLowerThan' -value {
|
||||
param($critical);
|
||||
|
||||
|
|
@ -246,16 +404,98 @@ function New-IcingaCheck()
|
|||
$this.AddMessage([string]::Format(
|
||||
'{0} {1}{4} is {2} {3}{4}', $this.name, $this.value, $type, $value, $this.unit
|
||||
), $state);
|
||||
|
||||
switch ($state) {
|
||||
$IcingaEnums.IcingaExitCode.Warning {
|
||||
$this.warning = $value;
|
||||
break;
|
||||
};
|
||||
$IcingaEnums.IcingaExitCode.Critical {
|
||||
$this.critical = $value;
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'AddMessage' -value {
|
||||
param($message, $exitcode);
|
||||
param($message, [int]$exitcode);
|
||||
|
||||
$this.messages += [string]::Format(
|
||||
'{0}: {1}',
|
||||
$IcingaEnums.IcingaExitCodeText[$exitcode],
|
||||
$message
|
||||
);
|
||||
[string]$outputMessage = [string]::Format(
|
||||
'{0}: {1}',
|
||||
$IcingaEnums.IcingaExitCodeText[$exitcode],
|
||||
$message
|
||||
);
|
||||
$this.messages += $outputMessage;
|
||||
|
||||
switch ([int]$exitcode) {
|
||||
$IcingaEnums.IcingaExitCode.Ok {
|
||||
$this.oks += $outputMessage;
|
||||
break;
|
||||
};
|
||||
$IcingaEnums.IcingaExitCode.Warning {
|
||||
$this.warnings += $outputMessage;
|
||||
break;
|
||||
};
|
||||
$IcingaEnums.IcingaExitCode.Critical {
|
||||
$this.criticals += $outputMessage;
|
||||
break;
|
||||
};
|
||||
$IcingaEnums.IcingaExitCode.Unknown {
|
||||
$this.unknowns += $outputMessage;
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'PrintOkMessages' -value {
|
||||
param([string]$spaces);
|
||||
$this.OutputMessageArray($this.oks, $spaces);
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'PrintWarningMessages' -value {
|
||||
param([string]$spaces);
|
||||
$this.OutputMessageArray($this.warnings, $spaces);
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'PrintCriticalMessages' -value {
|
||||
param([string]$spaces);
|
||||
$this.OutputMessageArray($this.criticals, $spaces);
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'PrintUnknownMessages' -value {
|
||||
param([string]$spaces);
|
||||
$this.OutputMessageArray($this.unknowns, $spaces);
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'PrintAllMessages' -value {
|
||||
[string]$spaces = New-StringTree $this.spacing;
|
||||
$this.OutputMessageArray($this.unknowns, $spaces);
|
||||
$this.OutputMessageArray($this.criticals, $spaces);
|
||||
$this.OutputMessageArray($this.warnings, $spaces);
|
||||
$this.OutputMessageArray($this.oks, $spaces);
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'OutputMessageArray' -value {
|
||||
param($msgArray, [string]$spaces);
|
||||
|
||||
foreach ($msg in $msgArray) {
|
||||
Write-Host ([string]::Format('{0}{1}', $spaces, $msg));
|
||||
}
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'PrintOutputMessages' -value {
|
||||
[string]$spaces = New-StringTree $this.spacing;
|
||||
if ($this.unknowns.Count -ne 0) {
|
||||
$this.PrintUnknownMessages($spaces);
|
||||
} elseif ($this.criticals.Count -ne 0) {
|
||||
$this.PrintCriticalMessages($spaces);
|
||||
} elseif ($this.warnings.Count -ne 0) {
|
||||
$this.PrintWarningMessages($spaces);
|
||||
} else {
|
||||
if ($this.oks.Count -ne 0) {
|
||||
$this.PrintOkMessages($spaces);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'SetExitCode' -value {
|
||||
|
|
@ -267,18 +507,41 @@ function New-IcingaCheck()
|
|||
return $this;
|
||||
}
|
||||
|
||||
switch ($code) {
|
||||
0 { break; };
|
||||
1 {
|
||||
$this.oks = @();
|
||||
break;
|
||||
};
|
||||
2 {
|
||||
$this.oks = @();
|
||||
$this.warnings = @();
|
||||
break;
|
||||
};
|
||||
3 {
|
||||
$this.oks = @();
|
||||
$this.warnings = @();
|
||||
$this.criticals = @();
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
||||
$this.exitcode = $code;
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'WriteUnitError' -value {
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'ValidateUnit' -value {
|
||||
if ($null -ne $this.unit -And (-Not $IcingaEnums.IcingaMeasurementUnits.ContainsKey($this.unit))) {
|
||||
Write-Host (
|
||||
[string]::Format(
|
||||
'Error: Usage of invalid plugin unit "{0}". Allowed units are: {1}',
|
||||
$this.unit,
|
||||
(($IcingaEnums.IcingaMeasurementUnits.Keys | Sort-Object name) -Join ', ')
|
||||
)
|
||||
);
|
||||
$this.AddMessage(
|
||||
[string]::Format(
|
||||
'Error on check "{0}": Usage of invalid plugin unit "{1}". Allowed units are: {2}',
|
||||
$this.name,
|
||||
$this.unit,
|
||||
(($IcingaEnums.IcingaMeasurementUnits.Keys | Sort-Object name) -Join ', ')
|
||||
),
|
||||
$IcingaEnums.IcingaExitCode.Unknown
|
||||
)
|
||||
$this.unit = '';
|
||||
$this.exitcode = $IcingaEnums.IcingaExitCode.Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -290,22 +553,78 @@ function New-IcingaCheck()
|
|||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'SilentCompile' -value {
|
||||
$this.WriteUnitError();
|
||||
if ($this.compiled) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this.AddOkOutput();
|
||||
$this.compiled = $TRUE;
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'Compile' -value {
|
||||
param([bool]$Verbose = $FALSE);
|
||||
|
||||
if ($Verbose) {
|
||||
Write-Host ($this.messages | Out-String);
|
||||
if ($this.compiled) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this.WriteUnitError();
|
||||
$this.AddOkOutput();
|
||||
$this.compiled = $TRUE;
|
||||
|
||||
if ($Verbose) {
|
||||
$this.PrintOutputMessages();
|
||||
}
|
||||
|
||||
return $this.exitcode;
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'GetPerfData' -value {
|
||||
|
||||
if ($this.completed) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$this.AutodiscoverMinMax();
|
||||
|
||||
if ([string]::IsNullOrEmpty($this.minimum) -eq $FALSE) {
|
||||
$this.minimum = [string]::Format(';{0}', $this.minimum);
|
||||
}
|
||||
if ([string]::IsNullOrEmpty($this.maximum) -eq $FALSE) {
|
||||
$this.maximum = [string]::Format(';{0}', $this.maximum);
|
||||
}
|
||||
|
||||
$this.completed = $TRUE;
|
||||
|
||||
return [string]::Format(
|
||||
"'{0}'={1}{2};{3};{4}{5}{6} ",
|
||||
$this.name,
|
||||
$this.value,
|
||||
$this.unit,
|
||||
$this.warning,
|
||||
$this.critical,
|
||||
$this.minimum,
|
||||
$this.maximum
|
||||
);
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'AutodiscoverMinMax' -value {
|
||||
if ([string]::IsNullOrEmpty($this.minimum) -eq $FALSE -Or [string]::IsNullOrEmpty($this.maximum) -eq $FALSE) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch ($this.unit) {
|
||||
'%' {
|
||||
$this.minimum = '0';
|
||||
$this.maximum = '100';
|
||||
if ($this.value -gt $this.maximum) {
|
||||
$this.maximum = $this.value
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$Check.ValidateUnit();
|
||||
|
||||
return $Check;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
Import-IcingaLib icinga\enums;
|
||||
Import-IcingaLib core\tools;
|
||||
|
||||
function New-IcingaCheckPackage()
|
||||
{
|
||||
|
|
@ -7,25 +8,44 @@ function New-IcingaCheckPackage()
|
|||
[switch]$OperatorAnd,
|
||||
[switch]$OperatorOr,
|
||||
[switch]$OperatorNone,
|
||||
[int]$OperatorMin = -1,
|
||||
[int]$OperatorMax = -1,
|
||||
[array]$Checks = @()
|
||||
[int]$OperatorMin = -1,
|
||||
[int]$OperatorMax = -1,
|
||||
[array]$Checks = @(),
|
||||
[int]$Verbose = 0
|
||||
);
|
||||
|
||||
$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 '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 'hasoutput' -value $TRUE;
|
||||
$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;
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'Compile' -value {
|
||||
param([bool]$Silent);
|
||||
|
||||
Write-Host ([string]::Format('Check result for package {0} ({1}):{2}', $this.name, $this.GetPackageConfigMessage(), "`r`n"));
|
||||
if ($this.compiled) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this.opand) {
|
||||
if ($this.CheckAllOk() -eq $FALSE) {
|
||||
|
|
@ -59,15 +79,22 @@ function New-IcingaCheckPackage()
|
|||
}
|
||||
}
|
||||
|
||||
if ($this.hasoutput -eq $FALSE) {
|
||||
Write-Host $IcingaEnums.IcingaExitCodeText.($this.exitcode);
|
||||
if ([int]$this.exitcode -eq -1) {
|
||||
$this.exitcode = $IcingaEnums.IcingaExitCode.Ok;
|
||||
}
|
||||
|
||||
if ($Silent -eq $FALSE) {
|
||||
#Write-Host ([string]::Format('Check result for package {0} ({1}):{2}', $this.name, $this.GetPackageConfigMessage(), "`r`n"));
|
||||
$this.PrintOutputMessages();
|
||||
}
|
||||
|
||||
$this.compiled = $TRUE;
|
||||
|
||||
return $this.exitcode;
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'SilentCompile' -value {
|
||||
$this.Compile() | Out-Null;
|
||||
$this.Compile($TRUE) | Out-Null;
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'GetOkCount' -value {
|
||||
|
|
@ -114,11 +141,6 @@ function New-IcingaCheckPackage()
|
|||
$Check | Add-Member -membertype ScriptMethod -name 'CheckOneOk' -value {
|
||||
foreach ($check in $this.checks) {
|
||||
if ([int]$check.exitcode -eq [int]$IcingaEnums.IcingaExitCode.Ok) {
|
||||
if ($check.messages.Count -ne 0) {
|
||||
Write-Host ($check.messages | Out-String);
|
||||
} else {
|
||||
$this.hasoutput = $FALSE;
|
||||
}
|
||||
$this.exitcode = $check.exitcode;
|
||||
return $TRUE;
|
||||
}
|
||||
|
|
@ -142,15 +164,66 @@ function New-IcingaCheckPackage()
|
|||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'WriteAllOutput' -value {
|
||||
$printedOutput = $FALSE;
|
||||
foreach ($check in $this.checks) {
|
||||
if ($check.messages.Count -ne 0) {
|
||||
Write-Host ($check.messages | Out-String);
|
||||
$printedOutput = $TRUE;
|
||||
$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();
|
||||
}
|
||||
}
|
||||
if ($printedOutput -eq $FALSE) {
|
||||
$this.hasoutput = $FALSE;
|
||||
}
|
||||
|
||||
$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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -162,15 +235,18 @@ function New-IcingaCheckPackage()
|
|||
$worstCheck = $check;
|
||||
}
|
||||
}
|
||||
|
||||
if ($null -ne $worstCheck) {
|
||||
if ($worstCheck.messages.Count -ne 0) {
|
||||
Write-Host ($worstCheck.messages | Out-String);
|
||||
} else {
|
||||
$this.hasoutput = $FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$Check | Add-Member -membertype ScriptMethod -name 'GetPerfData' -value {
|
||||
[string]$perfData = '';
|
||||
foreach ($check in $this.checks) {
|
||||
$perfData += $check.GetPerfData();
|
||||
}
|
||||
|
||||
return $perfData;
|
||||
}
|
||||
|
||||
$Check.Initialise();
|
||||
|
||||
return $Check;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,130 +2,32 @@ Import-IcingaLib icinga\enums;
|
|||
|
||||
function New-IcingaCheckresult()
|
||||
{
|
||||
param(
|
||||
$Check,
|
||||
[switch]$NoPerfData,
|
||||
[switch]$Compile
|
||||
);
|
||||
|
||||
$CheckResult = New-Object -TypeName PSObject;
|
||||
$CheckResult | Add-Member -membertype NoteProperty -name 'HasResult' -value $FALSE;
|
||||
$CheckResult | Add-Member -membertype NoteProperty -name 'ExitCode' -value -1;
|
||||
$CheckResult | Add-Member -membertype NoteProperty -name 'OutputMessage' -value @();
|
||||
$CheckResult | Add-Member -membertype NoteProperty -name 'PerfData' -value @();
|
||||
$CheckResult | Add-Member -membertype NoteProperty -name 'check' -value $Check;
|
||||
$CheckResult | Add-Member -membertype NoteProperty -name 'noperfdata' -value $NoPerfData;
|
||||
|
||||
$CheckResult | Add-Member -membertype ScriptMethod -name 'Matches' -value {
|
||||
param($match, $value, $message, [bool]$negate = $FALSE);
|
||||
|
||||
[string]$phrase = 'IS';
|
||||
[int]$exitcode = $IcingaEnums.IcingaExitCode.Unknown;
|
||||
|
||||
if ($negate) {
|
||||
if ($value -ne $match) {
|
||||
$phrase = 'IS';
|
||||
$exitcode = $IcingaEnums.IcingaExitCode.Ok;
|
||||
} else {
|
||||
$phrase = 'is NOT';
|
||||
$exitcode = $IcingaEnums.IcingaExitCode.Critical;
|
||||
}
|
||||
} else {
|
||||
if ($value -ne $match) {
|
||||
$phrase = 'is NOT';
|
||||
$exitcode = $IcingaEnums.IcingaExitCode.Critical;
|
||||
} else {
|
||||
$phrase = 'IS';
|
||||
$exitcode = $IcingaEnums.IcingaExitCode.Ok;
|
||||
}
|
||||
$CheckResult | Add-Member -membertype ScriptMethod -name 'Compile' -value {
|
||||
if ($this.check -eq $null) {
|
||||
return $IcingaEnums.IcingaExitCode.Unknown;
|
||||
}
|
||||
|
||||
$message = [string]::Format('{0} (Input value "{1}" {3} matching "{2}")', $message, $value, $match, $phrase);
|
||||
$this.SetExitCode($exitcode);
|
||||
$this.AddOutputMessage($message, $exitcode);
|
||||
# Compile the check / package if not already done
|
||||
$this.check.Compile();
|
||||
|
||||
Write-Host ([string]::Format('| {0}', $this.check.GetPerfData()));
|
||||
|
||||
return $this.check.exitcode;
|
||||
}
|
||||
|
||||
$CheckResult | Add-Member -membertype ScriptMethod -name 'Lower' -value {
|
||||
param($warning, $critical, $value, $message);
|
||||
|
||||
[int]$exitcode = $IcingaEnums.IcingaExitCode.Unknown;
|
||||
|
||||
if ($value -ge $critical) {
|
||||
$exitcode = $IcingaEnums.IcingaExitCode.Critical;
|
||||
} elseif ($value -ge $warning) {
|
||||
$exitcode = $IcingaEnums.IcingaExitCode.Warning;
|
||||
} else {
|
||||
$exitcode = $IcingaEnums.IcingaExitCode.Ok;
|
||||
}
|
||||
|
||||
$this.SetExitCode($exitcode);
|
||||
$this.AddOutputMessage($message, $exitcode);
|
||||
if ($Compile) {
|
||||
return $CheckResult.Compile();
|
||||
}
|
||||
|
||||
$CheckResult | Add-Member -membertype ScriptMethod -name 'LowerEqual' -value {
|
||||
param($warning, $critical, $value, $message);
|
||||
|
||||
[int]$exitcode = $IcingaEnums.IcingaExitCode.Unknown;
|
||||
|
||||
if ($value -gt $critical) {
|
||||
$exitcode = $IcingaEnums.IcingaExitCode.Critical;
|
||||
} elseif ($value -gt $warning) {
|
||||
$exitcode = $IcingaEnums.IcingaExitCode.Warning;
|
||||
} else {
|
||||
$exitcode = $IcingaEnums.IcingaExitCode.Ok;
|
||||
}
|
||||
|
||||
$this.SetExitCode($exitcode);
|
||||
$this.AddOutputMessage($message, $exitcode);
|
||||
}
|
||||
|
||||
$CheckResult | Add-Member -membertype ScriptMethod -name 'Greater' -value {
|
||||
param($warning, $critical, $value, $message);
|
||||
|
||||
[int]$exitcode = $IcingaEnums.IcingaExitCode.Unknown;
|
||||
|
||||
if ($value -le $critical) {
|
||||
$exitcode = $IcingaEnums.IcingaExitCode.Critical;
|
||||
} elseif ($value -le $warning) {
|
||||
$exitcode = $IcingaEnums.IcingaExitCode.Warning;
|
||||
} else {
|
||||
$exitcode = $IcingaEnums.IcingaExitCode.Ok;
|
||||
}
|
||||
|
||||
$this.SetExitCode($exitcode);
|
||||
$this.AddOutputMessage($message, $exitcode);
|
||||
}
|
||||
|
||||
$CheckResult | Add-Member -membertype ScriptMethod -name 'GreaterEqual' -value {
|
||||
param($warning, $critical, $value, $message);
|
||||
|
||||
[int]$exitcode = $IcingaEnums.IcingaExitCode.Unknown;
|
||||
|
||||
if ($value -lt $critical) {
|
||||
$exitcode = $IcingaEnums.IcingaExitCode.Critical;
|
||||
} elseif ($value -lt $warning) {
|
||||
$exitcode = $IcingaEnums.IcingaExitCode.Warning;
|
||||
} else {
|
||||
$exitcode = $IcingaEnums.IcingaExitCode.Ok;
|
||||
}
|
||||
|
||||
$this.SetExitCode($exitcode);
|
||||
$this.AddOutputMessage($message, $exitcode);
|
||||
}
|
||||
|
||||
$CheckResult | Add-Member -membertype ScriptMethod -name 'SetExitCode' -value {
|
||||
param($exitcode);
|
||||
|
||||
# Only overwrite the exit code in case our new value is greater then
|
||||
# the current one Ok > Warning > Critical
|
||||
if ($this.ExitCode -gt $exitcode) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this.ExitCode = $exitcode;
|
||||
}
|
||||
|
||||
$CheckResult | Add-Member -membertype ScriptMethod -name 'AddOutputMessage' -value {
|
||||
param($exitcode, $message);
|
||||
|
||||
$this.OutputMessage.Add(
|
||||
[string]::Format(
|
||||
'{0}: {1}',
|
||||
$IcingaEnums.IcingaExitCodeText[$exitcode],
|
||||
$message
|
||||
)
|
||||
)
|
||||
}
|
||||
return $CheckResult;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue