Skip to content

Commit 6631810

Browse files
committed
Improves service check for automatic startup type, performance and excludes
Fix #16
1 parent 8e8f034 commit 6631810

File tree

5 files changed

+95
-13
lines changed

5 files changed

+95
-13
lines changed

doc/plugins/11-Invoke-IcingaCheckService.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
Checks if a service has a specified status.
77

88
Invoke-icingaCheckService returns either 'OK' or 'CRITICAL', if a service status is matching status to be checked.
9+
If no specific services are configured to check for, the plugin will lookup all services which are configured to
10+
run automatically on the Windows startup and report an error in case the service is not running and was not
11+
terminated properly.
912
More Information on https://github.com/Icinga/icinga-powershell-plugins
1013

1114
## Arguments
1215

1316
| Argument | Type | Required | Default | Description |
1417
| --- | --- | --- | --- | --- |
1518
| Service | Array | false | | Used to specify an array of services which should be checked against the status. Seperated with ',' |
19+
| Exclude | Array | false | @() | Allows to exclude services which might come in handy for checking services which are configured to start automatically on Windows but are not running and werent exited properly. Seperated with ',' |
1620
| Status | String | false | Running | Status for the specified service or services to check against. |
1721
| Verbosity | Int32 | false | 0 | |
1822
| NoPerfData | SwitchParameter | false | False | |
@@ -22,11 +26,35 @@ More Information on https://github.com/Icinga/icinga-powershell-plugins
2226
### Example Command 1
2327

2428
```powershell
25-
Invoke-IcingaCheckService -Service WiaRPC, Spooler -Status '1' -Verbose 3
29+
Invoke-IcingaCheckService
2630
```
2731

2832
### Example Output 1
2933

34+
```powershell
35+
[OK] Check package "Services"
36+
```
37+
38+
### Example Command 2
39+
40+
```powershell
41+
Invoke-IcingaCheckService -Service WiaRPC, Spooler -Status 'Running' -Verbose 3
42+
```
43+
44+
### Example Output 2
45+
3046
```powershell
3147
[CRITICAL]: Check package "Services" is [CRITICAL] (Match All) \_ [OK]: Service "Ereignisse zum Abrufen von Standbildern (WiaRPC)" is Stopped \_ [CRITICAL]: Service "Druckwarteschlange (Spooler)" Running is not matching Stopped
3248
```
49+
50+
### Example Command 3
51+
52+
```powershell
53+
Invoke-IcingaCheckService -Exclude icinga2
54+
```
55+
56+
### Example Output 3
57+
58+
```powershell
59+
[OK] Check package "Services"
60+
```

