Reworked Icinga Plugin Framework

This commit is contained in:
Lord Hepipud 2019-07-19 19:38:09 +02:00
parent 89f7507809
commit 7fd9f35de0
3 changed files with 468 additions and 171 deletions

View file

@ -1,19 +1,83 @@
Import-IcingaLib icinga\enums; Import-IcingaLib icinga\enums;
Import-IcingaLib core\tools;
function New-IcingaCheck() function New-IcingaCheck()
{ {
param( param(
[string]$Name = '', [string]$Name = '',
$Value = $null, $Value = $null,
$Unit = $null $Unit = $null,
[string]$Minimum = '',
[string]$Maximum = ''
); );
$Check = New-Object -TypeName PSObject; $Check = New-Object -TypeName PSObject;
$Check | Add-Member -membertype NoteProperty -name 'name' -value $Name; $Check | Add-Member -membertype NoteProperty -name 'name' -value $Name;
$Check | Add-Member -membertype NoteProperty -name 'messages' -value @(); $Check | Add-Member -membertype NoteProperty -name 'verbose' -value 0;
$Check | Add-Member -membertype NoteProperty -name 'value' -value $Value; $Check | Add-Member -membertype NoteProperty -name 'messages' -value @();
$Check | Add-Member -membertype NoteProperty -name 'exitcode' -value -1; $Check | Add-Member -membertype NoteProperty -name 'oks' -value @();
$Check | Add-Member -membertype NoteProperty -name 'unit' -value $Unit; $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 { $Check | Add-Member -membertype ScriptMethod -name 'WarnIfLike' -value {
param($warning); param($warning);
@ -71,6 +135,34 @@ function New-IcingaCheck()
return $this; 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 { $Check | Add-Member -membertype ScriptMethod -name 'WarnIfLowerThan' -value {
param($warning); param($warning);
@ -127,6 +219,44 @@ function New-IcingaCheck()
return $this; 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 { $Check | Add-Member -membertype ScriptMethod -name 'CritIfLike' -value {
param($critical); param($critical);
@ -183,6 +313,34 @@ function New-IcingaCheck()
return $this; 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 { $Check | Add-Member -membertype ScriptMethod -name 'CritIfLowerThan' -value {
param($critical); param($critical);
@ -246,16 +404,98 @@ function New-IcingaCheck()
$this.AddMessage([string]::Format( $this.AddMessage([string]::Format(
'{0} {1}{4} is {2} {3}{4}', $this.name, $this.value, $type, $value, $this.unit '{0} {1}{4} is {2} {3}{4}', $this.name, $this.value, $type, $value, $this.unit
), $state); ), $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 { $Check | Add-Member -membertype ScriptMethod -name 'AddMessage' -value {
param($message, $exitcode); param($message, [int]$exitcode);
$this.messages += [string]::Format( [string]$outputMessage = [string]::Format(
'{0}: {1}', '{0}: {1}',
$IcingaEnums.IcingaExitCodeText[$exitcode], $IcingaEnums.IcingaExitCodeText[$exitcode],
$message $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 { $Check | Add-Member -membertype ScriptMethod -name 'SetExitCode' -value {
@ -267,18 +507,41 @@ function New-IcingaCheck()
return $this; 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; $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))) { if ($null -ne $this.unit -And (-Not $IcingaEnums.IcingaMeasurementUnits.ContainsKey($this.unit))) {
Write-Host ( $this.AddMessage(
[string]::Format( [string]::Format(
'Error: Usage of invalid plugin unit "{0}". Allowed units are: {1}', 'Error on check "{0}": Usage of invalid plugin unit "{1}". Allowed units are: {2}',
$this.unit, $this.name,
(($IcingaEnums.IcingaMeasurementUnits.Keys | Sort-Object name) -Join ', ') $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 { $Check | Add-Member -membertype ScriptMethod -name 'SilentCompile' -value {
$this.WriteUnitError(); if ($this.compiled) {
return;
}
$this.AddOkOutput(); $this.AddOkOutput();
$this.compiled = $TRUE;
} }
$Check | Add-Member -membertype ScriptMethod -name 'Compile' -value { $Check | Add-Member -membertype ScriptMethod -name 'Compile' -value {
param([bool]$Verbose = $FALSE); param([bool]$Verbose = $FALSE);
if ($Verbose) { if ($this.compiled) {
Write-Host ($this.messages | Out-String); return;
} }
$this.WriteUnitError();
$this.AddOkOutput(); $this.AddOkOutput();
$this.compiled = $TRUE;
if ($Verbose) {
$this.PrintOutputMessages();
}
return $this.exitcode; 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; return $Check;
} }

View file

@ -1,4 +1,5 @@
Import-IcingaLib icinga\enums; Import-IcingaLib icinga\enums;
Import-IcingaLib core\tools;
function New-IcingaCheckPackage() function New-IcingaCheckPackage()
{ {
@ -7,25 +8,44 @@ function New-IcingaCheckPackage()
[switch]$OperatorAnd, [switch]$OperatorAnd,
[switch]$OperatorOr, [switch]$OperatorOr,
[switch]$OperatorNone, [switch]$OperatorNone,
[int]$OperatorMin = -1, [int]$OperatorMin = -1,
[int]$OperatorMax = -1, [int]$OperatorMax = -1,
[array]$Checks = @() [array]$Checks = @(),
[int]$Verbose = 0
); );
$Check = New-Object -TypeName PSObject; $Check = New-Object -TypeName PSObject;
$Check | Add-Member -membertype NoteProperty -name 'name' -value $Name; $Check | Add-Member -membertype NoteProperty -name 'name' -value $Name;
$Check | Add-Member -membertype NoteProperty -name 'exitcode' -value -1; $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 'checks' -value $Checks;
$Check | Add-Member -membertype NoteProperty -name 'opand' -value $OperatorAnd; $Check | Add-Member -membertype NoteProperty -name 'opand' -value $OperatorAnd;
$Check | Add-Member -membertype NoteProperty -name 'opor' -value $OperatorOr; $Check | Add-Member -membertype NoteProperty -name 'opor' -value $OperatorOr;
$Check | Add-Member -membertype NoteProperty -name 'opnone' -value $OperatorNone; $Check | Add-Member -membertype NoteProperty -name 'opnone' -value $OperatorNone;
$Check | Add-Member -membertype NoteProperty -name 'opmin' -value $OperatorMin; $Check | Add-Member -membertype NoteProperty -name 'opmin' -value $OperatorMin;
$Check | Add-Member -membertype NoteProperty -name 'opmax' -value $OperatorMax; $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 { $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.opand) {
if ($this.CheckAllOk() -eq $FALSE) { if ($this.CheckAllOk() -eq $FALSE) {
@ -59,15 +79,22 @@ function New-IcingaCheckPackage()
} }
} }
if ($this.hasoutput -eq $FALSE) { if ([int]$this.exitcode -eq -1) {
Write-Host $IcingaEnums.IcingaExitCodeText.($this.exitcode); $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; return $this.exitcode;
} }
$Check | Add-Member -membertype ScriptMethod -name 'SilentCompile' -value { $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 { $Check | Add-Member -membertype ScriptMethod -name 'GetOkCount' -value {
@ -114,11 +141,6 @@ function New-IcingaCheckPackage()
$Check | Add-Member -membertype ScriptMethod -name 'CheckOneOk' -value { $Check | Add-Member -membertype ScriptMethod -name 'CheckOneOk' -value {
foreach ($check in $this.checks) { foreach ($check in $this.checks) {
if ([int]$check.exitcode -eq [int]$IcingaEnums.IcingaExitCode.Ok) { 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; $this.exitcode = $check.exitcode;
return $TRUE; return $TRUE;
} }
@ -142,15 +164,66 @@ function New-IcingaCheckPackage()
} }
$Check | Add-Member -membertype ScriptMethod -name 'WriteAllOutput' -value { $Check | Add-Member -membertype ScriptMethod -name 'WriteAllOutput' -value {
$printedOutput = $FALSE;
foreach ($check in $this.checks) { foreach ($check in $this.checks) {
if ($check.messages.Count -ne 0) { $check.PrintAllMessages();
Write-Host ($check.messages | Out-String); }
$printedOutput = $TRUE; }
$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; $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; return $Check;
} }

View file

@ -2,130 +2,32 @@ Import-IcingaLib icinga\enums;
function New-IcingaCheckresult() function New-IcingaCheckresult()
{ {
param(
$Check,
[switch]$NoPerfData,
[switch]$Compile
);
$CheckResult = New-Object -TypeName PSObject; $CheckResult = New-Object -TypeName PSObject;
$CheckResult | Add-Member -membertype NoteProperty -name 'HasResult' -value $FALSE; $CheckResult | Add-Member -membertype NoteProperty -name 'check' -value $Check;
$CheckResult | Add-Member -membertype NoteProperty -name 'ExitCode' -value -1; $CheckResult | Add-Member -membertype NoteProperty -name 'noperfdata' -value $NoPerfData;
$CheckResult | Add-Member -membertype NoteProperty -name 'OutputMessage' -value @();
$CheckResult | Add-Member -membertype NoteProperty -name 'PerfData' -value @();
$CheckResult | Add-Member -membertype ScriptMethod -name 'Matches' -value { $CheckResult | Add-Member -membertype ScriptMethod -name 'Compile' -value {
param($match, $value, $message, [bool]$negate = $FALSE); if ($this.check -eq $null) {
return $IcingaEnums.IcingaExitCode.Unknown;
[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;
}
} }
$message = [string]::Format('{0} (Input value "{1}" {3} matching "{2}")', $message, $value, $match, $phrase); # Compile the check / package if not already done
$this.SetExitCode($exitcode); $this.check.Compile();
$this.AddOutputMessage($message, $exitcode);
Write-Host ([string]::Format('| {0}', $this.check.GetPerfData()));
return $this.check.exitcode;
} }
$CheckResult | Add-Member -membertype ScriptMethod -name 'Lower' -value { if ($Compile) {
param($warning, $critical, $value, $message); return $CheckResult.Compile();
[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);
} }
$CheckResult | Add-Member -membertype ScriptMethod -name 'LowerEqual' -value { return $CheckResult;
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
)
)
}
} }