Added first draft for permission exception handling

This commit is contained in:
Lord Hepipud 2019-09-13 19:04:27 +02:00
parent 806e6909e2
commit 2172ce2506
3 changed files with 61 additions and 0 deletions

View file

@ -0,0 +1,36 @@
Import-IcingaLib icinga\enums;
Import-IcingaLib icinga\exception;
function Exit-IcingaMissingPermission()
{
param(
[string]$Input,
[string]$StringPattern,
[string]$CustomMessage,
[string]$ExeptionType
);
if ($null -eq $Input -Or [string]::IsNullOrEmpty($Input)) {
return;
}
if (-Not $Input.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,22 @@
<#
# This script will provide 'Enums' we can use within our module to
# easier access constants and to maintain a better overview of the
# entire components
#>
[hashtable]$Throw = @{
PerformanceCounter = 'Icinga failed to fetch Performance Counter information. This may be caused when the Icinga Service User is not permited to access these information. To fix this, please add the User the Icinga Agent is running on into the "Performance Log Users" group.';
};
<#
# Once we defined a new enum hashtable above, simply add it to this list
# to make it available within the entire module.
#
# Example usage:
# $IcingaExceptionEnums.IcingaExecptionHandlers.PerformanceCounter
#>
[hashtable]$IcingaExceptions = @{
Throw = $Throw;
}
Export-ModuleMember -Variable @( 'IcingaExceptions' );

View file

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