Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A PowerShell script that emits Nagios compatible output when asked max number of processes running (over a certain amount of time) - use with NSClient++
Param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]
$Process,
[Parameter(Mandatory=$true)]
#[string]
[ValidatePattern("^(\d\.){0,1}(([0|1]\d)|(2[0-3])):[0-5]\d:[0-5]\d")]
$TimeSpan,
[Parameter(Mandatory=$false)]
[ValidateRange(0, 1000)]
[Int]
$MaxWarn = 1,
[Parameter(Mandatory=$false)]
[ValidateRange(0, 1000)]
[Int]
$MaxCritical = 0
)
Function ProcessesExceeding
{
Param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]
$ProcessName,
[Parameter(Mandatory=$true)]
[TimeSpan]
$MaximumRunTime
)
Process
{
Get-Process |
? { $_.Name -eq $ProcessName -and `
((New-TimeSpan $_.StartTime) -gt $MaximumRunTime) } |
% {
$params = @{
InputObject = $_;
MemberType = 'NoteProperty';
Name = 'TotalMinutesRunning';
Value = ((New-TimeSpan $_.StartTime).TotalMinutes);
PassThru = $true;
}
Add-Member @params
}
}
}
$parsedTime = [TimeSpan]::Parse($TimeSpan)
$procs = ProcessesExceeding $Process $parsedTime
$running = if ($null -eq $procs) { 0 }
else { $procs.Length }
$averageRuntime = if ($null -eq $procs) { 0 }
else
{
$procs |
Measure-Object TotalMinutesRunning -Average |
% { [Math]::Round($_.Average, 2) }
}
$status = if ($running -ge $MaxCritical) { "Critical" }
elseif ($running -ge $MaxWarn) { "Warning" }
else {"OK" }
Write-Host ("${status}: Processes Running More Than ${parsedTime}: $running|" +
"'$Process'=$running;$averageRuntime;$MaxWarn;$MaxCritical")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.