home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume18 / diskhog2 / dcheck < prev    next >
Encoding:
Text File  |  1989-03-26  |  3.2 KB  |  131 lines

  1.  
  2. #
  3. # check all users disk allocations, and mail disk hogs
  4. #
  5. # change IFS so that only <NL> is a separator - needed to pick up lines from
  6. # /etc/passwd
  7. #
  8. IFS='
  9. '
  10. TEMP=/tmp/dcheck            # temp file for mail etc.
  11. DQUOTAS=/usr/local/lib/disk            # directory containing all the info
  12. USAGE=$DQUOTAS/usage            # usages recorded here each night
  13. MALIASES=/usr/lib/mailx/mailx.rc    # system mail aliases.
  14. VIPS=100                # logins with uids less than this are not checked
  15. LOG=$DQUOTAS/log            # log of warnings etc.
  16. BOOK=$DQUOTAS/allowed            # where the index of allowances is kept
  17. ALTERNATE=$DQUOTAS/alternate        # list of other directories to add in
  18. ADMIN=root                # user informed of diskhogs
  19. MAX=5                    # no of warnings issued before login restricted
  20. #
  21. rm -f $USAGE
  22. MC=`uname`
  23. for u in `cat /etc/passwd`
  24. do
  25. #
  26. # scan /etc/passwd file for users.
  27. # get USER (name) UID, and HDIR (home directory).
  28. #
  29.     USER=`echo $u | awk 'BEGIN{FS=":"} {print $1}'`
  30.     UID=`echo $u | awk 'BEGIN{FS=":"} {print $3}'`
  31.     HDIR=`echo $u | awk 'BEGIN{FS=":"} {print $6}'`
  32.     if [ -f "$ALTERNATE" ]
  33.     then
  34.         OTHERS=`grep "$USER" "$ALTERNATE" | sed 's/[a-zA-Z]* //'`
  35.     else
  36.         OTHERS=""
  37.     fi
  38. #
  39. # check MALIASES for "alias $USER real_user"
  40. #
  41.     MALIAS=`grep "alias $USER " /usr/lib/mailx/mailx.rc | awk '{print $3}'`
  42.     if [ "$MALIAS" = "" ]
  43.     then
  44.         MALIAS=$USER
  45.     fi
  46. #
  47. # ignore users with uids less than $VIPS
  48. #
  49.     if [ $UID -lt "$VIPS" ]
  50.     then
  51.         continue
  52.     fi
  53.     ALLOWED=`grep "^$USER[     ]" $BOOK | awk '{print $2}'`
  54.     if [ "$ALLOWED" = "" ]
  55.     then
  56.         continue        # no definition of disk usage
  57.     fi
  58.     DISK=`du -s $HDIR $OTHERS | awk '{total += $1} END{print total}'`
  59. #
  60. # keep record of current disk use
  61. #
  62.     echo "$USER has $DISK, allowed $ALLOWED" >> $USAGE
  63. #
  64. # send warning if disk usage is over 90% of allowed
  65. #
  66.     THRESHOLD=`expr "$ALLOWED" - \( "$ALLOWED" / 10 \)`
  67.     if [ "$DISK" -gt "$THRESHOLD" ] && [ "$DISK" -lt "$ALLOWED" ]
  68.     then
  69.         /bin/mail $MALIAS << EOF
  70. Subject:  disk usage warning
  71.  
  72. Your disk usage ($ALLOWED) is nearly used up
  73. You have $DISK blocks
  74. EOF
  75.     fi
  76.     if [ "$DISK" -gt "$ALLOWED" ]
  77.     then
  78.         TIME=`date`
  79. #
  80. # if no count file present, then create one.
  81. #
  82.         if [ ! -f $DQUOTAS/$USER ]
  83.         then
  84.             cat "1" > $DQUOTAS/$USER
  85.         fi
  86.         COUNT=`cat $DQUOTAS/$USER`
  87.         EXCESS=`expr "$DISK" - "$ALLOWED"`
  88.         /bin/mail $MALIAS << EOF
  89. Subject:  Disk usage
  90.  
  91. Your disk usage on $MC is $DISK blocks.
  92. You are allowed only $ALLOWED blocks -- please remove $EXCESS blocks.
  93. EOF
  94.         echo "$USER allowed $ALLOWED has $DISK - warned $TIME" >> $LOG
  95.         COUNT=`expr "$COUNT" + 1`
  96.         echo $COUNT > $DQUOTAS/$USER
  97.         if [ "$COUNT" -gt "$MAX" ]
  98.         then
  99. #
  100. # warned too many times -
  101. # mail supervisor, and give restricted logins until files removed.
  102.             /bin/mail $ADMIN << EOF
  103. Subject:  Disk hog $USER
  104.  
  105. $USER has ignored all my warnings about disk quotas. I have therefore
  106. restricted ${USER}'s login.
  107. $USER has $EXCESS too many blocks on $MC, with a quota of $ALLOWED blocks.
  108. EOF
  109.             echo "Disk hog $USER restricted on $TIME" >> $LOG
  110. #
  111. # create tag file, to be found by the login shell
  112. #
  113.             touch $DQUOTAS/hogs/$USER
  114. #
  115. # make sure the files can't be accessed by anyone else - peter logs in as
  116. # other people! You can remove the check for peter, if you want it to work
  117. # for everyone.
  118. #
  119. #            if [ "$USER" = "peter" ]
  120. #            then
  121.                 chmod go-rx $HDIR
  122. #            fi
  123.         fi
  124.     else
  125.         echo "1" > $DQUOTAS/$USER
  126.     fi
  127. done
  128.  
  129.  
  130.