Skip to content

Commit

Permalink
Fixes service provider for brackets in service names
Browse files Browse the repository at this point in the history
  • Loading branch information
LordHepipud committed Aug 15, 2024
1 parent fad86f9 commit e6fd95d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
48 changes: 27 additions & 21 deletions lib/core/tools/Get-IcingaServices.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,26 @@ function Get-IcingaServices()
[array]$Exclude = @()
);

$ServiceInformation = Get-Service -Name $Service -ErrorAction SilentlyContinue;
$ServiceInformation = Get-Service;
$ServiceWmiInfo = $null;

if ($Service.Count -eq 0) {
$ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service;
} else {
$ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service |
try {
$ServiceWmiInfo = Get-IcingaWindowsInformation Win32_Service |
ForEach-Object {
foreach ($svc in $Service) {
if ($_.Name -Like $svc) {
return $_;
}
}
} | Select-Object StartName, Name, ExitCode, StartMode, PathName;
} catch {
Exit-IcingaThrowException -InputString $_.Exception.Message -StringPattern 'wildcard' -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.RegexError;
Exit-IcingaThrowException -CustomMessage $_.Exception.Message -ExceptionType 'Input' -ExceptionThrown $_.Exception.Message;
return $null;
}
}

if ($null -eq $ServiceInformation) {
Expand All @@ -27,7 +33,7 @@ function Get-IcingaServices()

[hashtable]$ServiceData = @{ };

foreach ($service in $ServiceInformation) {
foreach ($si in $ServiceInformation) {

[array]$DependentServices = $null;
[array]$DependingServices = $null;
Expand All @@ -37,12 +43,12 @@ function Get-IcingaServices()
[int]$StartModeId = 5;
[string]$StartMode = 'Unknown';

if ((Test-IcingaArrayFilter -InputObject $Service.ServiceName -Exclude $Exclude) -eq $FALSE) {
if ((Test-IcingaArrayFilter -InputObject $si.ServiceName -Include $Service -Exclude $Exclude) -eq $FALSE) {
continue;
}

foreach ($wmiService in $ServiceWmiInfo) {
if ($wmiService.Name -eq $service.ServiceName) {
if ($wmiService.Name -eq $si.ServiceName) {
$ServiceUser = $wmiService.StartName;
$ServicePath = $wmiService.PathName;
$ServiceExitCode = $wmiService.ExitCode;
Expand All @@ -55,45 +61,45 @@ function Get-IcingaServices()
}

#Dependent / Child
foreach ($dependency in $service.DependentServices) {
foreach ($dependency in $si.DependentServices) {
if ($null -eq $DependentServices) {
$DependentServices = @();
}
$DependentServices += $dependency.Name;
}

#Depends / Parent
foreach ($dependency in $service.ServicesDependedOn) {
foreach ($dependency in $si.ServicesDependedOn) {
if ($null -eq $DependingServices) {
$DependingServices = @();
}
$DependingServices += $dependency.Name;
}

$ServiceData.Add(
$service.Name, @{
$si.Name, @{
'metadata' = @{
'DisplayName' = $service.DisplayName;
'ServiceName' = $service.ServiceName;
'Site' = $service.Site;
'Container' = $service.Container;
'ServiceHandle' = $service.ServiceHandle;
'DisplayName' = $si.DisplayName;
'ServiceName' = $si.ServiceName;
'Site' = $si.Site;
'Container' = $si.Container;
'ServiceHandle' = $si.ServiceHandle;
'Dependent' = $DependentServices;
'Depends' = $DependingServices;
};
'configuration' = @{
'CanPauseAndContinue' = $service.CanPauseAndContinue;
'CanShutdown' = $service.CanShutdown;
'CanStop' = $service.CanStop;
'CanPauseAndContinue' = $si.CanPauseAndContinue;
'CanShutdown' = $si.CanShutdown;
'CanStop' = $si.CanStop;
'Status' = @{
'raw' = [int]$service.Status;
'value' = $service.Status;
'raw' = [int]$si.Status;
'value' = $si.Status;
};
'ServiceType' = @{
'raw' = [int]$service.ServiceType;
'value' = $service.ServiceType;
'raw' = [int]$si.ServiceType;
'value' = $si.ServiceType;
};
'ServiceHandle' = $service.ServiceHandle;
'ServiceHandle' = $si.ServiceHandle;
'StartType' = @{
'raw' = $StartModeId;
'value' = $StartMode;
Expand Down
28 changes: 17 additions & 11 deletions lib/core/tools/Test-IcingaArrayFilter.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,27 @@ function Test-IcingaArrayFilter()

return $FilteredArray;
} else {
foreach ($entry in $Exclude) {
if (([string]$InputObject).ToLower() -Like ([string]$entry).ToLower()) {
return $FALSE;
try {
foreach ($entry in $Exclude) {
if (([string]$InputObject).ToLower() -Like ([string]$entry).ToLower()) {
return $FALSE;
}
}
}

if ($Include.Count -eq 0) {
return $TRUE;
}
if ($Include.Count -eq 0) {
return $TRUE;
}

foreach ($entry in $Include) {
if (([string]$InputObject).ToLower() -Like ([string]$entry).ToLower()) {
$IncludeFound = $TRUE;
break;
foreach ($entry in $Include) {
if (([string]$InputObject).ToLower() -Like ([string]$entry).ToLower()) {
$IncludeFound = $TRUE;
break;
}
}
} catch {
Exit-IcingaThrowException -InputString $_.Exception.Message -StringPattern 'wildcard' -ExceptionType 'Input' -ExceptionThrown $IcingaExceptions.Inputs.RegexError;
Exit-IcingaThrowException -CustomMessage $_.Exception.Message -ExceptionType 'Input' -ExceptionThrown $_.Exception.Message;
return $FALSE;
}

return $IncludeFound;
Expand Down
3 changes: 2 additions & 1 deletion lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
CimClassNameUnknown = 'The provided class name you try to fetch with Get-CimInstance is not known on this system.';
WmiObjectClassUnknown = 'The provided class name you try to fetch with Get-WmiObject is not known on this system.';
MSSQLCredentialHandling = 'The connection to MSSQL was not possible because your login credential was not correct.';
MSSQLCommandMissing = 'Failed to build a SQL query'
MSSQLCommandMissing = 'Failed to build a SQL query';
RegexError = 'A request was not handled properly because a provided regex could not be interpreted. Please validate your regex and try again. In case you are trying to access a ressource containing [], you will have to escape each symbol by using `. Example: myservice`[`]';
};

[hashtable]$Configuration = @{
Expand Down

0 comments on commit e6fd95d

Please sign in to comment.