mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-21 07:10:15 -05:00
Merge pull request #306 from Icinga:feature/critical_exception_exit_code_handling
Feature: Adds critical exception cmdlet and exit code catch Adds new Cmdlet `Exit-IcingaThrowCritical` to throw critical exit with a custom message, either by force or by using string filtering and adds storing of plugin exit codes internally.
This commit is contained in:
commit
5012bb29b3
6 changed files with 99 additions and 1 deletions
|
|
@ -12,6 +12,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
|
||||||
[Issue and PRs](https://github.com/Icinga/icinga-powershell-framework/milestone/15?closed=1)
|
[Issue and PRs](https://github.com/Icinga/icinga-powershell-framework/milestone/15?closed=1)
|
||||||
|
|
||||||
* [#305](https://github.com/Icinga/icinga-powershell-framework/pull/305) Adds a new Cmdlet to test if functions with `Add-Type` are already present inside the current scope of the shell
|
* [#305](https://github.com/Icinga/icinga-powershell-framework/pull/305) Adds a new Cmdlet to test if functions with `Add-Type` are already present inside the current scope of the shell
|
||||||
|
* [#306](https://github.com/Icinga/icinga-powershell-framework/pull/306) Adds new Cmdlet `Exit-IcingaThrowCritical` to throw critical exit with a custom message, either by force or by using string filtering and adds storing of plugin exit codes internally
|
||||||
|
|
||||||
## 1.5.2 (2021-07-09)
|
## 1.5.2 (2021-07-09)
|
||||||
|
|
||||||
|
|
|
||||||
32
lib/icinga/exception/Exit-IcingaThrowCritical.psm1
Normal file
32
lib/icinga/exception/Exit-IcingaThrowCritical.psm1
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
function Exit-IcingaThrowCritical()
|
||||||
|
{
|
||||||
|
param (
|
||||||
|
[string]$Message = '',
|
||||||
|
[string]$FilterString = $null,
|
||||||
|
[string]$SearchString = $null,
|
||||||
|
[switch]$Force = $FALSE
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($Force -eq $FALSE) {
|
||||||
|
if ([string]::IsNullOrEmpty($FilterString) -Or [string]::IsNullOrEmpty($SearchString)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($FilterString -NotLike "*$SearchString*") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[string]$OutputMessage = [string]::Format(
|
||||||
|
'[CRITICAL] {0}',
|
||||||
|
$Message
|
||||||
|
);
|
||||||
|
|
||||||
|
Set-IcingaInternalPluginExitCode -ExitCode $IcingaEnums.IcingaExitCode.Critical;
|
||||||
|
Set-IcingaInternalPluginException -PluginException $OutputMessage;
|
||||||
|
|
||||||
|
if ($null -eq $global:IcingaDaemonData -Or $global:IcingaDaemonData.FrameworkRunningAsDaemon -eq $FALSE) {
|
||||||
|
Write-IcingaConsolePlain $OutputMessage;
|
||||||
|
exit $IcingaEnums.IcingaExitCode.Critical;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -107,6 +107,9 @@ function Exit-IcingaThrowException()
|
||||||
$ExceptionTypeString
|
$ExceptionTypeString
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Set-IcingaInternalPluginExitCode -ExitCode $IcingaEnums.IcingaExitCode.Unknown;
|
||||||
|
Set-IcingaInternalPluginException -PluginException $OutputMessage;
|
||||||
|
|
||||||
if ($null -eq $global:IcingaDaemonData -Or $global:IcingaDaemonData.FrameworkRunningAsDaemon -eq $FALSE) {
|
if ($null -eq $global:IcingaDaemonData -Or $global:IcingaDaemonData.FrameworkRunningAsDaemon -eq $FALSE) {
|
||||||
Write-IcingaConsolePlain $OutputMessage;
|
Write-IcingaConsolePlain $OutputMessage;
|
||||||
exit $IcingaEnums.IcingaExitCode.Unknown;
|
exit $IcingaEnums.IcingaExitCode.Unknown;
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,11 @@ function New-IcingaCheckResult()
|
||||||
# Ensure we reset our internal cache once the plugin was executed
|
# Ensure we reset our internal cache once the plugin was executed
|
||||||
$Global:Icinga.ThresholdCache[$this.Check.__GetCheckCommand()] = $null;
|
$Global:Icinga.ThresholdCache[$this.Check.__GetCheckCommand()] = $null;
|
||||||
|
|
||||||
return $this.Check.__GetCheckState();
|
$ExitCode = $this.Check.__GetCheckState();
|
||||||
|
|
||||||
|
Set-IcingaInternalPluginExitCode -ExitCode $ExitCode;
|
||||||
|
|
||||||
|
return $ExitCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($Compile) {
|
if ($Compile) {
|
||||||
|
|
|
||||||
29
lib/icinga/plugin/Set-IcingaInternalPluginException.psm1
Normal file
29
lib/icinga/plugin/Set-IcingaInternalPluginException.psm1
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
function Set-IcingaInternalPluginException()
|
||||||
|
{
|
||||||
|
param (
|
||||||
|
[string]$PluginException = ''
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($null -eq $Global:Icinga) {
|
||||||
|
$Global:Icinga = @{ };
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($Global:Icinga.ContainsKey('PluginExecution') -eq $FALSE) {
|
||||||
|
$Global:Icinga.Add(
|
||||||
|
'PluginExecution',
|
||||||
|
@{
|
||||||
|
'PluginException' = $PluginException;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
if ($Global:Icinga.PluginExecution.ContainsKey('PluginException') -eq $FALSE) {
|
||||||
|
$Global:Icinga.PluginExecution.Add('PluginException', $PluginException);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Only catch the first exception
|
||||||
|
if ([string]::IsNullOrEmpty($Global:Icinga.PluginExecution.PluginException)) {
|
||||||
|
$Global:Icinga.PluginExecution.PluginException = $PluginException;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
29
lib/icinga/plugin/Set-IcingaInternalPluginExitCode.psm1
Normal file
29
lib/icinga/plugin/Set-IcingaInternalPluginExitCode.psm1
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
function Set-IcingaInternalPluginExitCode()
|
||||||
|
{
|
||||||
|
param (
|
||||||
|
$ExitCode = 0
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($null -eq $Global:Icinga) {
|
||||||
|
$Global:Icinga = @{ };
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($Global:Icinga.ContainsKey('PluginExecution') -eq $FALSE) {
|
||||||
|
$Global:Icinga.Add(
|
||||||
|
'PluginExecution',
|
||||||
|
@{
|
||||||
|
'LastExitCode' = $ExitCode;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
if ($Global:Icinga.PluginExecution.ContainsKey('LastExitCode') -eq $FALSE) {
|
||||||
|
$Global:Icinga.PluginExecution.Add('LastExitCode', $ExitCode);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Only add the first exit code we should cover during one runtime
|
||||||
|
if ($null -eq $Global:Icinga.PluginExecution.LastExitCode) {
|
||||||
|
$Global:Icinga.PluginExecution.LastExitCode = $ExitCode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue