Disk space check and email warning if over threshold

This is pretty simple, but it’s one of those things I find myself needing every so often and not being able to remember how I did it the last time.

A script to check how full a disk is and send you a mail if it is over a certain threshold – ideal as a nightly cron task for example.

Assuming vda1 is the name of the disk in this case, and 85% is the threshold in question.

#!/bin/bash
THRESH=80

for disk in disk1 disk2; do
    DF=$(df -h|awk '/'$disk'/ {gsub("%",""); print $5}')

    if [[ $DF > $THRESH ]]; then
        echo "Used space: $DF%"|mail -s "Disk $disk getting full" email.address@example.com
    fi
done

Yes, it could probably be improved. For example the -h option to df is unnecessary as we’re only using the value from the percentage column, but I tend to add it by force of habit.