Shell script to monitor disk usage

Linux Shell Script To Automate Disk Usage Monitoring

If you are running mission-critical applications on servers, it is important to monitor and notify the administrators about the disk usage.

Many open source monitoring tools do the monitoring and can be integrated with various notification services.

But let’s say you are running a single server and you don’t have a monitoring solution in place. In this case, it is better to write a script that automates the monitoring and sends reports to mail if it reaches the threshold.

Automate Disk Usage Monitoring

In this post, we have explained a shell script that monitors the disk usage at a regular interval you specify in the crontab and emails the report if it reaches the threshold you specify.

Let’s Implement It

First, you need to make sure that mail command is working on your server to get the notifications. You can do this test by executing the following command.

echo "This message will go into the body of the mail." | mail -s "Testing from the server." (email id here)

For example,

echo "This message will go into the body of the mail." | mail -s "Testing from the server." [email protected]

While executing the above command, you might get the following error.

-bash: mail: command not found

If you get the above error, install mail component using the following command and test the command again. Otherwise, proceed to next step.

RHEL/Centos

sudo yum install mailx

For Ubuntu/Debian

sudo apt-get install mailutils

Script to Test Disk Usage

Following is the script that tests the disk usage and sends a report over to the mail id you specify. The explanation for the script is given after the snippet.

#!/bin/bash

MAILID="[email protected]"

VALUE=80

SERVERNAME=$(hostname)

MAIL=/bin/mail

for line in $(df -hP | egrep '^/dev/' | awk '{ print $1 "_:_" $5 }')
  do
    FILESYSTEM=$(echo "$line" | awk -F"_:_" '{ print $1 }')
    DISK_USAGE=$(echo "$line" | awk -F"_:_" '{ print $2 }' | cut -d'%' -f1 )

    if [ $DISK_USAGE -ge $VALUE ];
    then
      EMAIL="$SERVERNAME - $(date): $FILESYSTEM Exceeded the threshold VALUE\n"
      EMAIL="$EMAIL\n Usage Details\n Current Usage:($DISK_USAGE%) Threshold value: ($VALUE%)"
      echo -e "$EMAIL" | $MAIL -s ""$SERVERNAME" Disk Usage Alert: Needs Attention!" "$MAILID"
    elif [ $DISK_USAGE -lt $VALUE ];
    then
      EMAIL="$EMAIL\n$FILESYSTEM ($DISK_USAGE%) is lessthan the threshold ($VALUE%)"
      EMAIL="$EMAIL\n"
      echo -e "$EMAIL" | $MAIL -s ""$SERVERNAME" Disk Usage Alert: Threshold Not Reached" "$MAILID"
  fi
done

Monitoring Script explanation

The above script gets the memory information using “df hP” command.

The environment variable MAILID holds the email id in which the report has to be sent.

VALUE holds the threshold you want to set.

SERVERNAME holds the hostname of the server for server identification in the email notification.

Using a for loop it will loop through all the filesystem which has “/dev” mount point. Then it gets the filesystem name and disk usage percentage to two variables FILESYSTEM and DISK_USAGE.

Using the above variables the disk usage is equated with the threshold VALUE variable. Based on the result, an email will be sent to the specified id.

Monitoring Script Execution

To execute the script, copy the script content on to a file named disk-monitor.sh.

Now, change the execution permission of the script using the following command.

chmod +x disk-monitor.sh

To run the script, using the following command.

 ./disk-monitor.sh

Automating The Monitoring Process

Running the script manually does not make any sense as we want to automate the process. The best way to automate the process is by adding the script to crontab entry.

You can open the crontab using the following command.

crontab -e

Add the following line to run the script every 5 minutes.

Note: Change the script path accordingly.

*/5 * * * * sh /home/ec2-user/disk-monitor.sh

Now you can list the crontab entry using the following command.

crontab -l
Shell script to monitor disk usage

Other Interesting Blogs

1 thought on “Linux Shell Script To Automate Disk Usage Monitoring”

  1. Here in the below line integer expecting by the system. What should I do here as already we have declared the VALUE as 80. Arrow marked for the lines.

    VALUE=80

    do
    FILESYSTEM=$(echo “$line” | awk -F”_:_” ‘{ print $1 }’)
    DISK_USAGE=$(echo “$line” | awk -F”_:_” ‘{ print $2 }’ | cut -d’%’ -f1 )

    –> if [ $DISK_USAGE -ge $VALUE ];
    then
    EMAIL=”$SERVERNAME – $(date): $FILESYSTEM Exceeded the threshold VALUE\n”
    EMAIL=”$EMAIL\n Usage Details\n Current Usage:($DISK_USAGE%) Threshold value: ($VALUE%)”
    echo -e “$EMAIL” | $MAIL -s “”$SERVERNAME” Disk Usage Alert: Needs Attention!” “$MAILID”
    –> elif [ $DISK_USAGE -lt $VALUE ];
    then
    EMAIL=”$EMAIL\n$FILESYSTEM ($DISK_USAGE%) is less than the threshold ($VALUE%)”
    EMAIL=”$EMAIL\n”
    echo -e “$EMAIL” | $MAIL -s “”$SERVERNAME” Disk Usage Alert: Threshold Not Reached” “$MAILID”
    fi
    done

    Reply

Leave a Comment

Share via
Copy link
Powered by Social Snap