Skip to content

Feature: Add support to filter startuptype for service monitoring #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/director/Invoke-IcingaCheckService.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion config/director/Plugins_Bundle.json

Large diffs are not rendered by default.

35 changes: 31 additions & 4 deletions config/icinga/Invoke-IcingaCheckService.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,42 @@ object CheckCommand "Invoke-IcingaCheckService" {
value = "try { Use-Icinga -Minimal; } catch { Write-Output 'The Icinga PowerShell Framework is either not installed on the system or not configured properly. Please check https://icinga.com/docs/windows for further details'; Write-Output 'Error:' $$($$_.Exception.Message)Components:`r`n$$( Get-Module -ListAvailable 'icinga-powershell-*' )`r`n'Module-Path:'`r`n$$($$Env:PSModulePath); exit 3; }; Exit-IcingaExecutePlugin -Command 'Invoke-IcingaCheckService' "
order = 0
}
"-Verbosity" = {
description = "Changes the behavior of the plugin output which check states are printed: 0 (default): Only service checks/packages with state not OK will be printed 1: Only services with not OK will be printed including OK checks of affected check packages including Package config 2: Everything will be printed regardless of the check state 3: Identical to Verbose 2, but prints in addition the check package configuration e.g (All must be [OK])"
value = "$IcingaCheckService_Int32_Verbosity$"
order = 5
"-MitigateUnknown" = {
set_if = "$IcingaCheckService_Switchparameter_MitigateUnknown$"
order = 99
}
"-ThresholdInterval" = {
description = "Change the value your defined threshold checks against from the current value to a collected time threshold of the Icinga for Windows daemon, as described here: https://icinga.com/docs/icinga-for-windows/latest/doc/service/10-Register-Service-Checks/ An example for this argument would be 1m or 15m which will use the average of 1m or 15m for monitoring."
value = "$IcingaCheckService_String_ThresholdInterval$"
order = 100
}
"-Verbosity" = {
description = "Changes the behavior of the plugin output which check states are printed: 0 (default): Only service checks/packages with state not OK will be printed 1: Only services with not OK will be printed including OK checks of affected check packages including Package config 2: Everything will be printed regardless of the check state 3: Identical to Verbose 2, but prints in addition the check package configuration e.g (All must be [OK])"
value = "$IcingaCheckService_Int32_Verbosity$"
order = 6
}
"-FilterStartupType" = {
description = "Allows to include only services with a specific startup type inside the monitoring, in case you check for a list of specific services by using `-Service`"
value = {{
var arr = macro("$IcingaCheckService_Array_FilterStartupType$");
if (len(arr) == 0) {
return "@()";
}
return arr.map(
x => if (typeof(x) == String) {
var argLen = len(x);
if (argLen != 0 && x.substr(0,1) == "'" && x.substr(argLen - 1, argLen) == "'") {
x;
} else {
"'" + x + "'";
}
} else {
x;
}
).join(",");
}}
order = 5
}
"-Status" = {
description = "Status for the specified service or services to check against."
value = "$IcingaCheckService_String_Status$"
Expand Down Expand Up @@ -71,4 +97,5 @@ object CheckCommand "Invoke-IcingaCheckService" {
}
}
vars.IcingaCheckService_Switchparameter_NoPerfData = false
vars.IcingaCheckService_Switchparameter_MitigateUnknown = false
}
37 changes: 32 additions & 5 deletions config/icinga/Plugins_Bundle.conf
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,7 @@ object CheckCommand "Invoke-IcingaCheckUNCPath" {
order = 7
}
"-WarningTotal" = {
description = "A warning threshold for the shares total free space in byte units, like '50GB:' Please note that this value is decreasing over time, therefor you will have to use the plugin handler and add ':' at the end of your input to check for 'current value < threshold' like in the previous example Allowed units: B, KB, MB, GB, TB, PB, KiB, MiB, GiB, TiB, PiB"
description = "A warning threshold for the shares free space in either % or byte units, like '20%:' or '50GB:' Please note that this value is decreasing over time, therefor you will have to use the plugin handler and add ':' at the end of your input to check for 'current value < threshold' like in the previous example Allowed units: %, B, KB, MB, GB, TB, PB, KiB, MiB, GiB, TiB, PiB"
value = "$IcingaCheckUNCPath_Object_WarningTotal$"
order = 6
}
Expand Down Expand Up @@ -2050,16 +2050,42 @@ object CheckCommand "Invoke-IcingaCheckService" {
value = "try { Use-Icinga -Minimal; } catch { Write-Output 'The Icinga PowerShell Framework is either not installed on the system or not configured properly. Please check https://icinga.com/docs/windows for further details'; Write-Output 'Error:' $$($$_.Exception.Message)Components:`r`n$$( Get-Module -ListAvailable 'icinga-powershell-*' )`r`n'Module-Path:'`r`n$$($$Env:PSModulePath); exit 3; }; Exit-IcingaExecutePlugin -Command 'Invoke-IcingaCheckService' "
order = 0
}
"-Verbosity" = {
description = "Changes the behavior of the plugin output which check states are printed: 0 (default): Only service checks/packages with state not OK will be printed 1: Only services with not OK will be printed including OK checks of affected check packages including Package config 2: Everything will be printed regardless of the check state 3: Identical to Verbose 2, but prints in addition the check package configuration e.g (All must be [OK])"
value = "$IcingaCheckService_Int32_Verbosity$"
order = 5
"-MitigateUnknown" = {
set_if = "$IcingaCheckService_Switchparameter_MitigateUnknown$"
order = 99
}
"-ThresholdInterval" = {
description = "Change the value your defined threshold checks against from the current value to a collected time threshold of the Icinga for Windows daemon, as described here: https://icinga.com/docs/icinga-for-windows/latest/doc/service/10-Register-Service-Checks/ An example for this argument would be 1m or 15m which will use the average of 1m or 15m for monitoring."
value = "$IcingaCheckService_String_ThresholdInterval$"
order = 100
}
"-Verbosity" = {
description = "Changes the behavior of the plugin output which check states are printed: 0 (default): Only service checks/packages with state not OK will be printed 1: Only services with not OK will be printed including OK checks of affected check packages including Package config 2: Everything will be printed regardless of the check state 3: Identical to Verbose 2, but prints in addition the check package configuration e.g (All must be [OK])"
value = "$IcingaCheckService_Int32_Verbosity$"
order = 6
}
"-FilterStartupType" = {
description = "Allows to include only services with a specific startup type inside the monitoring, in case you check for a list of specific services by using `-Service`"
value = {{
var arr = macro("$IcingaCheckService_Array_FilterStartupType$");
if (len(arr) == 0) {
return "@()";
}
return arr.map(
x => if (typeof(x) == String) {
var argLen = len(x);
if (argLen != 0 && x.substr(0,1) == "'" && x.substr(argLen - 1, argLen) == "'") {
x;
} else {
"'" + x + "'";
}
} else {
x;
}
).join(",");
}}
order = 5
}
"-Status" = {
description = "Status for the specified service or services to check against."
value = "$IcingaCheckService_String_Status$"
Expand Down Expand Up @@ -2115,6 +2141,7 @@ object CheckCommand "Invoke-IcingaCheckService" {
}
}
vars.IcingaCheckService_Switchparameter_NoPerfData = false
vars.IcingaCheckService_Switchparameter_MitigateUnknown = false
}

