Simple/hacky disk usage monitoring script with Bash and mail

If you do not know bash (most used Unix shell), check out the TLDP guide for Bash, i find it really good:
http://www.tldp.org/LDP/abs/html/

The df command is a command to get a report of file system disk space usage. We will base our script on this commands output.

I use the -h flag to get the sizes in G. Not used in this post as i only use the percentage.

So run the df command to see the disk usage on your partitions:
$ df -h

Filesystem            Size  Used Avail Use% Mounted on
/dev/simfs             20G  4.7G   16G  24% /
...

Choose the partition your want to monitor and grep that value, and print out 5th row with awk:
$ df -h | grep '/dev/simfs' | awk {'print $5'}

This will return the percentage used on the chosen partition (in my case "/dev/simfs"):
24%

Now we need to create a script, which sends me an e-mail, if the use in percentage is greater than 90%.

Check if you have the standard mail software installed on your machine. Check this by running $ mail or send a test mail with $ echo "Mail body" | mail -s "Subject" "yourown@mail.tld"

If not install:
$ sudo apt-get install mailx.

I just wrote this small script (check_disk.sh), which will use the df and mail commands, to check the disk usage and send mail if it is over 90% (defined in the variable as max_usage). I will maybe comment the code later.

Version 1:

#!/bin/bash

current_usage=$( df -h | grep '/dev/simfs' | awk {'print $5'} )
max_usage=90%

if [ ${current_usage%?} -ge ${max_usage%?} ]; then
    mailbody="Max usage exceeded. Your disk usage is at ${current_usage}."
    echo "Sending mail..."
    echo ${mailbody} | mail -s "Disk alert!" "yourown@mail.tld"
elif [ ${current_usage%?} -lt ${max_usage%?} ]; then
    echo "No problems. Disk usage at ${current_usage}." > /dev/null
fi

Version 2 (an optimized, commented version, i wrote the day after i wrote the original post):

#!/usr/bin/env bash

# vars #
disk="/dev/simfs" # disk to monitor
current_usage=$(df -h | grep ${disk} | awk {'print $5'}) # get disk usage from monitored disk
max_usage="90%" # max 90% disk usage
mail="yourown@mail.tld" # mail to sent alert to

# functions #
function max_exceeded() {

    # tell that mail is being sent
    echo "Max usage (${max_usage}) exceeded. Your disk usage is it at ${current_usage}. Trying to send mail-alert..."

    # check if the mail program exist
    type mail > /dev/null 2>&1 || { 
        echo >&2 "Mail does not exist. Install it and run script again. Aborting script..."; exit; 
    }

    # if the mail program exist we continue to this and send the alert
    mailbody="Max usage (${max_usage}) exceeded. Your disk (${disk}) usage is at ${current_usage}."
    echo ${mailbody} | mail -s "Disk alert!" "${mail}"

    echo "Mail was sent to ${mail}"
}

function no_problems() {

    echo "No problems. Disk (${disk}) usage at ${current_usage}. Max is set to ${max_usage}."
}

function main() {

    # check if a valid disk is chosen
    if [ ${current_usage} ]; then
        # check if current disk usage is greater than or equal to max usage.
        if [ ${current_usage%?} -ge ${max_usage%?} ]; then 
            # if it is greater than or equal to max usage we call our max_exceeded function and send mail
            max_exceeded
        else
            # if it is ok we do nothing
            no_problems
        fi
    else
        # if the disk is not valid, print valid disks on system
        echo "Set a valid disk, and run script again. Your disks:"
        df -h
    fi
}

# init #
main

Run the script with $ bash check_disk.sh and you will get a mail sent if you have exceeded 90% disk space.

We would like to automate this, so it will run once per day. This is done with cronjobs. I will access my cronjobs by running:
$ crontab -e

Go down to the bottom after all the comments, and define when you want it to run, and point this to the path of the script, like this:
0 22 * * * /root/check_disk.sh > /dev/null

This will make it run once per day at 22:00 (10 pm) with no output, as i send to /dev/null. Read more about cron here: https://www.google.com/search?q=crontab

comments powered by Disqus