User Tools

Site Tools


email_alerts_on_low_free_space

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
email_alerts_on_low_free_space [2009/08/17 14:33] – created tkbletscemail_alerts_on_low_free_space [2017/05/28 15:34] (current) tkbletsc
Line 1: Line 1:
 ====== Email alerts on low free space ====== ====== Email alerts on low free space ======
  
-Create ''/root/free-space-alerts.pl'': +Create ''/root/free-space-alerts.py'': 
-<file+<code=python
-#!/usr/bin/perl+#!/usr/bin/python 
 +  
 +import sys,os,socket,subprocess
  
-# Based on+def get_free_GB(path)
-#   http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html+ st = os.statvfs(dir) 
 + avail = st.f_bavail * st.f_bsize (1024.0**3) # free space in GB 
 + total = st.f_blocks * st.f_bsize / (1024.0**3) # free space in GB 
 + used = total - avail 
 + return (avail,used,total)
  
-use strict; +def send_email(to,subject,message): 
-use warnings; +  fp_mail = subprocess.Popen(['/usr/bin/mail',to,'-s',subject], stdin=subprocess.PIPE).stdin 
-use Filesys::DiskSpace;+ fp_mail.write(message) 
 + fp_mail.close()
  
-if (@ARGV != 3) { 
-        die "Syntax: $0 <email_address> <path> <min_space_GB>\n"; 
-} 
  
-my ($to,$dir,$warning_level) = @ARGV;+if len(sys.argv) != 4: 
 + print "Syntax: %s <email_address> <path> <min_space_GB>" % sys.argv[0] 
 + sys.exit(1) 
 +  
 +(_,to,dir,min_space_GB) = sys.argv 
 +min_space_GB = float(min_space_GB)
  
-my $hostname = `hostname` || 'localhost'; +hostname = socket.gethostname()
-chomp $hostname;+
  
-# email setup +(avail,used,total) get_free_GB(dir)
-my $subject "$hostname: Low disk space on $dir";+
  
-# get df +pct = avail/total*100 
-my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir; +  
-$used /= 1024*1024; # to GB +if avail < min_space_GB: 
-$avail /= 1024*1024; # to GB + subject "%s: Low disk space on %s" % (hostname, dir) 
-my $total $used + $avail; + message """WARNING from '%s'.
-my $pct $avail/$total*100;+
  
-# compare +Low disk space on $dir: %.1fGB / %.1fGB free (%.1f%%).  Threshold is %.1fGB. 
-if ($avail < $warning_level) { +""% (hostname, avail, total, pct, min_space_GB) 
-        open MAIL, "| /usr/bin/mail $to -s '$subject'" or die "mail: $!\n"; + send_email(to,subject,message) 
-        printf MAIL "WARNING from '$hostname'.\n\nLow disk space on $dir: %.1fGB / %.1fGB free (%.1f%%)\n",$avail,$total,$pct; +</code>
-        close MAIL; +
-} +
-</file>+
  
-Then create ''/etc/cron.hourly/free-space/alerts'' with one line per mountpoint to monitor:+Then create ''/etc/cron.hourly/free-space-alerts'' with one line per mountpoint to monitor:
 <file> <file>
-/root/free-space-alerts.pl <YOUR_EMAIL_ADDRESS> <DIRECTORY> <MINIMUM_SPACE_IN_GIGABYTES>+/root/free-space-alerts.py <YOUR_EMAIL_ADDRESS> <DIRECTORY> <MINIMUM_SPACE_IN_GIGABYTES>
 ... ...
 </file> </file>
 +
 +
email_alerts_on_low_free_space.1250544805.txt.gz · Last modified: 2009/08/17 14:33 by tkbletsc

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki