Monitor disk usage and send email when disk is nearly full


Vinh Nguyen


October 17, 2010

This site and this site describe some scripts to monitor disk usage of a computer based on the df command and email the ADMIN when disk usage reaches a certain percentage. The scripts can be ran periodically based on cron.

These scripts did not work on my local NAS because I have one filesystem called /dev/mapper/acer-root that takes up two lines in the output of df; hence, the scripts run into an error when trying to grab the 5th column of the output. Also, in recent versions of Ubuntu, /bin/sh is symbolically linked to dash; to get the scripts to work, remove the function keyword or change to /bin/bash on the shebang (#!) line in order for the scripts to run.

I adapted a script found on this site for my own use:

## adapted from
#### check disk space on filesystems listed
filesystems=(/ /boot /h2tb)
for fs in ${filesystems[@]}; do
disk_space=`df -h | grep -E $fs$ | awk '{print $(NF-1)}'`
##[[ "${disk_space%'%'}" -ge "$max_space" ]] && { clear ; echo -e "nWARNING...DISK SPACE ON "$fs" IS AT $disk_spacen"; }
[[ "${disk_space%'%'}" -ge "$max_space" ]] && { echo "" | mutt -s "WARNING...DISK SPACE ON "$fs" IS AT $disk_space" MY.EMAIL.ADDRESS ; }
unset filesystems fs disk_space max_space

Note that I use mutt to send emails; mail would be simpler and more portable, but it isn't set up on my server.

I added the following to cron via crontab -e to get the script to run hourly:

0 * * * * /path/to/