Email when Linux server disk is full?

How can I set up an email notification on a Linux server for when a disk/volume exceeds a certain usage quota?


Solution 1:

Option 1:
Write a script that runs df, parses the output for the percent utilization & sends an email when it exceeds a given threshold, then run this script from cron.
(If you're feeling lazy you can find a bunch of pre-written scripts by asking The Knower of All Things for Unix Disk space check script.)


Option 2 (The better solution):
Deploy a monitoring system (Nagios, InterMapper, OpenNMS, etc. -- look around here for lots of suggestions & opinions), and configure it to send you a notification when your disks are filling up. While you're at it configure alerts for other stuff you might be concerned about :-)

Solution 2:

One point for solution 2 too ! I recommend you the "Monit" software that is very light and easy to configure : http://mmonit.com/monit/

Solution 3:

For people who do not have a monitoring system, this simple script can do the job :

#!/bin/bash
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=90

if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
    mail -s 'Disk Space Alert' [email protected] << EOF
Your root partition remaining free space is critically low. Used: $CURRENT%
EOF
fi

Then just add a cron job.