public
Created

  • Download Gist
check_smart_attr.py
Python
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
#!/usr/bin/python
# -*- coding: utf-8-*-
####################################################################
# FILENAME: check_smart_attr.py
# PROJECT: Misc Scripts
# DESCRIPTION: Nagios health check to monitor a SMART attribute
# and alert against it.
#
#
# $Id$
####################################################################
# (C)2011 DigiTar, All Rights Reserved
####################################################################
 
import subprocess, sys, re
from argparse import ArgumentParser
 
EXIT_OK = 0
EXIT_WARNING = 1
EXIT_CRITICAL = 2
EXIT_UNKNOWN = 3
 
 
 
re_id = re.compile(r"^(?P<id>\d+)\s+(?P<attr_name>[-A-Za-z_0-9]+)\s+(?P<flag>0x\d+)\s+(?P<value>\d+)\s+(?P<worst>\d+)\s+(?P<thresh>\d+)\s+(?P<type>[-A-Za-z_]+)\s+(?P<updated>[A-Za-z]+)\s+(?P<when_failed>[^\s]+)\s+(?P<raw_value>[^\s]+)$")
 
parser = ArgumentParser()
parser.add_argument("--disk", dest="disk", type=str, required=True)
parser.add_argument("--disk_type", dest="disk_type", type=str, default="ata")
parser.add_argument("--smartctlpath", dest="spath", default="/usr/sbin/smartctl")
parser.add_argument("--attrid", dest="attribute_id", type=int, required=True)
parser.add_argument("--critical", dest="critical_threshold", type=int, required=True)
parser.add_argument("--warning", dest="warning_threshold", type=int, required=True)
parser.add_argument("--descending", dest="descending_values", action="store_const", const=True, default=False)
 
 
if __name__ == "__main__":
args = parser.parse_args()
try:
output = subprocess.Popen([args.spath,
"-d",
args.disk_type,
"-A",
args.disk],
stdout=subprocess.PIPE).communicate()[0]
except subprocess.CalledProcessError, e:
print "Error running check: " + e.output
sys.exit(EXIT_UNKNOWN)
for line in output.split("\n"):
match = re_id.match(line)
if match:
parsed_match = match.groupdict()
if int(parsed_match["id"]) == args.attribute_id:
value = int(parsed_match["value"])
if args.descending_values:
if value < args.critical_threshold:
print "CRITICAL: %s - %s value is %d. Threshold: %d" % (args.disk,
parsed_match["attr_name"],
value,
args.critical_threshold)
sys.exit(EXIT_CRITICAL)
elif value < args.warning_threshold:
print "WARNING: %s - %s value is %d. Threshold: %d" % (args.disk,
parsed_match["attr_name"],
value,
args.warning_threshold)
sys.exit(EXIT_WARNING)
else:
print "OK: %s - %s value is %d" % (args.disk,
parsed_match["attr_name"],
value)
sys.exit(EXIT_OK)
else:
if value > args.critical_threshold:
print "CRITICAL: %s - %s value is %d. Threshold: %d" % (args.disk,
parsed_match["attr_name"],
value,
args.critical_threshold)
sys.exit(EXIT_CRITICAL)
elif value > args.warning_threshold:
print "WARNING: %s - %s value is %d. Threshold: %d" % (args.disk,
parsed_match["attr_name"],
value,
args.warning_threshold)
sys.exit(EXIT_WARNING)
else:
print "OK: %s - %s value is %d" % (args.disk,
parsed_match["attr_name"],
value)
sys.exit(EXIT_OK)
# No matching ID found kemosabe...kick out
print "UNKNOWN: Could not find SMART ID %d in device %s output." % (args.attribute_id, args.disk)
sys.exit(EXIT_UNKNOWN)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.