Feb 14

Here is a simple bash script that I wrote to get a notification email whenever any of the server partition grows over the defined size. The size can be defined for MAXSIZE variable. You can then setup a cron to run this script after every few hours. You will not be bombarded with the notification emails as this script will send the notification email only when a partition is over the MAXSIZE.

#Written by WebScHoLaR

#!/bin/sh
loop=`df -h | sed “1 d” | wc -l`
MAXSIZE=95;
while [ 0 -lt "$loop" ]
do

usage=`df -h |sed “1 d” |sed -n “$loop s/%//p” | awk ‘{print $5}’`

if [ $usage -ge $MAXSIZE ]; then
headerborder=”———————————————-”;
header=”$headerborder\nHostname: `hostname`\nDate: `date +”%A, %B %d”`\nServer Time: `date +”%r”`\n$headerborder\n”;
print=”`df -h |sed “1 d” |sed -n ” $loop s/\///p” | awk ‘{print $6 ” is ” $5}’`\n$print”
echo -e $header > /tmp/df.out ;
echo -e $print >> /tmp/df.out ;
fi
loop=`expr $loop – 1`
done
mail -s “Disk Usage Alert: `hostname`” email@domain.com < /tmp/df.out ;
rm -rf /tmp/df.out ;

The output of this script will be like:

————————————————————
Hostname: server.hostname.com
Date: Thursday, February 14
Server Time: 06:01:02 AM
————————————————————

/var is 98%

Leave a Reply

You must be logged in to post a comment.