7

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

4 Answers 4

7

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 :-)

4
  • Option 2 is definitely the better solution. We are mostly using Option 1 now, but if something fills /var too fast, you won't get any email, because it can't spool it. You may also have issues if the volume the script runs out of space, or if it writes to /tmp and it runs out of space. It is still a quick solution that works most of the time and is certainly a lot better than nothing.
    – Alex
    Jul 6, 2010 at 17:00
  • Option 1 is great for the short term, but often results in a ton of kruft when you adapt this script to 5 functions on 10 servers. Option 2 is great for the long term, and is simpler to adapt to many server for many different checks. Jul 7, 2010 at 22:40
  • Option 1 isn't really intended as a viable a solution (it was offered only for completeness in terms of a quick hack if you're in a "this server keeps filling up, we don't know why, and we need to monitor it YESTERDAY" situation) -- That said, lots of places use it :-)
    – voretaq7
    Jul 8, 2010 at 16:00
  • To be more specific about Option 2, I'd recommend monit. It's the easiest monitoring tool to configure for that purpose. It can do more than that as well, and is simpler to set up IMO than something like Nagios, which is for more complicated scenarios.
    – Dan
    Aug 10, 2011 at 10:40
3

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

1
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.

0

+1 for option 2 above. You may think you only need to monitor disk space right now, but the reality is you almost certainly need monitoring for more than one or two servers. It will benefit you in ways that will amaze you long term.

I'd personally recommend Zenoss, it's F/LOSS, relatively easy to setup and get going, and they have great documentation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .