If you would like to monitor [disk] space usage and receive emails when a certain threshold is passed, you can refer to this excellent example from Linix.com.

Let’s begin with creating an empty shell script file:

$ nano monitor_space_usage.sh

Copy and paste the contents of this self explanatory script:

#!/bin/bash

LIMIT='80'
#Here we declare variable LIMIT with max of used spave

DIR='/var'
#Here we declare variable DIR with name of directory

MAILTO='monitor@gmail.com'
#Here we declare variable MAILTO with email address

SUBJECT="$DIR disk usage"
#Here we declare variable SUBJECT with subject of email

MAILX='mailx'
#Here we declare variable MAILX with mailx command that will send email

which $MAILX > /dev/null 2>&1
#Here we check if mailx command exist

if ! [ $? -eq 0 ]
#We check exit status of previous command if exit status not 0 this mean that mailx is not installed on system
then
          echo "Please install $MAILX"
#Here we warn user that mailx not installed
          exit 1
#Here we will exit from script
fi

cd $DIR
#To check real used size, we need to navigate to folder

USED=`df . | awk '{print $5}' | sed -ne 2p | cut -d"%" -f1`    
#This line will get used space of partition where we currently, this will use df command, and get used space in %, and after cut % from value.

if [ $USED -gt $LIMIT ]
#If used space is bigger than LIMIT

then
      du -sh ${DIR}/* | $MAILX -s "$SUBJECT" "$MAILTO"
#This will print space usage by each directory inside directory $DIR, and after MAILX will send email with SUBJECT to MAILTO
fi

After making sure that you have modified it to match your needs (and set your e-mail address as the recipient by modifying MAILTO variable), you can save it by pressing “CTRL+X” and confirming with “Y”.

Set again the file as executable and you have your second Linux system monitoring tool ready.

To give the file execution permission, run the following:

$ chmod +x monitor_space_usage.sh