Category Archives: 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.

Send email from command line

If you need to send an email through a smtp server (and you are allowed to do that) you can use the following lines after you telnet to that server:

helo mail
mail from: sender@example.com
rcpt to: recipient@example.com
data
From: "Sender" <sender@example.com>
To: "Recipient" <recipient@example.com>
Subject: Here you type your subject
Here you type your body.
.