How to Send an Email Alert When Your Disk Space Gets Low

Server monitoring should always be in place especially when you are handling a lot of servers, there are few opensource monitoring application like Nagios, Cacti and many more. You can create your own script for monitoring, such as for disk usage and all. Once they have reached over the threshold you have set they will automatically send you an email alert.

A sample script below will send you an alert once the root partition is almost full:

#!/bin/bash
#
# Created by Burnz Barbosa
# This script should send an email if the disk usage on root partition reached up more than 85%
#
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=85

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

Explanation:

  • It will run a df on root partition (/)
  • It will print the output on the fifth column/lines but will delete the “%” character which will be called the CURRENT usage
  • In the condition if the CURRENT usage is greater than the threshold which is 85 it will send an email to burnz@thesysad.com with a Subject “Disk Space Alert” and message as Your root partition remaining free space is critically low. Used: $CURRENT%

If you don’t have monitoring server you can put this script in all of your servers and run them daily using a cron jobs.

The file is downloadable at this link.

Spread the love

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.