icinga-powershell-framework/lib/plugins/Invoke-IcingaCheckCPU.psm1

67 lines
2.9 KiB
PowerShell
Raw Normal View History

2019-07-22 09:25:00 -04:00
Import-IcingaLib core\perfcounter;
Import-IcingaLib core\tools;
2019-07-22 09:25:00 -04:00
Import-IcingaLib icinga\plugin;
<#
.SYNOPSIS
Checks cpu usage of cores.
.DESCRIPTION
Invoke-IcingaCheckCPU returns either 'OK', 'WARNING' or 'CRITICAL', based on the thresholds set.
e.g A system has 4 cores, each running at 60% usage, WARNING is set to 50%, CRITICAL is set to 75%. In this case the check will return WARNING.
2019-10-31 12:24:30 -04:00
More Information on https://github.com/Icinga/icinga-powershell-framework
.FUNCTIONALITY
This module is intended to be used to check on the current cpu usage of a specified core.
Based on the thresholds set the status will change between 'OK', 'WARNING' or 'CRITICAL'. The function will return one of these given codes.
.EXAMPLE
PS>Invoke-IcingaCheckCpu -Warning 50 -Critical 75
[OK]: Check package "CPU Load" is [OK]
| 'Core #0'=4,588831%;50;75;0;100 'Core #1'=0,9411243%;50;75;0;100 'Core #2'=11,53223%;50;75;0;100 'Core #3'=4,073013%;50;75;0;100
.EXAMPLE
PS>Invoke-IcingaCheckCpu -Warning 50 -Critical 75 -Core 1
[OK]: Check package "CPU Load" is [OK]
| 'Core #1'=2,612651%;50;75;0;100
.PARAMETER Warning
Used to specify a Warning threshold. In this case an integer value.
.PARAMETER Critical
Used to specify a Critical threshold. In this case an integer value.
.PARAMETER Core
Used to specify a single core to check on.
.INPUTS
System.String
.OUTPUTS
System.String
.LINK
2019-10-31 12:24:30 -04:00
https://github.com/Icinga/icinga-powershell-framework
.NOTES
#>
2019-07-22 09:25:00 -04:00
function Invoke-IcingaCheckCPU()
{
param(
2019-10-29 05:51:17 -04:00
$Warning = $null,
$Critical = $null,
2019-10-29 05:47:24 -04:00
[string]$Core = '*',
2019-07-22 09:25:00 -04:00
[switch]$NoPerfData,
[ValidateSet(0, 1, 2, 3)]
2019-10-29 05:47:24 -04:00
[int]$Verbosity = 0
2019-07-22 09:25:00 -04:00
);
$CpuCounter = New-IcingaPerformanceCounter -Counter ([string]::Format('\Processor({0})\% processor time', $Core));
$CpuPackage = New-IcingaCheckPackage -Name 'CPU Load' -OperatorAnd -Verbose $Verbosity;
$CpuCount = ([string](Get-IcingaCpuCount)).Length;
2019-07-22 09:25:00 -04:00
if ($CpuCounter.Counters.Count -ne 0) {
foreach ($counter in $CpuCounter.Counters) {
2019-09-16 12:07:57 -04:00
$IcingaCheck = New-IcingaCheck -Name ([string]::Format('Core {0}', (Format-IcingaDigitCount $counter.Instance.Replace('_', '') -Digits $CpuCount -Symbol ' '))) -Value $counter.Value().Value -Unit '%';
2019-07-22 09:25:00 -04:00
$IcingaCheck.WarnOutOfRange($Warning).CritOutOfRange($Critical) | Out-Null;
$CpuPackage.AddCheck($IcingaCheck);
}
} else {
2019-09-16 12:07:57 -04:00
$IcingaCheck = New-IcingaCheck -Name ([string]::Format('Core {0}', (Format-IcingaDigitCount $Core.Replace('_', '') -Digits $CpuCount -Symbol ' '))) -Value $CpuCounter.Value().Value -Unit '%';
2019-07-22 09:25:00 -04:00
$IcingaCheck.WarnOutOfRange($Warning).CritOutOfRange($Critical) | Out-Null;
$CpuPackage.AddCheck($IcingaCheck);
}
return (New-IcingaCheckResult -Name 'CPU Load' -Check $CpuPackage -NoPerfData $NoPerfData -Compile);
2019-07-22 09:25:00 -04:00
}