The following script will send an email when given filesystem usage has exceeded given percentage. Nothing fancy here.
#!/bin/sh
#
# Simple script to send email if usage on given filesystem exceeds given amount
#
# 2006 (c) Aleksandr Koltsoff
#
# check that we have all the necessary parameters to run
if [ $# -ne 3 ]; then
echo "USAGE: checkfs.sh mount-point cut-off email-address"
exit 1
fi
# copy command line parameters into local variables (easier to use)
MP=$1
CUTOFF=$2
EMAIL=$3
# we also get the hostname here so that the email can be more informative
HOSTNAME=`hostname`
# get usage in percentage
# 1) use df to get filesystem usage info on mount-point
# 2) delete all repeating spaces (so that we can use cut)
# 3) select fifth field (usage %)
# 4) remove the '%' so that we can use the number
USAGE=`df $MP | tail -1 | tr -s ' ' | cut -f5 -d' ' | tr -d '%'`
# echo $USAGE
# compare usage to cutoff point
# here we use [ foo ] so that shell will evaluate the contents
# as integers as opposed as text strings. we also use the -ge
# operator to test whether USAGE is greater or equal to CUTOFF
if [ $USAGE -ge $CUTOFF ]; then
#echo "USAGE($USAGE) more than CUTOFF($CUTOFF)"
# send email
echo "Disk space usage on $HOSTNAME:$MP exceeded $CUTOFF% (now at $USAGE%)" \
| mail -s "WARNING: $HOSTNAME:$MP disk space running low" $EMAIL
fi
# terminate with success code (this is a lie, we "succeed" even if there
# are errors. we just don't handle the errors)
exit 0
[ Send email when filesystem usage has exceeded ]
To use the script you need three parameters:
- The mount point of the filesystem that you wish to check
- The percentage of usage at which the email will be sent
- The email address of the email recipint who will receive the notice. Email will not be sent if usage is not exceeded.
No guarantees are given that the script works or does the intended thing. checkfs.sh.
All content copyright 2006 Aleksandr Koltsoff (including program sources). Use for personal education permitted, all other use must be negotiated (czr(at)iki(dot)fi).