object CheckCommand "Invoke-IcingaCheckCPU" {
Expand Down
1 change: 1 addition & 0 deletions doc/31-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
### Enhancements

* [#199](https://github.com/Icinga/icinga-powershell-plugins/issues/199) Adds additional metadata to `Invoke-IcingaCheckDiskHealth` for disk serial number, assigned partitions, boot device, device id, system device and device name
* [#211](https://github.com/Icinga/icinga-powershell-plugins/issues/211) Adds feature to `Invoke-IcingaCheckService`, allowing to filter for specific service startup types in case the plugin is used to check for specific services with `-Service`
* [#237](https://github.com/Icinga/icinga-powershell-plugins/issues/237) Adds support for `Invoke-IcingaCheckHTTPStatus` to add the content of the website output by using the flag `-AddOutputContent`
* [#241](https://github.com/Icinga/icinga-powershell-plugins/pull/241) Adds possibility to switch between free and used monitoring for `Invoke-IcingaCheckUNCPath` and consolidates monitoring like other plugins, with dynamic % monitoring.

Expand Down
2 changes: 2 additions & 0 deletions doc/plugins/13-Invoke-IcingaCheckService.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ To execute this plugin you will require to grant the following user permissions.
| Service | Array | false | @() | Used to specify an array of services which should be checked against the status. Supports '*' for wildcards. |
| 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 weren't exited properly. |
| Status | String | false | Running | Status for the specified service or services to check against. |
| FilterStartupType | Array | false | @() | Allows to include only services with a specific startup type inside the monitoring, in case you check for a list of specific services by using `-Service` |
| MitigateUnknown | SwitchParameter | false | False | This will tell the plugin to return OK instead of UNKNOWN, in case no service was added to this check |
| Verbosity | Int32 | false | 0 | Changes the behavior of the plugin output which check states are printed: 0 (default): Only service checks/packages with state not OK will be printed 1: Only services with not OK will be printed including OK checks of affected check packages including Package config 2: Everything will be printed regardless of the check state 3: Identical to Verbose 2, but prints in addition the check package configuration e.g (All must be [OK]) |
| NoPerfData | SwitchParameter | false | False | Disables the performance data output of this plugin |
| ThresholdInterval | Object | | | Change the value your defined threshold checks against from the current value to a collected time threshold of the Icinga for Windows daemon, as described [here](https://icinga.com/docs/icinga-for-windows/latest/doc/service/10-Register-Service-Checks/). An example for this argument would be 1m or 15m which will use the average of 1m or 15m for monitoring. |
Expand Down
76 changes: 46 additions & 30 deletions plugins/Invoke-IcingaCheckService.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
on Windows but are not running and weren't exited properly.
.PARAMETER Status
Status for the specified service or services to check against.
.PARAMETER FilterStartupType
Allows to include only services with a specific startup type inside the monitoring,
in case you check for a list of specific services by using `-Service`
.PARAMETER MitigateUnknown
This will tell the plugin to return OK instead of UNKNOWN, in case no service was added to this
check
.PARAMETER NoPerfData
Disables the performance data output of this plugin
.PARAMETER Verbosity
Expand All @@ -65,16 +71,19 @@
function Invoke-IcingaCheckService()
{
param (
[array]$Service = @(),
[array]$Exclude = @(),
[array]$Service = @(),
[array]$Exclude = @(),
[ValidateSet('Stopped', 'StartPending', 'StopPending', 'Running', 'ContinuePending', 'PausePending', 'Paused')]
[string]$Status = 'Running',
[string]$Status = 'Running',
[ValidateSet('Boot', 'System', 'Automatic', 'Manual', 'Disabled', 'Unknown')]
[array]$FilterStartupType = @(),
[switch]$MitigateUnknown = $FALSE,
[ValidateSet(0, 1, 2, 3)]
[int]$Verbosity = 0,
[int]$Verbosity = 0,
[switch]$NoPerfData
);

$ServicesPackage = New-IcingaCheckPackage -Name 'Services' -OperatorAnd -Verbose $Verbosity -AddSummaryHeader;
$ServicesPackage = New-IcingaCheckPackage -Name 'Services' -OperatorAnd -Verbose $Verbosity -AddSummaryHeader -IgnoreEmptyPackage:$MitigateUnknown;
$ServicesCountPackage = New-IcingaCheckPackage -Name 'Count Services' -OperatorAnd -Verbose $Verbosity -Hidden;
$FetchedServices = @{};
$ServiceSummary = $null;
Expand Down Expand Up @@ -116,6 +125,11 @@ function Invoke-IcingaCheckService()
} else {
$FetchedServices = Get-IcingaServices -Service $Service -Exclude $Exclude;
foreach ($services in $FetchedServices.Values) {
if ($FilterStartupType.Count -ne 0) {
if ($FilterStartupType -NotContains $ProviderEnums.ServiceStartupTypeName[$services.configuration.StartType.Raw]) {
continue;
}
}
$ServicesPackage.AddCheck(
(New-IcingaWindowsServiceCheckObject -Status $Status -Service $services)
);
Expand All @@ -136,32 +150,34 @@ function Invoke-IcingaCheckService()
}
}

$ServicesCountPackage.AddCheck(
(New-IcingaCheck -Name 'stopped services' -Value $ServiceSummary.StoppedCount)
);
$ServicesCountPackage.AddCheck(
(New-IcingaCheck -Name 'pending started services' -Value $ServiceSummary.StartPendingCount)
);
$ServicesCountPackage.AddCheck(
(New-IcingaCheck -Name 'pending stopped services' -Value $ServiceSummary.StopPendingCount)
);
$ServicesCountPackage.AddCheck(
(New-IcingaCheck -Name 'running services' -Value $ServiceSummary.RunningCount)
);
$ServicesCountPackage.AddCheck(
(New-IcingaCheck -Name 'pending continued services' -Value $ServiceSummary.ContinuePendingCount)
);
$ServicesCountPackage.AddCheck(
(New-IcingaCheck -Name 'pending paused services' -Value $ServiceSummary.PausePendingCount)
);
$ServicesCountPackage.AddCheck(
(New-IcingaCheck -Name 'paused services' -Value $ServiceSummary.PausedCount)
);
$ServicesCountPackage.AddCheck(
(New-IcingaCheck -Name 'service count' -Value $ServiceSummary.ServicesCounted)
);
if ($ServicesPackage.HasChecks()) {
$ServicesCountPackage.AddCheck(
(New-IcingaCheck -Name 'stopped services' -Value $ServiceSummary.StoppedCount)
);
$ServicesCountPackage.AddCheck(
(New-IcingaCheck -Name 'pending started services' -Value $ServiceSummary.StartPendingCount)
);
$ServicesCountPackage.AddCheck(
(New-IcingaCheck -Name 'pending stopped services' -Value $ServiceSummary.StopPendingCount)
);
$ServicesCountPackage.AddCheck(
(New-IcingaCheck -Name 'running services' -Value $ServiceSummary.RunningCount)
);
$ServicesCountPackage.AddCheck(
(New-IcingaCheck -Name 'pending continued services' -Value $ServiceSummary.ContinuePendingCount)
);
$ServicesCountPackage.AddCheck(
(New-IcingaCheck -Name 'pending paused services' -Value $ServiceSummary.PausePendingCount)
);
$ServicesCountPackage.AddCheck(
(New-IcingaCheck -Name 'paused services' -Value $ServiceSummary.PausedCount)
);
$ServicesCountPackage.AddCheck(
(New-IcingaCheck -Name 'service count' -Value $ServiceSummary.ServicesCounted)
);

$ServicesPackage.AddCheck($ServicesCountPackage)
$ServicesPackage.AddCheck($ServicesCountPackage)
}

return (New-IcingaCheckResult -Name 'Services' -Check $ServicesPackage -NoPerfData $NoPerfData -Compile);
}