Adds wrapper for WMI and CIM fetching with error handling

Implements #80
This commit is contained in:
Lord Hepipud 2020-07-27 11:32:36 +02:00
parent c7a2660969
commit 64a7724340
2 changed files with 58 additions and 0 deletions

View file

@ -0,0 +1,56 @@
function Get-IcingaWindowsInformation()
{
param (
[string]$ClassName,
$Filter
);
$Arguments = @{
'ClassName' = $ClassName;
}
if ([string]::IsNullOrEmpty($Filter) -eq $FALSE) {
$Arguments.Add(
'Filter', $Filter
);
}
if ((Get-Command 'Get-CimInstance' -ErrorAction SilentlyContinue)) {
try {
return (Get-CimInstance @Arguments -ErrorAction Stop)
} catch {
$ErrorName = $_.Exception.NativeErrorCode;
$ErrorMessage = $_.Exception.Message;
switch ($_.Exception.StatusCode) {
# InvalidClass
5 {
Exit-IcingaThrowException -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.CimClassNameUnknown -CustomMessage $ClassName -Force;
};
# TODO: Find error Id for permission errors
# Permission error
#x {
# Exit-IcingaThrowException -ExceptionType 'Permission' -ExceptionThrown $IcingaExceptions.Permission.CimInstance -CustomMessage $ClassName -Force;
#};
# All other errors
default {
Exit-IcingaThrowException -ExceptionType 'Custom' -InputString $ErrorMessage -CustomMessage ([string]::Format('CimInstanceUnhandledError: Class "{0}": Error "{1}"', $ClassName, $ErrorName)) -Force;
}
}
}
}
if ((Get-Command 'Get-WmiObject' -ErrorAction SilentlyContinue)) {
try {
return (Get-WmiObject @Arguments -ErrorAction Stop)
} catch {
$ErrorName = $_.CategoryInfo.Category;
$ErrorMessage = $_.Exception.Message;
Exit-IcingaThrowException -ExceptionType 'Custom' -InputString $ErrorMessage -CustomMessage ([string]::Format('WmiObjectUnhandledError: Class "{0}": Error "{1}"', $ClassName, $ErrorName)) -Force;
}
}
# Exception
Exit-IcingaThrowException -ExceptionType 'Custom' -InputString 'Failed to fetch Windows information by using CimInstance or WmiObject. Both commands are not present on the system.' -CustomMessage ([string]::Format('CimWmiUnhandledError: Class "{0}"', $ClassName)) -Force;
}

View file

@ -7,6 +7,7 @@
[hashtable]$Permission = @{
PerformanceCounter = 'A Plugin failed to fetch Performance Counter information. This may be caused when the used Service User is not permitted to access these information. To fix this, please add the User the Icinga Agent is running on into the "Performance Log Users" group and restart the service.';
CacheFolder = "A plugin failed to write new data into the configured cache directory. Please update the permissions of this folder to allow write access for the user the Icinga Service is running with or use another folder as cache directory.";
CimInstance = 'The user you are running this command as does not have permission to access the requested Cim-Object.';
};
[hashtable]$Inputs = @{
@ -14,6 +15,7 @@
EventLogLogName = 'Failed to fetch EventLog information. Please specify a valid LogName.';
EventLog = 'Failed to fetch EventLog information. Please check your inputs for EntryTypes and other categories and try again.';
ConversionUnitMissing = 'Unable to parse input value. You have to add an unit to your input value. Example: "10GB". Allowed units are: "B, KB, MB, GB, TB, PB, KiB, MiB, GiB, TiB, PiB".';
CimClassNameUnknown = 'The provided class name you try to fetch with Get-CimInstance is not known on this system.';
};
[hashtable]$Configuration = @{