Checking Free Disk Space on Linux

Long time ago, there was a utility on Gentoo that was able to check and report free disk space left in case the free disk space reached some treshold. Recently I tried to find the tool again but failed. Maybe it was diskfree which is hosted at sourceforge.net, but that application is not maintained anymore. In the end, I decided to write my own script that will report to me information about filesystems that have reached some usage treshold and need my attention. Here is the script:

#!/bin/bash

# Copyright 2010 Miroslav Ć ulc
#
# You can always find the latest version at http://www.fordfrog.name/posts/checking-free-disk-space-on-linux/
#
# This script is licensed under GPLv3 or newer.

# Percentage of disk usage from which to issue alerts
ALERT_USAGE=90
# You can pass extra switches to 'df' command if you want. Here are those that might be useful:
# -h     = outputs the information about filesystem usage in human readable form
# -l     = performs check only on local filesystems (so ignores filesystems connected via network)
# --sync = performs sync operation which makes the output more up to date, mainly on SunOS systems
#          (for more information see 'df' man page)
DF_EXTRA_SWITCHES="-h -l"

LC_ALL="C" df -P $DF_EXTRA_SWITCHES | grep -vE "(^Filesystem|cdrom)" \
                | awk '{ print $1 " " $5 " " $2 " " $4 }' | while read output
do
        usage=`echo $output | awk '{ print $2 }' | cut -d'%' -f1`

        if [ $usage -ge $ALERT_USAGE ]; then
                part=`echo $output | awk '{ print $1 }'`
                total=`echo $output | awk '{ print $3 }'`
                left=`echo $output | awk '{ print $4 }'`
                echo "$part usage $usage% (left $left out of $total)"
        fi
done

The scripts needs df, grep, awk and cut utilities present on your system, but these are already installed on most of the Linux systems so you should not have to install them manually.

The script has two configuration variables. ALERT_USAGE specifies usage percentage that if reached on single filesystem, it will trigger output of alert about the filesystem usage. The other configuration variable is DF_EXTRA_SWITCHES which you can use to finetune the output, like to ignore mounted network filesystems, output information about disk usage in more readable form, sync disk information before getting information from disk etc. You can see man page of df for all the possible switches.

So, with the default configuration, if you run the script and none of your filesystems has 90%+ usage, no output will be displayed. But if some of your filesystems will use 90% or more of its space, it will appear in the output. Then the output will look like this:

/dev/mapper/vg-var usage 96% (left 1.1G out of 25G)

The output says that device /dev/mapper/vg-var (which is LVM filesystem in this case) has 96% disk usage and there is 1.1GB of data left out of total 25GB which is the filesystem size.

You sure do not want to run the script manually, so put it in your cron configuration so it will be triggered automatically:

0 * * * * root bash /path/to/the/script.sh

This line will trigger the script each hour. Set it up so that it meets your needs. Also, make sure you have correctly set up your email address for cron and that your mailing system works on the box. As the script produces output only if there are any filesystems with usage over the treshold, you will receive emails only in these cases.

Here you can download the script: diskcheck-0.1.zip.