#!/usr/bin/env python ##################################################################### # $Id$ # # Stefano Rivera's DiskFull Warning script # Version 0.0.1 # Copyright (C) 2003, 2004 Stefano Rivera # # 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; version 2 of the # License. # # 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., 59 Temple Place, Suite 330, Boston, # MA 02111-1307 USA ##################################################################### import email.MIMEText import optparse import os import re import smtplib import socket import sys # Version __version__ = "0.0.1" ##################################################################### def main(): parseOptions() df = os.popen("df -P") mounts = [] dfRE = re.compile("^.*\s+\d+\s+\d+\s+\d+\s+(\d+)%\s+(.*)$") for line in df.readlines(): m = dfRE.match(line) if m != None: mpoint = m.group(2) mpercent = int(m.group(1)) if options.verbose: print "Found that %s is %d%% full." % (mpoint, mpercent) if mpoint in options.overrides: wpercent = options.overrides[mpoint] else: wpercent = options.percentage if mpercent >= wpercent: warn(mpoint, mpercent, wpercent) ##################################################################### def warn(mpoint, full, warnlevel): "Warn the user" sys.stderr.write("Filesystem %s is %d%% full!\n" % (mpoint, full)) hostname = socket.gethostname() fromAddr = "root@" + hostname toAddr = options.mail msg = email.MIMEText.MIMEText("""This is an automatically generated mail message from df-warn running on %s Filesystem %s is %d%% full! I was told to tell you when it reached %d%% full. Faithfully yours, etc. """ % (hostname, mpoint, full, warnlevel)) msg['Subject'] = "%s is %d%% full on %s" % (mpoint, full, hostname) msg['From'] = "df-warn monitoring <%s>" % fromAddr msg['To'] = toAddr if options.verbose: print "Sending message: " + msg.as_string() s = smtplib.SMTP() s.connect() s.sendmail(fromAddr, toAddr, msg.as_string()) s.close() ##################################################################### def parseOptions(): "Parse the command line options" global options usage = """%prog [OPTION]... [MOUNT=PERCENTAGE]... Warn root when a mount point reaches 80% capacity. Mount points can be individually overriden with the MOUNT=PERCENTAGE options i.e. /tmp=50 0 ignores a mountpoint""" version = "%prog " + __version__ parser = optparse.OptionParser(usage=usage, version=version) parser.add_option("-v", "--verbose", dest = "verbose", action = "store_true", default = False, help = "Be verbose") parser.add_option("-m", "--mail", dest = "mail", metavar = "ADDRESS", default = "root", help = "The address to email when there is a warning") parser.add_option("-p", "--percentage", dest = "percentage", metavar = "DEFAULT_PERCENTAGE", default = 80, type = "int", help = "Change the default warn percentage from 80%") (options, args) = parser.parse_args() options.overrides = {} overrideRE=re.compile("^(.*)=(\d\d?)$") for opt in args: m = overrideRE.match(opt) if m != False: options.overrides[m.group(1)] = int(m.group(2)) if options.verbose: print "Options: " + str(options) ##################################################################### ##################################################################### ##################################################################### if __name__ == '__main__': main()