# Monitor disk usage and send email when disk is nearly full

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:

 <pre class="src src-sh"><span style="color: #ff4500;">#</span><span style="color: #ff4500;">!/bin/</span><span style="color: #00ffff;">bash</span>


## adapted from ## http://www.unix.com/unix-dummies-questions-answers/100941-email-warning-script-disk-usage.html #### 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)}' max_space=“80” disk_space=${disk_space:-$((max_space-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 ; } done 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:

 <pre class="src src-sh">0 * * * * /path/to/diskAlert.sh


Statistician