-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_process.ps1
123 lines (99 loc) · 4.62 KB
/
check_process.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<#
.DESCRIPTION
A PowerShell based plugin for Nagios and Nagios-like systems. This plugin checks the process information of a specified process on Windows machines. This plugin can monitor the total number of a specified process running, the total memory of all instances of said process, and the total CPU usage of said process.
Remember, thresholds must be breached before they are thrown.
E.g. numwarning 10 will need the number of files to be 11 or higher to throw a WARNING.
.SYNOPSIS
A PowerShell based plugin to the process information of a specified process on Windows machines
.NOTES
This plugin will return performance data.
.PARAMETER processname
This is the name of the process. E.g. iexplore, not iexplore.exe
.PARAMETER metric
The metric you want to alert on. I.e. Count, Memory, CPU
.PARAMETER outputType
outputType mainly affects the Memory metric. You can specify the output in KB, MB, or GB
.PARAMETER warning
The number you will tolerate before throwing a WARNING, be that for Count, Memory, or CPU
.PARAMETER critical
The number you will tolerate before throwing a CRITICAL, be that for Count, Memory, or CPU
.EXAMPLE
PS> .\check_process.ps1 -processname iexplore
.EXAMPLE
PS> .\check_process.ps1 -processname iexplore -metric Memory -outputType MB -warning 400 -critical 500 (basically throw a critical if iexplore is running, lolololol)
#>
param(
[Parameter(Mandatory=$true)][string]$processname = $null,
[Parameter(Mandatory=$false)][ValidateSet('Count', 'Memory', 'CPU')][string]$metric = 'Count',
[Parameter(Mandatory=$false)][ValidateSet('KB', 'MB', 'GB')][string]$outputType = 'MB',
[Parameter(Mandatory=$false)][int]$warning = $null,
[Parameter(Mandatory=$false)][int]$critical = $null
)
$message = "Nothing changed the status output!"
$exitcode = 3
function processCheck {
param (
[Parameter(Mandatory=$true)][int]$checkResult,
[Parameter(Mandatory=$true)][int]$warningThresh,
[Parameter(Mandatory=$true)][int]$criticalThresh,
[Parameter(Mandatory=$false)][string]$returnMessage
)
[array]$returnArray
if ((!$criticalThresh) -and (!$warningThresh) ) {
$returnArray = @(0, "OK: $returnMessage")
}
elseif ($checkResult -gt $criticalThresh) {
$returnArray = @(2, "CRITICAL: $returnMessage")
}
elseif ($checkResult -le $criticalThresh -and $checkResult -gt $warningThresh) {
$returnArray = @(1, "WARNING: $returnMessage")
}
else {
$returnArray = @(0, "OK: $returnMessage")
}
return $returnArray
}
$procinfo = Get-Process -Name $processname | select -Property WS,CPU,ProcessName
#Insert error handling here if the process is not running
$message = ''
$processArray = @()
switch ($metric) {
'Memory' {
foreach ($mem in $procinfo.WS) { $memtotal = $memtotal + $mem }
switch ($outputType) {
'KB' {
$memtotal = [math]::Round($memtotal / 1024,2)
}
'MB' {
$memtotal = [math]::Round($memtotal / 1024 / 1024,2)
}
'GB' {
$memtotal = [math]::Round($memtotal / 1024 /1024 /1024,2)
}
}
$message = "Process $processname memory utilization is $memtotal"
$processArray = processCheck -checkResult $memtotal `
-warningThresh $warning `
-criticalThresh $critical `
-returnMessage "Process $processname memory usage is $memtotal | '$processname memory'=$memtotal;$warning;$critical"
}
'CPU' {
$cputotal = [math]::Round(((get-counter "\Process($processname)\% Processor Time" -SampleInterval 1).CounterSamples).CookedValue,2)
$processArray = processCheck -checkResult $cputotal `
-warningThresh $warning `
-criticalThresh $critical `
-returnMessage "Process $processname CPU usage is $cputotal | '$processname CPU usage'=$cputotal;$warning;$critical"
}
'Count' {
$counttotal = @($procinfo).Count
$message = "Process $processname Count is {0}" -f $counttotal
$processArray = processCheck -checkResult @($procinfo).Count `
-warningThresh $warning `
-criticalThresh $critical `
-returnMessage "Process $processname count is $counttotal | '$processname count'=$counttotal;$warning;$critical"
}
}
$exitcode = $processArray[1]
$exitMessage = $processArray[2]
write-output $exitMessage
exit $exitcode