2019-09-14 10:12:24 -04:00
|
|
|
function Exit-IcingaThrowException()
|
|
|
|
|
{
|
|
|
|
|
param(
|
|
|
|
|
[string]$InputString,
|
|
|
|
|
[string]$StringPattern,
|
|
|
|
|
[string]$CustomMessage,
|
2020-11-18 13:17:21 -05:00
|
|
|
$ExceptionThrown,
|
2020-08-28 03:10:23 -04:00
|
|
|
[ValidateSet('Permission', 'Input', 'Configuration', 'Connection', 'Unhandled', 'Custom')]
|
2019-09-17 11:12:06 -04:00
|
|
|
[string]$ExceptionType = 'Unhandled',
|
2021-04-28 03:43:07 -04:00
|
|
|
[hashtable]$ExceptionList = @{ },
|
2020-11-18 13:17:21 -05:00
|
|
|
[string]$KnowledgeBaseId,
|
2019-09-17 11:12:06 -04:00
|
|
|
[switch]$Force
|
2019-09-14 10:12:24 -04:00
|
|
|
);
|
|
|
|
|
|
2019-09-17 11:12:06 -04:00
|
|
|
if ($Force -eq $FALSE) {
|
|
|
|
|
if ($null -eq $InputString -Or [string]::IsNullOrEmpty($InputString)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-09-14 10:12:24 -04:00
|
|
|
|
2019-09-17 11:12:06 -04:00
|
|
|
if (-Not $InputString.Contains($StringPattern)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-09-14 10:12:24 -04:00
|
|
|
}
|
|
|
|
|
|
2021-04-28 03:43:07 -04:00
|
|
|
if ($null -eq $ExceptionList -Or $ExceptionList.Count -eq 0) {
|
|
|
|
|
$ExceptionList = $IcingaExceptions;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-14 10:12:24 -04:00
|
|
|
$ExceptionMessageLib = $null;
|
|
|
|
|
$ExceptionTypeString = '';
|
|
|
|
|
|
|
|
|
|
switch ($ExceptionType) {
|
|
|
|
|
'Permission' {
|
|
|
|
|
$ExceptionTypeString = 'Permission';
|
2021-04-28 03:43:07 -04:00
|
|
|
$ExceptionMessageLib = $ExceptionList.Permission;
|
2019-09-14 10:12:24 -04:00
|
|
|
};
|
|
|
|
|
'Input' {
|
|
|
|
|
$ExceptionTypeString = 'Invalid Input';
|
2021-04-28 03:43:07 -04:00
|
|
|
$ExceptionMessageLib = $ExceptionList.Inputs;
|
2019-09-14 10:12:24 -04:00
|
|
|
};
|
2020-05-19 11:29:42 -04:00
|
|
|
'Configuration' {
|
|
|
|
|
$ExceptionTypeString = 'Invalid Configuration';
|
2021-04-28 03:43:07 -04:00
|
|
|
$ExceptionMessageLib = $ExceptionList.Configuration;
|
2020-05-19 11:29:42 -04:00
|
|
|
};
|
2020-08-28 03:10:23 -04:00
|
|
|
'Connection' {
|
|
|
|
|
$ExceptionTypeString = 'Connection error';
|
2021-04-28 03:43:07 -04:00
|
|
|
$ExceptionMessageLib = $ExceptionList.Connection;
|
2020-08-28 03:10:23 -04:00
|
|
|
};
|
2019-09-14 10:12:24 -04:00
|
|
|
'Unhandled' {
|
|
|
|
|
$ExceptionTypeString = 'Unhandled';
|
|
|
|
|
};
|
2020-05-19 11:29:42 -04:00
|
|
|
'Custom' {
|
|
|
|
|
$ExceptionTypeString = 'Custom';
|
|
|
|
|
};
|
2019-09-14 10:12:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[string]$ExceptionName = '';
|
2020-11-18 13:17:21 -05:00
|
|
|
[string]$ExceptionIWKB = $KnowledgeBaseId;
|
|
|
|
|
|
2019-09-14 10:12:24 -04:00
|
|
|
if ($null -ne $ExceptionMessageLib) {
|
|
|
|
|
foreach ($definedError in $ExceptionMessageLib.Keys) {
|
|
|
|
|
if ($ExceptionMessageLib.$definedError -eq $ExceptionThrown) {
|
|
|
|
|
$ExceptionName = $definedError;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-18 03:16:30 -04:00
|
|
|
}
|
|
|
|
|
if ($null -eq $ExceptionMessageLib -Or [string]::IsNullOrEmpty($ExceptionName)) {
|
2020-05-19 11:29:42 -04:00
|
|
|
$ExceptionName = [string]::Format('{0} Exception', $ExceptionTypeString);
|
2021-05-18 03:16:30 -04:00
|
|
|
if ([string]::IsNullOrEmpty($InputString)) {
|
|
|
|
|
$InputString = $ExceptionThrown;
|
|
|
|
|
}
|
2019-09-14 10:12:24 -04:00
|
|
|
$ExceptionThrown = [string]::Format(
|
2020-05-19 11:29:42 -04:00
|
|
|
'{0} exception occured:{1}{2}',
|
|
|
|
|
$ExceptionTypeString,
|
2019-09-14 10:12:24 -04:00
|
|
|
"`r`n",
|
|
|
|
|
$InputString
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 10:52:24 -05:00
|
|
|
if ($ExceptionThrown -is [hashtable]) {
|
|
|
|
|
$ExceptionIWKB = $ExceptionThrown.IWKB;
|
|
|
|
|
$ExceptionThrown = $ExceptionThrown.Message;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 13:17:21 -05:00
|
|
|
if ([string]::IsNullOrEmpty($ExceptionIWKB) -eq $FALSE) {
|
|
|
|
|
$ExceptionIWKB = [string]::Format(
|
|
|
|
|
'{0}{0}Further details can be found on the Icinga for Windows Knowledge base: https://icinga.com/docs/windows/latest/doc/knowledgebase/{1}',
|
2021-02-16 10:10:00 -05:00
|
|
|
(New-IcingaNewLine),
|
2020-11-18 13:17:21 -05:00
|
|
|
$ExceptionIWKB
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$OutputMessage = '{0}: Icinga {6} Error was thrown: {4}: {5}{2}{2}{3}{1}';
|
2019-09-14 10:12:24 -04:00
|
|
|
if ([string]::IsNullOrEmpty($CustomMessage) -eq $TRUE) {
|
2020-11-18 13:17:21 -05:00
|
|
|
$OutputMessage = '{0}: Icinga {6} Error was thrown: {4}{2}{2}{3}{5}{1}';
|
2019-09-14 10:12:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$OutputMessage = [string]::Format(
|
|
|
|
|
$OutputMessage,
|
|
|
|
|
$IcingaEnums.IcingaExitCodeText.($IcingaEnums.IcingaExitCode.Unknown),
|
2020-11-18 13:17:21 -05:00
|
|
|
$ExceptionIWKB,
|
|
|
|
|
(New-IcingaNewLine),
|
2019-09-14 10:12:24 -04:00
|
|
|
$ExceptionThrown,
|
|
|
|
|
$ExceptionName,
|
|
|
|
|
$CustomMessage,
|
|
|
|
|
$ExceptionTypeString
|
|
|
|
|
);
|
|
|
|
|
|
2021-07-16 05:31:41 -04:00
|
|
|
Set-IcingaInternalPluginExitCode -ExitCode $IcingaEnums.IcingaExitCode.Unknown;
|
|
|
|
|
Set-IcingaInternalPluginException -PluginException $OutputMessage;
|
|
|
|
|
|
2021-08-06 12:12:27 -04:00
|
|
|
if ($null -eq $global:IcingaDaemonData -Or ($global:IcingaDaemonData.FrameworkRunningAsDaemon -eq $FALSE -And $global:IcingaDaemonData.JEAContext -eq $FALSE)) {
|
2020-05-13 10:53:15 -04:00
|
|
|
Write-IcingaConsolePlain $OutputMessage;
|
2019-10-05 16:01:15 -04:00
|
|
|
exit $IcingaEnums.IcingaExitCode.Unknown;
|
|
|
|
|
}
|
2019-09-14 10:12:24 -04:00
|
|
|
}
|