#!/usr/bin/python # Small script which takes output of "df -h", checks to see if the # disk % used is above a threshhold, and if so, mails someone to # alert them to a possible problem coming when the disk fills up. # # Usage: df -h | disk_space_check # # Expected to be run regularly from a cron job. # # Note that we COULD have done this completely from the command line, # but our list host didn't provide anything like /bin/mail (binmail) # for sending email from the command line. So this script exists # really just for connecting to SMTP server in a structured way, and # so we can make a better-looking alert email than just a single line. # # The command-line version would be something like: # # df -h | egrep simfs | perl -ne '($fs, $sz, $used, $avail, $usep) = split(); ($usenum = $usep) =~ s#\%##; if ($usenum > 80) { print $usenum, qq^\n^;}; ' # # Anthony R. Thompson, June 2010 # Contact: put @ between art and sigilservices.com # # Copyright (C) 1998-2010 by the Free Software Foundation, Inc. # # 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. http://www.fsf.org/licensing/licenses/gpl.html # import os, pwd, sys, re, smtplib, socket recip = 'First Last ' # INSTALL (comma-sep OK for multi) watchfs = '/' # filesystem of interest to monitor toofull = 90 # percentage which is "too full", i.e. notify admin thisuser = pwd.getpwuid(os.getuid())[0] thishost = socket.gethostname() sender = thisuser + '@' + thishost smtp_server = 'localhost' subj = "Warning: %s disk space usage too high" % (thishost) alldf = sys.stdin.readlines() # to mail all later fslines = alldf[1:] # ignore header line for line in fslines: (fstype, size, used, avail, usedp, mount) = re.split('\s+', line, 5) usedpnum = int(re.sub('%', '', usedp)) mount = mount.rstrip("\n") if ( (mount == watchfs) and (usedpnum >= toofull) ): smtp = smtplib.SMTP(smtp_server) subj += ' (' + str(usedpnum) + '%)' msgbody = \ """Warning! The disk space on the %s filesystem on %s is too high; it is above the threshhold of %s percent: """ % (watchfs, thishost, toofull) for line in alldf: msgbody += line msgbody += "\nThis message was automatically generated by: " + \ os.path.abspath(sys.argv[0]) msghead = 'From: ' + sender + "\n" msghead += 'To: ' + recip + "\n" msghead += 'Subject: ' + subj + "\n" message = msghead + "\n" + msgbody smtp.sendmail(sender, recip, message)