Recently a friend of mine asked me something to periodically monitor the Disk Usage of
a Linux Server...
Well, actually I'm not doing System Administration, and there are a lot of tools/script
out there on the Internet which can do the work for you, but..
true Engineers can't resist in 'Reinventig the wheel' :-)
This is a part of a Script which I wrote some years ago... surely It's something that
should need a good refactoring for a more general usage, but It can give you an idea
of what to do....
Yes, It's the famous 'write-only' Perl... It needs no libraries... to use it like a daemon
you just have to make some modifications to the code and put it in your crontab file...
enjoy it! :-)
#!/usr/bin/perl
#Threshold set
$GlobalDiskLimit=1000000; #1GB
#SendMail (pass Problem Kind and Value)
sub Send_Mail
{
my $Problem=shift;
my $Value=shift;
open (MAIL,"|/usr/sbin/sendmail -t");
print MAIL "To: youremail\@youremail\n";
print MAIL "Subject: ALERT ALERT!\n";
print MAIL $Problem . ": " . $Value . "\n";
close (MAIL);
}
#Query Disk Space (pass Disk number)
sub Disk_Query
{
my $DiskNumber=shift;
my $DiskCounter=0;
my @Disk=`df -t ext3 -l`;
my $line, $Available;
#Scan output
DISKSCAN: foreach $line (@Disk)
{
$line =~ /(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/;
($_,$_,$_,$Available,$_,$_)=($1,$2,$3,$4,$5,$6);
last DISKSCAN if($DiskCounter==$DiskNumber);
$DiskCounter++;
}
return $Available;
}
#Scan for Disk ALERT
sub Scan_Disk
{
my $DiskAlert="Disk Alert";
my $ActualSpace=Disk_Query(1);
if($ActualSpace<$GlobalDiskLimit)
{
Send_Mail($DiskAlert,$ActualSpace);
}
}
#Main
Scan_Disk();