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.plThis would run every day at 10:35am. And permissions...
chmod 755 diskspace.plCode:
- #!/usr/bin/perl -w
- # Script to check free diskspace and email notifications. Change the email and alert levels and you should be good to go.
- # created by lb
- use strict;
-
- # Alert levels Warning and Critical - Below what percent level of free disk space do you want an alert?
- my $alert1 = 30; #Warning level free space below 30%
- my $alert2 = 10; #Critical level free space below 10%
-
- # Put the email address to notify here
- my $email = 'you@foo.com';
-
- my ($size,$used,$avail,$use,$mounted);
- my $message;
- my @list;
- my $sysname = `/bin/uname -n`;
- chomp $sysname;
- my @df = `/bin/df`;
- my $df;
- foreach $df (@df) {
- if ($df =~ /\/\n/) {
- @list = split(/\s+/, $df);
- }
- else {next;}
- }
-
- # Check the usage
- my $diskfree = (($list[3]) / ($list[2]+$list[3])) * 100.00;
-
- # Round the number off to 2 decimals
- $diskfree = sprintf("%.2f", $diskfree);
-
- # See if free disk space is below any of our levels
- if ( ($diskfree < $alert1) && ($diskfree > $alert2) ) {
- $message = "Warning Diskspace threshold reached...free space below $alert1% at $diskfree%\n";
- &mailer;
- }
-
- elsif ( ($diskfree < $alert1) && ($diskfree < $alert2) ) {
- $message = "Critical Diskspace threshold reached...free space below $alert2% at $diskfree%\n";
- &mailer;
- }
-
- else {
- $message = "Free diskspace is good at $diskfree%\n";
- }
-
- #Output to terminal (comment out if you wish)
- print $message;
- print "~" x 75, "\n@df","~" x 75,"\n","From system: $sysname\n";
-
- #Subroutine for Mail, notifies on warning and critical levels.
- sub mailer {
- open(MAIL, "|/usr/sbin/sendmail -t") or die "Cannot open sendmail!: $!";
- print MAIL "To: $email\n";
- print MAIL "From: $sysname\n";
- print MAIL "Subject: $message\n\n";
- print MAIL "$message";
- print MAIL "~" x 75, "\n@df","~" x 75,"\n","From system: $sysname";
- close(MAIL);
- }
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