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

Both sides previous revisionPrevious revision
Next revision
Previous revision
email_alerts_on_low_free_space [2009/08/18 10:24] 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'': 
-<code=perl+<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; +
-        close MAIL; +
-}+
 </code> </code>
  
 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>
  
-===== Alternate version that doesn't use Filesys::DiskSpace ===== 
- 
-If you don't have that module, use this: 
-<code=perl> 
-#!/usr/bin/perl 
- 
-# Based on: 
-#   http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html 
- 
-use strict; 
-use warnings; 
-#use Filesys::DiskSpace; 
- 
-if (@ARGV != 3) { 
-    die "Syntax: $0 <email_address> <path> <min_space_GB>\n"; 
-} 
- 
-my ($to,$dir,$warning_level) = @ARGV; 
- 
-my $hostname = `hostname` || 'localhost'; 
-chomp $hostname; 
- 
-# email setup 
-my $subject = "$hostname: Low disk space on $dir"; 
- 
-# get df 
-my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = my_df($dir); 
-$used /= 1024*1024; # to GB 
-$avail /= 1024*1024; # to GB 
-my $total = $used + $avail; 
-my $pct = $avail/$total*100; 
- 
-# compare 
-if ($avail < $warning_level) { 
-    open MAIL, "| mail $to -s '$subject'" or die "mail: $!\n"; 
-    printf MAIL "WARNING from '$hostname'.\n\nLow disk space on $dir: %.1fGB / %.1fGB free (%.1f%%)\n",$avail,$total,$pct; 
-    close MAIL; 
-} 
- 
- 
-sub my_df { 
-        my ($p) = @_; 
-        #Filesystem         1024-blocks      Used Available Capacity Mounted on 
-        open DF, "df -P $p |" or die "df: $!\n"; 
-        scalar <DF>; # eat header 
-        my $line = <DF>; 
-        close DF; 
-        my ($dev,$total,$used,$avail,$pct,$mountpoint) = split(/\s+/,$line); 
-        return (0, '', $used, $avail, 0, 0); # (mostly) compatible with Filesys::DiskSpace 
- 
-} 
-</code> 
  
email_alerts_on_low_free_space.txt · Last modified: 2017/05/28 15:34 by tkbletsc

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki