home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / tutorial / eg / dfbitch < prev    next >
Encoding:
Text File  |  1990-11-16  |  2.7 KB  |  110 lines

  1. #!/usr/bin/perl 
  2. #
  3. # diskfull - check for "full" disks, mailing top users about the problem
  4. # tom christiansen, 9-sep-90
  5. #
  6. # "full" is defined as having more than $MIN_FREE percent usesd,
  7. # unless the file system is mentioned in the $EXCEPT file, which
  8. # should consist of ordered pairs of filesystems and minfree values.
  9.  
  10. $MIN_FREE  = 97;    # unless in EXCEPT file, want this much free
  11. $MIN_WHINE = 5;     # don't whine at someone with less than this %
  12.  
  13. $EXCEPT    = '/usr/adm/etc/capacities';
  14.  
  15. chop($hostname = `hostname`);
  16.  
  17. &read_exceptions;
  18.  
  19. open(DF, "df -t 4.2|");
  20. $_ = <DF>;         # skip header
  21. while ( <DF> ) {
  22.     chop;
  23.     ($disk, $kbytes, $used, $avail, $capacity, $mpnt) = split;
  24.     $capacity =~ s/%//;
  25.     if ( $capacity > (defined($Max{$mpnt}) ? $Max{$mpnt} : $MIN_FREE)) {
  26.     &too_full($mpnt, $used, $capacity);
  27.     } 
  28. close DF;
  29. exit;
  30.  
  31. # ---------------------------------------------------------------------------
  32.  
  33. sub read_exceptions {
  34.     local($fs, $min_free);
  35.     # global $EXCEPT %Max
  36.  
  37.     if (-e $EXCEPT) {
  38.     open EXCEPT || die "can't open $EXCEPT: $!";
  39.     while ( <EXCEPT> ) {
  40.         next if /^\s*#/ || /^\s*$/;
  41.         chop;
  42.         ($fs, $min_free) = split(' ');
  43.         if ($fs =~ /^min_?free/i) {
  44.         $MIN_FREE = $min_free;
  45.         } else {
  46.         $Max{$fs} = $min_free;
  47.         } 
  48.     } 
  49.     close EXCEPT;
  50.     } 
  51. }
  52.  
  53. # ---------------------------------------------------------------------------
  54.  
  55. sub too_full {
  56.     local($fs, $kused, $percen) = @_;
  57.     local($_, $used, $user, $anon, @anon, @lines, @allusers, @abusers);
  58.  
  59.     open (QUOT, "/usr/etc/quot $fs|");
  60.     $_ = <QUOT>;  # skip header
  61.  
  62.     while ( <QUOT> ) {
  63.     push(@lines, $_);
  64.     chop;
  65.     ($used, $user) = split(' ');
  66.     if ($user =~ /^#/) {
  67.         push(@anon, $user);
  68.         $anon =+ $used;
  69.         next;
  70.     } 
  71.     push(@allusers, $user);
  72.     push(@abusers, $user)     unless $used/$kused < ($MIN_WHINE/100);
  73.     } 
  74.     close QUOT;
  75.     die "couldn't run quot on $fs" if $? || $#allusers < 0;
  76.  
  77.     $rcpts = join(", ", ($#abusers < 0) ? @allusers : @abusers);
  78.  
  79.     if (0 && $anon) { # maybe someday
  80.     print "Should remind root that $anon kbytes on $fs are used by ";
  81.     print "defunct users ", join(', ', @anon), "\n";
  82.     } 
  83.  
  84.     open (MAIL, "| /usr/lib/sendmail -oi -t");
  85.     select(MAIL);
  86.  
  87.     print <<EOM;
  88. From: the Disk Monitor Daemon ($0) <daemon>
  89. To: $rcpts
  90. Cc: root
  91. Subject: $fs on $hostname is $percen% full
  92.  
  93. Each of you is taking up at least $MIN_WHINE% of the space on $hostname:$fs.
  94. Please do what you can to reduce this.  You might consider simply removing
  95. extraneous files, moving some files to other filesystems, compressing them, 
  96. or backing them up on tape and then deleting them.
  97.  
  98. your friend, the daemon
  99.  
  100. PS: here are the exact totals in kilobytes for top users:
  101.  
  102. EOM
  103.  
  104.     print @lines[0..$#abusers];
  105.     print "\n";
  106.     close MAIL;
  107.     select(STDOUT);
  108.