Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Full Filesystem Notification

by prodevel (Scribe)
on Jun 30, 2000 at 04:57 UTC ( [id://20524]=perlcraft: print w/replies, xml ) Need Help??

   1: #!/usr/bin/perl
   2: #
   3: # Sorry for the simplicity of this script, but as a first submission...
   4: #
   5: # Emails/Pages me when a filesystem > 90% full
   6: 
   7: $df=`df -lk 2>/dev/null`;
   8: $fs=`df -lk 2>/dev/null | grep -v used | cut -d'%' -f2 | grep -v denied`;
   9: $full=`df -lk 2>/dev/null | grep -v used | cut -d'%' -f1`;
  10: 
  11: $full =~ s/.*(..\d)$/\1/mg;
  12: 
  13: $fs=~s/\s+/ /g;
  14: $full=~s/\s+/ /g;
  15: 
  16: @fs=split(" ",$fs);
  17: @full=split(" ",$full);
  18: $host=`hostname`;
  19: chomp($host);
  20: $fs=@fs;
  21: chomp($date);
  22: while ($fs)
  23: {$fs=shift(@fs);
  24:  $full=shift(@full);
  25:  chomp($fs);
  26:  chomp($full);
  27:  if ($full > 90)
  28:  {print "Host: $host Filesystem: $fs is ${full}% full!\n";
  29:   @chk=grep(/$fs:$full/,@log);
  30:   undef(@log);
  31:   $chk=@chk;
  32:   close(LOG);
  33:   if ($chk == 0)
  34:   {`mailx -s "$host:$fs is ${full}%" youremail\@host.com <<EOF
  35: $df
  36: EOF
  37: `;
  38:    `mailx -s "$host:$fs is ${full}%" mypager\@pagerhost.com <<EOF
  39: 
  40: EOF
  41: `;
  42:   }
  43:  }
  44: }
  45: 

Replies are listed 'Best First'.
RE: Full Filesystem Notification
by Shendal (Hermit) on Jun 30, 2000 at 18:44 UTC
    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!
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-28 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found