Linux bash script for checking and sending email alerts on disk space issues

#!/bin/sh
FILE=/tmp/mailcontent.txt

> /tmp/mailcontent.txt

df -H | awk '{ print $5 " " $6 }'|head -6|tail -5|while read output;

do
#  echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
echo $usep
  if [ $usep -ge 90 ]; then
   echo -e "Running out of space \n\nPartition:\"$partition ($usep%)\" \nHost:$(hostname) \nDate:$(date)\n" >> /tmp/mailcontent.txt

 fi

done

if [[ -s $FILE ]] ; then

mail -s "CRITICAL ALERT:Disk space of $hostname more that 90%" disk-alert@yourcompany.com
else
echo "$FILE is empty."
fi

exit

3 comments:

Purandaran V said...

Hi

the below commands not working on my system

df -H | awk '{ print $5 " " $6 }'|head -6|tail -5|while read output;

I am getting empty string from output variable

Please explain...

Naresh Surampudi said...

While executing commands from scripts

df -H | awk '{ print $5 " " $6 }'|head -6|tail -5|while read output; we have to remove pipes and check

Try this and it will work

df -H | awk '{ print $5 " " $6 }'|head -6|tail -5

Himanshu Negi said...

Don't know much about linux and shell programming but like you work on this blog!

Wish to get a partner like you on my blog :)