
#
# check all users disk allocations, and mail disk hogs
#
# change IFS so that only <NL> is a separator - needed to pick up lines from
# /etc/passwd
#
IFS='
'
TEMP=/tmp/dcheck			# temp file for mail etc.
DQUOTAS=/usr/local/lib/disk			# directory containing all the info
USAGE=$DQUOTAS/usage			# usages recorded here each night
MALIASES=/usr/lib/mailx/mailx.rc	# system mail aliases.
VIPS=100				# logins with uids less than this are not checked
LOG=$DQUOTAS/log			# log of warnings etc.
BOOK=$DQUOTAS/allowed			# where the index of allowances is kept
ALTERNATE=$DQUOTAS/alternate		# list of other directories to add in
ADMIN=root				# user informed of diskhogs
MAX=5					# no of warnings issued before login restricted
#
rm -f $USAGE
MC=`uname`
for u in `cat /etc/passwd`
do
#
# scan /etc/passwd file for users.
# get USER (name) UID, and HDIR (home directory).
#
	USER=`echo $u | awk 'BEGIN{FS=":"} {print $1}'`
	UID=`echo $u | awk 'BEGIN{FS=":"} {print $3}'`
	HDIR=`echo $u | awk 'BEGIN{FS=":"} {print $6}'`
	if [ -f "$ALTERNATE" ]
	then
		OTHERS=`grep "$USER" "$ALTERNATE" | sed 's/[a-zA-Z]* //'`
	else
		OTHERS=""
	fi
#
# check MALIASES for "alias $USER real_user"
#
	MALIAS=`grep "alias $USER " /usr/lib/mailx/mailx.rc | awk '{print $3}'`
	if [ "$MALIAS" = "" ]
	then
		MALIAS=$USER
	fi
#
# ignore users with uids less than $VIPS
#
	if [ $UID -lt "$VIPS" ]
	then
		continue
	fi
	ALLOWED=`grep "^$USER[ 	]" $BOOK | awk '{print $2}'`
	if [ "$ALLOWED" = "" ]
	then
		continue		# no definition of disk usage
	fi
	DISK=`du -s $HDIR $OTHERS | awk '{total += $1} END{print total}'`
#
# keep record of current disk use
#
	echo "$USER has $DISK, allowed $ALLOWED" >> $USAGE
#
# send warning if disk usage is over 90% of allowed
#
	THRESHOLD=`expr "$ALLOWED" - \( "$ALLOWED" / 10 \)`
	if [ "$DISK" -gt "$THRESHOLD" ] && [ "$DISK" -lt "$ALLOWED" ]
	then
		/bin/mail $MALIAS << EOF
Subject:  disk usage warning

Your disk usage ($ALLOWED) is nearly used up
You have $DISK blocks
EOF
	fi
	if [ "$DISK" -gt "$ALLOWED" ]
	then
		TIME=`date`
#
# if no count file present, then create one.
#
		if [ ! -f $DQUOTAS/$USER ]
		then
			cat "1" > $DQUOTAS/$USER
		fi
		COUNT=`cat $DQUOTAS/$USER`
		EXCESS=`expr "$DISK" - "$ALLOWED"`
		/bin/mail $MALIAS << EOF
Subject:  Disk usage

Your disk usage on $MC is $DISK blocks.
You are allowed only $ALLOWED blocks -- please remove $EXCESS blocks.
EOF
		echo "$USER allowed $ALLOWED has $DISK - warned $TIME" >> $LOG
		COUNT=`expr "$COUNT" + 1`
		echo $COUNT > $DQUOTAS/$USER
		if [ "$COUNT" -gt "$MAX" ]
		then
#
# warned too many times -
# mail supervisor, and give restricted logins until files removed.
# 
			/bin/mail $ADMIN << EOF
Subject:  Disk hog $USER

$USER has ignored all my warnings about disk quotas. I have therefore
restricted ${USER}'s login.
$USER has $EXCESS too many blocks on $MC, with a quota of $ALLOWED blocks.
EOF
			echo "Disk hog $USER restricted on $TIME" >> $LOG
#
# create tag file, to be found by the login shell
#
			touch $DQUOTAS/hogs/$USER
#
# make sure the files can't be accessed by anyone else - peter logs in as
# other people! You can remove the check for peter, if you want it to work
# for everyone.
#
#			if [ "$USER" = "peter" ]
#			then
				chmod go-rx $HDIR
#			fi
		fi
	else
		echo "1" > $DQUOTAS/$USER
	fi
done


