-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_volume.ps1
115 lines (95 loc) · 4.22 KB
/
check_volume.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
<#
.DESCRIPTION
A PowerShell based plugin for Nagios and Nagios-like systems. This plugin checks various metrics related to volumes on Windows systems. Typically this is the plugin people are looking for when they want to know the space usage of a specific drive letter in Windows. The rest of the world calls them logical volumes, but even in Windows, they are different from physical disks.
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 various metrics related to volumes on Windows systems.
.NOTES
This plugin checks various metrics related to volumes on Windows systems. Typically this is the plugin people are looking for when they want to know the space usage of a specific drive letter in Windows.
.PARAMETER volumename
Default is C:\. Important to note that currently this does not work on NTFS mount points...yet.
.PARAMETER outputType
MB, GB, TB, PCT. How do you want the output reported? Megabytes, Gigabytes, Terabytes, Percent.
.PARAMETER metric
Used or Available. Do you want to see the space used or the space available?
.PARAMETER warning
The amount of reported space before you throw a WARNING.
.PARAMETER critical
The amount of reported space before you throw a CRITICAL.
.EXAMPLE
PS> .\check_volume.ps1 -volumename C:\ -outputType GB -metric Used
.EXAMPLE
PS> .\check_volume.ps1 -volumename D:\ -outputType GB -metric Available -warning 40 -critical 20
#>
param(
[Parameter(Mandatory=$false)][string]$volumename = 'C:\',
[Parameter(Mandatory=$false)][ValidateSet('MB', 'GB', 'TB', 'PCT')][string]$outputType = 'PCT',
[parameter(Mandatory=$false)][ValidateSet('Used', 'Available')][string]$metric = 'Used',
[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
}
if (!$volumename.EndsWith('\')) {
$volumename = $volumename + "\"
}
$volumeresult = 0
$volume = (Get-WmiObject -Class Win32_Volume | where -Property DriveType -in -value 3,6 | where -Property Name -eq -value "$volumename")
if ($metric -eq "Used") {
$volumeresult = $volume.Capacity - $volume.FreeSpace
}
else {
$volumeresult = $volume.FreeSpace
}
switch ($outputType) {
'MB' {
$volumeresult = [math]::Round($volumeresult / 1024 / 1024)
}
'GB' {
$volumeresult = [math]::Round($volumeresult / 1024 / 1024 / 1024)
}
'TB' {
$volumeresult = [math]::Round($volumeresult / 1024 / 1024 / 1024 / 1024)
}
'PCT'{
$volumeresult = [math]::Round(($volume.FreeSpace / $volume.Capacity) * 100, 2)
}
}
if ($volumename.Length -le 3) {
$volumename = $volumename.Substring(0,1)
}
else {
$volumename = $volumename.Substring(3, ($volumename.Length - 3))
$volumename = $volumename.Substring(0, ($volumename.Length - 1))
}
$processArray = processCheck -checkResult $volumeresult `
-warningThresh $warning `
-criticalThresh $critical `
-returnMessage "$volumename $metric space is $volumeresult $outputType | '$volumename $metric Space'=$volumeresult$outputType;$warning;$critical"
$exitcode = $processArray[1]
$exitMessage = $processArray[2]
write-output $exitMessage
exit $exitcode