Home   >>   Scripts   >>   Script to Check Free Diskspace and Email Notifications
Script to Check Free Diskspace and Email Notifications PDF Print E-mail
( 0 Votes )
How To - Scripts
Written by Christian Foronda   
Tuesday, 05 January 2010 09:28

Perl script to check free diskspace and email notification.

    #!/usr/bin/perl
# Script to check free diskspace and email notifications.
# Change the email and alert levels and you should be good to go.
# Lazyman add this to cron @daily
# This work on SunCellular network, it may also work on other network.
# Edited: Christian Foronda
# Last Edited: 07/30/2009

use strict;

# Alert levels Warning and Critical - Below what percent level of free disk space do you want an alert?
my $alert1 = 20;        # Warning level free space below 30%
my $alert2 = 10;        # Critical level free space below 10%

# email address to notify
my $email = 'admin at systmbx.com';
my ($size,$used,$avail,$use,$mounted);
my $message;
my @message;
my @list;
my $sysname = `/bin/uname -n`;
chomp $sysname;
my @df = `/bin/df -h`;
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 {

# Uncomment if sendmail is preffered
#        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);

use Net::SMTP;

my $relay = "xxx.xxx.xxx.xxx";
my $smtp = Net::SMTP->new($relay)
|| die "Can't open mail connection: $!";

$smtp->mail($sysname);
$smtp->to($email);

$smtp->data();
$smtp->datasend("To: $email\n");
$smtp->datasend("From: $sysname\n");
$smtp->datasend("Subject: $message\n\n");
$smtp->datasend("$message");
$smtp->datasend("~" x 75, "\n@df","~" x 75,"\n","From system: $sysname");
$smtp->dataend();
$smtp->quit();
}

 




blog comments powered by Disqus
Last Updated on Wednesday, 06 January 2010 08:56