Skip to content

Instantly share code, notes, and snippets.

@Iristyle
Created September 26, 2012 17:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Iristyle/3789363 to your computer and use it in GitHub Desktop.
Save Iristyle/3789363 to your computer and use it in GitHub Desktop.
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