mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-21 15:19:58 -05:00
Added first draft for permission exception handling
This commit is contained in:
parent
806e6909e2
commit
2172ce2506
3 changed files with 61 additions and 0 deletions
36
lib/icinga/exception/Exit-IcingaMissingPermission.psm1
Normal file
36
lib/icinga/exception/Exit-IcingaMissingPermission.psm1
Normal 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;
|
||||
}
|
||||
22
lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1
Normal file
22
lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1
Normal 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' );
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
Import-IcingaLib core\perfcounter;
|
||||
Import-IcingaLib icinga\plugin;
|
||||
Import-IcingaLib icinga\exception;
|
||||
|
||||
function Invoke-IcingaCheckCPU()
|
||||
{
|
||||
|
|
@ -14,6 +15,8 @@ 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 -Input $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 '%';
|
||||
|
|
|
|||
Loading…
Reference in a new issue