scripthat.com

Scripting and Programming Forums
It is currently Fri Sep 03, 2010 1:23 pm


All times are UTC - 7 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Perl script to monitor free disk space and send email alert
PostPosted: Sun Jan 04, 2009 4:21 pm 
Offline
Site Admin
Site Admin
User avatar

Joined: Sun Dec 21, 2008 5:36 pm
Posts: 80
Location: USA, Colorado
I was looking around for some scripts to see if other people had made a diskspace check script, there were a few but they all seem to depend on modules that don't install too well. I wanted one that didn't require any modules since a few of the systems I work on are a pain to get modules on and it seems like this is simple enough to do without requiring them.

So I made this script to take care of that problem. It's based off of the df command and grabs the results, does some math and makes a decision to send an email or not based on the threshold levels set. There are 2 notification levels, warning and critical that will send emails. If the free disk space is higher than those 2 levels, it will not send out an email. Remember to set permissions and a cronjob for this script to run like so:
35 10 * * * /root/scripts/diskspace.pl
This would run every day at 10:35am. And permissions...
chmod 755 diskspace.pl

Code:
  1. #!/usr/bin/perl -w
  2. # Script to check free diskspace and email notifications.  Change the email and alert levels and you should be good to go.
  3. #                                                               created by lb
  4. use strict;
  5.  
  6. # Alert levels Warning and Critical - Below what percent level of free disk space do you want an alert?
  7. my $alert1 = 30;        #Warning level free space below 30%
  8. my $alert2 = 10;        #Critical level free space below 10%
  9.  
  10. # Put the email address to notify here
  11. my $email = 'you@foo.com';
  12.  
  13. my ($size,$used,$avail,$use,$mounted);
  14. my $message;
  15. my @list;
  16. my $sysname = `/bin/uname -n`;
  17. chomp $sysname;
  18. my @df = `/bin/df`;
  19. my $df;
  20. foreach $df (@df) {
  21.         if ($df =~ /\/\n/) {
  22.         @list = split(/\s+/, $df);
  23.         }
  24.         else {next;}
  25. }
  26.  
  27. # Check the usage
  28. my $diskfree = (($list[3]) / ($list[2]+$list[3])) * 100.00;
  29.  
  30. # Round the number off to 2 decimals
  31. $diskfree = sprintf("%.2f", $diskfree);
  32.  
  33. # See if free disk space is below any of our levels
  34. if ( ($diskfree < $alert1) && ($diskfree > $alert2) ) {
  35.         $message = "Warning Diskspace threshold reached...free space below $alert1% at $diskfree%\n";
  36.         &mailer;
  37.         }
  38.  
  39. elsif ( ($diskfree < $alert1) && ($diskfree < $alert2) ) {
  40.         $message = "Critical Diskspace threshold reached...free space below $alert2% at $diskfree%\n";
  41.         &mailer;
  42.         }
  43.  
  44. else {
  45.         $message = "Free diskspace is good at $diskfree%\n";
  46.         }
  47.  
  48. #Output to terminal (comment out if you wish)
  49. print $message;
  50. print "~" x 75, "\n@df","~" x 75,"\n","From system: $sysname\n";
  51.  
  52. #Subroutine for Mail, notifies on warning and critical levels.
  53. sub mailer {
  54.          open(MAIL, "|/usr/sbin/sendmail -t") or die "Cannot open sendmail!: $!";
  55.          print MAIL "To: $email\n";
  56.          print MAIL "From: $sysname\n";
  57.          print MAIL "Subject: $message\n\n";
  58.          print MAIL "$message";
  59.          print MAIL "~" x 75, "\n@df","~" x 75,"\n","From system: $sysname";
  60.          close(MAIL);
  61. }



If all is well the commandline response will look like the following:

[root@rain]# perl diskspace.pl
Free diskspace is good at 97.76%
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
305410304 6505676 283390620 3% /
/dev/sda1 194442 19731 164672 11% /boot
tmpfs 900456 48 900408 1% /dev/shm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From system: rain


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron




Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group