How to receive an email alert for disk usage on linux server

Certain actions on your online application need ample free space on disk to complete. For example, when you want to install new software. Even when you want to run updates on your server your server needs free memory and space to run those updates. There are many more.

On the other hand your online application may stop functioning properly if your Disk Space gets low. You may not be able to upload new files, may not send large attachments with emails, may not install new websites or even your apps goes down permanently until you get them up again.

Having said that, the low disk space may result in unexpected errors and  failures on your website and sometime it may leave your website non-usable.

In order to avoid this kind of situation the most ideal thing would be to receive an email in case of your system reaches a set disk space usage threshold. For example receive an email alert if your disk space reaches 90% usage threshold.

Let’s see it in practice and do some action now!

View Disk Usage

You can view disk usage using df command with -h option..

sudo df -h

It would display Used and Available space on different partitions in human readable format.

Set an email alert for disk usage on linux server

In example above my main disk partition is /dev/xvdal which has 40GB used and 11GB available space. The current percent usage is 80%.

Related: 10 Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories

There are various methods to free your disk space on your server which including deleting of unused large file, archived files, temp files and cache. If you want to view top files and directories check my another simple tutorial. If you want to free up space you can follow simple instructions I have put in this post.

Related: How to Free Up Disk Space on Ubuntu and Linux Mint

Getting Disk Usage Alert

Let’s get back to actual task. Since it is a manual task to review disk space usage using above method it would be ideal to receive an automated email alert when disk usage is high and available disk space is low.

For example, it would be handy to receive an email when your Disk Space reaches certain threshold. It would allow the server admin to take appropriate action in time.

Create Shell Script

In order to achieve this we will place a shell script on our server which periodically calculates current Disk Usage and sends email alert if the disk Space threshold has been reached or surpassed.

Create a diskusagealert.sh file on your server. In this example I created this script in /var/scripts/ folder.

sudo nano /var/scripts/diskusagealert.sh

Paste the following code in it

#!/bin/bash
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=90
TO_EMAIL=xxxxxx@xxxxxxx.com
CC_EMAIL=xxxxxx@xxxxxxx.com
SOMEINFO=xxxxxx

if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
mail -s 'MyServer: Disk Space Critically Low' ${TO_EMAIL} ${CC_EMAIL} << EOF
Hi xxxxx,

Your root partition remaining free space is critically low.

Disk Space Used: $CURRENT%

Server: xxxxxxx
Additional Info: ${SOMEINFO}
EOF
fi

Set executable permissions

sudo chmod +x diskusagealert.sh

Test it

Set threshold THRESHOLD=90 to something larger than your current disk usage and you should receive an email with the detailed defined in the file above. Once tested adjust threshold value to something desirable. Then run:

sudo sh diskusagealert.sh

If all good, you should receive an email with information defined in your diskusagealert.sh file.

Adding a Cronjob

Once you have created a shell script with executable permissions and tested it OK you can set up a cron job to monitor disk usage and receive email alerts.

Edit Cron File

crontab -e

Paste the following to the end of crontab -e file. Cron will run this script once in an hour.

0 * * * * sh /var/scripts/diskusagealert.sh > /dev/null 2>&1

(Hint: You can set time interval which suits you)

Restart Cron service

In Redhat/Fedora/Centos

sudo service crond restart

In Debian/Ubutnu

sudo /etc/init.d/cron restart

I hope this tutorial to learn setting email alert for disk usage help you with your work.

Leave a Reply