1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#!/usr/bin/perl
#
# Monitor disk space usage
#
# Arguments are:
#
# path:kBfree [path:kBfree...]
# or
# path:free% [path:free%...]
#
# This script will exit with value 1 if "path" has less than
# "kBfree" kilobytes, or less than "free" percent available.
#
# 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 Filesys::DiskSpace module, available from
# your friendly neighborhood CPAN mirror. See http://www.perl.com/perl/
#
# Jim Trocki, trockij@arctic.org
#
# $Id: freespace.monitor,v 1.2 2005/04/17 07:42:27 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 Filesys::DiskSpace;
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 ($minavail =~ /(\d+(\.\d+)?)%/o) {
$minavail = int(($used + $avail) * $1 / 100);
}
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;
|