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

 in

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
______________________

Comments

Comment viewing options

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

When i execute the script i get ./disk.sh: Command not found

Anonymous's picture

Hello,

I have created the script and put it in my home directory , When i excute the script i get the following error

./diskmonitor.sh: Command not found.

Am not sure what's causing this

Code:

#!/bin/bash
ADMIN="myemail@domain.com"
# set alert-level 90 % standard
ALERT=10
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $6 }' | while r
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

There are some similar scripts available

Anonymous's picture

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.

There are some problems with

T-One's picture

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

Is it possible to set the

Supernoob's picture

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

Thanx

Yes, see these

Mitch Frazier's picture

Check these: cron video and cron article.

Mitch Frazier is an Associate Editor for Linux Journal.

Fixed

CyberCowboy's picture

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.

Perhaps check all

David Tangye's picture

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

Here you do not need grep and sed

Andreas Schamanek's picture

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.

total noob question

Anonymous's picture

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

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

lefty.crupps's picture

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$

If you have long device

Anonymous's picture

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.

Geek Guide
The DevOps Toolbox

Tools and Technologies for Scale and Reliability
by Linux Journal Editor Bill Childers

Get your free copy today

Sponsored by IBM

Webcast
8 Signs You're Beyond Cron

Scheduling Crontabs With an Enterprise Scheduler
On Demand
Moderated by Linux Journal Contributor Mike Diehl

Sign up now

Sponsored by Skybot