Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.


How can I monitor disk space usage by mrn
Posted: 18 Mar 02

Below is a simple script to monitor and inform of impending full disks. It was written on a AIX box, but should run on any *nix version.

#@(#) Check filesystem space -
#@(#)    used to automate the monitoring of filesystems to
#@(#)    confirm that they do not pass a pre-defined threshold.
#  This job is set up in cron to run on a regular basis.
#  It monitors space in directories, making sure that free space
#  does not fall below pre-defined thresholds.

export DATE=`date +%m%d`
export TMP="/tmp/.chkfilspc.$DATE"
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/ucb:/usr/local/bin:
export HOSTNAME=`hostname`
export PROGRAM=$0
export PAGE='/usr/local/bin/sendpage -n'

# If you are using this under AIX 3.2.5, leave DF='df', but on
# an AIX 4 system you will have to change it to DF='df -k'
export DF='df -k'

function CHK_FILE_SPC
export FILESYS=$1

if [ "$3" = "%" ]
      export AMOUNT=`$DF $FILESYS | tail -1 | \
         awk -v PERCENT=$2  '{print $2*PERCENT/100/1024}'`
      export AMOUNT=$2


FREE_SPACE=`$DF $FILESYS | tail -1 | awk '{print $3}'`
(( LOW_WATER_MARK = $AMOUNT * 1024 ))

      (( MB_FREE = $FREE_SPACE / 1024 ))
      FILE_NAME="$TMP`echo $FILESYS | sed 's/\//\./g'`"
      if [ ! -s $FILE_NAME ]

#  If the log file doesn't exist, then check the filesystem one more
#  time before paging the administrator

#  If the log file exists, then just log that it was still full at this
#  time, but don't do any more (unless /tmp is the file that's full and
#  the log could not be created.
            date >> $FILE_NAME

function RE_CHK_FILE_SPC
FREE_SPACE=`$DF $FILESYS | tail -1 | awk '{print $3}'`
(( LOW_WATER_MARK = $AMOUNT * 1024 ))

      (( MB_FREE = $FREE_SPACE / 1024 ))
      FILE_NAME="$TMP`echo $FILESYS | sed 's/\//\./g'`"
      MESSAGE="Only $MB_FREE MB free in filesystem - $FILESYS - on $HOSTNAME"

#  Comment out the next line if you do not have any paging software
#  installed.

#  The following lines open the log file and save the message. This
#  file is used to check against each time the script is run. If
#  the log file exists then it will not page again, until after the
#  date changes
      date > $FILE_NAME
      echo "$MESSAGE" >> $FILE_NAME

#  The following lines will be sent to the mailbox of the root user.
      echo "PROGRAM: \t $PROGRAM\n"
      echo "LOG FILE:\t $HOSTNAME:$FILE_NAME\n"
      echo "MESSAGE: \t $MESSAGE\n"
      echo "PAGED:   \t UNIX_CRITICAL"

############### MAIN
#  The format of the following lines - are as follows:
#     FUNCTION_NAME  filesystem  amount_free  [%]
#  Examples:
#     CHK_FILE_SPC /tmp 10  
#               - check /tmp for 10 MB of free space
#     CHK_FILE_SPC / 40 %
#               - check / to see that there is 40 % of free space

CHK_FILE_SPC /tmp 10

CHK_FILE_SPC /home 50


Back to UNIX Scripting FAQ Index
Back to UNIX Scripting Forum

My Archive


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close