mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-20 23:00:35 -05:00
Merge branch 'master' into feature/rewrite_exception_handler
This commit is contained in:
commit
71f54b29e7
29 changed files with 1988 additions and 1324 deletions
|
|
@ -13,17 +13,23 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
|
|||
|
||||
### Enhancements
|
||||
|
||||
* [#228](https://github.com/Icinga/icinga-powershell-framework/issues/228) Adds feature to suppress any kind of console output except for plugin output and performance data
|
||||
* [#229](https://github.com/Icinga/icinga-powershell-framework/pull/229) CustomFields defined as `SecureString` are now set to `hidden` within the Icinga Director configuration basket - please read the [upgrading docs](30-upgrading-framework.md) carefully
|
||||
* [#234](https://github.com/Icinga/icinga-powershell-framework/pull/234) Adds support to allow custom exception lists for Icinga Exceptions, making it easier for different modules to ship their own exception messages
|
||||
* [#235](https://github.com/Icinga/icinga-powershell-framework/pull/235) Adds new Cmdlet `Show-IcingaEventLogAnalysis` to get a better overview on how many log entries are present within the EventLog based on hour, minute and day average/maximum for allowing a more dynamic configuration for `Invoke-IcingaCheckEventLog`
|
||||
* [#236](https://github.com/Icinga/icinga-powershell-framework/pull/236) Adds feature which stops the Icinga Agent before upgrading the Icinga PowerShell Framework and starting it again afterwards (in case it was running), to resolve some possible file lock issues
|
||||
* [#241](https://github.com/Icinga/icinga-powershell-framework/pull/241) Ensures we use TLS 1.1 and 1.2 for REST-Api calls, as used certificates in general are created with these
|
||||
* [#243](https://github.com/Icinga/icinga-powershell-framework/pull/243) Adds stacktrace output for exceptions in case plugin execution fails
|
||||
* [#248](https://github.com/Icinga/icinga-powershell-framework/pull/248) Improves `Test-IcingaPerformanceCounterCategory` by creating an object for the Performance Counter category provided and checking if it is a valid object instead of relying on the registry which might not contain all categories in the correct language.
|
||||
* [#249](https://github.com/Icinga/icinga-powershell-framework/pull/249) Improves internal exception handler to get rid if misplaced `:` and adds all fields properly
|
||||
* [#250](https://github.com/Icinga/icinga-powershell-framework/pull/250) Improve error handling on plugin execution by informing the user if the plugin is simply not installed or the entire module was not loaded because of errors or missing dependencies
|
||||
|
||||
### Bugfixes
|
||||
|
||||
* [#231](https://github.com/Icinga/icinga-powershell-framework/issues/231) Fixes error while using Icinga Director Self-Service API, in case the host or host API key was deleted inside the Icinga Director and the installation wizard was called with the correct template key, while the old host key was still present inside the Icinga for Windows configuration
|
||||
* [#232](https://github.com/Icinga/icinga-powershell-framework/pull/232) Fixes wrong encoding while using REST-Api checks experimental feature, and now forces UTF8
|
||||
* [#240](https://github.com/Icinga/icinga-powershell-framework/pull/240) While filtering for certain services with `Get-IcingaServices`, there were some attributes missing from the collection. These are now added resulting in always correct output data.
|
||||
* [#245](https://github.com/Icinga/icinga-powershell-framework/pull/245) Fixes loading of `.pfx` certificates by properly checking the file type
|
||||
|
||||
## 1.4.1 (2021-03-10)
|
||||
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ For performance metrics you can provide a `Unit` to ensure your graphing is disp
|
|||
| Unit | Units | | Specify the unit for a value to display graph properly |
|
||||
| Minimum | String | | The minimum value which is displayed on your graphs |
|
||||
| Maximum | String | | The maximum value which is displayed on your graphs |
|
||||
| BaseValue | Object | | Sets a base value for the check which allows to use dynamic `%` usage on thresholds. The base value will calculate the `%` value from the current value, allowing generic `%` monitoring |
|
||||
| ObjectExists | Bool | | If you are using values coming from objects, like Services, you can use this argument to determin if the object itself exist or not. In case it doesn't, you will receive a proper output on the check result |
|
||||
| Translation | Hashtable | | In case you want to map values to certain descriptions, you can place a hashtable at this argument which will then map the value to the description on the check result. For example this would apply to service running states |
|
||||
| LabelName | String | | Allows to override the default label name generated based on the `-Name` argument to a custom name. Please ensure to remove any special characters manually, as the name set here is the exact name for the label |
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ $IcingaPackage = New-IcingaCheckPackage -Name 'My Package' -OperatorAnd;
|
|||
| Checks | Array | | Array of checks to be added to the check package |
|
||||
| Verbose | int | | Defines the level of output detail from 0 lowest to 3 highest detail |
|
||||
| IgnoreEmptyPackage | Switch | | By default a check package will return `Unknown` in case no checks are assigned. Setting this argument will ignore this and return `Ok` instead
|
||||
| AddSummaryHeader | Switch | | Adds a summary on how many checks are Unknown, Critical, Warning and Ok on the output of the package
|
||||
| Hidden | Switch | | If set, the check package doesn't generate output |
|
||||
|
||||
### Examples
|
||||
|
|
|
|||
|
|
@ -20,6 +20,14 @@ function Use-Icinga()
|
|||
Disable-IcingaProgressPreference;
|
||||
|
||||
if ($Minimal) {
|
||||
if ($null -eq $global:Icinga) {
|
||||
$global:Icinga = @{ };
|
||||
}
|
||||
|
||||
if ($global:Icinga.ContainsKey('Minimal') -eq $FALSE) {
|
||||
$global:Icinga.Add('Minimal', $TRUE);
|
||||
}
|
||||
|
||||
# If we load the minimal Framework files, we have to ensure our enums are loaded
|
||||
Import-Module ([string]::Format('{0}\lib\icinga\exception\Icinga_IcingaExceptionEnums.psm1', $PSScriptRoot)) -Global;
|
||||
Import-Module ([string]::Format('{0}\lib\icinga\enums\Icinga_IcingaEnums.psm1', $PSScriptRoot)) -Global;
|
||||
|
|
|
|||
|
|
@ -53,12 +53,18 @@ function Install-IcingaFrameworkUpdate()
|
|||
Write-IcingaConsoleNotice ([string]::Format('Using content of folder "{0}" for updates', $ModuleContent));
|
||||
|
||||
$ServiceStatus = (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue).Status;
|
||||
$AgentStatus = (Get-Service 'icinga2' -ErrorAction SilentlyContinue).Status;
|
||||
|
||||
if ($ServiceStatus -eq 'Running') {
|
||||
Write-IcingaConsoleNotice 'Stopping Icinga PowerShell service';
|
||||
Stop-IcingaService 'icingapowershell';
|
||||
Start-Sleep -Seconds 1;
|
||||
}
|
||||
if ($AgentStatus -eq 'Running') {
|
||||
Write-IcingaConsoleNotice 'Stopping Icinga Agent service';
|
||||
Stop-IcingaService 'icinga2';
|
||||
Start-Sleep -Seconds 1;
|
||||
}
|
||||
|
||||
$ModuleDirectory = (Join-Path -Path $Archive.ModuleRoot -ChildPath $RepositoryName);
|
||||
|
||||
|
|
@ -104,4 +110,8 @@ function Install-IcingaFrameworkUpdate()
|
|||
Write-IcingaConsoleNotice 'Starting Icinga PowerShell service';
|
||||
Start-IcingaService 'icingapowershell';
|
||||
}
|
||||
if ($AgentStatus -eq 'Running') {
|
||||
Write-IcingaConsoleNotice 'Starting Icinga Agent service';
|
||||
Start-IcingaService 'icinga2';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ function Start-IcingaAgentDirectorWizard()
|
|||
[string]$SelfServiceAPIKey = $null,
|
||||
$OverrideDirectorVars = $null,
|
||||
[bool]$RunInstaller = $FALSE,
|
||||
[switch]$ForceTemplateKey
|
||||
[switch]$ForceTemplateKey = $FALSE
|
||||
);
|
||||
|
||||
[hashtable]$DirectorOverrideArgs = @{ }
|
||||
|
|
@ -54,6 +54,19 @@ function Start-IcingaAgentDirectorWizard()
|
|||
}
|
||||
}
|
||||
|
||||
if ([string]::IsNullOrEmpty($LocalAPIKey) -eq $FALSE -And $LocalAPIKey -ne $TemplateKey -And $ForceTemplateKey -eq $FALSE) {
|
||||
try {
|
||||
$Arguments = Get-IcingaDirectorSelfServiceConfig -DirectorUrl $DirectorUrl -ApiKey $LocalAPIKey;
|
||||
} catch {
|
||||
Write-IcingaConsoleError 'Your local stored host key is no longer valid. Using provided template key';
|
||||
|
||||
return Start-IcingaAgentDirectorWizard `
|
||||
-DirectorUrl $DirectorUrl `
|
||||
-SelfServiceAPIKey $TemplateKey `
|
||||
-OverrideDirectorVars $OverrideDirectorVars `
|
||||
-ForceTemplateKey;
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
$Arguments = Get-IcingaDirectorSelfServiceConfig -DirectorUrl $DirectorUrl -ApiKey $SelfServiceAPIKey;
|
||||
} catch {
|
||||
|
|
@ -61,8 +74,8 @@ function Start-IcingaAgentDirectorWizard()
|
|||
|
||||
return Start-IcingaAgentDirectorWizard `
|
||||
-SelfServiceAPIKey ((Get-IcingaAgentInstallerAnswerInput -Prompt 'Please re-enter your SelfService API Key for the Host-Template in case the key is no longer assigned to your host' -Default 'v' -DefaultInput $SelfServiceAPIKey).answer) `
|
||||
-OverrideDirectorVars $OverrideDirectorVars `
|
||||
-ForceTemplateKey;
|
||||
-OverrideDirectorVars $OverrideDirectorVars;
|
||||
}
|
||||
}
|
||||
|
||||
$Arguments = Convert-IcingaDirectorSelfServiceArguments -JsonInput $Arguments;
|
||||
|
|
|
|||
|
|
@ -28,9 +28,17 @@ function Test-IcingaPerformanceCounterCategory()
|
|||
[string]$Category
|
||||
);
|
||||
|
||||
$Counters = Show-IcingaPerformanceCounterCategories -Filter $Category;
|
||||
if ([string]::IsNullOrEmpty($Category)) {
|
||||
return $FALSE;
|
||||
}
|
||||
|
||||
if ($Counters.Count -eq 0) {
|
||||
try {
|
||||
$Counter = New-Object System.Diagnostics.PerformanceCounterCategory($Category);
|
||||
|
||||
if ($null -eq $Counter -Or [string]::IsNullOrEmpty($Counter.CategoryType)) {
|
||||
return $FALSE;
|
||||
}
|
||||
} catch {
|
||||
return $FALSE;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,9 @@ function Convert-Bytes()
|
|||
[string]$Unit
|
||||
);
|
||||
|
||||
# Ensure we always use proper formatting of values
|
||||
$Value = $Value.Replace(',', '.');
|
||||
|
||||
If (($Value -Match "(^[\d\.]*) ?(B|KB|MB|GB|TB|PT|KiB|MiB|GiB|TiB|PiB)") -eq $FALSE) {
|
||||
$Value = [string]::Format('{0}B', $Value);
|
||||
}
|
||||
|
|
|
|||
24
lib/core/tools/Convert-IcingaCheckArgumentToPSObject.psm1
Normal file
24
lib/core/tools/Convert-IcingaCheckArgumentToPSObject.psm1
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
function Convert-IcingaCheckArgumentToPSObject()
|
||||
{
|
||||
param (
|
||||
$Parameter = $null
|
||||
);
|
||||
|
||||
$ParamValue = New-Object -TypeName PSObject;
|
||||
|
||||
if ($null -eq $parameter) {
|
||||
return $ParamValue;
|
||||
}
|
||||
|
||||
$ParamValue | Add-Member -MemberType NoteProperty -Name 'type' -Value (New-Object -TypeName PSObject);
|
||||
$ParamValue | Add-Member -MemberType NoteProperty -Name 'Description' -Value (New-Object -TypeName PSObject);
|
||||
$ParamValue | Add-Member -MemberType NoteProperty -Name 'Attributes' -Value (New-Object -TypeName PSObject);
|
||||
$ParamValue | Add-Member -MemberType NoteProperty -Name 'position' -Value $Parameter.position;
|
||||
$ParamValue | Add-Member -MemberType NoteProperty -Name 'Name' -Value $Parameter.name;
|
||||
$ParamValue | Add-Member -MemberType NoteProperty -Name 'required' -Value $Parameter.required;
|
||||
$ParamValue.type | Add-Member -MemberType NoteProperty -Name 'name' -Value $Parameter.type.name;
|
||||
$ParamValue.Description | Add-Member -MemberType NoteProperty -Name 'Text' -Value $Parameter.Description.Text;
|
||||
$ParamValue.Attributes | Add-Member -MemberType NoteProperty -Name 'ValidValues' -Value $null;
|
||||
|
||||
return $ParamValue;
|
||||
}
|
||||
38
lib/core/tools/Convert-IcingaPluginValueToString.psm1
Normal file
38
lib/core/tools/Convert-IcingaPluginValueToString.psm1
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
function Convert-IcingaPluginValueToString()
|
||||
{
|
||||
param (
|
||||
$Value,
|
||||
[string]$Unit = '',
|
||||
[string]$OriginalUnit = ''
|
||||
);
|
||||
|
||||
$AdjustedValue = $Value;
|
||||
|
||||
if ([string]::IsNullOrEmpty($OriginalUnit)) {
|
||||
$OriginalUnit = $Unit;
|
||||
}
|
||||
|
||||
try {
|
||||
$AdjustedValue = ([math]::Round([decimal]$Value, 6))
|
||||
} catch {
|
||||
$AdjustedValue = $Value;
|
||||
}
|
||||
|
||||
if ($Unit -eq '%' -Or [string]::IsNullOrEmpty($Unit)) {
|
||||
return ([string]::Format('{0}{1}', $AdjustedValue, $Unit));
|
||||
}
|
||||
|
||||
switch ($OriginalUnit) {
|
||||
{ ($_ -eq "B") -or ($_ -eq "KiB") -or ($_ -eq "MiB") -or ($_ -eq "GiB") -or ($_ -eq "TiB") -or ($_ -eq "PiB") -or ($_ -eq "EiB") -or ($_ -eq "ZiB") -or ($_ -eq "YiB") } {
|
||||
return (ConvertTo-BytesNextUnit -Value $Value -Unit $Unit -Units @('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'));
|
||||
};
|
||||
{ ($_ -eq "KB") -or ($_ -eq "MB") -or ($_ -eq "GB") -or ($_ -eq "TB") -or ($_ -eq "PB") -or ($_ -eq "EB") -or ($_ -eq "ZB") -or ($_ -eq "YB") } {
|
||||
return (ConvertTo-BytesNextUnit -Value $Value -Unit $Unit -Units @('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'));
|
||||
};
|
||||
's' {
|
||||
return (ConvertFrom-TimeSpan -Seconds $AdjustedValue)
|
||||
};
|
||||
}
|
||||
|
||||
return ([string]::Format('{0}{1}', $AdjustedValue, $Unit));
|
||||
}
|
||||
14
lib/core/tools/ConvertFrom-Percent.psm1
Normal file
14
lib/core/tools/ConvertFrom-Percent.psm1
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
function ConvertFrom-Percent()
|
||||
{
|
||||
param (
|
||||
$Value = $null,
|
||||
$Percent = $null,
|
||||
[int]$Digits = 0
|
||||
);
|
||||
|
||||
if ($null -eq $Value -Or $null -eq $Percent) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ([math]::Round(($Value / 100 * $Percent), $Digits));
|
||||
}
|
||||
|
|
@ -3,16 +3,51 @@ Import-IcingaLib core\tools;
|
|||
function ConvertFrom-TimeSpan()
|
||||
{
|
||||
param (
|
||||
$Seconds
|
||||
$Seconds = 0
|
||||
);
|
||||
|
||||
$TimeSpan = [TimeSpan]::FromSeconds($Seconds);
|
||||
|
||||
return [string]::Format(
|
||||
'{0}d {1}h {2}m {3}s',
|
||||
$TimeSpan.Days,
|
||||
$TimeSpan.Hours,
|
||||
$TimeSpan.Minutes,
|
||||
$TimeSpan.Seconds
|
||||
if ($TimeSpan.TotalDays -ge 1.0) {
|
||||
return (
|
||||
[string]::Format(
|
||||
'{0}d',
|
||||
$TimeSpan.TotalDays
|
||||
)
|
||||
);
|
||||
}
|
||||
if ($TimeSpan.TotalHours -ge 1.0) {
|
||||
return (
|
||||
[string]::Format(
|
||||
'{0}h',
|
||||
$TimeSpan.TotalHours
|
||||
)
|
||||
);
|
||||
}
|
||||
if ($TimeSpan.TotalMinutes -ge 1.0) {
|
||||
return (
|
||||
[string]::Format(
|
||||
'{0}m',
|
||||
$TimeSpan.TotalMinutes
|
||||
)
|
||||
);
|
||||
}
|
||||
if ($TimeSpan.TotalSeconds -ge 1.0) {
|
||||
return (
|
||||
[string]::Format(
|
||||
'{0}s',
|
||||
$TimeSpan.TotalSeconds
|
||||
)
|
||||
);
|
||||
}
|
||||
if ($TimeSpan.TotalMilliseconds -gt 0) {
|
||||
return (
|
||||
[string]::Format(
|
||||
'{0}ms',
|
||||
$TimeSpan.TotalMilliseconds
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return ([string]::Format('{0}s', $Seconds));
|
||||
}
|
||||
|
|
|
|||
30
lib/core/tools/ConvertTo-BytesNextUnit.psm1
Normal file
30
lib/core/tools/ConvertTo-BytesNextUnit.psm1
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
function ConvertTo-BytesNextUnit()
|
||||
{
|
||||
param (
|
||||
[string]$Value = $null,
|
||||
[string]$Unit = $null,
|
||||
[array]$Units = @('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB')
|
||||
);
|
||||
|
||||
[string]$UnitValue = [string]::Format('{0}{1}', $Value, $Unit);
|
||||
|
||||
while ($TRUE) {
|
||||
$Unit = Get-IcingaNextUnitIteration -Unit $Unit -Units $Units;
|
||||
[decimal]$NewValue = (Convert-Bytes -Value $UnitValue -Unit $Unit).Value;
|
||||
if ($NewValue -ge 1.0) {
|
||||
if ($Unit -eq $RetUnit) {
|
||||
break;
|
||||
}
|
||||
$RetValue = [math]::Round([decimal]$NewValue, 2);
|
||||
$RetUnit = $Unit;
|
||||
} else {
|
||||
if ([string]::IsNullOrEmpty($RetUnit)) {
|
||||
$RetValue = $Value;
|
||||
$RetUnit = 'B';
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ([string]::Format('{0}{1}', $RetValue, $RetUnit));
|
||||
}
|
||||
|
|
@ -77,6 +77,10 @@ function Get-IcingaCheckCommandConfig()
|
|||
[switch]$IcingaConfig
|
||||
);
|
||||
|
||||
[array]$BlacklistedArguments = @(
|
||||
'ThresholdInterval'
|
||||
);
|
||||
|
||||
# Check whether all Checks will be exported or just the ones specified
|
||||
if ([string]::IsNullOrEmpty($CheckName) -eq $true) {
|
||||
$CheckName = (Get-Command Invoke-IcingaCheck*).Name
|
||||
|
|
@ -109,21 +113,50 @@ function Get-IcingaCheckCommandConfig()
|
|||
}
|
||||
);
|
||||
|
||||
$ThresholdIntervalArg = New-Object -TypeName PSObject;
|
||||
$ThresholdIntervalArg | Add-Member -MemberType NoteProperty -Name 'type' -Value (New-Object -TypeName PSObject);
|
||||
$ThresholdIntervalArg | Add-Member -MemberType NoteProperty -Name 'Description' -Value (New-Object -TypeName PSObject);
|
||||
$ThresholdIntervalArg | Add-Member -MemberType NoteProperty -Name 'position' -Value 99;
|
||||
$ThresholdIntervalArg | Add-Member -MemberType NoteProperty -Name 'Name' -Value 'ThresholdInterval';
|
||||
$ThresholdIntervalArg | Add-Member -MemberType NoteProperty -Name 'required' -Value $FALSE;
|
||||
$ThresholdIntervalArg.type | Add-Member -MemberType NoteProperty -Name 'name' -Value 'String';
|
||||
$ThresholdIntervalArg.Description | Add-Member -MemberType NoteProperty -Name 'Text' -Value 'Change the value your defined threshold checks against from the current value to a collected time threshold of the Icinga for Windows daemon, as described here: https://icinga.com/docs/icinga-for-windows/latest/doc/service/10-Register-Service-Checks/ An example for this argument would be 1m or 15m which will use the average of 1m or 15m for monitoring.';
|
||||
|
||||
# Loop through ${CheckName}, to get information on every command specified/all commands.
|
||||
foreach ($check in $CheckName) {
|
||||
|
||||
# Get necessary syntax-information and more through cmdlet "Get-Help"
|
||||
$Data = (Get-Help $check);
|
||||
$ParameterList = (Get-Command -Name $check).Parameters;
|
||||
$CheckParamList = @( $ThresholdIntervalArg );
|
||||
$PluginNameSpace = $Data.Name.Replace('Invoke-', '');
|
||||
|
||||
foreach ($entry in $Data.parameters.parameter) {
|
||||
foreach ($BlackListArg in $BlacklistedArguments) {
|
||||
if ($BlackListArg.ToLower() -eq $entry.Name.ToLower()) {
|
||||
Write-IcingaConsoleError -Message 'The argument "{0}" for check command "{1}" is not allowed, as this is reserved as Framework constant argument and can not be used.' -Objects $BlackListArg, $check;
|
||||
return;
|
||||
}
|
||||
}
|
||||
$CheckParamList += (Convert-IcingaCheckArgumentToPSObject -Parameter $entry);
|
||||
}
|
||||
|
||||
foreach ($arg in $ParameterList.Keys) {
|
||||
foreach ($entry in $CheckParamList) {
|
||||
if ($entry.Name -eq $arg) {
|
||||
$entry.Attributes.ValidValues = $ParameterList[$arg].Attributes.ValidValues;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Add command Structure
|
||||
$Basket.Command.Add(
|
||||
$Data.Name, @{
|
||||
'arguments' = @{
|
||||
# Set the Command handling for every check command
|
||||
'-C' = @{
|
||||
'value' = [string]::Format('try {{ Use-Icinga -Minimal; }} catch {{ Write-Output {1}The Icinga PowerShell Framework is either not installed on the system or not configured properly. Please check https://icinga.com/docs/windows for further details{1}; exit 3; }}; Exit-IcingaExecutePlugin -Command {1}{0}{1} ', $Data.Name, "'");
|
||||
'value' = [string]::Format('try {{ Use-Icinga -Minimal; }} catch {{ Write-Output {1}The Icinga PowerShell Framework is either not installed on the system or not configured properly. Please check https://icinga.com/docs/windows for further details{1}; Write-Output "Error: $$($$_.Exception.Message)Components:`r`n$$( Get-Module -ListAvailable "icinga-powershell-*" )`r`nModule-Path:`r`n$$($$Env:PSModulePath)"; exit 3; }}; Exit-IcingaExecutePlugin -Command {1}{0}{1} ', $Data.Name, "'");
|
||||
'order' = '0';
|
||||
}
|
||||
}
|
||||
|
|
@ -136,7 +169,7 @@ function Get-IcingaCheckCommandConfig()
|
|||
);
|
||||
|
||||
# Loop through parameters of a given command
|
||||
foreach ($parameter in $Data.parameters.parameter) {
|
||||
foreach ($parameter in $CheckParamList) {
|
||||
|
||||
$IsDataList = $FALSE;
|
||||
|
||||
|
|
@ -243,9 +276,9 @@ function Get-IcingaCheckCommandConfig()
|
|||
|
||||
$DataListName = [string]::Format('{0} {1}', $PluginNameSpace, $parameter.Name);
|
||||
|
||||
if ($null -ne $ParameterList[$parameter.Name].Attributes.ValidValues) {
|
||||
if ($null -ne $parameter.Attributes.ValidValues) {
|
||||
$IcingaDataType = 'Datalist';
|
||||
Add-PowerShellDataList -Name $DataListName -Basket $Basket -Arguments $ParameterList[$parameter.Name].Attributes.ValidValues;
|
||||
Add-PowerShellDataList -Name $DataListName -Basket $Basket -Arguments $parameter.Attributes.ValidValues;
|
||||
$IsDataList = $TRUE;
|
||||
} elseif ($parameter.type.name -eq 'SwitchParameter') {
|
||||
$IcingaDataType = 'Boolean';
|
||||
|
|
@ -315,8 +348,13 @@ function Get-IcingaCheckCommandConfig()
|
|||
|
||||
$Data = (Get-Help $check)
|
||||
$PluginNameSpace = $Data.Name.Replace('Invoke-', '');
|
||||
$CheckParamList = @( $ThresholdIntervalArg );
|
||||
|
||||
foreach ($parameter in $Data.parameters.parameter) {
|
||||
foreach ($entry in $Data.parameters.parameter) {
|
||||
$CheckParamList += (Convert-IcingaCheckArgumentToPSObject -Parameter $entry);;
|
||||
}
|
||||
|
||||
foreach ($parameter in $CheckParamList) {
|
||||
$IcingaCustomVariable = [string]::Format('{0}_{1}_{2}', $PluginNameSpace, (Get-Culture).TextInfo.ToTitleCase($parameter.type.name), $parameter.Name);
|
||||
|
||||
# Todo: Should we improve this? Actually the handling would be identical, we just need to assign
|
||||
|
|
|
|||
20
lib/core/tools/Get-IcingaNextUnitIteration.psm1
Normal file
20
lib/core/tools/Get-IcingaNextUnitIteration.psm1
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
function Get-IcingaNextUnitIteration()
|
||||
{
|
||||
param (
|
||||
[string]$Unit = '',
|
||||
[array]$Units = @()
|
||||
);
|
||||
|
||||
[bool]$Found = $FALSE;
|
||||
|
||||
foreach ($entry in $Units) {
|
||||
if ($Found) {
|
||||
return $entry;
|
||||
}
|
||||
if ($entry -eq $Unit) {
|
||||
$Found = $TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
|
@ -10,8 +10,9 @@ function Get-IcingaHelpThresholds()
|
|||
|
||||
if ([string]::IsNullOrEmpty($Value) -eq $FALSE) {
|
||||
$ExampleCheck = New-IcingaCheck -Name 'Example' -Value $Value;
|
||||
$ExampleCheck.WarnOutOfRange($Warning).CritOutOfRange($Critical).Compile($TRUE) | Out-Null;
|
||||
return;
|
||||
$ExampleCheck.WarnOutOfRange($Warning).CritOutOfRange($Critical) | Out-Null;
|
||||
|
||||
return (New-IcingaCheckResult -Check $ExampleCheck -Compile);
|
||||
}
|
||||
|
||||
Write-IcingaConsolePlain
|
||||
|
|
|
|||
|
|
@ -12,11 +12,11 @@
|
|||
[hashtable]$TestIcingaWindowsInfoText = @{
|
||||
1 = 'Everything is fine.';
|
||||
2 = 'No class specified to check';
|
||||
4 = 'Unable to query data from your Windows Cluster. You are either missing permissions or your cluster is not running properly';
|
||||
4 = 'Unable to query data using the given WMI-Class. You are either missing permissions or the service is not running properly';
|
||||
8 = 'The specified WMI Class could not be found in the specified NameSpace.';
|
||||
16 = 'No namespace with the specified name could be found on this system.';
|
||||
32 = 'Windows unhandled exception is thrown. Please enable frame DebugMode for information.';
|
||||
64 = 'The Cluster Service is Stopped or you are not authorized to access the Cluster Service';
|
||||
64 = 'Either the service has been stopped or you are not authorized to access the service.';
|
||||
128 = 'The Cmdlet Get-CimClass is not available on your system.';
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,9 +24,27 @@
|
|||
function Exit-IcingaPluginNotInstalled()
|
||||
{
|
||||
param (
|
||||
$Command
|
||||
[string]$Command
|
||||
);
|
||||
|
||||
$PowerShellModule = Get-Module 'icinga-powershell-*' -ListAvailable |
|
||||
ForEach-Object {
|
||||
foreach ($cmd in $_.ExportedCommands.Values) {
|
||||
if ($Command.ToLower() -eq $cmd.Name.ToLower()) {
|
||||
return $cmd.Source;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ([string]::IsNullOrEmpty($PowerShellModule) -eq $FALSE) {
|
||||
try {
|
||||
Import-Module $PowerShellModule -ErrorAction Stop;
|
||||
} catch {
|
||||
$ExMsg = $_.Exception.Message;
|
||||
Exit-IcingaThrowException -CustomMessage 'Module not loaded' -ExceptionType 'Configuration' -ExceptionThrown $ExMsg -Force;
|
||||
}
|
||||
}
|
||||
|
||||
if ([string]::IsNullOrEmpty($Command)) {
|
||||
Exit-IcingaThrowException -CustomMessage 'Null-Command' -ExceptionType 'Configuration' -ExceptionThrown $IcingaExceptions.Configuration.PluginNotAssigned -Force;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@
|
|||
|
||||
[hashtable]$Configuration = @{
|
||||
PluginArgumentConflict = 'Your plugin argument configuration is causing a conflict. Mostly this error is caused by missmatching configurations by enabling multiple switch arguments which are resulting in a conflicting configuration for the plugin.';
|
||||
PluginArgumentMissing = 'Your plugin argument configuration is missing mandatory arguments. This is error is caused when mandatory or required arguments are missing from a plugin call and the operation is unable to process without them.';
|
||||
PluginArgumentMissing = 'Your plugin argument configuration is missing mandatory arguments. This error is caused when mandatory or required arguments are missing from a plugin call and the operation is unable to process without them.';
|
||||
PluginNotInstalled = 'The plugin assigned to this service check seems not to be installed on this machine. Please review your service check configuration for spelling errors and check if the plugin is installed and executable on this machine by PowerShell.';
|
||||
PluginNotAssigned = 'Your check for this service could not be processed because it seems like no valid Cmdlet was assigned to the check command. Please review your check command to ensure that a valid Cmdlet is assigned and executed by a PowerShell call.';
|
||||
EventLogNotInstalled = 'Your Icinga PowerShell Framework has been executed by an unprivileged user before it was properly installed. The Windows EventLog application could not be registered because the current user has insufficient permissions. Please log into the machine and run "Use-Icinga" once from an administrative shell to complete the setup process. Once done this error should vanish.';
|
||||
|
|
|
|||
421
lib/icinga/plugin/Compare-IcingaPluginThresholds.psm1
Normal file
421
lib/icinga/plugin/Compare-IcingaPluginThresholds.psm1
Normal file
|
|
@ -0,0 +1,421 @@
|
|||
function Compare-IcingaPluginThresholds()
|
||||
{
|
||||
param (
|
||||
[string]$Threshold = $null,
|
||||
$InputValue = $null,
|
||||
$BaseValue = $null,
|
||||
[switch]$Matches = $FALSE,
|
||||
[switch]$NotMatches = $FALSE,
|
||||
[string]$Unit = '',
|
||||
$ThresholdCache = $null,
|
||||
[string]$CheckName = '',
|
||||
[hashtable]$Translation = @{ },
|
||||
$Minium = $null,
|
||||
$Maximum = $null,
|
||||
[switch]$IsBetween = $FALSE,
|
||||
[switch]$IsLowerEqual = $FALSE,
|
||||
[switch]$IsGreaterEqual = $FALSE,
|
||||
[string]$TimeInterval = $null
|
||||
);
|
||||
|
||||
$IcingaThresholds = New-Object -TypeName PSObject;
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'Value' -Value $InputValue;
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'BaseValue' -Value $BaseValue;
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'RawValue' -Value $InputValue;
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'Unit' -Value $Unit;
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'OriginalUnit' -Value $Unit;
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'PerfUnit' -Value $Unit;
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'IcingaThreshold' -Value $Threshold;
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'RawThreshold' -Value $Threshold;
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'CompareValue' -Value $null;
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'MinRangeValue' -Value $null;
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'MaxRangeValue' -Value $null;
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'PercentValue' -Value '';
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'TimeSpan' -Value '';
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'InRange' -Value $TRUE;
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'Message' -Value '';
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'Range' -Value '';
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'FullMessage' -Value (
|
||||
[string]::Format('{0}', (ConvertTo-IcingaPluginOutputTranslation -Translation $Translation -Value (Convert-IcingaPluginValueToString -Unit $Unit -Value $InputValue)))
|
||||
);
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'HeaderValue' -Value $IcingaThresholds.FullMessage;
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'ErrorMessage' -Value '';
|
||||
$IcingaThresholds | Add-Member -MemberType NoteProperty -Name 'HasError' -Value $FALSE;
|
||||
|
||||
# In case we are using % values, we should set the BaseValue always to 100
|
||||
if ($Unit -eq '%' -And $null -eq $BaseValue) {
|
||||
$BaseValue = 100;
|
||||
}
|
||||
|
||||
if ([string]::IsNullOrEmpty($TimeInterval) -eq $FALSE -And $null -ne $ThresholdCache) {
|
||||
$TimeSeconds = ConvertTo-Seconds $TimeInterval;
|
||||
$MinuteInterval = ([TimeSpan]::FromSeconds($TimeSeconds)).Minutes;
|
||||
$CheckPerfDataLabel = [string]::Format('{0}_{1}', (Format-IcingaPerfDataLabel $CheckName), $MinuteInterval);
|
||||
|
||||
if ($null -ne $ThresholdCache.$CheckPerfDataLabel) {
|
||||
$InputValue = $ThresholdCache.$CheckPerfDataLabel;
|
||||
$InputValue = [math]::round([decimal]$InputValue, 6);
|
||||
$IcingaThresholds.TimeSpan = $MinuteInterval;
|
||||
} else {
|
||||
$IcingaThresholds.HasError = $TRUE;
|
||||
$IcingaThresholds.ErrorMessage = [string]::Format(
|
||||
'The provided time interval "{0}" which translates to "{1}m" in your "-ThresholdInterval" argument does not exist',
|
||||
$TimeInterval,
|
||||
$MinuteInterval
|
||||
);
|
||||
}
|
||||
} <#else {
|
||||
# The symbol splitting our threshold from the time index value
|
||||
# Examples:
|
||||
# @20:40#15m
|
||||
# ~:40#15m
|
||||
# 40#15m
|
||||
$TimeIndexSeparator = '#';
|
||||
|
||||
# In case we found a ~ not starting at the beginning, we should load the
|
||||
# time index values created by our background daemon
|
||||
# Allows us to specify something like "40:50#15"
|
||||
if ($Threshold.Contains($TimeIndexSeparator) -And $null -ne $ThresholdCache) {
|
||||
[int]$LastIndex = $Threshold.LastIndexOf($TimeIndexSeparator);
|
||||
if ($LastIndex -ne 0) {
|
||||
$TmpValue = $Threshold;
|
||||
$Threshold = $TmpValue.Substring(0, $LastIndex);
|
||||
$TimeIndex = $TmpValue.Substring($LastIndex + 1, $TmpValue.Length - $LastIndex - 1);
|
||||
$TimeSeconds = ConvertTo-Seconds $TimeIndex;
|
||||
$MinuteInterval = ([TimeSpan]::FromSeconds($TimeSeconds)).Minutes;
|
||||
|
||||
$CheckPerfDataLabel = [string]::Format('{0}_{1}', (Format-IcingaPerfDataLabel $CheckName), $MinuteInterval);
|
||||
|
||||
if ($null -ne $ThresholdCache.$CheckPerfDataLabel) {
|
||||
$InputValue = $ThresholdCache.$CheckPerfDataLabel;
|
||||
$InputValue = [math]::round([decimal]$InputValue, 6);
|
||||
$IcingaThresholds.TimeSpan = $MinuteInterval;
|
||||
} else {
|
||||
$IcingaThresholds.HasError = $TRUE;
|
||||
$IcingaThresholds.ErrorMessage = [string]::Format(
|
||||
'The provided time interval "{0}{1}" which translates to "{2}m" in your "-ThresholdInterval" argument does not exist',
|
||||
$TimeIndexSeparator,
|
||||
$TimeIndex,
|
||||
$MinuteInterval
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}#>
|
||||
|
||||
[bool]$UseDynamicPercentage = $FALSE;
|
||||
[hashtable]$ConvertedThreshold = Convert-IcingaPluginThresholds -Threshold $Threshold;
|
||||
$Minimum = (Convert-IcingaPluginThresholds -Threshold $Minimum).Value;
|
||||
$Maximum = (Convert-IcingaPluginThresholds -Threshold $Maximum).Value;
|
||||
[string]$ThresholdValue = $ConvertedThreshold.Value;
|
||||
$IcingaThresholds.Unit = $ConvertedThreshold.Unit;
|
||||
$IcingaThresholds.IcingaThreshold = $ThresholdValue;
|
||||
$TempValue = (Convert-IcingaPluginThresholds -Threshold ([string]::Format('{0}{1}', $InputValue, $Unit)));
|
||||
$InputValue = $TempValue.Value;
|
||||
$TmpUnit = $TempValue.Unit;
|
||||
$IcingaThresholds.RawValue = $InputValue;
|
||||
$TempValue = (Convert-IcingaPluginThresholds -Threshold ([string]::Format('{0}{1}', $BaseValue, $Unit)));
|
||||
$BaseValue = $TempValue.Value;
|
||||
$Unit = $TmpUnit;
|
||||
$IcingaThresholds.PerfUnit = $Unit;
|
||||
$IcingaThresholds.BaseValue = $BaseValue;
|
||||
|
||||
if ([string]::IsNullOrEmpty($IcingaThresholds.Unit)) {
|
||||
$IcingaThresholds.Unit = $Unit;
|
||||
}
|
||||
|
||||
# Calculate % value from base value of set
|
||||
if ($null -ne $BaseValue -And $IcingaThresholds.Unit -eq '%') {
|
||||
$InputValue = $InputValue / $BaseValue * 100;
|
||||
$UseDynamicPercentage = $TRUE;
|
||||
} elseif ($null -eq $BaseValue -And $IcingaThresholds.Unit -eq '%') {
|
||||
$IcingaThresholds.HasError = $TRUE;
|
||||
$IcingaThresholds.ErrorMessage = 'This argument does not support the % unit';
|
||||
}
|
||||
|
||||
# Always override our InputValue, case we might have change it
|
||||
$IcingaThresholds.Value = $InputValue;
|
||||
|
||||
# If we simply provide a numeric number, we always check Value > Threshold or Value < 0
|
||||
if ($Matches) {
|
||||
# Checks if the InputValue Matches the Threshold
|
||||
if ($InputValue -Like $ThresholdValue) {
|
||||
$IcingaThresholds.InRange = $FALSE;
|
||||
$IcingaThresholds.Message = 'is matching threshold';
|
||||
$IcingaThresholds.Range = [string]::Format(
|
||||
'{0}{1}',
|
||||
(ConvertTo-IcingaPluginOutputTranslation -Translation $Translation -Value $ThresholdValue),
|
||||
$IcingaThresholds.Unit
|
||||
);
|
||||
}
|
||||
} elseif ($NotMatches) {
|
||||
# Checks if the InputValue not Matches the Threshold
|
||||
if ($InputValue -NotLike $ThresholdValue) {
|
||||
$IcingaThresholds.InRange = $FALSE;
|
||||
$IcingaThresholds.Message = 'is not matching threshold';
|
||||
$IcingaThresholds.Range = [string]::Format(
|
||||
'{0}{1}',
|
||||
(ConvertTo-IcingaPluginOutputTranslation -Translation $Translation -Value $ThresholdValue),
|
||||
$IcingaThresholds.Unit
|
||||
);
|
||||
}
|
||||
} elseif ($IsBetween) {
|
||||
if ($InputValue -gt $Minium -And $InputValue -lt $Maximum) {
|
||||
$IcingaThresholds.InRange = $FALSE;
|
||||
$IcingaThresholds.Message = 'is inside range';
|
||||
$IcingaThresholds.Range = [string]::Format(
|
||||
'{0} and {1}',
|
||||
(ConvertTo-IcingaPluginOutputTranslation -Translation $Translation -Value (Convert-IcingaPluginValueToString -Unit $IcingaThresholds.Unit -Value $Minium -OriginalUnit $IcingaThresholds.OriginalUnit)),
|
||||
(ConvertTo-IcingaPluginOutputTranslation -Translation $Translation -Value (Convert-IcingaPluginValueToString -Unit $IcingaThresholds.Unit -Value $Maximum -OriginalUnit $IcingaThresholds.OriginalUnit))
|
||||
);
|
||||
}
|
||||
|
||||
if ($IcingaThresholds.Unit -eq '%') {
|
||||
$IcingaThresholds.RawThreshold = [string]::Format(
|
||||
'{0}% ({2}) {1}% ({3})',
|
||||
(ConvertFrom-Percent -Value $BaseValue -Percent $Minium),
|
||||
(ConvertFrom-Percent -Value $BaseValue -Percent $Maximum),
|
||||
(Convert-IcingaPluginValueToString -Unit $Unit -Value $Minium -OriginalUnit $IcingaThresholds.OriginalUnit),
|
||||
(Convert-IcingaPluginValueToString -Unit $Unit -Value $Maximum -OriginalUnit $IcingaThresholds.OriginalUnit)
|
||||
);
|
||||
$IcingaThresholds.PercentValue = [string]::Format(
|
||||
'@{0}:{1}',
|
||||
(ConvertFrom-Percent -Value $BaseValue -Percent $Minium),
|
||||
(ConvertFrom-Percent -Value $BaseValue -Percent $Maximum)
|
||||
);
|
||||
}
|
||||
} elseif ($IsLowerEqual) {
|
||||
if ($InputValue -le $ThresholdValue) {
|
||||
$IcingaThresholds.InRange = $FALSE;
|
||||
$IcingaThresholds.Message = 'is lower equal than threshold';
|
||||
$IcingaThresholds.Range = [string]::Format(
|
||||
'{0}',
|
||||
(ConvertTo-IcingaPluginOutputTranslation -Translation $Translation -Value (Convert-IcingaPluginValueToString -Unit $IcingaThresholds.Unit -Value $ThresholdValue -OriginalUnit $IcingaThresholds.OriginalUnit))
|
||||
);
|
||||
}
|
||||
|
||||
if ($IcingaThresholds.Unit -eq '%') {
|
||||
$IcingaThresholds.RawThreshold = [string]::Format(
|
||||
'{0}% ({1})',
|
||||
(ConvertFrom-Percent -Value $BaseValue -Percent $ThresholdValue),
|
||||
(Convert-IcingaPluginValueToString -Unit $Unit -Value $ThresholdValue -OriginalUnit $IcingaThresholds.OriginalUnit)
|
||||
);
|
||||
$IcingaThresholds.PercentValue = [string]::Format(
|
||||
'{0}:',
|
||||
(ConvertFrom-Percent -Value $BaseValue -Percent $ThresholdValue)
|
||||
);
|
||||
}
|
||||
} elseif ($IsGreaterEqual) {
|
||||
if ($InputValue -ge $ThresholdValue) {
|
||||
$IcingaThresholds.InRange = $FALSE;
|
||||
$IcingaThresholds.Message = 'is greater equal than threshold';
|
||||
$IcingaThresholds.Range = [string]::Format(
|
||||
'{0}',
|
||||
(ConvertTo-IcingaPluginOutputTranslation -Translation $Translation -Value (Convert-IcingaPluginValueToString -Unit $IcingaThresholds.Unit -Value $ThresholdValue -OriginalUnit $IcingaThresholds.OriginalUnit))
|
||||
);
|
||||
}
|
||||
|
||||
if ($IcingaThresholds.Unit -eq '%') {
|
||||
$IcingaThresholds.RawThreshold = [string]::Format(
|
||||
'{0}% ({1})',
|
||||
(ConvertFrom-Percent -Value $BaseValue -Percent $ThresholdValue),
|
||||
(Convert-IcingaPluginValueToString -Unit $Unit -Value $ThresholdValue -OriginalUnit $IcingaThresholds.OriginalUnit)
|
||||
);
|
||||
|
||||
$IcingaThresholds.PercentValue = [string]::Format(
|
||||
'~:{0}',
|
||||
(ConvertFrom-Percent -Value $BaseValue -Percent $ThresholdValue)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if ((Test-Numeric $ThresholdValue)) {
|
||||
if ($InputValue -gt $ThresholdValue -Or $InputValue -lt 0) {
|
||||
$IcingaThresholds.InRange = $FALSE;
|
||||
$IcingaThresholds.Message = 'is greater than threshold';
|
||||
$IcingaThresholds.Range = [string]::Format('{0}', (Convert-IcingaPluginValueToString -Unit $Unit -Value $ThresholdValue -OriginalUnit $IcingaThresholds.OriginalUnit));
|
||||
}
|
||||
|
||||
$IcingaThresholds.CompareValue = [decimal]$ThresholdValue;
|
||||
|
||||
if ($IcingaThresholds.Unit -eq '%') {
|
||||
$IcingaThresholds.RawThreshold = [string]::Format('{0}% ({1})', $ThresholdValue, (Convert-IcingaPluginValueToString -Unit $Unit -Value (ConvertFrom-Percent -Value $BaseValue -Percent $ThresholdValue) -OriginalUnit $IcingaThresholds.OriginalUnit));
|
||||
|
||||
$IcingaThresholds.PercentValue = [string]::Format(
|
||||
'{0}',
|
||||
(ConvertFrom-Percent -Value $BaseValue -Percent $ThresholdValue)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
# Transform our provided thresholds to split everything into single objects
|
||||
[array]$thresholds = $ThresholdValue.Split(':');
|
||||
[string]$rangeMin = $thresholds[0];
|
||||
[string]$rangeMax = $thresholds[1];
|
||||
[bool]$IsNegating = $rangeMin.Contains('@');
|
||||
[string]$rangeMin = $rangeMin.Replace('@', '');
|
||||
|
||||
if ((Test-Numeric ($rangeMin.Replace('@', '').Replace('~', '')))) {
|
||||
$IcingaThresholds.MinRangeValue = [decimal]($rangeMin.Replace('@', '').Replace('~', ''));
|
||||
}
|
||||
if ((Test-Numeric $rangeMax)) {
|
||||
$IcingaThresholds.MaxRangeValue = [decimal]$rangeMax;
|
||||
}
|
||||
|
||||
if ($IsNegating -eq $FALSE -And (Test-Numeric $rangeMin) -And (Test-Numeric $rangeMax)) {
|
||||
# Handles: 30:40
|
||||
# Error on: < 30 or > 40
|
||||
# Ok on: between {30 .. 40}
|
||||
|
||||
if ($InputValue -lt $rangeMin -Or $InputValue -gt $rangeMax) {
|
||||
$IcingaThresholds.InRange = $FALSE;
|
||||
$IcingaThresholds.Message = 'is outside range';
|
||||
$IcingaThresholds.Range = [string]::Format(
|
||||
'{0} and {1}',
|
||||
(ConvertTo-IcingaPluginOutputTranslation -Translation $Translation -Value (Convert-IcingaPluginValueToString -Unit $IcingaThresholds.Unit -Value $rangeMin -OriginalUnit $IcingaThresholds.OriginalUnit)),
|
||||
(ConvertTo-IcingaPluginOutputTranslation -Translation $Translation -Value (Convert-IcingaPluginValueToString -Unit $IcingaThresholds.Unit -Value $rangeMax -OriginalUnit $IcingaThresholds.OriginalUnit))
|
||||
);
|
||||
}
|
||||
|
||||
if ($IcingaThresholds.Unit -eq '%') {
|
||||
$IcingaThresholds.RawThreshold = [string]::Format(
|
||||
'{0}% ({2}) and {1}% ({3})',
|
||||
$rangeMin,
|
||||
$rangeMax,
|
||||
(Convert-IcingaPluginValueToString -Unit $Unit -Value (ConvertFrom-Percent -Value $BaseValue -Percent $rangeMin -OriginalUnit $IcingaThresholds.OriginalUnit)),
|
||||
(Convert-IcingaPluginValueToString -Unit $Unit -Value (ConvertFrom-Percent -Value $BaseValue -Percent $rangeMax -OriginalUnit $IcingaThresholds.OriginalUnit))
|
||||
);
|
||||
|
||||
$IcingaThresholds.PercentValue = [string]::Format(
|
||||
'{0}:{1}',
|
||||
(ConvertFrom-Percent -Value $BaseValue -Percent $rangeMin),
|
||||
(ConvertFrom-Percent -Value $BaseValue -Percent $rangeMax)
|
||||
);
|
||||
}
|
||||
} elseif ((Test-Numeric $rangeMin) -And [string]::IsNullOrEmpty($rangeMax) -eq $TRUE) {
|
||||
# Handles: 20:
|
||||
# Error on: 20:
|
||||
# Ok on: between 20 .. ∞
|
||||
|
||||
if ($InputValue -lt $rangeMin) {
|
||||
$IcingaThresholds.InRange = $FALSE;
|
||||
$IcingaThresholds.Message = 'is lower than threshold';
|
||||
$IcingaThresholds.Range = [string]::Format(
|
||||
'{0}',
|
||||
(ConvertTo-IcingaPluginOutputTranslation -Translation $Translation -Value (Convert-IcingaPluginValueToString -Unit $IcingaThresholds.Unit -Value $rangeMin -OriginalUnit $IcingaThresholds.OriginalUnit))
|
||||
);
|
||||
}
|
||||
|
||||
if ($IcingaThresholds.Unit -eq '%') {
|
||||
$IcingaThresholds.RawThreshold = [string]::Format(
|
||||
'{0}% ({1})',
|
||||
$rangeMin,
|
||||
(Convert-IcingaPluginValueToString -Unit $Unit -Value (ConvertFrom-Percent -Value $BaseValue -Percent $rangeMin -OriginalUnit $IcingaThresholds.OriginalUnit))
|
||||
);
|
||||
|
||||
$IcingaThresholds.PercentValue = [string]::Format(
|
||||
'{0}:',
|
||||
(ConvertFrom-Percent -Value $BaseValue -Percent $rangeMin)
|
||||
);
|
||||
}
|
||||
} elseif ($rangeMin -eq '~' -And (Test-Numeric $rangeMax)) {
|
||||
# Handles: ~:20
|
||||
# Error on: > 20
|
||||
# Ok on: between -∞ .. 20
|
||||
|
||||
if ($InputValue -gt $rangeMax) {
|
||||
$IcingaThresholds.InRange = $FALSE;
|
||||
$IcingaThresholds.Message = 'is greater than threshold';
|
||||
$IcingaThresholds.Range = [string]::Format(
|
||||
'{0}',
|
||||
(ConvertTo-IcingaPluginOutputTranslation -Translation $Translation -Value (Convert-IcingaPluginValueToString -Unit $IcingaThresholds.Unit -Value $rangeMax -OriginalUnit $IcingaThresholds.OriginalUnit))
|
||||
);
|
||||
}
|
||||
|
||||
if ($IcingaThresholds.Unit -eq '%') {
|
||||
$IcingaThresholds.RawThreshold = [string]::Format(
|
||||
'{0}% ({1})',
|
||||
$rangeMax,
|
||||
(Convert-IcingaPluginValueToString -Unit $Unit -Value (ConvertFrom-Percent -Value $BaseValue -Percent $rangeMax -OriginalUnit $IcingaThresholds.OriginalUnit))
|
||||
);
|
||||
|
||||
$IcingaThresholds.PercentValue = [string]::Format(
|
||||
'~:{0}',
|
||||
(ConvertFrom-Percent -Value $BaseValue -Percent $rangeMax)
|
||||
);
|
||||
}
|
||||
} elseif ($IsNegating -And (Test-Numeric $rangeMin) -And (Test-Numeric $rangeMax)) {
|
||||
# Handles: @30:40
|
||||
# Error on: ≥ 30 and ≤ 40
|
||||
# Ok on: -∞ .. 29 and 41 .. ∞
|
||||
|
||||
if ($InputValue -ge $rangeMin -And $InputValue -le $rangeMax) {
|
||||
$IcingaThresholds.InRange = $FALSE;
|
||||
$IcingaThresholds.Message = 'is inside range';
|
||||
$IcingaThresholds.Range = [string]::Format(
|
||||
'{0} and {1}',
|
||||
(ConvertTo-IcingaPluginOutputTranslation -Translation $Translation -Value (Convert-IcingaPluginValueToString -Unit $IcingaThresholds.Unit -Value $rangeMin -OriginalUnit $IcingaThresholds.OriginalUnit)),
|
||||
(ConvertTo-IcingaPluginOutputTranslation -Translation $Translation -Value (Convert-IcingaPluginValueToString -Unit $IcingaThresholds.Unit -Value $rangeMax -OriginalUnit $IcingaThresholds.OriginalUnit))
|
||||
);
|
||||
}
|
||||
|
||||
if ($IcingaThresholds.Unit -eq '%') {
|
||||
$IcingaThresholds.RawThreshold = [string]::Format(
|
||||
'{0}% ({2}) {1}% ({3})',
|
||||
$rangeMin,
|
||||
$rangeMax,
|
||||
(Convert-IcingaPluginValueToString -Unit $Unit -Value (ConvertFrom-Percent -Value $BaseValue -Percent $rangeMin -OriginalUnit $IcingaThresholds.OriginalUnit)),
|
||||
(Convert-IcingaPluginValueToString -Unit $Unit -Value (ConvertFrom-Percent -Value $BaseValue -Percent $rangeMax -OriginalUnit $IcingaThresholds.OriginalUnit))
|
||||
);
|
||||
|
||||
$IcingaThresholds.PercentValue = [string]::Format(
|
||||
'@{0}:{1}',
|
||||
(ConvertFrom-Percent -Value $BaseValue -Percent $rangeMin),
|
||||
(ConvertFrom-Percent -Value $BaseValue -Percent $rangeMax)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if ([string]::IsNullOrEmpty($Threshold) -eq $FALSE) {
|
||||
# Unhandled
|
||||
$IcingaThresholds.ErrorMessage = [string]::Format(
|
||||
'Invalid range specified for threshold: InputValue "{0}" and Threshold {1}',
|
||||
$InputValue,
|
||||
$Threshold
|
||||
);
|
||||
$IcingaThresholds.HasError = $TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$PluginOutputMessage = [System.Text.StringBuilder]::New();
|
||||
|
||||
[string]$PluginCurrentValue = [string]::Format(
|
||||
'{0}',
|
||||
(ConvertTo-IcingaPluginOutputTranslation -Translation $Translation -Value (Convert-IcingaPluginValueToString -Unit $IcingaThresholds.Unit -Value $IcingaThresholds.Value -OriginalUnit $IcingaThresholds.OriginalUnit))
|
||||
);
|
||||
|
||||
[string]$PluginThresholdValue = $IcingaThresholds.Range;
|
||||
|
||||
if ($UseDynamicPercentage -And $Unit -ne '%') {
|
||||
$IcingaThresholds.IcingaThreshold = $IcingaThresholds.PercentValue;
|
||||
$PluginCurrentValue = [string]::Format('{0}% ({1})', ([math]::Round($IcingaThresholds.Value, 2)), (Convert-IcingaPluginValueToString -Unit $Unit -Value $IcingaThresholds.RawValue -OriginalUnit $IcingaThresholds.OriginalUnit));
|
||||
$PluginThresholdValue = $IcingaThresholds.RawThreshold;
|
||||
}
|
||||
|
||||
$IcingaThresholds.HeaderValue = $PluginCurrentValue;
|
||||
$PluginOutputMessage.Append($PluginCurrentValue) | Out-Null;
|
||||
|
||||
if ([string]::IsNullOrEmpty($IcingaThresholds.Message) -eq $FALSE) {
|
||||
$PluginOutputMessage.Append(' ') | Out-Null;
|
||||
$PluginOutputMessage.Append($IcingaThresholds.Message) | Out-Null;
|
||||
|
||||
if ([string]::IsNullOrEmpty($PluginThresholdValue) -eq $FALSE) {
|
||||
$PluginOutputMessage.Append(' ') | Out-Null;
|
||||
$PluginOutputMessage.Append($PluginThresholdValue) | Out-Null;
|
||||
}
|
||||
}
|
||||
|
||||
# Lets build our full message for adding on the value
|
||||
$IcingaThresholds.FullMessage = $PluginOutputMessage.ToString();
|
||||
|
||||
return $IcingaThresholds;
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
function ConvertTo-IcingaPluginOutputTranslation()
|
||||
{
|
||||
param (
|
||||
$Value = $null,
|
||||
[hashtable]$Translation = @{ }
|
||||
);
|
||||
|
||||
if ($null -eq $Value) {
|
||||
return 'Nothing';
|
||||
}
|
||||
|
||||
if ($null -eq $Translation -Or $Translation.Count -eq 0) {
|
||||
return $Value;
|
||||
}
|
||||
|
||||
[array]$TranslationKeys = $Translation.Keys;
|
||||
[array]$TranslationValues = $Translation.Values;
|
||||
[int]$Index = 0;
|
||||
[bool]$FoundTranslation = $FALSE;
|
||||
|
||||
foreach ($entry in $TranslationKeys) {
|
||||
if (([string]($Value)).ToLower() -eq ([string]($entry)).ToLower()) {
|
||||
$FoundTranslation = $TRUE;
|
||||
break;
|
||||
}
|
||||
$Index += 1;
|
||||
}
|
||||
|
||||
if ($FoundTranslation -eq $FALSE) {
|
||||
return $Value;
|
||||
}
|
||||
|
||||
return $TranslationValues[$Index];
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
189
lib/icinga/plugin/New-IcingaCheckBaseObject.psm1
Normal file
189
lib/icinga/plugin/New-IcingaCheckBaseObject.psm1
Normal file
|
|
@ -0,0 +1,189 @@
|
|||
function New-IcingaCheckBaseObject()
|
||||
{
|
||||
$IcingaCheckBaseObject = New-Object -TypeName PSObject;
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType NoteProperty -Name 'Name' -Value '';
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType NoteProperty -Name 'Verbose' -Value 0;
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType NoteProperty -Name '__CheckPerfData' -Value @{ };
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType NoteProperty -Name '__Hidden' -Value $FALSE;
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType NoteProperty -Name '__Parent' -Value $IcingaCheckBaseObject;
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType NoteProperty -Name '__Indention' -Value 0;
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType NoteProperty -Name '__ErrorMessage' -Value '';
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType NoteProperty -Name '__CheckState' -Value $IcingaEnums.IcingaExitCode.Ok;
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType NoteProperty -Name '__CheckCommand' -Value '';
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType NoteProperty -Name '__CheckOutput' -Value $null;
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType NoteProperty -Name '__ObjectType' -Value 'IcingaCheckBaseObject';
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__SetCheckCommand' -Value {
|
||||
$CallStack = Get-PSCallStack;
|
||||
|
||||
foreach ($entry in $CallStack) {
|
||||
[string]$CheckCommand = $entry.Command;
|
||||
if ($CheckCommand.ToLower() -Like 'invoke-icingacheck*') {
|
||||
$this.__CheckCommand = $CheckCommand;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($null -eq $Global:Icinga) {
|
||||
$Global:Icinga = @{ };
|
||||
}
|
||||
|
||||
if ($Global:Icinga.ContainsKey('ThresholdCache') -eq $FALSE) {
|
||||
$Global:Icinga.Add('ThresholdCache', @{ });
|
||||
}
|
||||
|
||||
if ($Global:Icinga.ThresholdCache.ContainsKey($this.__CheckCommand)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$Global:Icinga.ThresholdCache.Add(
|
||||
$this.__CheckCommand,
|
||||
(Get-IcingaCacheData -Space 'sc_daemon' -CacheStore 'checkresult' -KeyName $this.__CheckCommand)
|
||||
);
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__SetParent' -Value {
|
||||
param ($Parent);
|
||||
|
||||
$this.__Parent = $Parent;
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__GetParent' -Value {
|
||||
return $this.__Parent;
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__IsHidden' -Value {
|
||||
return $this.__Hidden;
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__SetHidden' -Value {
|
||||
param ([bool]$Hidden);
|
||||
|
||||
$this.__Hidden = $Hidden;
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__GetName' -Value {
|
||||
return $this.Name;
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__SetIndention' -Value {
|
||||
param ($Indention);
|
||||
|
||||
$this.__Indention = $Indention;
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__GetIndention' -Value {
|
||||
return $this.__Indention;
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__NewIndention' -Value {
|
||||
return ($this.__Indention + 1);
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__GetCheckState' -Value {
|
||||
return $this.__CheckState;
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__GetCheckCommand' -Value {
|
||||
return $this.__CheckCommand;
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Force -Name '__SetCheckOutput' -Value {
|
||||
param ($PluginOutput);
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__GetCheckOutput' -Value {
|
||||
|
||||
if ($this.__IsHidden()) {
|
||||
return ''
|
||||
};
|
||||
|
||||
if ($this._CanOutput() -eq $FALSE) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return (
|
||||
[string]::Format(
|
||||
'{0}{1}',
|
||||
(New-StringTree -Spacing $this.__GetIndention()),
|
||||
$this.__CheckOutput
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name 'Compile' -Value {
|
||||
$this.__ValidateThresholdInput();
|
||||
$this.__SetCheckOutput();
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__SetVerbosity' -Value {
|
||||
param ($Verbosity);
|
||||
|
||||
$this.Verbose = $Verbosity;
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__GetVerbosity' -Value {
|
||||
return $this.Verbose;
|
||||
}
|
||||
|
||||
# Shared function
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__GetHeaderOutputValue' -Value {
|
||||
return '';
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '_CanOutput' -Value {
|
||||
# Always allow the output of the top parent elements
|
||||
if ($this.__GetIndention() -eq 0) {
|
||||
return $TRUE;
|
||||
}
|
||||
|
||||
switch ($this.Verbose) {
|
||||
0 { # Only print states not being OK
|
||||
if ($this.__CheckState -ne $IcingaEnums.IcingaExitCode.Ok) {
|
||||
return $TRUE;
|
||||
}
|
||||
|
||||
if ($this.__ObjectType -eq 'IcingaCheckPackage') {
|
||||
return $this.__HasNotOkChecks();
|
||||
}
|
||||
|
||||
return $FALSE;
|
||||
};
|
||||
1 { # Print states not being OK and all content of affected check packages
|
||||
if ($this.__CheckState -ne $IcingaEnums.IcingaExitCode.Ok) {
|
||||
return $TRUE;
|
||||
}
|
||||
|
||||
if ($this.__ObjectType -eq 'IcingaCheckPackage') {
|
||||
return $this.__HasNotOkChecks();
|
||||
}
|
||||
|
||||
if ($this.__GetParent().__ObjectType -eq 'IcingaCheckPackage') {
|
||||
return $this.__GetParent().__HasNotOkChecks();
|
||||
}
|
||||
|
||||
return $FALSE;
|
||||
};
|
||||
}
|
||||
|
||||
# For any other verbosity, print everything
|
||||
return $TRUE;
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__GetPerformanceData' -Value {
|
||||
return $this.__CheckPerfData;
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name '__ValidateThresholdInput' -Value {
|
||||
# Shared function
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject | Add-Member -MemberType ScriptMethod -Name 'HasChecks' -Value {
|
||||
# Shared function
|
||||
}
|
||||
|
||||
$IcingaCheckBaseObject.__SetCheckCommand();
|
||||
|
||||
return $IcingaCheckBaseObject;
|
||||
}
|
||||
|
|
@ -1,456 +1,342 @@
|
|||
Import-IcingaLib icinga\enums;
|
||||
Import-IcingaLib core\tools;
|
||||
|
||||
function New-IcingaCheckPackage()
|
||||
{
|
||||
param (
|
||||
[string]$Name,
|
||||
[switch]$OperatorAnd,
|
||||
[switch]$OperatorOr,
|
||||
[switch]$OperatorNone,
|
||||
[string]$Name = '',
|
||||
[switch]$OperatorAnd = $FALSE,
|
||||
[switch]$OperatorOr = $FALSE,
|
||||
[switch]$OperatorNone = $FALSE,
|
||||
[int]$OperatorMin = -1,
|
||||
[int]$OperatorMax = -1,
|
||||
[array]$Checks = @(),
|
||||
[int]$Verbose = 0,
|
||||
[switch]$IgnoreEmptyPackage = $FALSE,
|
||||
[switch]$Hidden = $FALSE
|
||||
[switch]$Hidden = $FALSE,
|
||||
[switch]$AddSummaryHeader = $FALSE
|
||||
);
|
||||
|
||||
$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 @();
|
||||
$IcingaCheckPackage = New-IcingaCheckBaseObject;
|
||||
|
||||
$Check | Add-Member -MemberType ScriptMethod -Name 'HasChecks' -Value {
|
||||
if ($this.checks -ne 0) {
|
||||
return $TRUE
|
||||
}
|
||||
$IcingaCheckPackage.Name = $Name;
|
||||
$IcingaCheckPackage.__ObjectType = 'IcingaCheckPackage';
|
||||
$IcingaCheckPackage.__SetHidden($Hidden);
|
||||
$IcingaCheckPackage.__SetVerbosity($Verbose);
|
||||
|
||||
return $FALSE;
|
||||
}
|
||||
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name 'OperatorAnd' -Value $OperatorAnd;
|
||||
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name 'OperatorOr' -Value $OperatorOr;
|
||||
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name 'OperatorNone' -Value $OperatorNone;
|
||||
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name 'OperatorMin' -Value $OperatorMin;
|
||||
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name 'OperatorMax' -Value $OperatorMax;
|
||||
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name 'IgnoreEmptyPackage' -Value $IgnoreEmptyPackage;
|
||||
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name 'AddSummaryHeader' -Value $AddSummaryHeader;
|
||||
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name '__Checks' -Value @();
|
||||
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name '__OkChecks' -Value @();
|
||||
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name '__WarningChecks' -Value @();
|
||||
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name '__CriticalChecks' -Value @();
|
||||
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name '__UnknownChecks' -Value @();
|
||||
|
||||
$Check | Add-Member -MemberType ScriptMethod -Name 'Initialise' -Value {
|
||||
foreach ($check in $this.checks) {
|
||||
$this.InitCheck($check);
|
||||
}
|
||||
}
|
||||
|
||||
$Check | Add-Member -MemberType ScriptMethod -Name 'InitCheck' -Value {
|
||||
param($check);
|
||||
|
||||
if ($null -eq $check) {
|
||||
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Name 'ValidateOperators' -Value {
|
||||
if ($this.OperatorMin -ne -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
$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();
|
||||
}
|
||||
}
|
||||
|
||||
$Check | Add-Member -MemberType ScriptMethod -Name 'AddCheck' -Value {
|
||||
param($check);
|
||||
|
||||
if ($null -eq $check) {
|
||||
if ($this.OperatorMax -ne -1) {
|
||||
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);
|
||||
|
||||
if ($this.compiled) {
|
||||
if ($this.OperatorNone -ne $FALSE) {
|
||||
return;
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($this.ignoreemptypackage) {
|
||||
$this.exitcode = $IcingaEnums.IcingaExitCode.Ok;
|
||||
} else {
|
||||
$this.exitcode = $IcingaEnums.IcingaExitCode.Unknown;
|
||||
}
|
||||
}
|
||||
|
||||
if ([int]$this.exitcode -eq -1) {
|
||||
$this.exitcode = $IcingaEnums.IcingaExitCode.Ok;
|
||||
}
|
||||
|
||||
if ($Verbose -eq $TRUE) {
|
||||
$this.PrintOutputMessages();
|
||||
}
|
||||
|
||||
return $this.exitcode;
|
||||
}
|
||||
|
||||
$Check | Add-Member -MemberType ScriptMethod -Name 'SilentCompile' -Value {
|
||||
$this.Compile($FALSE) | Out-Null;
|
||||
}
|
||||
|
||||
$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-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;
|
||||
}
|
||||
|
||||
[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;
|
||||
}
|
||||
|
||||
[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 'PrintOutputMessageSorted' -Value {
|
||||
param($skipHidden, $skipExitCode);
|
||||
|
||||
if ($this.hidden -And $skipHidden) {
|
||||
if ($this.OperatorOr -ne $FALSE) {
|
||||
return;
|
||||
}
|
||||
|
||||
[hashtable]$MessageOrdering = @{};
|
||||
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 no operator is set, use And as default
|
||||
$this.OperatorAnd = $TRUE;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Name 'AddCheck' -Value {
|
||||
param([array]$Checks);
|
||||
|
||||
$SortedArray = $MessageOrdering.GetEnumerator() | Sort-Object name;
|
||||
|
||||
foreach ($entry in $SortedArray) {
|
||||
$entry.Value.PrintAllMessages();
|
||||
}
|
||||
}
|
||||
|
||||
$Check | Add-Member -MemberType ScriptMethod -Name 'WriteAllOutput' -Value {
|
||||
$this.PrintOutputMessageSorted($TRUE, $IcingaEnums.IcingaExitCode.Ok);
|
||||
}
|
||||
|
||||
$Check | Add-Member -MemberType ScriptMethod -Name 'PrintAllMessages' -Value {
|
||||
$this.WritePackageOutputStatus();
|
||||
$this.WriteAllOutput();
|
||||
}
|
||||
|
||||
$Check | Add-Member -MemberType ScriptMethod -Name 'WriteCheckErrors' -Value {
|
||||
$this.PrintOutputMessageSorted($FALSE, $IcingaEnums.IcingaExitCode.Ok);
|
||||
}
|
||||
|
||||
$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) {
|
||||
if ($null -eq $Checks -Or $Checks.Count -eq 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
[string]$outputMessage = '{0}{1} Check package "{2}"';
|
||||
if ($this.verbose -ne 0) {
|
||||
$outputMessage += ' ({3})';
|
||||
foreach ($check in $Checks) {
|
||||
$check.__SetIndention($this.__NewIndention());
|
||||
$check.__SetCheckOutput();
|
||||
$check.__SetVerbosity($this.__GetVerbosity());
|
||||
$check.__SetParent($this);
|
||||
$this.__Checks += $check;
|
||||
}
|
||||
}
|
||||
|
||||
if ($this.exitcode -ne 0 -And $this.spacing -eq 0) {
|
||||
$outputMessage += ' - {4}';
|
||||
# Override shared function
|
||||
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name '__SetIndention' -Value {
|
||||
param ($Indention);
|
||||
|
||||
$this.__Indention = $Indention;
|
||||
|
||||
foreach ($check in $this.__Checks) {
|
||||
$check.__SetIndention($this.__NewIndention());
|
||||
}
|
||||
}
|
||||
|
||||
Write-IcingaPluginOutput (
|
||||
[string]::Format(
|
||||
$outputMessage,
|
||||
(New-StringTree $this.spacing),
|
||||
$IcingaEnums.IcingaExitCodeText.($this.exitcode),
|
||||
$this.name,
|
||||
$this.GetPackageConfigMessage(),
|
||||
$this.headermsg
|
||||
)
|
||||
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Name '__SetCheckState' -Value {
|
||||
param ($State);
|
||||
|
||||
if ($this.__GetCheckState() -lt $State) {
|
||||
$this.__CheckState = $State;
|
||||
$this.__SetCheckOutput();
|
||||
}
|
||||
}
|
||||
|
||||
# Override shared function
|
||||
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name '__SetCheckOutput' -Value {
|
||||
param ($PluginOutput);
|
||||
|
||||
$UnknownChecks = '';
|
||||
$CriticalChecks = '';
|
||||
$WarningChecks = '';
|
||||
$CheckSummary = [System.Text.StringBuilder]::New();
|
||||
[bool]$HasContent = $FALSE;
|
||||
|
||||
# Only apply this to the top parent package
|
||||
if ($this.__GetIndention() -eq 0) {
|
||||
if ($this.__UnknownChecks.Count -ne 0) {
|
||||
$UnknownChecks = [string]::Format(' [UNKNOWN] {0}', ([string]::Join(', ', $this.__UnknownChecks)));
|
||||
$HasContent = $TRUE;
|
||||
$CheckSummary.Append(
|
||||
[string]::Format(' {0} Unknown', $this.__UnknownChecks.Count)
|
||||
) | Out-Null;
|
||||
}
|
||||
if ($this.__CriticalChecks.Count -ne 0) {
|
||||
$CriticalChecks = [string]::Format(' [CRITICAL] {0}', ([string]::Join(', ', $this.__CriticalChecks)));
|
||||
$HasContent = $TRUE;
|
||||
$CheckSummary.Append(
|
||||
[string]::Format(' {0} Critical', $this.__CriticalChecks.Count)
|
||||
) | Out-Null;
|
||||
}
|
||||
if ($this.__WarningChecks.Count -ne 0) {
|
||||
$WarningChecks = [string]::Format(' [WARNING] {0}', ([string]::Join(', ', $this.__WarningChecks)));
|
||||
$HasContent = $TRUE;
|
||||
$CheckSummary.Append(
|
||||
[string]::Format(' {0} Warning', $this.__WarningChecks.Count)
|
||||
) | Out-Null;
|
||||
}
|
||||
}
|
||||
if ([string]::IsNullOrEmpty($this.__ErrorMessage) -eq $FALSE) {
|
||||
$HasContent = $TRUE;
|
||||
}
|
||||
if ($this.__OkChecks.Count -ne 0) {
|
||||
$CheckSummary.Append(
|
||||
[string]::Format(' {0} Ok', $this.__OkChecks.Count)
|
||||
) | Out-Null;
|
||||
$HasContent = $TRUE;
|
||||
}
|
||||
|
||||
if ($this.AddSummaryHeader -eq $FALSE) {
|
||||
$CheckSummary.Clear() | Out-Null;
|
||||
$CheckSummary.Append('') | Out-Null;
|
||||
}
|
||||
|
||||
$this.__CheckOutput = [string]::Format(
|
||||
'{0} {1}{2}{3}{4}{5}{6}{7}{8}',
|
||||
$IcingaEnums.IcingaExitCodeText[$this.__GetCheckState()],
|
||||
$this.Name,
|
||||
(&{ if ($HasContent) { return ':'; } else { return ''; } }),
|
||||
$CheckSummary.ToString(),
|
||||
([string]::Format('{0}{1}', (&{ if ($this.__ErrorMessage.Length -gt 1) { return ' '; } else { return ''; } }), $this.__ErrorMessage)),
|
||||
$UnknownChecks,
|
||||
$CriticalChecks,
|
||||
$WarningChecks,
|
||||
$this.__ShowPackageConfig()
|
||||
);
|
||||
}
|
||||
|
||||
$Check | Add-Member -MemberType ScriptMethod -Name 'PrintOutputMessages' -Value {
|
||||
[bool]$printAll = $FALSE;
|
||||
$IcingaCheckPackage | Add-Member -Force -MemberType ScriptMethod -Name 'Compile' -Value {
|
||||
$this.__OkChecks.Clear();
|
||||
$this.__WarningChecks.Clear();
|
||||
$this.__CriticalChecks.Clear();
|
||||
$this.__UnknownChecks.Clear();
|
||||
|
||||
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
|
||||
break;
|
||||
};
|
||||
Default {
|
||||
$printAll = $TRUE;
|
||||
break;
|
||||
}
|
||||
$WorstState = $IcingaEnums.IcingaExitCode.Ok;
|
||||
$BestState = $IcingaEnums.IcingaExitCode.Ok;
|
||||
$NotOkChecks = 0;
|
||||
$OkChecks = 0;
|
||||
|
||||
if ($this.__Checks.Count -eq 0 -And $this.IgnoreEmptyPackage -eq $FALSE) {
|
||||
$this.__ErrorMessage = 'No checks added to this package';
|
||||
$this.__SetCheckState($IcingaEnums.IcingaExitCode.Unknown);
|
||||
$this.__SetCheckOutput();
|
||||
return;
|
||||
}
|
||||
|
||||
$this.WritePackageOutputStatus();
|
||||
$this.__Checks = $this.__Checks | Sort-Object -Property Name;
|
||||
|
||||
if ($printAll) {
|
||||
$this.WriteAllOutput();
|
||||
$this.PrintNoChecksConfigured();
|
||||
} else {
|
||||
if ([int]$this.exitcode -ne $IcingaEnums.IcingaExitCode.Ok) {
|
||||
$this.WriteCheckErrors();
|
||||
$this.PrintNoChecksConfigured();
|
||||
}
|
||||
}
|
||||
# Loop all checks to understand the content of result
|
||||
foreach ($check in $this.__Checks) {
|
||||
|
||||
$check.Compile();
|
||||
|
||||
if ($WorstState -lt $check.__GetCheckState()) {
|
||||
$WorstState = $check.__GetCheckState();
|
||||
}
|
||||
|
||||
$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);
|
||||
}
|
||||
if ($BestState -gt $check.__GetCheckState()) {
|
||||
$BestState = $check.__GetCheckState();
|
||||
}
|
||||
|
||||
[array]$SortedCheckArray = $CheckHash.GetEnumerator() | Sort-Object name;
|
||||
|
||||
if ($SortedCheckArray.Count -ne 0) {
|
||||
$this.headermsg += [string]::Format(
|
||||
[string]$CheckStateOutput = [string]::Format(
|
||||
'{0}{1}',
|
||||
$IcingaEnums.IcingaExitCodeText[$state],
|
||||
[string]::Join(', ', $SortedCheckArray.Key)
|
||||
$check.__GetName(),
|
||||
$check.__GetHeaderOutputValue()
|
||||
);
|
||||
|
||||
switch ($check.__GetCheckState()) {
|
||||
$IcingaEnums.IcingaExitCode.Ok {
|
||||
$this.__OkChecks += $CheckStateOutput;
|
||||
$OkChecks += 1;
|
||||
break;
|
||||
};
|
||||
$IcingaEnums.IcingaExitCode.Warning {
|
||||
$this.__WarningChecks += $CheckStateOutput;
|
||||
$NotOkChecks += 1;
|
||||
break;
|
||||
};
|
||||
$IcingaEnums.IcingaExitCode.Critical {
|
||||
$this.__CriticalChecks += $CheckStateOutput;
|
||||
$NotOkChecks += 1;
|
||||
break;
|
||||
};
|
||||
$IcingaEnums.IcingaExitCode.Unknown {
|
||||
$this.__UnknownChecks += $CheckStateOutput;
|
||||
$NotOkChecks += 1;
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
$Check | Add-Member -MemberType ScriptMethod -Name 'GetWorstExitCode' -Value {
|
||||
if ([int]$this.exitcode -eq [int]$IcingaEnums.IcingaExitCode.Unknown) {
|
||||
return;
|
||||
if ($this.OperatorAnd -And $NotOkChecks -ne 0) {
|
||||
$this.__SetCheckState($WorstState);
|
||||
} elseif ($this.OperatorOr -And $OkChecks -eq 0 ) {
|
||||
$this.__SetCheckState($WorstState);
|
||||
} elseif ($this.OperatorNone -And $OkChecks -ne 0 ) {
|
||||
$this.__SetCheckState($WorstState);
|
||||
} elseif ($this.OperatorMin -ne -1) {
|
||||
if (-Not ($this.__Checks.Count -eq 0 -And $this.IgnoreEmptyPackage -eq $TRUE)) {
|
||||
if ($this.OperatorMin -gt $this.__Checks.Count) {
|
||||
$this.__SetCheckState($IcingaEnums.IcingaExitCode.Unknown);
|
||||
$this.__ErrorMessage = [string]::Format('Minium check count ({0}) is larger than number of assigned checks ({1})', $this.OperatorMin, $this.__Checks.Count);
|
||||
} elseif ($OkChecks -lt $this.OperatorMin) {
|
||||
$this.__SetCheckState($WorstState);
|
||||
$this.__ErrorMessage = '';
|
||||
}
|
||||
}
|
||||
} elseif ($this.OperatorMax -ne -1) {
|
||||
if (-Not ($this.__Checks.Count -eq 0 -And $this.IgnoreEmptyPackage -eq $TRUE)) {
|
||||
if ($this.OperatorMax -gt $this.__Checks.Count) {
|
||||
$this.__SetCheckState($IcingaEnums.IcingaExitCode.Unknown);
|
||||
$this.__ErrorMessage = [string]::Format('Maximum check count ({0}) is larger than number of assigned checks ({1})', $this.OperatorMax, $this.__Checks.Count);
|
||||
} elseif ($OkChecks -gt $this.OperatorMax) {
|
||||
$this.__SetCheckState($WorstState);
|
||||
$this.__ErrorMessage = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($check in $this.checks) {
|
||||
if ([int]$this.exitcode -lt $check.exitcode) {
|
||||
$this.exitcode = $check.exitcode;
|
||||
$this.__SetCheckOutput();
|
||||
}
|
||||
|
||||
$this.criticalchecks += $check.GetCriticals();
|
||||
$this.warningchecks += $check.GetWarnings();
|
||||
$this.unknownchecks += $check.GetUnknowns();
|
||||
# Override default behaviour from shared function
|
||||
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name '__SetVerbosity' -Value {
|
||||
param ($Verbosity);
|
||||
# Do nothing for check packages
|
||||
}
|
||||
|
||||
# Only apply this to our top package
|
||||
if ($this.spacing -ne 0) {
|
||||
return;
|
||||
# Override default behaviour from shared function
|
||||
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name '__GetCheckOutput' -Value {
|
||||
|
||||
if ($this.__IsHidden()) {
|
||||
return ''
|
||||
};
|
||||
|
||||
if ($this._CanOutput() -eq $FALSE) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$this.AddUniqueSortedChecksToHeader(
|
||||
$this.criticalchecks, $IcingaEnums.IcingaExitCode.Critical
|
||||
$CheckOutput = [string]::Format(
|
||||
'{0}{1}',
|
||||
(New-StringTree -Spacing $this.__GetIndention()),
|
||||
$this.__CheckOutput
|
||||
);
|
||||
$this.AddUniqueSortedChecksToHeader(
|
||||
$this.warningchecks, $IcingaEnums.IcingaExitCode.Warning
|
||||
);
|
||||
$this.AddUniqueSortedChecksToHeader(
|
||||
$this.unknownchecks, $IcingaEnums.IcingaExitCode.Unknown
|
||||
|
||||
foreach ($check in $this.__Checks) {
|
||||
if ($check.__IsHidden()) {
|
||||
continue;
|
||||
};
|
||||
|
||||
if ($check._CanOutput() -eq $FALSE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$CheckOutput = [string]::Format(
|
||||
'{0}{1}{2}',
|
||||
$CheckOutput,
|
||||
"`n",
|
||||
$check.__GetCheckOutput()
|
||||
);
|
||||
}
|
||||
|
||||
$Check | Add-Member -MemberType ScriptMethod -Name 'GetPerfData' -Value {
|
||||
return $CheckOutput;
|
||||
}
|
||||
|
||||
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name '__HasNotOkChecks' -Value {
|
||||
|
||||
if ($this.__WarningChecks.Count -ne 0) {
|
||||
return $TRUE;
|
||||
}
|
||||
|
||||
if ($this.__CriticalChecks.Count -ne 0) {
|
||||
return $TRUE;
|
||||
}
|
||||
|
||||
if ($this.__UnknownChecks.Count -ne 0) {
|
||||
return $TRUE;
|
||||
}
|
||||
|
||||
return $FALSE;
|
||||
}
|
||||
|
||||
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name '__ShowPackageConfig' -Value {
|
||||
if ($this.__GetVerbosity() -lt 3) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($this.OperatorAnd) {
|
||||
return ' (All must be [OK])';
|
||||
}
|
||||
if ($this.OperatorOr) {
|
||||
return ' (Atleast one must be [OK])';
|
||||
}
|
||||
if ($this.OperatorMin -ne -1) {
|
||||
return ([string]::Format(' (Atleast {0} must be [OK])', $this.OperatorMin));
|
||||
}
|
||||
if ($this.OperatorMax -ne -1) {
|
||||
return ([string]::Format(' (Not more than {0} must be [OK])', $this.OperatorMax));
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
# Override default behaviour from shared function
|
||||
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name '__GetPerformanceData' -Value {
|
||||
[string]$perfData = '';
|
||||
[hashtable]$CollectedPerfData = @{ };
|
||||
|
||||
# At first lets collect all perf data, but ensure we only add possible label duplication only once
|
||||
foreach ($check in $this.checks) {
|
||||
$data = $check.GetPerfData();
|
||||
foreach ($check in $this.__Checks) {
|
||||
$data = $check.__GetPerformanceData();
|
||||
|
||||
if ($null -eq $data -Or $null -eq $data.label) {
|
||||
continue;
|
||||
|
|
@ -463,22 +349,42 @@ function New-IcingaCheckPackage()
|
|||
$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;
|
||||
'label' = $this.Name;
|
||||
'perfdata' = $CollectedPerfData;
|
||||
'package' = $TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
$Check.Initialise();
|
||||
# __GetTimeSpanThreshold(0, 'Core_30_20', 'Core_30')
|
||||
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name '__GetTimeSpanThreshold' -Value {
|
||||
param ($TimeSpanLabel, $Label);
|
||||
|
||||
return $Check;
|
||||
foreach ($check in $this.__Checks) {
|
||||
$Result = $check.__GetTimeSpanThreshold($TimeSpanLabel, $Label);
|
||||
|
||||
if ([string]::IsNullOrEmpty($Result) -eq $FALSE) {
|
||||
return $Result;
|
||||
}
|
||||
}
|
||||
|
||||
return @{
|
||||
'Warning' = '';
|
||||
'Critical' = '';
|
||||
};
|
||||
}
|
||||
|
||||
# Override shared function
|
||||
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name 'HasChecks' -Value {
|
||||
if ($this.__Checks.Count -eq 0) {
|
||||
return $FALSE;
|
||||
}
|
||||
|
||||
return $TRUE;
|
||||
}
|
||||
|
||||
$IcingaCheckPackage.ValidateOperators();
|
||||
$IcingaCheckPackage.AddCheck($Checks);
|
||||
|
||||
return $IcingaCheckPackage;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,38 +1,38 @@
|
|||
Import-IcingaLib icinga\enums;
|
||||
|
||||
function New-IcingaCheckresult()
|
||||
function New-IcingaCheckResult()
|
||||
{
|
||||
param (
|
||||
$Check,
|
||||
[bool]$NoPerfData,
|
||||
[switch]$Compile
|
||||
[bool]$NoPerfData = $FALSE,
|
||||
[switch]$Compile = $FALSE
|
||||
);
|
||||
|
||||
$CheckResult = New-Object -TypeName PSObject;
|
||||
$CheckResult | Add-Member -MemberType NoteProperty -Name 'check' -Value $Check;
|
||||
$CheckResult | Add-Member -MemberType NoteProperty -Name 'noperfdata' -Value $NoPerfData;
|
||||
$IcingaCheckResult = New-Object -TypeName PSObject;
|
||||
$IcingaCheckResult | Add-Member -MemberType NoteProperty -Name 'Check' -Value $Check;
|
||||
$IcingaCheckResult | Add-Member -MemberType NoteProperty -Name 'NoPerfData' -Value $NoPerfData;
|
||||
|
||||
$CheckResult | Add-Member -MemberType ScriptMethod -Name 'Compile' -Value {
|
||||
if ($null -eq $this.check) {
|
||||
$IcingaCheckResult | Add-Member -MemberType ScriptMethod -Name 'Compile' -Value {
|
||||
if ($null -eq $this.Check) {
|
||||
return $IcingaEnums.IcingaExitCode.Unknown;
|
||||
}
|
||||
|
||||
$CheckCommand = (Get-PSCallStack)[2].Command;
|
||||
|
||||
# Compile the check / package if not already done
|
||||
$this.check.AssignCheckCommand($CheckCommand);
|
||||
$this.check.Compile($TRUE) | Out-Null;
|
||||
$this.Check.Compile();
|
||||
|
||||
if ([int]$this.check.exitcode -ne [int]$IcingaEnums.IcingaExitCode.Unknown -And -Not $this.noperfdata) {
|
||||
Write-IcingaPluginPerfData -PerformanceData ($this.check.GetPerfData()) -CheckCommand $CheckCommand;
|
||||
Write-IcingaPluginOutput -Output ($this.Check.__GetCheckOutput());
|
||||
|
||||
if ($this.NoPerfData -eq $FALSE) {
|
||||
Write-IcingaPluginPerfData -IcingaCheck $this.Check;
|
||||
}
|
||||
|
||||
return $this.check.exitcode;
|
||||
# Ensure we reset our internal cache once the plugin was executed
|
||||
$Global:Icinga.ThresholdCache[$this.Check.__GetCheckCommand()] = $null;
|
||||
|
||||
return $this.Check.__GetCheckState();
|
||||
}
|
||||
|
||||
if ($Compile) {
|
||||
return $CheckResult.Compile();
|
||||
return $IcingaCheckResult.Compile();
|
||||
}
|
||||
|
||||
return $CheckResult;
|
||||
return $IcingaCheckResult;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@ function New-IcingaPerformanceDataEntry()
|
|||
param (
|
||||
$PerfDataObject,
|
||||
$Label = $null,
|
||||
$Value = $null
|
||||
$Value = $null,
|
||||
$Warning = $null,
|
||||
$Critical = $null
|
||||
);
|
||||
|
||||
if ($null -eq $PerfDataObject) {
|
||||
|
|
@ -12,6 +14,8 @@ function New-IcingaPerformanceDataEntry()
|
|||
|
||||
[string]$LabelName = $PerfDataObject.label;
|
||||
[string]$PerfValue = $PerfDataObject.value;
|
||||
[string]$WarningValue = $PerfDataObject.warning;
|
||||
[string]$CriticalValue = $PerfDataObject.critical;
|
||||
|
||||
if ([string]::IsNullOrEmpty($Label) -eq $FALSE) {
|
||||
$LabelName = $Label;
|
||||
|
|
@ -20,6 +24,13 @@ function New-IcingaPerformanceDataEntry()
|
|||
$PerfValue = $Value;
|
||||
}
|
||||
|
||||
# Override our warning/critical values only if the label does not match.
|
||||
# Eg. Core_1 not matching Core_1_5 - this is only required for time span checks
|
||||
if ([string]::IsNullOrEmpty($Label) -eq $FALSE -And $Label -ne $PerfDataObject.label) {
|
||||
$WarningValue = $Warning;
|
||||
$CriticalValue = $Critical;
|
||||
}
|
||||
|
||||
$minimum = '';
|
||||
$maximum = '';
|
||||
|
||||
|
|
@ -36,8 +47,8 @@ function New-IcingaPerformanceDataEntry()
|
|||
$LabelName.ToLower(),
|
||||
(Format-IcingaPerfDataValue $PerfValue),
|
||||
$PerfDataObject.unit,
|
||||
(Format-IcingaPerfDataValue $PerfDataObject.warning),
|
||||
(Format-IcingaPerfDataValue $PerfDataObject.critical),
|
||||
(Format-IcingaPerfDataValue $WarningValue),
|
||||
(Format-IcingaPerfDataValue $CriticalValue),
|
||||
(Format-IcingaPerfDataValue $minimum),
|
||||
(Format-IcingaPerfDataValue $maximum)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -5,6 +5,9 @@ function Write-IcingaPluginOutput()
|
|||
);
|
||||
|
||||
if ($global:IcingaDaemonData.FrameworkRunningAsDaemon -eq $FALSE) {
|
||||
if ($null -ne $global:Icinga -And $global:Icinga.Minimal) {
|
||||
Clear-Host;
|
||||
}
|
||||
Write-IcingaConsolePlain $Output;
|
||||
} else {
|
||||
# New behavior with local thread separated results
|
||||
|
|
|
|||
|
|
@ -1,10 +1,16 @@
|
|||
function Write-IcingaPluginPerfData()
|
||||
{
|
||||
param (
|
||||
$PerformanceData,
|
||||
$CheckCommand
|
||||
$IcingaCheck = $null
|
||||
);
|
||||
|
||||
if ($null -eq $IcingaCheck) {
|
||||
return;
|
||||
}
|
||||
|
||||
$PerformanceData = $IcingaCheck.__GetPerformanceData();
|
||||
$CheckCommand = $IcingaCheck.__GetCheckCommand();
|
||||
|
||||
if ($PerformanceData.package -eq $FALSE) {
|
||||
$PerformanceData = @{
|
||||
$PerformanceData.label = $PerformanceData;
|
||||
|
|
@ -13,13 +19,13 @@ function Write-IcingaPluginPerfData()
|
|||
$PerformanceData = $PerformanceData.perfdata;
|
||||
}
|
||||
|
||||
$CheckResultCache = Get-IcingaCacheData -Space 'sc_daemon' -CacheStore 'checkresult' -KeyName $CheckCommand;
|
||||
$CheckResultCache = $Global:Icinga.ThresholdCache[$CheckCommand];
|
||||
|
||||
if ($global:IcingaDaemonData.FrameworkRunningAsDaemon -eq $FALSE) {
|
||||
[string]$PerfDataOutput = (Get-IcingaPluginPerfDataContent -PerfData $PerformanceData -CheckResultCache $CheckResultCache);
|
||||
[string]$PerfDataOutput = (Get-IcingaPluginPerfDataContent -PerfData $PerformanceData -CheckResultCache $CheckResultCache -IcingaCheck $IcingaCheck);
|
||||
Write-IcingaConsolePlain ([string]::Format('| {0}', $PerfDataOutput));
|
||||
} else {
|
||||
[void](Get-IcingaPluginPerfDataContent -PerfData $PerformanceData -CheckResultCache $CheckResultCache -AsObject $TRUE);
|
||||
[void](Get-IcingaPluginPerfDataContent -PerfData $PerformanceData -CheckResultCache $CheckResultCache -AsObject $TRUE -IcingaCheck $IcingaCheck);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -28,7 +34,8 @@ function Get-IcingaPluginPerfDataContent()
|
|||
param(
|
||||
$PerfData,
|
||||
$CheckResultCache,
|
||||
[bool]$AsObject = $FALSE
|
||||
[bool]$AsObject = $FALSE,
|
||||
$IcingaCheck = $null
|
||||
);
|
||||
|
||||
[string]$PerfDataOutput = '';
|
||||
|
|
@ -36,13 +43,16 @@ function Get-IcingaPluginPerfDataContent()
|
|||
foreach ($package in $PerfData.Keys) {
|
||||
$data = $PerfData[$package];
|
||||
if ($data.package) {
|
||||
$PerfDataOutput += (Get-IcingaPluginPerfDataContent -PerfData $data.perfdata -CheckResultCache $CheckResultCache -AsObject $AsObject);
|
||||
$PerfDataOutput += (Get-IcingaPluginPerfDataContent -PerfData $data.perfdata -CheckResultCache $CheckResultCache -AsObject $AsObject -IcingaCheck $IcingaCheck);
|
||||
} else {
|
||||
foreach ($checkresult in $CheckResultCache.PSobject.Properties) {
|
||||
|
||||
$SearchPattern = [string]::Format('{0}_', $data.label);
|
||||
$SearchEntry = $checkresult.Name;
|
||||
if ($SearchEntry -like "$SearchPattern*") {
|
||||
$cachedresult = (New-IcingaPerformanceDataEntry -PerfDataObject $data -Label $SearchEntry -Value $checkresult.Value);
|
||||
$TimeSpan = $IcingaCheck.__GetTimeSpanThreshold($SearchEntry, $data.label);
|
||||
|
||||
$cachedresult = (New-IcingaPerformanceDataEntry -PerfDataObject $data -Label $SearchEntry -Value $checkresult.Value -Warning $TimeSpan.Warning -Critical $TimeSpan.Critical);
|
||||
|
||||
if ($AsObject) {
|
||||
# New behavior with local thread separated results
|
||||
|
|
|
|||
|
|
@ -10,8 +10,7 @@ function Get-IcingaSSLCertForSocket()
|
|||
# to get a proper certificate
|
||||
if ([string]::IsNullOrEmpty($CertFile) -eq $FALSE) {
|
||||
if ((Test-Path $CertFile)) {
|
||||
$FileType = Get-Item -Path $CertFile;
|
||||
if ($FileType -eq '.pfx') {
|
||||
if ([IO.Path]::GetExtension($CertFile) -eq '.pfx') {
|
||||
return (New-Object Security.Cryptography.X509Certificates.X509Certificate2 $CertFile);
|
||||
} else {
|
||||
return ConvertTo-IcingaX509Certificate -CertFile $CertFile;
|
||||
|
|
|
|||
Loading…
Reference in a new issue