CentOSDebianFedoraLinuxOperating SystemsRedHatUbuntu

Shell script to monitor disk space and send email

The df command in Linux is used to check the disk space available on all the mounted partitions. To keep the server healthy and continue running the applications, you might have to keep a certain percentage of mounted partitions free.

This can be achieved by creating a disk space monitoring script to check the disk space usage and send alerts when disk space usage is critical. Please note that this script should be set as a cronjob to check the disk usage automatically.

#Purpose: Script to check disk space usage and send alert on critical usage.
#Created by: Abhijit Sandhan
#####################################################
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $6 }' | while read output;
do
  #echo $output
  usage=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usage -ge 90 ]; then
    echo "Running out of space \"$partition ($usage%)\" on $(hostname) as on $(date), please check!" |
     mail -s "Alert: partition \"$partition\" almost out of disk space $usage%" email@example.com
  fi
done

In the above disk space usage script, you should modify the partitions and email address to send email alerts.
The disk space critical usage value is set to 90% to send alerts. You may modify the usage value as per the requirement.

Hope this helps!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top button