Skip to content

Commit 3bef2b4

Browse files
authored
Merge pull request #645 from Icinga:fix/error_and_exception_handling_while_using_api
Fix: Error and exception handling over API checks Fixes error and exception handling while using API-Checks, which now will in most cases always return a proper check-result object and also abort while running into plugin execution errors, in case a server is not reachable by the time sync plugin as example.
2 parents 68aba5f + fa79477 commit 3bef2b4

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1818
* [#615](https://github.com/Icinga/icinga-powershell-framework/issues/615) Fixes the framework migration tasks which fails in case multiple versions of the framework are installed, printing warnings in case there is
1919
* [#617](https://github.com/Icinga/icinga-powershell-framework/issues/617) Fixes failing calls for plugins which use a switch argument like `-NoPerfData`, which is followed directly by the `-ThresholdInterval` argument
2020
* [#621](https://github.com/Icinga/icinga-powershell-framework/pull/621) Fixes `-ThresholdInterval` key detection on newer systems
21+
* [#645](https://github.com/Icinga/icinga-powershell-framework/pull/645) Fixes error and exception handling while using API-Checks, which now will in most cases always return a proper check-result object and also abort while running into plugin execution errors, in case a server is not reachable by the time sync plugin for example
2122

2223
### Enhancements
2324

lib/daemons/modules/ApiChecks/Invoke-IcingaApiChecksRESTCall.psm1

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ function Invoke-IcingaApiChecksRESTCall()
99
[Hashtable]$ContentResponse = @{ };
1010

1111
# Short our call
12-
$CheckerAliases = $Global:Icinga.Public.Daemons.RESTApi.CommandAliases.checker;
13-
$CheckConfig = $Request.Body;
14-
[int]$ExitCode = 3; #Unknown
12+
$CheckerAliases = $Global:Icinga.Public.Daemons.RESTApi.CommandAliases.checker;
13+
$CheckConfig = $Request.Body;
14+
[int]$ExitCode = 3; #Unknown
15+
[string]$CheckResult = '';
16+
[string]$InternalError = '';
1517

1618
# Check if there are an inventory aliases configured
1719
# This should be maintained by the developer and not occur
@@ -56,10 +58,20 @@ function Invoke-IcingaApiChecksRESTCall()
5658
}
5759

5860
if ((Test-IcingaRESTApiCommand -Command $ExecuteCommand -Endpoint 'apichecks') -eq $FALSE) {
61+
62+
Add-IcingaHashtableItem `
63+
-Hashtable $ContentResponse `
64+
-Key $ExecuteCommand `
65+
-Value @{
66+
'exitcode' = 3;
67+
'checkresult' = [string]::Format('[UNKNOWN] Icinga Permission error was thrown: Permission denied for command "{0}"{1}{1}The command "{0}" you are trying to execute over the REST-Api endpoint "apichecks" is not whitelisted for remote execution.', $ExecuteCommand, (New-IcingaNewLine));
68+
'perfdata' = @();
69+
} | Out-Null;
70+
5971
Send-IcingaTCPClientMessage -Message (
6072
New-IcingaTCPClientRESTMessage `
6173
-HTTPResponse ($IcingaHTTPEnums.HTTPResponseType.'Forbidden') `
62-
-ContentBody ([string]::Format('The command "{0}" you are trying to execute over this REST-Api endpoint "apichecks" is not whitelisted for remote execution.', $ExecuteCommand))
74+
-ContentBody $ContentResponse
6375
) -Stream $Connection.Stream;
6476

6577
return;
@@ -100,9 +112,19 @@ function Invoke-IcingaApiChecksRESTCall()
100112
-Value $CmdArgValue | Out-Null;
101113
};
102114

103-
[int]$ExitCode = & $ExecuteCommand @Arguments;
115+
try {
116+
[int]$ExitCode = & $ExecuteCommand @Arguments;
117+
} catch {
118+
[int]$ExitCode = 3;
119+
$InternalError = $_.Exception.Message;
120+
}
104121
} elseif ($Request.Method -eq 'GET') {
105-
[int]$ExitCode = & $ExecuteCommand;
122+
try {
123+
[int]$ExitCode = & $ExecuteCommand;
124+
} catch {
125+
[int]$ExitCode = 3;
126+
$InternalError = $_.Exception.Message;
127+
}
106128
} else {
107129
Send-IcingaTCPClientMessage -Message (
108130
New-IcingaTCPClientRESTMessage `
@@ -115,7 +137,16 @@ function Invoke-IcingaApiChecksRESTCall()
115137

116138
# Once the check is executed, the plugin output and the performance data are stored
117139
# within a special cache map we can use for accessing
118-
$CheckResult = Get-IcingaCheckSchedulerPluginOutput;
140+
if ([string]::IsNullOrEmpty($InternalError)) {
141+
$CheckResult = Get-IcingaCheckSchedulerPluginOutput;
142+
} else {
143+
if ($InternalError.Contains('[UNKNOWN]') -eq $FALSE) {
144+
# Ensure we format the error message more user friendly
145+
$CheckResult = [string]::Format('[UNKNOWN] Icinga Plugin execution error was thrown during API request:{0}{0}{1}', (New-IcingaNewLine), $InternalError);
146+
} else {
147+
$CheckResult = $InternalError;
148+
}
149+
}
119150
[array]$PerfData = Get-IcingaCheckSchedulerPerfData;
120151

121152
# Ensure our PerfData variable is always an array

lib/icinga/exception/Exit-IcingaThrowException.psm1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,7 @@ function Exit-IcingaThrowException()
113113
if ($Global:Icinga.Protected.RunAsDaemon -eq $FALSE -And $Global:Icinga.Protected.JEAContext -eq $FALSE) {
114114
Write-IcingaConsolePlain $OutputMessage;
115115
exit $IcingaEnums.IcingaExitCode.Unknown;
116+
} else {
117+
throw $OutputMessage;
116118
}
117119
}

0 commit comments

Comments
 (0)