Updated Services Provider, Enums and Helper functions

* Added new function to get Service Name for Check output
* Added missing Service State enums
* Updated Service Status Code function to use existing Test-Numeric
* Update Service Check with new Cmdlets
This commit is contained in:
Lord Hepipud 2019-07-24 14:28:53 +02:00
parent 9b60037ebe
commit 93951cd607
4 changed files with 50 additions and 13 deletions

View file

@ -1,4 +1,5 @@
Import-IcingaLib provider\services;
Import-IcingaLib provider\enums;
Import-IcingaLib icinga\plugin;
function Invoke-IcingaCheckService()
@ -11,12 +12,11 @@ function Invoke-IcingaCheckService()
);
$FoundService = Get-IcingaServices -Service $Service;
$ServiceName = $FoundService.Values.metadata.ServiceName;
$DisplayName = $FoundService.Values.metadata.DisplayName;
$ServiceName = Get-IcingaServiceCheckName -ServiceInput $Service -Service $FoundService;
$Status = ConvertTo-ServiceStatusCode -Status $Status;
$StatusRaw = $FoundService.Values.configuration.Status.raw;
$IcingaCheck = New-IcingaCheck -Name ([string]::Format('Service "{0} ({1})"', $DisplayName, $ServiceName)) -Value $StatusRaw -ObjectExists $FoundService -ValueTranslation $ProviderEnums.ServicesStatus;
$IcingaCheck = New-IcingaCheck -Name $ServiceName -Value $StatusRaw -ObjectExists $FoundService -Translation $ProviderEnums.ServiceStatusName;
$IcingaCheck.CritIfNotMatch($Status) | Out-Null;
exit (New-IcingaCheckResult -Name "Service $Service" -Check $IcingaCheck -NoPerfData $TRUE -Compile);

View file

@ -475,9 +475,24 @@
################# /lib/provider/Services ###########################################################
##################################################################################################>
[hashtable]$ServiceStatusName = @{
1 = 'Stopped';
2 = 'StartPending';
3 = 'StopPending';
4 = 'Running';
5 = 'ContinuePending';
6 = 'PausePending';
7 = 'Paused';
}
[hashtable]$ServiceStatus = @{
'Stopped' = 1;
'Running' = 4;
'Stopped' = 1;
'StartPending' = 2;
'StopPending' = 3;
'Running' = 4;
'ContinuePending' = 5;
'PausePending' = 6;
'Paused' = 7;
}
[hashtable]$ProviderEnums = @{
@ -504,6 +519,7 @@
WindowsOSType = $WindowsOSType;
#/lib/provider/services
ServiceStatus = $ServiceStatus;
ServiceStatusName =$ServiceStatusName;
}
Export-ModuleMember -Variable @('ProviderEnums');

View file

@ -1,14 +1,15 @@
Import-IcingaLib core\tools;
Import-IcingaLib provider\enums;
function ConvertTo-ServiceStatusCode()
{
param (
$Status
)
if ($Status -match "^\d+$") {
return $Status
} else {
$Status = $ProviderEnums.ServiceStatus.($Status);
if (Test-Numeric $Status) {
return [int]$Status
}
return $Status;
return [int]($ProviderEnums.ServiceStatus.$Status);
}

View file

@ -0,0 +1,20 @@
function Get-IcingaServiceCheckName()
{
param (
[string]$ServiceInput,
$Service
);
if ($null -eq $Service) {
return [string]::Format(
'Service "{0}"',
$ServiceInput
);
}
return [string]::Format(
'Service "{0} ({1})"',
$Service.Values.metadata.DisplayName,
$Service.Values.metadata.ServiceName
);
}