-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_memory.ps1
120 lines (100 loc) · 4.26 KB
/
check_memory.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
<#
.DESCRIPTION
A PowerShell based plugin for Nagios and Nagios-like systems. This plugin checks the memory utilization of Windows systems. You can report the Used or Available memory in mega, giga, and terabyte, as well as percent.
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
This plugin checks the memory utilization of Windows systems.
.NOTES
I plan on expanding this to include more information about the page file. I learned a lot about Windows memory management in making this plugin, and I intend to correct some out-of-date assumptions about things like the page file.
.PARAMETER outputType
MB, GB, TB, PCT. How do you want to see the output? MB = Megabytes, GB = Gigabytes, TB = Terabytes, PCT = Percent
.PARAMETER metric
Used or Available. Do you want to see how much memory you are using, or how much memory is available?
.EXAMPLE
PS> .\check_memory.ps1 -outputType MB -metric Used
.EXAMPLE
PS> .\check_memory.ps1 -outputType MB -metric Available -warning 1024 -critical 512
.EXAMPLE
PS> .\check_memory.ps1 -outputType GB -metric Used -warning 6 -critical 8
#>
param (
[Parameter(Mandatory=$false)][ValidateSet('MB', 'GB', 'TB', 'PCT')][string]$outputType = 'MB',
[Parameter(Mandatory=$false)][ValidateSet('Used', 'Available')][string]$metric = 'Used',
[Parameter(Mandatory=$false)][int]$warning = $null,
[Parameter(Mandatory=$false)][int]$critical = $null
)
$exitcode = 3
$exitMessage = 'Nothing changed the status output!'
$memoryresult = 0
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 ($metric -eq 'Used') {
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")
}
}
else {
if ((!$criticalThresh) -and (!$warningThresh) ) {
$returnArray = @(0, "OK: $returnMessage")
}
elseif ($checkResult -lt $criticalThresh) {
$returnArray = @(2, "CRITICAL: $returnMessage")
}
elseif ($checkResult -ge $criticalThresh -and $checkResult -lt $warningThresh) {
$returnArray = @(1, "WARNING: $returnMessage")
}
else {
$returnArray = @(0, "OK: $returnMessage")
}
}
return $returnArray
}
$totalmem = 0
foreach ($mem in (Get-CimInstance -ClassName CIM_PhysicalMemory | select Capacity).Capacity) {
$totalmem += $mem
}
#Total memory in MB
$totalmem = [int]($totalmem / 1024) / 1024
$memoryresult = (get-counter -counter '\Memory\Available MBytes' -computername localhost).countersamples.cookedvalue
#If we're checkin' used memory, gotta change $memoryresult to reflect the used memory.
if ($metric -eq 'Used') {
$memoryresult = $totalmem - $memoryresult
}
switch ($outputType) {
'MB' {
#Nothing goes here. MB is the default.
}
'GB' {
$memoryresult = [math]::Round(($memoryresult / 1024), 2)
}
'TB' {
$memoryresult = [math]::Round(($memoryresult / 1024 / 1024), 2)
}
'PCT'{
$memoryresult = [math]::Round(($memoryresult / $totalmem) * 100, 2)
}
}
$processArray = processCheck -checkResult $memoryresult `
-warningThresh $warning `
-criticalThresh $critical `
-returnMessage "$metric memory is $memoryresult$outputType | '$metric Memory'=$memoryresult$outputType;$warning;$critical"
$exitcode = $processArray[1]
$exitMessage = $processArray[2]
write-output $exitMessage
exit $exitcode