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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disk size warning script 1

Status
Not open for further replies.

pobrika

IS-IT--Management
Mar 14, 2002
1
GB
I am new to unix and have a basic understanding. I require a script that will look at the disk size of my system every hour, and then notify me if it has reached 90% in any of the areas by email. eg live has reached 90%. If any one could let me know how this is done It would be great.
 
#!/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=&quot;$TMP`echo $FILESYS | sed 's/\//\./g'`&quot;
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=&quot;$TMP`echo $FILESYS | sed 's/\//\./g'`&quot;
MESSAGE=&quot;Only $MB_FREE MB free in filesystem - $FILESYS - on $HOSTNAME&quot;

# Comment out the next line if you do not have any paging software
# installed.
echo &quot;$MESSAGE&quot; | $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 &quot;$MESSAGE&quot; >> $FILE_NAME

# The following lines will be sent to the mailbox of the root user.
echo &quot;PROGRAM: \t $PROGRAM\n&quot;
echo &quot;LOG FILE:\t $HOSTNAME:$FILE_NAME\n&quot;
echo &quot;MESSAGE: \t $MESSAGE\n&quot;
echo &quot;PAGED: \t UNIX_CRITICAL&quot;
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 --
| Mike Nixon
| Unix Admin
| ----------------------------
 
Mike,

nice... Why don't you create a FAQ from your answer? Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
This is a script that might work also for you:

df | grep -v Used | awk '{print $7}' | while read FS
do
if [ &quot;${FS}&quot; != &quot;/proc&quot; ]
then
PERCENT_USED=$(df ${FS} | awk '{print $4}' | grep -v Used | cut -f1 -d&quot;%&quot;)
if [ &quot;${PERCENT_USED}&quot; -gt 89 ]
then
echo &quot;${FS} on `hostname` is ${PERCENT_USED}% Full!&quot; | mailx user@domain.com
fi
fi
done


Regards,
Chuck

chuck.spilman@nokia.com
 
Oh... forgot to mention that the reason I am not checking the /proc filesystem is that in AIX5L, there is no size for this filesystem, but it's the only version that has the /proc filesystem. So this script will work with AIX 3.2.5 all the way up to AIX5L.

Regards,
Chuck

chuck.spilman@nokia.com
 
Pretty similar to the previous one but here's another!

running on SunOS 2.6

#! /bin/ksh


CAPE=0
AREA=0
SIZE=0
echo
df -k|nawk '{print $6&quot; &quot;$5}'|tr -d '%'|grep -v capacity|while read AREA SIZE
do
SIZE=`expr $SIZE + 0`
if [ $SIZE -ge 90 ]
then
echo &quot;Warning! $AREA is $SIZE&quot;%&quot; full!&quot;
CAPE=1
fi
done

if [ $CAPE -lt 1 ]
then
echo &quot;All filesystems are currently less than 90% capacity!&quot;
fi

exit 0
 
Heres the one I run on SCO 5.0.5

#!/bin/sh
# Site specific utilities
# Monitoring Disk useage
# written by K.Haughton 17/01/2000
# modified 04/08/2000

set -x

# Cofigurable options START
DISK_FULL=10.00
LOW_SPACE=25.00
NORMAL=100.00
LOGDIR=&quot;&quot;
LOGFILE=hddspace.log
MAIL_LIST=kevinh@th1.theth.com
SERVER=`uname -n`
SCODE=`uname -n`

# Set Environment Variables
# Date
D1=`date +%d/%m/%y%n`

# Time
D2=`date +%H:%M:%S`

# Day
D3=`date +%a`

# ***** Main section ******
# Begin, Identify diskspace
/etc/dfspace | grep '^[^T*]' >> $$_FS
# Log space and date
echo &quot;-------------- BEGIN LOGGING ---------------&quot; >> $LOGDIR$LOGFILE
echo &quot;*** $D1 $d2 ***&quot; >> $LOGDIR$LOGFILE
/etc/dfspace | grep '^[^T*]' >> $LOGDIR$LOGFILE
echo &quot;------------- END OF LAST LOG ---------------&quot; >> $LOGDIR$LOGFILE
echo &quot; &quot; >> $LOGDIR$LOGFILE
# End log
# Get drives
DRIVES=`egrep '^' $$_FS | awk '{ print $1 }'`
for DRIVE in $DRIVES
do

# Identify space, used and percentage

USED=`egrep '^'${DRIVE}'[^A-Za-z]' $$_FS | awk '{ print $5 }'`
TOTAL=`egrep '^'${DRIVE}'[^A-Za-z]' $$_FS | awk '{ print $8 }'`

PERCENT=`egrep '^'${DRIVE}'[^A-Za-z]' $$_FS |sed -e 's/(/ /g;s/)\./ /g'| awk '{ print $11 }'`


# Option for reporting very low disk space by E-Mail tests space on /u and /appl
# also add any drive over 95% full
# NB: other partitions will naturally be small and near full by default

if [ $PERCENT -le $DISK_FULL ] || [ $DRIVE = '/u' ] || [ $DRIVE = '/appl' ] && [ $PERCENT -le $LOW_SPACE ] ; then

# add drive to message body

echo &quot;$DRIVE \\ton $SERVER has Low Disk Space, it has only $PERCENT free &quot;\\n >> mailmsg.tmp
fi

done

# tidyup
chmod 777 $LOGDIR$LOGFILE
chmod 777 $$_FS
#rm $$_FS

# message mailer
if test -s mailmsg.tmp ; then

cat $$_FS >> mailmsg.tmp
echo &quot;LOGDIR=$LOGDIR LOGFILE=hddspace.log&quot; >> mailmsg.tmp

# send message
mail -s&quot;Low Disk Space alert!!! $DEPOT&quot; $MAIL_LIST < mailmsg.tmp
rm mailmsg.tmp
fi ***************************************
Party on, dudes!
[cannon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top