Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I monitor disk space usage

How can I monitor disk space usage

by  mrn  Posted    (Edited  )
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.

#!/usr/bin/ksh
#
#@(#) 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" = "%" ]
then
export AMOUNT=`$DF $FILESYS | tail -1 | awk -v PERCENT=$2 '{print $2*PERCENT/100/1024}'`
else
export AMOUNT=$2
fi

integer FREE_SPACE LOW_WATER_MARK

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

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

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

# 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
fi
fi
}

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

if (( $FREE_SPACE < $LOW_WATER_MARK))
then
(( 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.
echo "$MESSAGE" | $PAGE UNIX_CRITICAL

# 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"
fi
}

############### 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

Mike
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top