#23 Disk Space Alarm

I ran out of disk space today. I was working on a program that produced a number of huge core dumps and filled up my disk. Of course I didn't notice it until I started to do a compile and found that my object files were getting truncated. It would have been nice to learn of the problem sooner. As it turned out, because the build broke, I was forced to clean out the core files and restart the build from scratch.

This script tells everyone when disk space is low.

The Code

  1 #!/usr/bin/perl
  2 use strict;
  3 use warnings;
  4
  5 use Filesys::DiskSpace;
  6
  7 my $space_limit = 5;    # Less than 5%, scream
  8
  9 if ($#ARGV == -1) {
 10     print "Usage is $0 <fs> [<fs>....]
";
 11     exit (8);
 12 }
 13
 14 # Loop through each directory in the
 15 # list.
 16 foreach my $dir (@ARGV) {
 17     # Get the file system information
 18     my ($fs_type, $fs_desc, $used,
 19         $avail, $fused, $favail) = df $dir;
 20
 21     # The amount of free space
 22     my $per_free = (($avail) / ($avail+$used)) * 100.0;
 23     if ($per_free < $space_limit) {
 24         # Tailor this command to meet your needs
 25         my $msg = sprintf(
 26           "WARNING: Free space on $dir ".
 27               "has dropped to %0.2f%%",
 28           $per_free);
 29         system("wall '$msg'");
 30     }
 31 }
 32

Running the Script

You'll probably want to set up some sort of cron job to run the script according to a schedule. But to run it manually, just put the name of one or more directories to check on the command line:

$ disk.pl /home

The Results

If there is space on the drive, nothing will happen. But if you are out of space, everyone on the system will get a message that looks something like this:

Broadcast message from root(pts/6) (Thu Oct 28 20:19:13 2004):

WARNING: Free space on /home has dropped to 4.00%

How It Works

The script loops through each directory on the command line checking for space:

 16 foreach my $dir (@ARGV) {

The Filesys::DiskSpace module tells you how much space is being used on the disk. From this, you can easily compute the percentage that is free:

 17     # Get the file system information
 18     my ($fs_type, $fs_desc, $used,
 19         $avail, $fused, $favail) = df $dir;
 20
 21     # The amount of free space
 22     my $per_free = (($avail) / ($avail+$used)) * 100.0;

Now you check to see if the free space falls below the specified limit:

 23     if ($per_free < $space_limit) {
 24         # Tailor this command to meet your needs

You have a space emergency. Use the system wall command to send out a panic message to everyone.

 25         my $msg = sprintf(
 26           "WARNING: Free space on $dir ".
 27               "has dropped to %0.2f%%",
 28           $per_free);
 29         system("wall '$msg'");
 30     }
 31 }

Hacking the Script

The free space limit is hard-coded to 5 percent. If the space falls below that, you get the message. This number can easily be changed to fit your situation.

As written, the script just warns everybody. But you can do more than just yell when you're in trouble. For example, the script could clean up the temporary directories, remove outdated log files, or remove old core files.

The script is good at discovering when a problem occurs and giving you a chance to handle it any way you want to.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset