Skip to content

Commit

Permalink
Merge pull request #126 from Icinga/fix/fetch_service_startup_type_ov…
Browse files Browse the repository at this point in the history
…er_wmi_instead_of_get_services

Fix: Prefer startup type of services fetching over WMI instead of Get-Services
  • Loading branch information
LordHepipud authored Jan 29, 2021
2 parents 3a873c7 + 37f0df1 commit 14ce201
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
1 change: 1 addition & 0 deletions doc/31-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic

* [#123](https://github.com/Icinga/icinga-powershell-plugins/pull/123) Fixes wrong documented user group for accessing Performance Counter objects which should be `Performance Monitor Users`
* [#124](https://github.com/Icinga/icinga-powershell-plugins/pull/124) Fixes crash on `Invoke-IcingaCheckService` if an automatic service is not running
* [#126](https://github.com/Icinga/icinga-powershell-plugins/pull/126) Fixes code base for `Invoke-IcingaCheckService` by preferring to fetch the startup type of services by using WMI instead of `Get-Services`, as the result of `Get-Services` might be empty in some cases

## 1.3.0 (2020-12-01)

Expand Down
23 changes: 23 additions & 0 deletions provider/enums/Icinga_ProviderEnums.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -863,9 +863,30 @@
}

[hashtable]$ServiceStartupType = @{
'Boot' = 0;
'System' = 1;
'Automatic' = 2;
'Manual' = 3;
'Disabled' = 4;
'Unknown' = 5; # Custom
}

[hashtable]$ServiceStartupTypeName = @{
0 = 'Boot';
1 = 'System';
2 = 'Automatic';
3 = 'Manual';
4 = 'Disabled';
5 = 'Unknown'; # Custom
}

[hashtable]$ServiceWmiStartupType = @{
'Boot' = 0;
'System' = 1;
'Auto' = 2;
'Manual' = 3;
'Disabled' = 4;
'Unknown' = 5; # Custom
}

<##################################################################################################
Expand Down Expand Up @@ -1013,6 +1034,8 @@
ServiceStatus = $ServiceStatus;
ServiceStatusName = $ServiceStatusName;
ServiceStartupType = $ServiceStartupType;
ServiceStartupTypeName = $ServiceStartupTypeName;
ServiceWmiStartupType = $ServiceWmiStartupType;
#/lib/provider/tasks
ScheduledTaskStatus = $ScheduledTaskStatus;
ScheduledTaskName = $ScheduledTaskName;
Expand Down
44 changes: 25 additions & 19 deletions provider/services/Icinga_ProviderServices.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function Get-IcingaServices()
[array]$DependingServices = $null;
$ServiceExitCode = 0;
[string]$ServiceUser = '';
[int]$StartModeId = 5;
[string]$StartMode = 'Unknown';

if ($Exclude -contains $service.ServiceName) {
continue;
Expand All @@ -35,6 +37,10 @@ function Get-IcingaServices()
if ($wmiService.Name -eq $service.ServiceName) {
$ServiceUser = $wmiService.StartName;
$ServiceExitCode = $wmiService.ExitCode;
if ([string]::IsNullOrEmpty($wmiService.StartMode) -eq $FALSE) {
$StartModeId = ([int]$ProviderEnums.ServiceWmiStartupType[$wmiService.StartMode]);
$StartMode = $ProviderEnums.ServiceStartupTypeName[$StartModeId];
}
break;
}
}
Expand All @@ -57,34 +63,34 @@ function Get-IcingaServices()

$ServiceData.Add(
$service.Name, @{
'metadata' = @{
'DisplayName' = $service.DisplayName;
'ServiceName' = $service.ServiceName;
'Site' = $service.Site;
'Container' = $service.Container;
'metadata' = @{
'DisplayName' = $service.DisplayName;
'ServiceName' = $service.ServiceName;
'Site' = $service.Site;
'Container' = $service.Container;
'ServiceHandle' = $service.ServiceHandle;
'Dependent' = $DependentServices;
'Depends' = $DependingServices;
'Dependent' = $DependentServices;
'Depends' = $DependingServices;
};
'configuration' = @{
'CanPauseAndContinue' = $service.CanPauseAndContinue;
'CanShutdown' = $service.CanShutdown;
'CanStop' = $service.CanStop;
'Status' = @{
'raw' = [int]$service.Status;
'CanShutdown' = $service.CanShutdown;
'CanStop' = $service.CanStop;
'Status' = @{
'raw' = [int]$service.Status;
'value' = $service.Status;
};
'ServiceType' = @{
'raw' = [int]$service.ServiceType;
'ServiceType' = @{
'raw' = [int]$service.ServiceType;
'value' = $service.ServiceType;
};
'ServiceHandle' = $service.ServiceHandle;
'StartType' = @{
'raw' = [int]$service.StartType;
'value' = $service.StartType;
'ServiceHandle' = $service.ServiceHandle;
'StartType' = @{
'raw' = $StartModeId;
'value' = $StartMode;
};
'ServiceUser' = $ServiceUser;
'ExitCode' = $ServiceExitCode;
'ServiceUser' = $ServiceUser;
'ExitCode' = $ServiceExitCode;
}
}
);
Expand Down

0 comments on commit 14ce201

Please sign in to comment.