Tag Archives: disk space email

Email alert for low disk space v2

A while back I wrote a post about a simple low disk space script that will email you if a specific partition on your system has free space lower than a percentage you specified. Now I improved that script, adding some more features:

– excluding some partitions (be careful here not too exclude to much)

– sending email to many recipients

– check if emails were already sent, then it would spam the users with emails

The script is below:

#!/bin/sh 
EXCLUDE_PARTITIONS="partition1\|partition2" 
WARNING=85 
RECIPIENTS="user1@domain.com user2@domain.com" 
DF=( `df -h | grep % | tail -n +2 | awk '{print $(NF-2)" "$(NF-1)" "$(NF)}' | sed 's/%//g' | sed 's#/\(.*\)#/\1 slash-\1#g' ` ) 
for ((i=1;i<${#DF[@]};i=i+4)) 
do 
  if [[ ${DF[i]} -gt $WARNING ]] 
  then 
    if [ ! -f /tmp/disk-warning-for-${DF[i+2]} ] 
    then 
      touch /tmp/disk-warning-for-${DF[i+2]} 
      echo "" | mail -s "SH Alert: Critical: `hostname` - ${DF[i+1]} has only ${DF[i]}% (${DF[i-1]}) free space" $RECIPIENTS 
    fi 
  else 
    if [ -f /tmp/disk-warning-for-${DF[i+2]} ] 
    then 
      echo "" | mail -s "SH Alert: Clear: `hostname` - ${DF[i+1]} has only ${DF[i]}% (${DF[i-1]}) free space" $RECIPIENTS 
      rm /tmp/disk-warning-for-${DF[i+2]} 
    fi 
  fi 
done

As in the previous post, you need to make the script executable:

chmod +x /path/to/script

And add it to the cronjob using “cronjob -e”:

# Minute   Hour   Day of Month       Month          Day of Week        Command
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)
*/10 * * * * /path/to/script

Note: This was tested on Oracle Linux 5/6 and is working great. For other distro’s you might need to change the email syntax, the order of the df columns, etc.