This answer is based on my specific directory needed but you could easily modify it to suite any volume name or even a multiple volumes. I'm not the best with scripting and adapted some functions I found, it will be good to see responses with improvement. Thanks
Script will check the size of /home and if it is too large then email the user who has the largest directory. Note: any non-human user that has a directory, like a service account, would need to be excluded using an exclude list. Service accounts do not check their email very often. This script assumes that once the top offender clears out all they can and if then the disk is over the limit then the next run will find the next largest disk hog and email them. Suggest running every 15 minutes so you may catch the user while they are still logged in.
#!/bin/bash
DISK="/home" # disk to monitor
CURRENT=$(df -h | grep ${DISK} | awk {'print $4'}) # get disk usage from monitored disk
MAX="85%" # max 85% disk usage
DOMAIN="your.com"
# functions #
function max_exceeded() {
# Max Exceeded now find the largest offender
cd $DISK
for i in `ls` ; do du -s $i ; done > /tmp/mail.1
sort -gk 1 /tmp/mail.1 | tail -1 | awk -F " " '{print $2}' > /tmp/mail.offender
OFFENDER=`cat /tmp/mail.offender`
echo $OFFENDER
du -sh $DISK/$OFFENDER > /tmp/mail.over85
mail -s "$HOSTNAME $DISK Alert!" "$OFFENDER@$DOMAIN, admin@$DOMAIN" < /tmp/mail.over85
}
function main() {
# check if current disk usage is greater than or equal to max usage.
if [ ${CURRENT} ]; then
if [ ${CURRENT%?} -ge ${MAX%?} ]; then
# if it is greater than or equal to max usage we call our max_exceeded function and send mail
echo "Max usage (${MAX}) exceeded. The /home disk usage is it at ${CURRENT}. Sending email."
max_exceeded
fi
fi
}
# init #
main
#CLEANUP ON AISLE ONE
rm /tmp/mail.1
rm /tmp/mail.offender
rm /tmp/mail.over85