#! /usr/bin/env python # $Id: check_disk.py 8 2009-03-15 17:41:58Z $ """ NAME check_disk.py -- report whenever target disk space is below threshold. SYNOPSIS check_disk.py -p -w -c [ -h ] [ --log ] Options: --version show program's version number and exit -h, --help show this help message and exit -p TARGET_PATH, --path=TARGET_PATH Path to device to monitor -c CRIT_THRESHOLD, --critical=CRIT_THRESHOLD Critical free space threshold in percentage -w WARN_THRESHOLD, --warning=WARN_THRESHOLD Warning free space threshold in percentage --log Set logging on RETURN CODES 0 when monitored target is OK 1 when monitored target is over WARNING threshold 2 when monitored target is over CRITICAL threshold EXAMPLE $ ./check_disk.py -p / -c 80 -w 90 --log WARNING: / free space: 612930211840 $ echo $? 1 AUTHOR Petri Koistinen Adapted from scripts and ideas stolen all around the Internet. """ version = '$Revision: 8 $' from optparse import OptionParser import sys, os import logging import logging.handlers import statvfs # Only available in UNIX. logger = logging.getLogger('check_disk') logger.setLevel(logging.DEBUG) handler = logging.handlers.SysLogHandler('/dev/log') logger.addHandler(handler) # Parse commandline options: parser = OptionParser(usage="%prog -p -w -c [ -h ] [ --log ]", version="%prog " + version) parser.add_option("-p", "--path", action="store", type="string", dest="target_path", help="Path to device to monitor") parser.add_option("-c", "--critical", action="store", type="string", dest="crit_threshold", help="Critical free space threshold in percentage") parser.add_option("-w", "--warning", action="store", type="string", dest="warn_threshold", help="Warning free space threshold in percentage") parser.add_option("", "--log", action="store_true", dest="log", default=False, help="Set logging on") (options, args) = parser.parse_args() def free_space(path): """ freespace(path) -> integer Return the number of free bytes available to the user on the file system pointed to by path.""" s = os.statvfs(path) return s[statvfs.F_BAVAIL] * s[statvfs.F_BSIZE] def total_space(path): """ totalspace(path) -> integer Return the number of total bytes on the file system pointed to by path.""" s = os.statvfs(path) return s[statvfs.F_BLOCKS] * s[statvfs.F_FRSIZE] def log(message): """ log(message) Print message to screen. Write message also to syslog if --log command line option is specified.""" print message if options.log: logger.error(os.path.basename(sys.argv[0]) + ': ' + message) def check_space(): if not options.target_path: log("UNKNOWN: Missing monitoring target path.") sys.exit(1) if not options.crit_threshold: log("UNKNOWN: Missing critical threshold value.") sys.exit(1) if not options.warn_threshold: log("UNKNOWN: Missing warning threshold value.") sys.exit(1) if int(options.crit_threshold) >= int(options.warn_threshold): log("UNKNOWN: Critical percentage can't be equal to or bigger than warning percentage.") sys.exit(1) percent_free = float(free_space(options.target_path)) / float(total_space(options.target_path)) * 100 if percent_free < float(options.crit_threshold): log("CRITICAL: " + options.target_path + " free space: " + str(free_space(options.target_path))) sys.exit(2) if percent_free < float(options.warn_threshold): log("WARNING: " + options.target_path + " free space: " + str(free_space(options.target_path))) sys.exit(1) sys.exit(0) if __name__ == '__main__': check_space()