Created
December 15, 2011 20:36
-
-
Save williamsjj/1482762 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment