in reply to Full Filesystem Notification

For what it's worth, I took a look at your code and I've tried to give you a few helpful hints on another way to do it. I've placed my comments inline.

#!/usr/bin/perl # # Sorry for the simplicity of this script, but as a first submission.. +. # # Emails/Pages me when a filesystem > 90% full # # always use strict use strict; # make a variable to easily change some behavior my($warn_percentage) = 90; # predeclare our variables (not necessary, but probably a good idea) my(%df); # df hash that holds all our information my($host); # hostname # Rather than 3 backticks for df, just do it once and parse it with pe +rl! # Also, make $fs into an array instead of string then split to array # Finally, we don't really need $full, since that's just grep /100/,@f +s #$df=`df -lk 2>/dev/null`; #$fs=`df -lk 2>/dev/null | grep -v used | cut -d'%' -f2 | grep -v deni +ed`; #$full=`df -lk 2>/dev/null | grep -v used | cut -d'%' -f1`; foreach (`df -lk 2>/dev/null`) { if (/(\b\d+)%\s+(\S+)$/) { $df{$2} = $1; } } # removed some string manipulation that isn't needed any longer #$full =~ s/.*(..\d)$/\1/mg; #$fs=~s/\s+/ /g; #$full=~s/\s+/ /g; #@fs=split(" ",$fs); #@full=split(" ",$full); # We can combine this into one statement #$host=`hostname`; #chomp($host); chomp($host = `hostname`); # this loop changes somewhat #$fs=@fs; #chomp($date); # where is $date coming from? # note that the mailx stuff isn't tested, as I don't have mailx on my +system foreach (keys %df) { if ($df{$_} > $warn_percentage) { print "Host: $host Filesystem: $_ is $df{$_}% full!\n"; system("mailx -s \"$host:$_ is $df{$_}%\" youremail\@host.com << / +dev/null"); } } #while ($fs) { # $fs=shift(@fs); # $full=shift(@full); # chomp($fs); # chomp($full); # if ($full > 90) { # print "Host: $host Filesystem: $fs is ${full}% full!\n"; # @chk=grep(/$fs:$full/,@log); # not sure what all this log stuff i +s... # undef(@log); # $chk=@chk; # close(LOG); # } # if ($chk == 0) { # `mailx -s "$host:$fs is ${full}%" youremail\@host.com <<EOF #$df #EOF #`; # `mailx -s "$host:$fs is ${full}%" mypager\@pagerhost.com <<EOF #EOF #`; # } #}
Hope that helps!

Replies are listed 'Best First'.
RE: Full Filesystem Notification
by prodevel (Scribe) on Jun 30, 2000 at 23:26 UTC
    Thanks very much, Shendal! Much appreciated. Actually, I probably should have gone over the code before submitting. FYI here's another version in pure ksh I wrote for a Oracle DBA friend of mine that didn't want to install Perl if you're interested. Bit more condensed.

    Hope I don't get into any trouble for posting shell! =)

    #!/usr/bin/ksh

    pct=90
    x=0

    for filesys in `df -k | awk '{print $1}'`
    do
     x=`expr $x + 1`
     dfline=`df -k | head -$x | tail -1`
     full=`echo $dfline | awk '{print $5}' | sed 's/%//g' | egrep -v capacity`

     if $full -gt pct
     then
     mailx -s "Filesystem: $filesys is ${full}% full!!!" mail@mailhost.com <<EOF
     $dfline
    EOF
     mailx -s "Filesystem: $filesys is ${full}% full!!!" pager@pagerhost.com <<EOF
     $dfline
    EOF

     fi
    done
      #!/usr/bin/ksh x=0 for filesys in `df -k | awk '{print $1}'` do x=`expr $x + 1` dfline=`df -k | head -$x | tail -1` full=`echo $dfline | awk '{print $5}' | sed 's/%//g' | egrep -v capacity` if $full -gt 76 then mailx -s "Filesystem: $filesys is ${full}% full!!!" pager@pagerhost.com <<EOF $dfline EOF fi done