Home > Server Administration > Monitor Disk Space with Perl

Monitor Disk Space with Perl

November 20th, 2010 Leave a comment Go to comments

Have a client who has a very popular web site that contains terabytes of images. He has been hitting the limit of his available disk space and for reasons I won’t bore you with, getting more storage has proven difficult. We’ve had a few occasions where drive space ran out and of course, MySQL stops working at that point, so we needed something to alert us when the drive was getting close to being full. I’m not a big perl programmer, but here is what I cobbled together.

Fist, using cpan or other methods, install Filesys::DfPortable. Then run the following from a cron as often as necessary:

use strict;
use warnings;
use Filesys::DfPortable;
my $warning_level=98;
my $ref = dfportable("/", 1024); # Display output in 1K blocks
my $to='you@domain.comt';
my $from='you@domain.com';
my $subject='Low Disk Space';
if(defined($ref)) {
my $use = $ref->{per};
if ($use > $warning_level) {
my $out = sprintf("WARNING Low Disk Space : %0.2f%% ()\n",$use);
open(MAIL, "|/usr/sbin/sendmail -t");
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$out\n";

Categories: Server Administration Tags:
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.

site tracking with Asynchronous Google Analytics plugin for Multisite by WordPress Expert at Web Design Jakarta.