-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_smartmon.py
414 lines (371 loc) · 14 KB
/
check_smartmon.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/usr/bin/python
"""Nagios plugin for monitoring S.M.A.R.T. status."""
# -*- coding: iso8859-1 -*-
#
# $Id: version.py 133 2006-03-24 10:30:20Z fuller $
#
# check_smartmon
# Copyright (C) 2006 daemogorgon.net
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#
# Fork author: nihlaeth
#
import os.path
import sys
import re
import psutil
import subprocess
from optparse import OptionParser
__author__ = "fuller <[email protected]>"
__version__ = "$Revision$"
# path to smartctl
# TODO use which to fetch path
_smartctl_path = "/usr/sbin/smartctl"
# application wide verbosity (can be adjusted with -v [0-3])
_verbosity = 0
def parse_cmd_line(arguments):
"""Commandline parsing."""
usage = "usage: %prog [options] device"
version = "%%prog %s" % (__version__)
parser = OptionParser(usage=usage, version=version)
parser.add_option(
"-d",
"--device",
action="store",
dest="device",
default="",
metavar="DEVICE",
help="device to check")
parser.add_option(
"-a",
"--all-disks",
action="store_true",
dest="alldisks",
default="",
help="Check all disks")
parser.add_option(
"-v",
"--verbosity",
action="store",
dest="verbosity",
type="int",
default=0,
metavar="LEVEL",
help="set verbosity level to LEVEL; defaults to 0 (quiet), \
possible values go up to 3")
parser.add_option(
"-w",
"--warning-threshold",
metavar="TEMP",
action="store",
type="int",
dest="warning_temp",
default=55,
help=("set temperature warning threshold to given temperature"
" (default:55)"))
parser.add_option(
"-c",
"--critical-threshold",
metavar="TEMP",
action="store",
type="int",
dest="critical_temp",
default="60",
help=("set temperature critical threshold to given temperature"
" (default:60)"))
return parser.parse_args(arguments)
def check_device_permissions(path):
"""Check if device exists and permissions are ok.
Returns:
- 0 ok
- 1 no such device
- 2 no read permission given
"""
vprint(3, "Check if %s does exist and can be read" % path)
if not os.access(path, os.F_OK):
return (3, "UNKNOWN: no such device found")
elif not os.access(path, os.R_OK):
return (3, "UNKNOWN: no read permission given")
else:
return (0, "")
return (0, "")
def check_smartmontools(path):
"""Check if smartctl is available and can be executed.
Returns:
- 0 ok
- 1 no such file
- 2 cannot execute file
"""
vprint(3, "Check if %s does exist and can be read" % path)
if not os.access(path, os.F_OK):
print "UNKNOWN: cannot find %s" % path
sys.exit(3)
elif not os.access(path, os.X_OK):
print "UNKNOWN: cannot execute %s" % path
sys.exit(3)
def call_smartmontools(path, device):
"""Get smartmontool output."""
cmd = "%s -a %s" % (path, device)
vprint(3, "Get device health status: %s" % cmd)
result = ""
message = ""
code_to_return = 0
try:
result = subprocess.check_output(cmd, shell=True)
except subprocess.CalledProcessError as error:
# smartctl passes a lot of information via the return code
return_code = error.returncode
if return_code % 2**1 > 0:
# bit 0 is set - command line did not parse
# output is not useful now, simply return
message += "UNKNOWN: smartctl parsing error "
return_code -= 2**0
code_to_return = 3
if return_code % 2**2 > 0:
# bit 1 is set - device open failed
# output is not useful now, simply return
message += "UNKNOWN: could not open device "
return_code -= 2**1
code_to_return = 3
if return_code % 2**3 > 0:
# bit 2 is set - some smart or ata command failed
# we still want to see what the output says
result = error.output
message += "CRITICAL: some SMART or ATA command to disk "
message += "failed "
return_code -= 2**2
code_to_return = 2
if return_code % 2**4 > 0:
# bit 3 is set - smart status returned DISK FAILING
# we still want to see what the output says
result = error.output
message += "CRITICAL: SMART statis is DISK FAILING "
return_code -= 2**3
code_to_return = 2
if return_code % 2**5 > 0:
# bit 4 is set - prefail attributes found
result = error.output
message += "CRITICAL: prefail attributes found "
return_code -= 2**4
code_to_return = 2
if return_code % 2**6 > 0:
# bit 5 is set - disk ok, but prefail attributes in the past
result = error.output
# this should be a warning, but that's too much hasle
message += "WARNING: some prefail attributes were critical "
message += "in the past "
return_code -= 2**5
code_to_return = 1
if return_code % 2**7 > 0:
# bit 6 is set - errors recorded in error log
result = error.output
message += "WARNING: errors recorded in error log "
return_code -= 2**6
code_to_return = 1
if return_code % 2**8 > 0:
# bit 7 is set - device self-test log contains errors
result = error.output
message += "CRITICAL: self-test log contains errors "
return_code -= 2**7
code_to_return = 2
except OSError as error:
code_to_return = 3
message = "UNKNOWN: call exits unexpectedly (%s)" % error
return (code_to_return, result, message)
def parse_output(output, warning_temp, critical_temp):
"""
Parse smartctl output.
Returns status of device.
"""
# parse health status
#
# look for line '=== START OF READ SMART DATA SECTION ==='
status_line = ""
health_status = ""
reallocated_sector_ct = 0
temperature = 0
reallocated_event_count = 0
current_pending_sector = 0
offline_uncorrectable = 0
error_count = 0
lines = output.split("\n")
for line in lines:
# extract status line
if "overall-health self-assessment test result" in line:
status_line = line
parts = status_line.rstrip().split()
health_status = parts[-1:][0]
vprint(3, "Health status: %s" % health_status)
# extract status line (compatibility with all smartctl versions)
if "Health Status" in line:
status_line = line
parts = status_line.rstrip().split()
health_status = parts[-1:][0]
vprint(3, "Health status: %s" % health_status)
parts = line.split()
if len(parts) > 0:
# self test spans can also start with 5, so we
# need a tighter check here than elsewhere
if parts[0] == "5" and \
parts[1] == "Reallocated_Sector_Ct" and \
reallocated_sector_ct == 0:
# extract reallocated_sector_ct
# 5 is the reallocated_sector_ct id
reallocated_sector_ct = int(parts[9])
vprint(3, "Reallocated_Sector_Ct: %d" % reallocated_sector_ct)
elif parts[0] == "190" and temperature == 0:
# extract temperature
# 190 can be temperature value id too
temperature = int(parts[9])
vprint(3, "Temperature: %d" % temperature)
elif parts[0] == "194" and temperature == 0:
# extract temperature
# 194 is the temperature value id
temperature = int(parts[9])
vprint(3, "Temperature: %d" % temperature)
elif parts[0] == "196" and reallocated_event_count == 0:
# extract reallocated_event_count
# 196 is the reallocated_event_count id
reallocated_event_count = int(parts[9])
vprint(
3,
"Reallocated_Event_Count: %d" % reallocated_event_count)
elif parts[0] == "197" and current_pending_sector == 0:
# extract current_pending_sector
# 197 is the current_pending_sector id
current_pending_sector = int(parts[9])
vprint(
3,
"Current_Pending_Sector: %d" % current_pending_sector)
elif parts[0] == "198" and offline_uncorrectable == 0:
# extract offline_uncorrectable
# 198 is the offline_uncorrectable id
offline_uncorrectable = int(parts[9])
vprint(
3,
"Offline_Uncorrectable: %d" % offline_uncorrectable)
elif "ATA Error Count" in line:
error_count = int(parts[3])
vprint(
3,
"ATA error count: %d" % error_count)
elif "No Errors Logged" in line:
error_count = 0
vprint(
3,
"ATA error count: 0")
# now create the return information for this device
return_status = 0
device_status = ""
# check if smartmon could read device
if health_status == "":
return (3, "UNKNOWN: could not parse output")
# check health status
while health_status not in ["PASSED", "OK"]:
return_status = 2
device_status += "CRITICAL: device does not pass health status "
# check sectors
if reallocated_sector_ct > 0 or \
reallocated_event_count > 0 or \
current_pending_sector > 0 or \
offline_uncorrectable > 0:
return_status = 2
device_status += "CRITICAL: there is a problem with bad sectors "
device_status += "on the drive. "
device_status += "Reallocated_Sector_Ct:%d, " % reallocated_sector_ct
device_status += "Reallocated_Event_Count:%d, " % reallocated_event_count
device_status += "Current_Pending_Sector:%d, " % current_pending_sector
device_status += "Offline_Uncorrectable:%d " % offline_uncorrectable
# check temperature
if temperature > critical_temp:
return_status = 2
device_status += "CRITICAL: device temperature (%d)" % temperature
device_status += "exceeds critical temperature "
device_status += "threshold (%s) " % critical_temp
elif temperature > warning_temp:
# don't downgrade return status!
if return_status < 2:
return_status = 1
device_status += "WARNING: device temperature (%d) " % temperature
device_status += "exceeds warning temperature "
device_status += "threshold (%s) " % warning_temp
# check error count
if error_count > 0:
if return_status < 2:
return_status = 1
device_status += "WARNING: error count %d " % error_count
if return_status == 0:
# no warnings or errors, report everything is ok
device_status = "OK: device is functional and stable "
device_status += "(temperature: %d) " % temperature
return (return_status, device_status)
def vprint(level, text):
"""Verbosity print.
Decide according to the given verbosity level if the message will be
printed to stdout.
"""
if level <= verbosity:
print text
if __name__ == "__main__":
# pylint: disable=invalid-name
(options, args) = parse_cmd_line(sys.argv)
verbosity = options.verbosity
check_smartmontools(_smartctl_path)
vprint(2, "Get device name")
# assemble device list to be monitored
if not options.alldisks:
devices = [options.device]
else:
devices = []
# Regex for Valid device name
valid_device_name = '/dev/[ahsv]d.*'
for partition in psutil.disk_partitions():
if re.search(valid_device_name, partition.device):
devices.append(partition.device.strip(partition.device[-1]))
vprint(1, "Devices: %s" % devices)
return_text = ""
exit_status = 0
for device in devices:
vprint(1, "Device: %s" % device)
return_text += "%s: " % device
# check if we can access 'path'
vprint(2, "Check device")
(return_status, message) = check_device_permissions(device)
if return_status != 0:
if exit_status < return_status:
exit_status = return_status
return_text += message
# call smartctl and parse output
vprint(2, "Call smartctl")
return_status, output, message = call_smartmontools(
_smartctl_path,
device)
if return_status != 0:
if exit_status < return_status:
exit_status = return_status
return_text += message
if output != "":
vprint(2, "Parse smartctl output")
return_status, device_status = parse_output(
output,
options.warning_temp,
options.critical_temp)
if exit_status < return_status:
exit_status = return_status
return_text += device_status
print return_text
sys.exit(exit_status)