Bash Script To Send Email Alert for Disk Storage status

Disk Storage Status
Disk Storage Status

Send Email Alert for Disk Storage Status

Overview

In this article, I am going to share with you the bash script which helps you to send an email alert for the disk storage status. This script will keep alerting you when your disk reaches a threshold level or value.

Condition use:

  • If threshold value 70 then send an email alert

This is a completely automated script that will send you an email notification when the above conditions are true.

Make sure you have a mail server configured on your server. if not then follow the below link which helps you to configure a mail server.

Read: Steps To Install Postfix Server on Linux
#!/bin/bash

##Global vars
email_ids='[email protected]'
threshold=70 ##Use Threshold value as per requirement
MAIL=$(which mail)
subject="Disk status $( hostname )"
temp_file=/tmp/disk_status

##Function      : check_disk_space
##Description   : it will check all partitions and it will return # non-zero exist status if use >= threshold

check_disk_space(){
awk -v threshold=$threshold 'BEGIN{
status = 0
cmd = "LC_ALL=C df -Ph"
while( cmd | getline ) {
used=$5
if ( $1 != "Filesystem" && int(used) >= threshold ) {
printf "CRITICAL : Disk space alert (%s) :: [ Partition %s | mount point %s ]n",
used,$1, $NF > "/dev/stderr"
status = 1
}
}
exit status
}'
}

check_disk_space > $temp_file 2>&1

if [[ -s $temp_file ]]
then
$MAIL -s $subject $email_ids < $temp_file
fi

Change Script File Permission

Once you save the script, provide executable permission to run the script.

$ chmod +x check_disk_status.sh

Schedule Cronjob

Now setup Cron job which will be executed every 10 minutes. By using Cron you will be able to monitor Disk space every ten minutes. you can set Cron according to your requirement.

vim /etc/crod.d/script

*/10 * * * * root /bin/bash /opt/scripts/check_disk_status.sh

Save and close the file.

This is a well-tested script and provides you data without using more memory on the server.

You can modify the above script according to your requirement. If you still face any issues to setup this script then comments down below in comment box. Stay connected & subscribe with Linuxgrow.com for more such articles.

Prashant

Welcome to Linuxgrow. I'm Prashant, a tech-blogger from Mumbai, India. I started Linuxgrow as a passion and to share my knowledge about technologies. Here at Linuxgrow, I write about Linux technologies, Aws Cloud, Wordpress blogging and scripting knowledge. You can read more about me at About us page. Thank You :)

You may also like...

Leave a Reply

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

Facebook