#!/usr/bin/perl
#
# Monitor disk space usage
#
# Arguments are:
#
# path:kBfree [path:kBfree...]
#
# This script will exit with value 1 if "path" has less than
# "kBfree" kilobytes.
#
# The first output line is a list of the paths which failed, and
# how much space is free, in megabytes.
#
# If you are testing NFS-mounted directories, should probably
# mount them with the ro,intr,soft options, so that operations
# on those mount points don't block forever if the server is
# down, and I may eventually change this code to use an alarm(2)
# to interrupt the stat and statfs system calls.
#
# This requires Fabien Tassin's File::Df module, available from 
# your friendly neighborhood CPAN mirror. See http://www.perl.com/perl/
# 
# Jim Trocki, trockij@transmeta.com
#
# $Id: freespace.monitor,v 1.5 1998/01/18 17:41:36 trockij Exp $
#
#    Copyright (C) 1998, Jim Trocki
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
use File::Df;

foreach (@ARGV) {
    ($path, $minavail) = split (/:/, $_, 2);

    ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df ($path);
    if (!defined ($used)) {
	push (@failures, "statfs error for $path: $!");
	next;
    }

    if ($avail < $minavail) {
    	push (@failures, sprintf ("%1.1fGB free on %s", $avail / 1024 / 1024,
	    $path));
    }
}

if (@failures) {
    print join (", ", @failures), "\n";
    exit 1;
}

exit 0;
