Using Perl for this would certainly be better/easier than using a shell script. You have the choice of using perl modules (as suggested in the earlier reply) or using back-tick runs of handy shell utilities to collect information and take appropriate actions (I often do both).
If you're working on a diverse network (i.e. running on different hosts that might have different flavors of unix, or at least different versions of the "standard" df utility), then you really want to use Filesys::DiskFree, so you won't need to worry about figuring out how to parse different formats of df output. (And then there's "Mail::Send" to make the mailing part easier too.)
I've never been a whiz at shell scripts, but they tend to look awfully clumsy and difficult when it comes to doing anything really computational. Here's an example of a script to do what you specified (tested on linux and solaris):
use strict;
use Mail::Send;
use Filesys::DiskFree;
# This will check all mounted volumes, both local and NFS,
# and send separate email reports listing the ones over
# 90% full and the ones over 70% full.
my $fs = new Filesys::DiskFree;
$fs->df();
my @vols = sort $fs->disks();
my ( $yellowFlag, $redFlag );
for my $vol ( @vols ) {
my $dev = $fs->device( $vol );
next unless ( $fs->total( $dev ); # some solaris vols may be 0
my $pct_used = $fs->used( $dev ) / $fs->total( $dev );
$yellowFlag .= "$vol ($dev) is over 70% full\n"
if ( $pct_used > 0.7 );
$redFlag .= "$vol ($dev) is over 90% full\n"
if ( $pct_used > 0.9 );
}
if ( $yellowFlag or $redFlag ) {
my $msg = new Mail::Send;
if ( $yellowFlag ) {
$msg->to( 'bureaucrat@my.org' );
$msg->subject( 'Time to buy more disk!' );
my $fh = $msg->open;
print $fh $yellowFlag;
$fh->close;
}
if ( $redFlag ) {
$msg->to( 'sysadmin@my.org' );
$msg->subject( 'Time to rattle some cages!' );
my $fh = $msg->open;
print $fh $redFlag;
$fh->close;
}
}
update: fixed the logic for appending lines to the two different reports. |