Shell Script to Monitor Disk Usage



Working as the TechGuy administrator I needed to create a shell script that would monitor disk usage. Doing this required several tweaks and learning the nuances of several shell commands. Below is the end result of this work. The script uses a combination of awk, egrep, cat, cut and mail commands.



The script is written to use a temporary file, temp.txt to store the messages when disk usage is above the limit I have set to monitor with the variable $error.


When the script first begins it sets the variables for the user to email and the error level, as well as deleting the temporary file if it exists. You can add mutiple recipients to the email users with something like:

emailUser="user1@email.com,user2@email.com"

Notice that the variables that are going contain numeric values have been set as integers using the typeset command.



Rather than use the df command to get the device names I've employed the use of the mount command, which with no parameters given will dump all mounted devices. I've then piped output of this command to egrep to make sure I only get the devices which are disk drives. The for loop then does a df on each device, cutting the output from characters 40 thru 42 to give the percentage, typeset as an integer. If that value is greater than the $error level previously set a messsage is appended to the temporary file.


At the end of the script, if the temporary file exists a mail message is composed and sent to the defined users in $emailUser. You'll notice that I made use of the $HOSTNAME variable in the subject of the email to make the script more portable. This script is used on 4 servers at TechGuy.


Hopefully you will find the script useful and it will save you time.

#!/bin/bash
#       This script determines the percentage of disk usage.
#       If that percentage is greater than 90% a user is emailed
#       a report notifying them of the usage.
#
#       Copyright 2006 Daniel McCarthy
# BE SURE TO CHANGE THIS TO YOUR EMAIL ADDRESS ;) 
emailUser="dan@linuxphile.org"
typeset -i error="90"
if [ -e temp.txt ]; then
  rm temp.txt
fi
for disc in `mount| egrep '^/dev' | egrep -iv 'cdrom|proc|sys|pts' |awk '{print $3}'`
do
  typeset -i discUsage=`df -h $disc|cut -c40-42|grep -i [^a-z]`
  if [ "$discUsage" -ge "$error" ]; then
    echo "Disc usage for $disc is at $discUsage%" >> temp.txt
  fi
done
if [ -e temp.txt ]; then
  message=`cat temp.txt`
fi
if [ ${#message} -gt 0 ]; then
  cat temp.txt | mail  -s "Disc Usage Report for: $HOSTNAME" $emailUser
fi

Comments

shadowman:

Hi,

Liked the script and I have some other ideas around improving it.

It sounds like this script would be scheduled on the system it is monitoring and would possible benefit from a logic adjustement. I say this because at the moment if scheduled, then if the disk usage stay's above 90% you will recieve a message all the time. It could be benificial to add a state file that keeps a record of how many times it has executed and set a theshold of say 3 times and then if it runs again to not notify the administrator. When it then goes below the threshold of 90%, then the state file is removed.

Kind regards
shadowman

linuxphile:

James,

Thanks for the tips I'll work on incorporating those ideas. I've also received feedback from an instructor at UMUC to make the temp file name I'm using unique so as to not delete/use a file that already exist on the system.

The way I'm using this script now is a scheduled cron job with the schedule being every night at midnight. For the systems I'm currently monitoring I want to continue to receive the emails every night if the system maintains disk usage above 90%, however, as I said I will definitely work to incorporate your ideas.

Thanks for the feedback!

Daniel McCarthy
A lust for Linux.