Created
May 13, 2013 14:14
-
-
Save peterjaap/5568606 to your computer and use it in GitHub Desktop.
diskspace.pl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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. | |
# Script PJ Blaakmeer - www.blaakmeer.com | |
use strict; | |
# Alert levels Warning and Critical - Below what percent level of free disk space do you want an alert? | |
my $alert1 = 10; #Warning level free space below 30% | |
my $alert2 = 5; #Critical level free space below 10% | |
# Put the email address to notify here | |
my $servername = 'Zanzibar'; | |
my $email = 'info@blaakmeer.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); | |
# Make vars to put in subject | |
$list[1] = sprintf("%.2f",$list[1]/1024/1024); | |
$list[2] = sprintf("%.2f",$list[2]/1024/1024); | |
$list[3] = sprintf("%.2f",$list[3]/1024/1024); | |
# See if free disk space is below any of our levels | |
if ( ($diskfree < $alert1) && ($diskfree > $alert2) ) { | |
$message = "$servername: Warning Diskspace threshold reached...free space below $alert1% at $diskfree% free - $list[2]GB / $list[1]GB used, $list[3]GB free\n"; | |
&mailer; | |
} | |
elsif ( ($diskfree < $alert1) && ($diskfree < $alert2) ) { | |
$message = "$servername: Critical Diskspace threshold reached...free space below $alert2% at $diskfree% free - $list[2]GB / $list[1]GB used, $list[3]GB free\n"; | |
&mailer; | |
} | |
else { | |
$message = "$servername: Free diskspace is good at $diskfree% free - $list[2]GB / $list[1]GB used, $list[3]GB free\n"; | |
} | |
#Output to terminal (comment out if you wish) | |
print $message; | |
print "~" x 75, "\n@df","~" x 75,"\n","From system: $servername\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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment