Setting up a Ubuntu Linux 12.04 LTS Disk Usage Email Alert

 

Computers and servers don’t work too well with full drives. In fact, it’s a pretty good way to bring down our Postfix email server. Here is a relatively simple script to email us when we get to 20% drive space remaining.

sudo mkdir \-p /root/scripts/ 
sudo nano /root/scripts/diskAlert

Paste this in

#!/bin/sh 
df \-H \| grep \-vE '^Filesystem\|none\|cdrom' \| awk '{ print $5 " " $1 }' \| while read output; 
do 
  echo $output
  usep=$(echo $output \| awk '{ print $1}' \| cut \-d'%' \-f1  ) 
  partition=$(echo $output \| awk '{ print $2 }' )
  if \[ $usep \-ge 80 \]; then 
    echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" \| 
     mail \-s "Alert: Almost out of disk space $usep%" youremail@yours.com 
   fi 
done

If you are getting a “-bash: /root/scripts/diskAlert: Permission denied” error then modify the rights on the script.

sudo chmod 0777 /root/scripts/diskAlert