plugins/Invoke-IcingaCheckService.psm1

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,31 @@
33
Checks if a service has a specified status.
44
.DESCRIPTION
55
Invoke-icingaCheckService returns either 'OK' or 'CRITICAL', if a service status is matching status to be checked.
6+
If no specific services are configured to check for, the plugin will lookup all services which are configured to
7+
run automatically on the Windows startup and report an error in case the service is not running and was not
8+
terminated properly.
69
More Information on https://github.com/Icinga/icinga-powershell-plugins
710
.FUNCTIONALITY
811
This module is intended to be used to check whether one or more services have a certain status.
912
As soon as one of the specified services does not match the status, the function returns 'CRITICAL' instead of 'OK'.
1013
.EXAMPLE
11-
PS>Invoke-IcingaCheckService -Service WiaRPC, Spooler -Status '1' -Verbose 3
14+
PS>Invoke-IcingaCheckService
15+
[OK] Check package "Services"
16+
.EXAMPLE
17+
PS>Invoke-IcingaCheckService -Service WiaRPC, Spooler -Status 'Running' -Verbose 3
1218
[CRITICAL]: Check package "Services" is [CRITICAL] (Match All)
1319
\_ [OK]: Service "Ereignisse zum Abrufen von Standbildern (WiaRPC)" is Stopped
1420
\_ [CRITICAL]: Service "Druckwarteschlange (Spooler)" Running is not matching Stopped
21+
.EXAMPLE
22+
PS>Invoke-IcingaCheckService -Exclude icinga2
23+
[OK] Check package "Services"
1524
.PARAMETER Service
1625
Used to specify an array of services which should be checked against the status.
1726
Seperated with ','
27+
.PARAMETER Exclude
28+
Allows to exclude services which might come in handy for checking services which are configured to start automatically
29+
on Windows but are not running and werent exited properly.
30+
Seperated with ','
1831
.PARAMETER Status
1932
Status for the specified service or services to check against.
2033
.INPUTS
@@ -30,6 +43,7 @@ function Invoke-IcingaCheckService()
3043
{
3144
param(
3245
[array]$Service,
46+
[array]$Exclude = @(),
3347
[ValidateSet('Stopped', 'StartPending', 'StopPending', 'Running', 'ContinuePending', 'PausePending', 'Paused')]
3448
[string]$Status = 'Running',
3549
[ValidateSet(0, 1, 2, 3)]
@@ -39,17 +53,42 @@ function Invoke-IcingaCheckService()
3953

4054
$ServicesPackage = New-IcingaCheckPackage -Name 'Services' -OperatorAnd -Verbose $Verbosity;
4155
$ServicesCountPackage = New-IcingaCheckPackage -Name 'Count Services' -OperatorAnd -Verbose $Verbosity -Hidden;
42-
56+
$FetchedServices = @{};
4357
[int]$StoppedCount,[int]$StartPendingCount,[int]$StopPendingCount,[int]$RunningCount,[int]$ContinuePendingCount,[int]$PausePendingCount,[int]$PausedCount,[int]$ServicesCounted = 0
44-
foreach ($services in $Service) {
58+
59+
# Automatic load auto start services and check for errors in case no service
60+
# to check for is configured
61+
if ($Service.Count -eq 0) {
62+
$AutoServices = Get-IcingaServices -Exclude $Exclude;
63+
foreach ($autoservice in $AutoServices.Values) {
64+
if ($autoservice.configuration.ExitCode -eq 0) {
65+
continue;
66+
}
67+
if ($autoservice.configuration.StartType.Raw -ne $ProviderEnums.ServiceStartupType.Automatic) {
68+
continue;
69+
}
70+
if ($autoservice.configuration.Status.Raw -eq $ProviderEnums.ServiceStatus.Running) {
71+
continue;
72+
}
73+
74+
$FetchedServices.Add(
75+
$autoservice.metadata.ServiceName,
76+
$autoservice
77+
);
78+
}
79+
} else {
80+
$FetchedServices = Get-IcingaServices -Service $Service -Exclude $Exclude;
81+
}
82+
83+
foreach ($services in $FetchedServices.Keys) {
84+
$services = $FetchedServices[$services];
4585
$IcingaCheck = $null;
4686

47-
$FoundService = Get-IcingaServices -Service $services;
48-
$ServiceName = Get-IcingaServiceCheckName -ServiceInput $services -Service $FoundService;
87+
$ServiceName = Get-IcingaServiceCheckName -ServiceInput $services.metadata.DisplayName -Service $services;
4988
$ConvertedStatus = ConvertTo-ServiceStatusCode -Status $Status;
50-
$StatusRaw = $FoundService.Values.configuration.Status.raw;
89+
$StatusRaw = $services.configuration.Status.raw;
5190

52-
$IcingaCheck = New-IcingaCheck -Name $ServiceName -Value $StatusRaw -ObjectExists $FoundService -Translation $ProviderEnums.ServiceStatusName;
91+
$IcingaCheck = New-IcingaCheck -Name $ServiceName -Value $StatusRaw -ObjectExists $services -Translation $ProviderEnums.ServiceStatusName;
5392
$IcingaCheck.CritIfNotMatch($ConvertedStatus) | Out-Null;
5493
$ServicesPackage.AddCheck($IcingaCheck)
5594

provider/enums/Icinga_ProviderEnums.psm1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,12 @@
495495
'Paused' = 7;
496496
}
497497

498+
[hashtable]$ServiceStartupType = @{
499+
'Automatic' = 2;
500+
'Manual' = 3;
501+
'Disabled' = 4;
502+
}
503+
498504
[hashtable]$ProviderEnums = @{
499505
#/lib/provider/bios
500506
BiosCharacteristics = $BiosCharacteristics;
@@ -519,7 +525,8 @@
519525
WindowsOSType = $WindowsOSType;
520526
#/lib/provider/services
521527
ServiceStatus = $ServiceStatus;
522-
ServiceStatusName =$ServiceStatusName;
528+
ServiceStatusName = $ServiceStatusName;
529+
ServiceStartupType = $ServiceStartupType;
523530
}
524531

525532
Export-ModuleMember -Variable @('ProviderEnums');

provider/services/Get-IcingaServiceCheckName.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function Get-IcingaServiceCheckName()
1414

1515
return [string]::Format(
1616
'Service "{0} ({1})"',
17-
$Service.Values.metadata.DisplayName,
18-
$Service.Values.metadata.ServiceName
17+
$Service.metadata.DisplayName,
18+
$Service.metadata.ServiceName
1919
);
2020
}

provider/services/Icinga_ProviderServices.psm1

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
function Get-IcingaServices()
22
{
33
param (
4-
[array]$Service
4+
[array]$Service,
5+
[array]$Exclude = @()
56
);
67

78
$ServiceInformation = Get-Service -Name $Service -ErrorAction SilentlyContinue;
@@ -23,11 +24,17 @@ function Get-IcingaServices()
2324

2425
[array]$DependentServices = $null;
2526
[array]$DependingServices = $null;
27+
$ServiceExitCode = 0;
2628
[string]$ServiceUser = '';
2729

30+
if ($Exclude -contains $service.ServiceName) {
31+
continue;
32+
}
33+
2834
foreach ($wmiService in $ServiceWmiInfo) {
2935
if ($wmiService.Name -eq $service.ServiceName) {
30-
$ServiceUser = $wmiService.StartName;
36+
$ServiceUser = $wmiService.StartName;
37+
$ServiceExitCode = $wmiService.ExitCode;
3138
break;
3239
}
3340
}
@@ -77,6 +84,7 @@ function Get-IcingaServices()
7784
'value' = $service.StartType;
7885
};
7986
'ServiceUser' = $ServiceUser;
87+
'ExitCode' = $ServiceExitCode;
8088
}
8189
}
8290
);

0 commit comments

Comments
 (0)