Improved exception handling on a more global state

This commit is contained in:
Lord Hepipud 2019-09-14 16:12:24 +02:00
parent d1b30c77ae
commit cf158dd3e8
4 changed files with 78 additions and 40 deletions

View file

@ -71,6 +71,11 @@
$AllCountersIntances += $NewCounter;
}
} catch {
# Throw an exception in case our permissions are not enough to fetch performance counter
Exit-IcingaThrowException -InputString $_.Exception -StringPattern 'System.UnauthorizedAccessException' -ExceptionType 'Permission' -ExceptionThrown $IcingaExceptions.Permission.PerformanceCounter;
Exit-IcingaThrowException -InputString $_.Exception -StringPattern 'System.InvalidOperationException' -ExceptionType 'Input' -CustomMessage $Counter -ExceptionThrown $IcingaExceptions.Inputs.PerformanceCounter;
Exit-IcingaThrowException -InputString $_.Exception -StringPattern '' -ExceptionType 'Unhandled';
# Shouldn't actually get down here anyways
return (New-IcingaPerformanceCounterNullObject -FullName $Counter -ErrorMessage ([string]::Format('Failed to deserialize instances for counter "{0}". Exception: "{1}".', $Counter, $_.Exception.Message)));
}

View file

@ -1,36 +0,0 @@
Import-IcingaLib icinga\enums;
Import-IcingaLib icinga\exception;
function Exit-IcingaMissingPermission()
{
param(
[string]$InputString,
[string]$StringPattern,
[string]$CustomMessage,
[string]$ExeptionType
);
if ($null -eq $InputString -Or [string]::IsNullOrEmpty($InputString)) {
return;
}
if (-Not $InputString.Contains($StringPattern)) {
return;
}
$OutputMessage = '{0}: Icinga Permission Error was thrown: {3}{1}{1}{2}';
if ([string]::IsNullOrEmpty($CustomMessage) -eq $TRUE) {
$OutputMessage = '{0}: Icinga Permission Error was thrown {1}{1}{2}{3}';
}
$OutputMessage = [string]::Format(
$OutputMessage,
$IcingaEnums.IcingaExitCodeText.($IcingaEnums.IcingaExitCode.Unknown),
"`r`n",
$ExeptionType,
$CustomMessage
);
Write-Host $OutputMessage;
exit $IcingaEnums.IcingaExitCode.Unknown;
}

View file

@ -0,0 +1,72 @@
function Exit-IcingaThrowException()
{
param(
[string]$InputString,
[string]$StringPattern,
[string]$CustomMessage,
[string]$ExceptionThrown,
[ValidateSet('Permission','Input','Unhandled')]
[string]$ExceptionType = 'Unhandled'
);
if ($null -eq $InputString -Or [string]::IsNullOrEmpty($InputString)) {
return;
}
if (-Not $InputString.Contains($StringPattern)) {
return;
}
$ExceptionMessageLib = $null;
$ExceptionTypeString = '';
switch ($ExceptionType) {
'Permission' {
$ExceptionTypeString = 'Permission';
$ExceptionMessageLib = $IcingaExceptions.Permission;
};
'Input' {
$ExceptionTypeString = 'Invalid Input';
$ExceptionMessageLib = $IcingaExceptions.Inputs;
};
'Unhandled' {
$ExceptionTypeString = 'Unhandled';
};
}
[string]$ExceptionName = '';
if ($null -ne $ExceptionMessageLib) {
foreach ($definedError in $ExceptionMessageLib.Keys) {
if ($ExceptionMessageLib.$definedError -eq $ExceptionThrown) {
$ExceptionName = $definedError;
break;
}
}
} else {
$ExceptionName = 'Unhandled Exception';
$ExceptionThrown = [string]::Format(
'Unhandled exception occured:{0}{1}',
"`r`n",
$InputString
);
}
$OutputMessage = '{0}: Icinga {5} Error was thrown: {3}: {4}{1}{1}{2}';
if ([string]::IsNullOrEmpty($CustomMessage) -eq $TRUE) {
$OutputMessage = '{0}: Icinga {5} Error was thrown: {3}{1}{1}{2}{4}';
}
$OutputMessage = [string]::Format(
$OutputMessage,
$IcingaEnums.IcingaExitCodeText.($IcingaEnums.IcingaExitCode.Unknown),
"`r`n",
$ExceptionThrown,
$ExceptionName,
$CustomMessage,
$ExceptionTypeString
);
Write-Host $OutputMessage;
exit $IcingaEnums.IcingaExitCode.Unknown;
}

View file

@ -1,6 +1,5 @@
Import-IcingaLib core\perfcounter;
Import-IcingaLib icinga\plugin;
Import-IcingaLib icinga\exception;
function Invoke-IcingaCheckCPU()
{
@ -15,8 +14,6 @@ function Invoke-IcingaCheckCPU()
$CpuCounter = New-IcingaPerformanceCounter -Counter ([string]::Format('\Processor({0})\% processor time', $Core));
$CpuPackage = New-IcingaCheckPackage -Name 'CPU Load' -OperatorAnd -Verbos $Verbose;
Exit-IcingaMissingPermission -InputString $CpuCounter.ErrorMessage -StringPattern '"Global"' -ExeptionType $IcingaExceptions.Throw.PerformanceCounter;
if ($CpuCounter.Counters.Count -ne 0) {
foreach ($counter in $CpuCounter.Counters) {
$IcingaCheck = New-IcingaCheck -Name ([string]::Format('Core #{0}', $counter.Instance)) -Value $counter.Value().Value -Unit '%';