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

DF=`df -h|awk '/vda1/ {gsub("%",""); print $5}'`

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

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.