Learn to use scripting and cron to run regular scripts that will monitor your disk space.

Monitoring disk usage is a critical part of running any server. There is nothing worse than having a runaway process or malicious user fill up a filesystem only to find out about it when the boss calls to indicate she can’t connect to or write to the server. With a little bit of shell scripting and cron usage, you can have a script run every hour or at any other interval that will indicate if you need to be concerned about disk space filling up.

The script itself is very small:

fs=`mount|egrep '^/dev'|grep -iv cdrom| awk '{print $3}'`
typeset -i thresh="90"
typeset -i warn="95"
for i in $fs
    typeset -i used=`df -k $i|tail -1|awk '{print $5}'|cut -d "%" -f 1`
    if [ "$used" -ge "$warn" ]; then
        echo "CRITICAL: filesystem $i is $used% full"
    if [ "$used" -ge "$thresh" -a "$used" -le "$warn" ]; then
        echo "WARNING: filesystem $i is $used %full"

The first thing the script does is get the list of mount points from the mount program and filters out any mounted CD-rom drives. It also doesn’t take into account any special file systems like /proc or /sys by making sure the mount point device is an actual device (by making sure the device name starts with /dev).

Next, set your threshold to something sensible, like 90% full. Adjust this to taste; this just generates a warning. Likewise, you set your critical warning value to 95%. Notice that the command typeset -i is used to ensure that bash treats these numbers as integers, which you need for comparing the values obtained from df.

The $used variable gets set on each filesystem with the Use% column output from the df tool. This is compared to your thresholds.

The script simply outputs the warnings to standard output. The script can easily be adjusted to have it send an e-mail to someone should one of the thresholds be breached. To have this executed every 30 minutes via cron, edit the system crontab to include:

0,30 * * * * * /usr/local/bin/diskmon.sh

In this case, all the warnings will be sent via cron to whoever receives cron's output mails.


MR. B