home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2946 < prev    next >
Encoding:
Internet Message Format  |  1991-03-04  |  4.7 KB

  1. From: tchrist@convex.COM (Tom Christiansen)
  2. Newsgroups: comp.unix.shell,alt.sources
  3. Subject: Re: Help w/Disk usage utility
  4. Message-ID: <1991Mar03.210154.13532@convex.com>
  5. Date: 3 Mar 91 21:01:54 GMT
  6.  
  7. From the keyboard of arthur@ccwf.cc.utexas.edu (Arthur Nghiem):
  8. :    I want to write a script that will check the disk usage of a group
  9. :of users periodically.  I want to be able to warn them when they first exceed
  10. :a limit, wait a few days and then automatically compress their files or
  11. :remove the newest files that put them over the limit.
  12. :        I'm hoping that someone has already done this and will be happy
  13. :to help out.  Please send mail to arthur@ccwf.ccutexas.edu.  Scripting in
  14. :any shell and/or alernate solutions will be joyfully accepted.
  15.  
  16. You should be able to massage this into somethin useful.    It finds
  17. full filesystems (you get to define how full is full, on a per-fs basis)
  18. and mails only the top offenders about it.  Works quite well here.
  19.  
  20.  
  21. For example, here's my $EXCEPT file:
  22.  
  23.     # this file used by dfbitch disk monitoring program to find
  24.     # filesystems whose allowable freespace is different
  25.     # from the default.
  26.  
  27.     # to reset minimum allowable free space, uncomment next line
  28.     # MIN_FREE    90
  29.  
  30.     # file system    percentage allowed full
  31.  
  32.      /export/swap    105
  33.      /export/swap2    105
  34.      /usr        100
  35.      /mnt         99
  36.      /tmp         50
  37.  
  38.     # vwork and vmaster changed by K<bob> 9/11/90:
  39.  
  40.      /vwork        111
  41.      /vmaster           111
  42.      /bobm        150
  43.  
  44.  
  45. The script follows.  It uses /usr/etc/quot, /bin/df,  /usr/lib/sendmail.
  46. You can get get by without sendmail, but not the others.
  47.  
  48. See also the posting by Johan Vromans <jv@mh.nl> in the article
  49. <1991Feb28.104805.24928@pronto.mh.nl> to alt.sources and comp.lang.perl
  50. entitled "dusage.pl (Was: Re: monitoring disk usage...)".  It came out
  51. last Thursday.
  52.  
  53.  
  54. --tom
  55.  
  56. #!/usr/bin/perl 
  57. #
  58. # dfbitch - check for "full" disks, mailing top users about the problem
  59. # tom christiansen, 9-sep-90
  60. #
  61. # "full" is defined as having more than $MIN_FREE percent usesd,
  62. # unless the file system is mentioned in the $EXCEPT file, which
  63. # should consist of ordered pairs of filesystems and minfree values.
  64.  
  65. $MIN_FREE  = 97;    # unless in EXCEPT file, want this much free
  66. $MIN_WHINE = 5;     # don't whine at someone with less than this %
  67.  
  68. $EXCEPT    = '/usr/adm/etc/capacities';
  69.  
  70. chop($hostname = `hostname`);
  71.  
  72. &read_exceptions;
  73.  
  74. open(DF, "df -t 4.2|");
  75. $_ = <DF>;         # skip header
  76. while ( <DF> ) {
  77.     chop;
  78.     ($disk, $kbytes, $used, $avail, $capacity, $mpnt) = split;
  79.     $capacity =~ s/%//;
  80.     if ( $capacity > (defined($Max{$mpnt}) ? $Max{$mpnt} : $MIN_FREE)) {
  81.     &too_full($mpnt, $used, $capacity);
  82.     } 
  83. close DF;
  84. exit;
  85.  
  86. # ---------------------------------------------------------------------------
  87.  
  88. sub read_exceptions {
  89.     local($fs, $min_free);
  90.     # global $EXCEPT %Max
  91.  
  92.     if (-e $EXCEPT) {
  93.     open EXCEPT || die "can't open $EXCEPT: $!";
  94.     while ( <EXCEPT> ) {
  95.         next if /^\s*#/ || /^\s*$/;
  96.         chop;
  97.         ($fs, $min_free) = split(' ');
  98.         if ($fs =~ /^min_?free/i) {
  99.         $MIN_FREE = $min_free;
  100.         } else {
  101.         $Max{$fs} = $min_free;
  102.         } 
  103.     } 
  104.     close EXCEPT;
  105.     } 
  106. }
  107.  
  108. # ---------------------------------------------------------------------------
  109.  
  110. sub too_full {
  111.     local($fs, $kused, $percen) = @_;
  112.     local($_, $used, $user, $anon, @anon, @lines, @allusers, @abusers);
  113.  
  114.     open (QUOT, "/usr/etc/quot $fs|");
  115.     $_ = <QUOT>;  # skip header
  116.  
  117.     while ( <QUOT> ) {
  118.     push(@lines, $_);
  119.     chop;
  120.     ($used, $user) = split(' ');
  121.     if ($user =~ /^#/) {
  122.         push(@anon, $user);
  123.         $anon =+ $used;
  124.         next;
  125.     } 
  126.     push(@allusers, $user);
  127.     push(@abusers, $user)     unless $used/$kused < ($MIN_WHINE/100);
  128.     } 
  129.     close QUOT;
  130.     die "couldn't run quot on $fs" if $? || $#allusers < 0;
  131.  
  132.     $rcpts = join(", ", ($#abusers < 0) ? @allusers : @abusers);
  133.  
  134.     if (0 && $anon) { # maybe someday
  135.     print "Should remind root that $anon kbytes on $fs are used by ";
  136.     print "defunct users ", join(', ', @anon), "\n";
  137.     } 
  138.  
  139.     open (MAIL, "| /usr/lib/sendmail -oi -t");
  140.     select(MAIL);
  141.  
  142.     print <<EOM;
  143. From: the Disk Monitor Daemon ($0) <daemon>
  144. To: $rcpts
  145. Cc: root
  146. Subject: $fs on $hostname is $percen% full
  147.  
  148. Each of you is taking up at least $MIN_WHINE% of the space on $hostname:$fs.
  149. Please do what you can to reduce this.  You might consider simply removing
  150. extraneous files, moving some files to other filesystems, compressing them, 
  151. or backing them up on tape and then deleting them.
  152.  
  153. your friend, the daemon
  154.  
  155. PS: here are the exact totals in kilobytes for top users:
  156.  
  157. EOM
  158.  
  159.     print @lines[0..$#abusers];
  160.     print "\n";
  161.     close MAIL;
  162.     select(STDOUT);
  163. --
  164. "UNIX was not designed to stop you from doing stupid things, because
  165.  that would also stop you from doing clever things." -- Doug Gwyn
  166.  
  167.  Tom Christiansen                tchrist@convex.com      convex!tchrist
  168.