Thursday, 4 June 2015

Monitor your disks/filesystems

Here is the script which will monitor the disk space and will generate alert once the file system usage has crossed WARING or CRITICAL threshold. 

I have re-directed the alert to the local domain and it can be configured to your inbox. 

script is self explanatory and can be found here :

#!/bin/bash
#this will monitor the disk uaage of the partitions and would generate an e-mail to the concerned. alert had been set to below 90 as WARNING and ABOVE 95 as CRITICAL

#Change your domain name according to your domain's & email's to the user account
ADMIN=root
WARNING=90
CRITICAL=95

#sda1 is the boot partition and /dev/sda2 being swap parition which I want to exclude
#you can also add other partitions if you don't want to monitor
EXCLHDD=/dev/sda1 /dev/sda2

function disk_monitoring()
{
while read output
do
#echo $output
usedpar=$(echo $output | awk '{print $1}' | cut -d'%' -f1)
tpar=$(echo $output | awk '{print $2}')
if ([ $usedpar -eq $WARNING ] && [ $usedpar -lt $CRITICAL ])
then
echo "WARNING|FILESYSTEM - $tpar|$usedpar%|$HOSTNAME" | \
mail -s "WARNING|ALERT|Housekeep the FileSystem" $ADMIN@
elif [ $usedpar -ge $CRITICAL ]
then
echo "CRITICAL|FILESYSTEM - $tpar|$usedpar%|$HOSTNAME" | \
mail -s "CRITICAL|ALERT|Filesystem running out of space" $ADMIN
fi
done
}
if [ "$EXCLHDD" != "" ]
then
df -H | grep -vE "^Filesystem|tmpfs|$EXCLHDD" | awk '{print $5 " " $6}' | disk_monitoring
else
df -H | grep -vE "^Filesystem|tmpfs"| awk '{print $5 " " $6}' | disk_monitoring
fi