Saturday, May 7, 2016

Shell Script : File System Usage Monitoring

File System Usage Monitoring Shell Script

File System Usage Monitoring Shell Script . This script is written using df command and it will send an email alert when your file system usage exceeds mentioned threshold.
Environment: Script will work in all the Unix / Linux environments. Tested in RHEL 5/6/7 and Centos All Versions
Warranty: This shell script does not have WARRANTY. Comes with an no warranty. Execution of this script in any of your environments, we are not responsible for any data loss.
Note: Please test the script before you deploy to the production Environment.
Prerequisites: Before executing the script please verify below commands are working and mail relay agent should work.
# df -hP
# awk
# mail -s "Testing Email" YOUREMAIL-ADDRESS

File System Usage Monitoring Shell Script


#!/bin/bash
################################################### File system monitoring 
###################################################
# This Script will send an Alert when Disk exceeds mentioned threshold.
# Author: Ravi Kumar
# Date: 26th Dec 2015
MAILADDRESS="aravikumar48@gmail.com"
# set usage alert VALUE
VALUE=90
# Hostname
HOSTNAME=$(hostname)
# Mail client
MAIL=/bin/mail
EMAIL=""
for line in $(df -hP | egrep '^/dev/' | awk '{ print $6 "_:_" $5 }')
do
part=$(echo "$line" | awk -F"_:_" '{ print $1 }')
part_usage=$(echo "$line" | awk -F"_:_" '{ print $2 }' | cut -d'%' -f1 )
if [ $part_usage -ge $VALUE -a -z "$EMAIL" ];
then
EMAIL="$HOSTNAME - $(date): Below File System is exceeding VALUE\n"
EMAIL="$EMAIL\n$part ($part_usage%) >= (VALUE = $VALUE%)"
elif [ $part_usage -ge $VALUE ];
then
EMAIL="$EMAIL\n$part ($part_usage%) >= (VALUE = $VALUE%)"
EMAIL="$EMAIL\n"
fi
done
if [ -n "$EMAIL" ];
then
echo -e "$EMAIL" | $MAIL -s "File System Usage Alert "$HOSTNAME" Need your Attention!" "$MAILADDRESS"
fi
## END Script ###
copy the above shell script code and do as mentioned below steps
#touch DfAlert.sh
#vi DfAlert.sh
Paste the shell script 
:wq <<-- Save & Exit
Set the execution permissions
#chmod +x DfAlert.sh
Execute the script as mentioned below
#sh DfAlert.sh
If your script execution is successful then schedule to execute the shell script every 5 minutes OR every 30 minutes.
Edit the crontab entries
#crontab -e
# File System Usage Monitoring Using Below Shell Script
*/5 * * * * sh /SCRIPT-PATH/DfAlert.sh
:wq   << -- Save & Exit
Verify the crontab entry
# crontab -l
That’s about this script. Script is successfully scheduled in your environment.

No comments:

Post a Comment