Loading
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
______________________


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.