Wednesday, April 23, 2014

Daemon for Disk Usage on Linux Servers

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! :-)


#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+)/; 
    last DISKSCAN if($DiskCounter==$DiskNumber); 
  return $Available; 

#Scan for Disk ALERT 
sub Scan_Disk 

  my $DiskAlert="Disk Alert"; 
  my $ActualSpace=Disk_Query(1);