#!/usr/bin/env perl # print information about file systems over a certain percentage full use warnings; use Getopt::Long; my $all; # report all file systems my $minimum; # report file systems with less than this many bytes free my $threshold; # report file systems over this percent used my $help; my $default = 95; # default threshold if no options specified GetOptions ("help|h" => \$help, "all|a" => \$all, "minimum|m:s" => \$minimum, "threshold|t:s" => \$threshold) or usage(); if ($help) { usage(); } # symbols for multiples of 1024 bytes my @suffixes = ("KB", "MB", "GB", "TB", "PB", "EB"); # output format for file systems at or above threshold format STDOUT = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @>>>> @< $mount, $free,$suffixes[$power] . my $full = 0; # number of file systems at or above threshold @output = `df -Pk`; # retrieve disk space information shift @output; # strip header foreach my $line (@output) { $\ = "\n"; $, = "\t"; # parse this output line our ($fs, $blocks, $used, $avail, $percent, $mount) = split (/\s+/, $line); # strip percent sign $percent =~ s/%$//; # convert to three figures plus binary suffix $free = $avail; $power = 0; while ($free >= 1000) { $free /= 1024; $power++; } $free = sprintf ("%.1f", $free); if (length ($free) > 3) { $free =~ s/\..*//; } # -a specified if (defined($all)) { $full++; write; } # both -t and -m specified if (defined($threshold) && defined($minimum)) { if ($percent >= $threshold && $avail < $minimum) { $full++; write; } } # only -t specified elsif (defined($threshold)) { if ($percent >= $threshold) { $full++; write; } } # only -m specified elsif (defined($minimum)) { if ($avail < $minimum) { $full++; write; } } # neither -t nor -m specified else { if ($percent >= $default) { $full++; write; } } } exit $full; # print a usage message sub usage { print " Usage: diskspace [options] Options: -h Print a message describing how to run this program -m bytes Show file systems that have less than bytes bytes free -t percent Show file systems that are at least percent full "; exit 2; } # vi: set sw=4 ts=33: