Tech Tip: Send an Email Alert When Your Disk Space Gets Low

October 13th, 2009 by Mallik Arjun in

Your rating: None Average: 4.5 (8 votes)

If you don't want to step up to a full monitoring solution such as Nagios you can create your own scripts for monitoring the things that you want to monitor, such as disk space. The following script alerts you when your root partition is almost full:

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

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

The script sends an email when the disk usage rises above the percentage specified by the THRESHOLD varialbe (90% here).

To run it daily, for example, save the script to the file sample.sh in your home directory, change the email to your email, and add the following line at the end of /etc/crontab file:

@daily ~/sample.sh
__________________________


Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Anonymous's picture

There are some similar scripts available

On October 28th, 2009 Anonymous (not verified) says:

It's a very basic topic for linux administration. There are some similar scripts available at Admon.org, this site is dedicated for system monitoring and administration.

T-One's picture

There are some problems with

On October 22nd, 2009 T-One (not verified) says:

There are some problems with filesystems like cdrom drives, proc and stuff like that, so it would be better to filter that stuff, thats the script i use:

ADMIN="yourmail@example.com"
# set alert-level 90 % standard
ALERT=10
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $6 }' | while read output;
do
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge $ALERT ]; then
    echo "space low on \"$partition ($usep%)\", on server $(hostname) at $(date)" |
     mail -s "Alert: Free space low, $usep % used on $partition" $ADMIN
  fi
done


if you use long devicenames you have to use df -p instead of df -h  
Supernoob's picture

Is it possible to set the

On October 21st, 2009 Supernoob (not verified) says:

Is it possible to set the execution of the script at a specific hour of the day?

Thanx

Mitch Frazier's picture

Yes, see these

On October 21st, 2009 Mitch Frazier says:

Check these: cron video and cron article.

__________________________

Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.

CyberCowboy's picture

Fixed

On October 14th, 2009 CyberCowboy (not verified) says:

on Ubuntu Karmic the 2nd line needs to read like this

CURRENT=$(df / | grep / | awk '{ print $4}' | sed 's/%//g')

Note the change from 5 to 4 in the awk portion of the line.

David Tangye's picture

Perhaps check all

On October 13th, 2009 David Tangye (not verified) says:

Perhaps check all automounted filesystems ?

for vFS in `awk '!/^#/{if ($6 > 0) print $2}' /etc/fstab`
 do df $vFS  | \
 awk -v THRESHOLD=90 '
  !/^Filesystem/{
   VUSE=substr($5,1,length($5)-1)
   if (VUSE >= THRESHOLD) {print VUSE " " $6}}'
 done | \
mail -e -s 'Disk Space Alert' mailid@domainname.com
Andreas Schamanek's picture

Here you do not need grep and sed

On October 13th, 2009 Andreas Schamanek (not verified) says:

If you use _awk_ you can make it also do what _grep_ and _sed_ do. An easy way is

CURRENT=$(df / | awk '/\// { print strtonum($5)}')

More correct would be

CURRENT=$(df / | awk '/\// { sub(/%$/,"",$5); print $5}')

But even cooler is the combination of df, awk and mail to get an alert for any file system above THRESHOLD:

df | awk "{ df=strtonum(\$5); if (df > $THRESHOLD) print; }" \
mail -e -s 'Disk Space Alert' mailid@domainname.com

This basically all that is needed. _mail -e_ does not send a mail if the body is empty, i.e. when no file system is above THRESHOLD.

HTH.

Anonymous's picture

total noob question

On October 13th, 2009 Anonymous (not verified) says:

That's great, but how do I set things up so I can email from the command line?

lefty.crupps's picture

> set things up so I can email from the command line

On October 14th, 2009 lefty.crupps (not verified) says:

You need to install a package which contains the 'mail' executable ('mail' program). On most systems that I've dealt with, this 'mail' command comes with the base install or comes as a part of an email transport system, such as Exim4 or Sendmail.

Try running this; the [Ctrl][D] key-combo tells the app when you're done composing:

shell$  mail -s "Subject here, hello there!" user@domain.com [enter]
whoa, ok i am sending an email! [enter]

[Ctrl][D]

Cc: user2@otherdomain.com [enter]

shell$
Anonymous's picture

If you have long device

On October 13th, 2009 Anonymous (not verified) says:

If you have long device names like:

/dev/mapper/VolGroup00-LogVol00

you have to use:

df -P (for POSIX output)

to ensure one line per device output.
Otherwise your example does not work in every situation.

Post new comment

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Tech Tip Videos

From the Magazine

December 2009, #188

If last month's Infrastrucuture issue was too "big" for you then try on this month's Embedded issue. Find out how to use Player for programming mobile robots, build a humidity controller for your root cellar, find out how to reduce the boot time of your embedded system, and if you're new to embedded systems find out the basics that go into one. You can also read about the Beagle Board, the Mesh Potato and a spate of other interestingly named items. And along with our regular columns don't miss our new monthly column: Economy Size Geek.


Read this issue