Skip to content

Instantly share code, notes, and snippets.

@Iristyle
Created October 11, 2011 20:05
Show Gist options
  • Save Iristyle/1279244 to your computer and use it in GitHub Desktop.
Save Iristyle/1279244 to your computer and use it in GitHub Desktop.
Show processes running longer than X (for NSClient++ with OpsView or Nagios nrpe)
Param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]
$Process,
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
#[string]
[ValidatePattern("^(\d\.){0,1}(([0|1]\d)|(2[0-3])):[0-5]\d:[0-5]\d")]
$TimeSpan,
[Parameter(Mandatory=$false, ValueFromPipeline=$true)]
[ValidateRange(0, 1000)]
[Int]
$MaxWarn = 1,
[Parameter(Mandatory=$false, ValueFromPipeline=$true)]
[ValidateRange(0, 1000)]
[Int]
$MaxCritical = 0
)
Function ProcessesExceeding
{
Param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string]
$ProcessName,
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[TimeSpan]
$MaximumRunTime
)
Process
{
Get-Process | ? { $_.Name -eq $ProcessName -and ((New-TimeSpan $_.StartTime) -gt $MaximumRunTime) } `
| % { Add-Member -InputObject $_ -MemberType NoteProperty -name TotalMinutesRunning -value (New-TimeSpan $_.StartTime).TotalMinutes -PassThru }
}
}
$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 -ave | % { [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