-
Notifications
You must be signed in to change notification settings - Fork 47
/
check_disks_by_ssh.py
executable file
·204 lines (162 loc) · 6.97 KB
/
check_disks_by_ssh.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python2
# Copyright (C) 2013-:
# Gabes Jean, [email protected]
# Pasche Sebastien, [email protected]
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
'''
This script is a check for lookup at disks consumption
'''
import os
import sys
# Ok try to load our directory to load the plugin utils.
my_dir = os.path.dirname(__file__)
sys.path.insert(0, my_dir)
try:
import schecks
except ImportError:
print "ERROR : this plugin needs the local schecks.py lib. Please install it"
sys.exit(2)
VERSION = "0.1"
DEFAULT_WARNING = '75%'
DEFAULT_CRITICAL = '90%'
MOUNTS = None
UNITS= {'B': 0,
'KB': 1,
'MB': 2,
'GB': 3,
'TB': 4
}
def convert_to(unit, value):
power = 0
if unit in UNITS:
power = UNITS[unit]
return round(float(value)/(1024**power), power)
def get_df(client):
# We are looking for a line like
#Filesystem Type 1K-blocks Used Available Use% Mounted on
#/dev/sda2 ext3 28834744 21802888 5567132 80% /
#udev devtmpfs 1021660 4 1021656 1% /dev
#tmpfs tmpfs 412972 1040 411932 1% /run
#none tmpfs 5120 4 5116 1% /run/lock
#none tmpfs 1032428 13916 1018512 2% /run/shm
#none tmpfs 102400 8 102392 1% /run/user
#/dev/sda5 fuseblk 251536380 184620432 66915948 74% /media/ntfs
#/dev/sdb1 ext3 961432072 833808328 78785744 92% /media/bigdata
# Beware of the export!
stdin, stdout, stderr = client.exec_command('export LC_LANG=C && unset LANG && df -l -T -k -P')
dfs = {}
for line in stdout:
line = line.strip()
# By pass the firt line, we already know about it
if not line or line.startswith('Filesystem'):
continue
# Only keep non void elements
tmp = [s for s in line.split(' ') if s]
_type = tmp[1]
# Ok maybe we got a none, iso9660 or devtmpfs system, if so, bailout
if _type in ['devtmpfs', 'iso9660']:
continue
# Exclude /dev/ /sys/ and /run/ folder
mounted = ' '.join(tmp[6:])
if mounted.startswith('/run/') or mounted.startswith('/sys/') or mounted.startswith('/dev/'):
continue
#if we specify a list of mountpoints to check then verify that current line is in the list
to_check = True
if MOUNTS:
to_check = False
for mnt in MOUNTS:
if tmp[6].startswith(mnt):
to_check = True
# Maybe this mount point did not match any required mount point
if not to_check:
continue
# Ok now grep values
fs = tmp[0]
size = int(tmp[2])*1024
used = int(tmp[3])*1024
avail = int(tmp[4])*1024
used_pct = int(tmp[5][:-1]) # we remove the %
dfs[mounted] = {'fs':fs, 'size':size, 'used':used, 'avail':avail, 'used_pct':used_pct}
# Before return, close the client
client.close()
return dfs
parser = schecks.get_parser()
## Specific options
parser.add_option('-w', '--warning',
dest="warning",
help='Warning value for physical used memory. In percent. Default : 75%')
parser.add_option('-c', '--critical',
dest="critical",
help='Critical value for physical used memory. In percent. Must be '
'superior to warning value. Default : 90%')
parser.add_option('-m', '--mount-points',
dest="mounts",
help='comma separated list of mountpoints to check. Default all mount '
'points except if mounted in /dev, /sys and /run')
parser.add_option('-U', '--unit',
dest="unit", help='Unit of Disk Space. B, KB, GB, TB. Default : B')
if __name__ == '__main__':
# Ok first job : parse args
opts, args = parser.parse_args()
if opts.mounts:
mounts = opts.mounts.split(',')
MOUNTS=mounts
# Try to get numeic warning/critical values
s_warning = opts.warning or DEFAULT_WARNING
s_critical = opts.critical or DEFAULT_CRITICAL
warning, critical = schecks.get_warn_crit(s_warning, s_critical)
# Get Unit
s_unit = opts.unit or 'B'
# Ok now got an object that link to our destination
client = schecks.get_client(opts)
## And get real data
dfs = get_df(client)
# Maybe we failed at getting data
if not dfs:
print "Error : cannot fetch disks values from host"
sys.exit(2)
perfdata = ''
status = 0 # all is green until it is no more ok :)
bad_volumes = []
for (mount, df) in dfs.iteritems():
size = convert_to(s_unit,df['size'])
used = convert_to(s_unit,df['used'])
used_pct = df['used_pct']
# Let first dump the perfdata
_size_warn = convert_to(s_unit,df['size'] * float(warning)/100)
_size_crit = convert_to(s_unit,df['size'] * float(critical)/100)
perfdata += '"%s_used_pct"=%s%%;%s%%;%s%%;0%%;100%% "%s_used"=%s%s;%s;%s;0;%s ' % (mount, used_pct, warning, critical, mount, used, s_unit, _size_warn, _size_crit, size)
# And compare to limits
if used_pct >= critical:
status = 2
bad_volumes.append( (mount, used_pct) )
if used_pct >= warning and status == 0:
status = 1
bad_volumes.append( (mount, used_pct) )
if status == 0:
print "Ok: all disks are in the limits | %s" % (perfdata)
sys.exit(0)
if status == 1:
print "Warning: some disks are not good : %s | %s" % (','.join( ["%s:%s%%" % (mount, used_pct) for (mount, used_pct) in bad_volumes]), perfdata)
sys.exit(1)
if status == 2:
print "Critical: some disks are not good : %s | %s" % (','.join( ["%s:%s%%" % (mount, used_pct) for (mount, used_pct) in bad_volumes]), perfdata)
sys.exit(2)