Filesystem usage checking script

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:

Download files

No guarantees are given that the script works or does the intended thing. checkfs.sh.

Epilog

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).