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. All disk partition utilization monitoring can be done using below shell script.

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
#Purpose: Monitoring Disk Space Utilization and Send Email Alert
#Version:1.0
#Created Date: Wed Jun 6 22:38:01 IST 2018
#Modified Date:
#WebSite: https://arkit.co.in
#Author: Ankam Ravi Kumar
# START #
THRESHOULD=40
mailto="root"
HOSTNAME=$(hostname)

for path in `/bin/df -h | grep -vE 'Filesystem|tmpfs' | awk '{print $5}' |sed 's/%//g'`
do
 if [ $path -ge $THRESHOULD ]; then
 df -h | grep $path% >> /tmp/temp
 fi
done

VALUE=`cat /tmp/temp | wc -l`
 if [ $VALUE -ge 1 ]; then
 mail -s "$HOSTNAME Disk Usage is Critical" $mailto < /tmp/temp
 fi

rm -rf /tmp/temp
echo "Thanks for Using Script, Please Visit http://www.arkit.co.in and write your feedback"
## 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. Whenever Disk space utilization more than specified threshold it will send an email alert.

Your Feedback is valuable to us. Please provide your valuable comments…

Disk Space Utilization Monitoring and send email alert

How to Increase XFS File System size

CPU Usage Monitoring

Video Tutorial

Thanks for your wonderful Support and Encouragement