Tech Tip: Send an Email Alert When Your Disk Space Gets Low
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
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- I once had a better way I
2 hours 45 min ago - Not only you I too assumed
3 hours 2 min ago - another very interesting
4 hours 55 min ago - Reply to comment | Linux Journal
6 hours 49 min ago - Reply to comment | Linux Journal
13 hours 43 min ago - Reply to comment | Linux Journal
13 hours 59 min ago - Favorite (and easily brute-forced) pw's
15 hours 50 min ago - Have you tried Boxen? It's a
21 hours 42 min ago - seo services in india
1 day 2 hours ago - For KDE install kio-mtp
1 day 2 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
When i execute the script i get ./disk.sh: Command not found
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
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
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 doneIs it possible to set the
Is it possible to set the execution of the script at a specific hour of the day?
Thanx
Yes, see these
Check these: cron video and cron article.
Mitch Frazier is an Associate Editor for Linux Journal.
Fixed
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
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.comHere you do not need grep and sed
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.comThis 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
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
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:
If you have long device
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.