Get Email Alert on low Disk Space

Get Email Alert on low Disk Space

Here is a small script that will send you an email when the disk usage rises above the percentage specified by the THRESHOLD variable (90% in the example bellow).

If you don’t want to step up to a full monitoring solution such as Nagios or Obkio 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' pejcha1994@gmail.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 variable (90% here).

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

 @daily /opt/alert.sh  

and for older Linux distros, here is a script that will send you an email when disk use in selected partition is bigger than 90%:

#!/bin/bash
MAX=90
EMAIL=stefan@example.com
PART=sda1

USE=`df -h |grep $PART | awk '{ print $5 }' | cut -d'%' -f1`
if [ $USE -gt $MAX ]; then
  echo "Percent used: $USE" | mail -s "Running out of disk space" $EMAIL
fi
Written by
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.