Monitor UNIX / Linux Server Disk Space with Shell Script

by on April 10, 2008 · 36 comments

Shell script to monitor or watch the disk space and send an email alert if the (free avilable) percentage of space is >= 90%

#!/bin/sh
# Shell script to monitor or watch the disk space
# It will send an email to $ADMIN, if the (free avilable) percentage
# of space is >= 90%
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
# Linux shell script to watch disk space (should work on other UNIX oses )
# SEE URL: http://www.cyberciti.biz/tips/shell-script-to-watch-the-disk-space.html
# set admin email so that you can get email
ADMIN="me@somewher.com"
# set alert level 90% is default
ALERT=90
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
  #echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge $ALERT ]; then
    echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
     mail -s "Alert: Almost out of disk space $usep" $ADMIN
  fi
done
4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

{ 36 comments… read them below or add one }

21 erdenebat November 4, 2009

very good!! it’s my was finding script.

22 Bill March 11, 2010

Thanks for the LVM fixed, been searching about for that answer!

23 John Mathew March 24, 2010

Hi,

I have the following script;

#!/bin/ksh
ADMIN=”me@somewhere.com”
# set alert level 90% is default
ALERT=80
df | grep -E “Filesystem|tmpfs|cdrom” | awk ‘{ print $5 ” ” $1 }’ | while read output;
do
#echo $output
usep=$(echo $output | awk ‘{ print $1}’ | cut -d’%’ -f1 )
partition=$(echo $output | awk ‘{ print $2 }’ )
if [ $usep -ge $ALERT ]; then
echo “Running out of space \”$partition ($usep%)\” on $(hostname) as on $(date)” |
mail -s “Alert: Almost out of disk space $usep” $ADMIN
fi
done

While I run the script it gives the following error, please help me. I have named the file as x.ksh and also let me know how to run the script.
Thanks.

24 charles May 28, 2010

this script seems to run but I never get an email. I tested with a simple email from the server and it came through fine. Here’s the output I get when I debug the script with bash -x (I’m a linux newbie):

+ ADMIN=me@mydomain.org
+ ALERT=20
+ df -H
+ grep /
+ grep -vE ‘^Filesystem|tmpfs|cdrom’
+ awk ‘{ print $5 ” ” $1 }’
+ read output
++ awk ‘{ print $1}’
++ cut -d% -f1
++ echo 54% /dev/vzfs
+ usep=54
++ echo 54% /dev/vzfs
++ awk ‘{ print $2 }’
+ partition=/dev/vzfs
+ ‘[' 54 -ge 20 ']‘
++ hostname
+ mail -s ‘Alert: Almost out of disk space 54′ me@mydomain.org
++ date
+ echo ‘Running out of space “/dev/vzfs (54%)” on ip-72-XXX-XX-XYZ.ip.secureserver.net as on Fri May 28 12:28:55 PDT 2010′
+ read output

25 Charles May 28, 2010

Actually just found out it works fine. My email was down temporarily! Thanks!

26 UWAYO Jacques September 8, 2010

i get the below error what could be the problem

but i dont receive anything
#!/bin/bash
# script to send simple email
# email subject
SUBJECT=”SET-EMAIL-SUBJECT”
# Email To ?
EMAIL=”myemail@gmail.com”
# Email text/message
EMAILMESSAGE=”/tmp/tutorials/emailmessage.txt”
echo “This is an email message test”> $EMAILMESSAGE
echo “This is email text” >>$EMAILMESSAGE
# send an email using /bin/mail
/bin/mail -s “$SUBJECT” “$EMAIL” < $EMAILMESSAGE

27 Ben October 19, 2010

you should also use the -P option of df “df -P -H”
(-P for POSIX compatibility so you dont get into trouble when your diskname ist to long like /dev/mapper/VolGroup00-LogVol00)
also df -i (for inodes) is highly recommended to use, because you easily forget to check for inodes if you have free space but cant create a file =)

28 nicole January 17, 2011

Hi, i’m a newbie, and i’ve been asked to create a script that will monitor different directories in different systems e.g.:

/home of system1 – 60%
/models of system2 – 70%
/bikes of system3 – 80%

and any of the above meet their respective threshold, will send an email to me
informing that that particular directory is on limit.

fyi, i have a server than can ssh to all of this directories/systems.

hoping for your quick response.

29 Anonymous March 24, 2011

Thanks UWAYO.
-P option worked with the script. Nice and simple script..

30 Anon April 20, 2011

I have tried the script and it does not appear to work.

I set the limit to “35″, as I wanted to test it out and the highest percentage usage was 38%. I changed the E-Mail address to what I want to use and then ran the script. The script has not sent me an E-Mail after 10 minutes of running it.

I tried the “-P” that was mentioned in several comments without luck.

The server that this script is currently running on is a basic Ubuntu 9.04 server and has only had SSH installed.

31 Dinesh July 6, 2011

Hi,

I am new to scripting.
I have run this and working perfectly.
Just want 2 modifications.

If i want to check for more than one server using same script.
And i want single mail instead of many.

Please Help. :)

32 d0rk July 27, 2011

you cannot run this with sh … you need bash as it is a BASH script.

33 d0rk July 27, 2011

you are using the korn shell, which has many differences from bash. first off, to use parameter substitution in that shell you need:

echo -e "string with backslash \n"
34 Andre October 18, 2011

Hi,

Right now, I have setup a cronjob to run this script hourly.
Does anyone know how to stop the email to be sent out if the previous quota level is the same as the current one?

i.e.: An email is sent once it hits the 95% threshold; however at the next hour if the level is still at 95%, no email is sent till it hits 96% or more.

35 Andre October 19, 2011

Just answered my own question after a few days of figuring out bash regex. Are there any other more elegant ways?

# set alert level 95% is default
ALERT=95
# The current variable takes from a file which gives the prior saved value of usage
current=`cat /tmp/currentUsage`

df -PH | grep -vE ‘^Filesystem|tmpfs|cdrom’ | awk ‘{ print $5 ” ” $1 }’ | while read output;
do
usep=$(echo $output | awk ‘{ print $1}’ | cut -d’%’ -f1)
partition=$(echo $output | awk ‘{ print $2 }’ )

# If current usage is greater than ALERT and is greater the prior value then send a mail.

if [ $usep -ge $ALERT ] && [ $usep -gt $current ]; then
echo “Running out of space \”$partition ($usep%)\” on $(hostname) as on $(date)” |
mail -s “Alert: Almost out of disk space $partition $usep%” $ADMIN

# establish the new current value
echo $output | awk ‘{ print $1}’ | cut -d’%’ -f1 > /tmp/currentUsage
fi
done

36 KC April 27, 2012

Thanks, VIVEK and BEN. Very nice script and the quick correction, thanks a lot.

Leave a Comment

You can use these HTML tags and attributes for UNIX commands or shell scripts: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 12 + 10 ?
Please leave these two fields as-is:
Are you a human being? Solve the simple math so we know that you are a human and not a script.



Tagged as: awk command, awk print, date command, df -H, df command, email alert, fi, filesystem, grep command, hostname, loop method, mail command, partition, running out of space, server disk space, shell loops, tmpfs, while loop

Previous Script:

Next Script: