Skip to content

Commit 5012bb2

Browse files
authored
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.
2 parents 9db417b + 2268d96 commit 5012bb2

6 files changed

+99
-1
lines changed

doc/31-Changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1212
[Issue and PRs](https://github.com/Icinga/icinga-powershell-framework/milestone/15?closed=1)
1313

1414
* [#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
15+
* [#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
1516

1617
## 1.5.2 (2021-07-09)
1718

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function Exit-IcingaThrowCritical()
2+
{
3+
param (
4+
[string]$Message = '',
5+
[string]$FilterString = $null,
6+
[string]$SearchString = $null,
7+
[switch]$Force = $FALSE
8+
);
9+
10+
if ($Force -eq $FALSE) {
11+
if ([string]::IsNullOrEmpty($FilterString) -Or [string]::IsNullOrEmpty($SearchString)) {
12+
return;
13+
}
14+
15+
if ($FilterString -NotLike "*$SearchString*") {
16+
return;
17+
}
18+
}
19+
20+
[string]$OutputMessage = [string]::Format(
21+
'[CRITICAL] {0}',
22+
$Message
23+
);
24+
25+
Set-IcingaInternalPluginExitCode -ExitCode $IcingaEnums.IcingaExitCode.Critical;
26+
Set-IcingaInternalPluginException -PluginException $OutputMessage;
27+
28+
if ($null -eq $global:IcingaDaemonData -Or $global:IcingaDaemonData.FrameworkRunningAsDaemon -eq $FALSE) {
29+
Write-IcingaConsolePlain $OutputMessage;
30+
exit $IcingaEnums.IcingaExitCode.Critical;
31+
}
32+
}

lib/icinga/exception/Exit-IcingaThrowException.psm1

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ function Exit-IcingaThrowException()
107107
$ExceptionTypeString
108108
);
109109

110+
Set-IcingaInternalPluginExitCode -ExitCode $IcingaEnums.IcingaExitCode.Unknown;
111+
Set-IcingaInternalPluginException -PluginException $OutputMessage;
112+
110113
if ($null -eq $global:IcingaDaemonData -Or $global:IcingaDaemonData.FrameworkRunningAsDaemon -eq $FALSE) {
111114
Write-IcingaConsolePlain $OutputMessage;
112115
exit $IcingaEnums.IcingaExitCode.Unknown;

lib/icinga/plugin/New-IcingaCheckResult.psm1

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ function New-IcingaCheckResult()
2727
# Ensure we reset our internal cache once the plugin was executed
2828
$Global:Icinga.ThresholdCache[$this.Check.__GetCheckCommand()] = $null;
2929

30-
return $this.Check.__GetCheckState();
30+
$ExitCode = $this.Check.__GetCheckState();
31+
32+
Set-IcingaInternalPluginExitCode -ExitCode $ExitCode;
33+
34+
return $ExitCode;
3135
}
3236

3337
if ($Compile) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function Set-IcingaInternalPluginException()
2+
{
3+
param (
4+
[string]$PluginException = ''
5+
);
6+
7+
if ($null -eq $Global:Icinga) {
8+
$Global:Icinga = @{ };
9+
}
10+
11+
if ($Global:Icinga.ContainsKey('PluginExecution') -eq $FALSE) {
12+
$Global:Icinga.Add(
13+
'PluginExecution',
14+
@{
15+
'PluginException' = $PluginException;
16+
}
17+
)
18+
} else {
19+
if ($Global:Icinga.PluginExecution.ContainsKey('PluginException') -eq $FALSE) {
20+
$Global:Icinga.PluginExecution.Add('PluginException', $PluginException);
21+
return;
22+
}
23+
24+
# Only catch the first exception
25+
if ([string]::IsNullOrEmpty($Global:Icinga.PluginExecution.PluginException)) {
26+
$Global:Icinga.PluginExecution.PluginException = $PluginException;
27+
}
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function Set-IcingaInternalPluginExitCode()
2+
{
3+
param (
4+
$ExitCode = 0
5+
);
6+
7+
if ($null -eq $Global:Icinga) {
8+
$Global:Icinga = @{ };
9+
}
10+
11+
if ($Global:Icinga.ContainsKey('PluginExecution') -eq $FALSE) {
12+
$Global:Icinga.Add(
13+
'PluginExecution',
14+
@{
15+
'LastExitCode' = $ExitCode;
16+
}
17+
)
18+
} else {
19+
if ($Global:Icinga.PluginExecution.ContainsKey('LastExitCode') -eq $FALSE) {
20+
$Global:Icinga.PluginExecution.Add('LastExitCode', $ExitCode);
21+
return;
22+
}
23+
24+
# Only add the first exit code we should cover during one runtime
25+
if ($null -eq $Global:Icinga.PluginExecution.LastExitCode) {
26+
$Global:Icinga.PluginExecution.LastExitCode = $ExitCode;
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)