Article Listing 1 Listing 2 Listing 3 oct2004.tar

Listing 1 df monitoring script

#!/usr/bin/ksh

#############################################################
#5/4/2002 #df monitoring script
#version 0.1 for AIX 5
#
#############################################################
# FILE NAME: dfk
# FILE DESCRIPTION: crontab tester df based on bb-scripts
#
# RETURN VALUE DESCRIPTION:
#	0 	sucess
#	1       failure
#
# EXTERNAL PROCEDURES CALLED:
#
# OPERATIONS
#
# this is based on standard df-k monitors from various sources
# scripts is capable of generating notifications if FS goes over the 
# limit. At the moment data blocks are only monitored not I nodes
# Script strores df -k into file and compares result with configuration
# file CONFFILE if there is event and this is first event it is noticed 
# message is send (or logged or echoed depened on configuration)
#
# swap monitoring is also added (lsps line) but it can turned off
#
# CONTROL
# there is CONTROLFILE=/etc/dfmon which if existst aborts execution
#
# there is CONFFILE=$CONFDIR/df_mon.conf which has limits for various FS
# format is:
# device<tab|space>Info<tab|space>Warning<tab|space>Fatal
#
# #FS     Info    Warning Fatal   # in first column is comment 
# DEF     10      10      10	  DEF is default value for each FS
# /       88      92      50	  specific value for part. FS
# /usr    95      97      99
# /var    70      80      90
#
# USAGE add into crontab and configure parameters
#
# QUIRCS 
#	lslp on AIX strange message rootvg locked no sense at all
#
# FUNCTIONS
#	actionSyslog 
#	actionLogfile 
#	actionEmail 
#	actionEcho 
#	sensorDf 
#	sensorSwap
#	walkFs 
#
############################################################
#Revison history
#0.0 just working with df
#0.1 df and lsps
############################################################

#notification actions
#to syslog 
actionSyslog ()
{
	logger  -p"$SYSLOG" "$SUBJECT"
}

#to logfile
actionLogfile () 
{
	{ 
	date 
	echo "$MESSAGE" 
	echo "###############################"
	} >> "$LOGFILE"
}


#to to email
actionEmail () 
{
#use printf to expand \n in message old trick ..
	printf "$MESSAGE" ""| mail -s "DF" "$DFADMIN"
}

#to stdout
actionEcho ()
{
	date
	echo "$MESSAGE"
	echo "###############################"
}

##############################################
#sensors

#df commadn on AIX - $4 is df%, to be chanched on other UNIXes
#NR > 1 to avoid comment line
#$4 is fs free value
sensorDf ()
{
	df -k | awk 'NR>1{ print $4+0" "$NF }' 
}

#lsps swap monitor AIX specific to be changed on other UNIXses
#NR > 1 to avoid comment line
#$5 is swap value
sensorSwap ()
{
	lsps -a | awk 'NR>1{ print $5+0" "$1 }'
}

################################################
#event detection - configuration parser
#walk trough df and test conditions
#FS loop 
################################################

walkFs ()
{

grep -v "^#" $DF | while read  ACTUAL FS
do

	#defaults for control
        LEVEL=""
	TRIGGER=0

	#get data for current FS or defaults and filter out commented lines
	##DATA=$(fgrep "$FS " "$CONFFILE"| grep -v "^#")
	DATA=$(egrep "$FS |$FS	" "$CONFFILE"| grep -v "^#")

	#if no line in conf use defaults
	test -z "$DATA" && DATA="$DATADEF"

	#error ... missing even defaults - go out!
	test -z "$DATA" && exit 1

	set $DATA
	INFO=$2; WARN=$3; FATAL=$4

	#find out the level of message

        if [ "$ACTUAL" -gt "$FATAL" ]
	then
                TRIGGER=1
                LEVEL="Fatal"

        elif [ "$ACTUAL" -gt "$WARN" ]
	then
                TRIGGER=1
                LEVEL="Warning"

        elif [ "$ACTUAL" -gt "$INFO" ]
	then
                TRIGGER=1
                LEVEL="Info"
        fi


#generate message if trigger is UP, actually add data to message

if [ "$TRIGGER" -gt 0 ]
then
	ACTION=$TRIGGER
	MESSAGE=$MESSAGE"\n $FS $LEVEL $ACTUAL"
fi

done

######################################################
#store the latest event and one previous into files
######################################################

test -f "$EV" && mv "$EV" "$EVO"
touch "$EVO"
echo "$MESSAGE" > "$EV"

}

####################################################
#main part
####################################################

export PATH=/bin:/usr/local/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:/sbin
VERSION="0.1"

BASEDIR=/rootop
CONFDIR=$BASEDIR
LOGDIR=$BASEDIR

#control file exists no test ..
CONTROLFILE=$BASEDIR/dfmon
test -f "$CONTROLFILE" || exit 0


##########################################################
# testing and creating directories nesecarry 
# for script operation

for d in $CONFDIR $LOGDIR
do
	if test ! -d "$d"
	then  
		mkdir -p $d || exit 1
	fi
done

#############################################################

#if lsps is executed, additional test
SWAPTEST="SWAP"

#############################################################
#there is no configuration - misconfigured exit 1
#############################################################
CONFFILE=$CONFDIR/df_mon.conf
test -f "$CONFFILE" || exit 1

#############################################################
#logs and event data
#in DF last df-k data is stored
#event, or two last messages cretaed
#############################################################
DF=$LOGDIR/df-k
EV=$LOGDIR/event.df-k
EVO=$LOGDIR/event.df-k.old

#############################################################
#Notification  info

#if LOGFILE exists message is automatically added to it
LOGFILE=""
#email where report is sent 
DFADMIN="root"
#logger syslog level to log message
SYSLOG="debug"
#to dump message on stdout
ECHO=""

#get the default values, line DEF in conf file
DATADEF=$(fgrep DEF "$CONFFILE")

######################################################
#set the message and global action variable
#if ACTION is > 0 it means do action

HOST=$(hostname)
MESSAGE="$HOST"
ACTION=0

#end configuration part
#######################################################
#sensor part - real data collection and parsing out into DF file

{
	sensorDf 
	test -n "$SWAPTEST" && sensorSwap
} > $DF

######################################################
#event detection part compare with limits in configuration
walkFs

#EVENTS are same - no sending messages just to AVOID flodding
diff "$EV" "$EVO" > /dev/null 2>&1 && exit 0

#######################################################
#action part

test "$ACTION" -eq 0  && exit 0

######################################################
#notification

#remove \n form subjet line outlook problem ..
SUBJECT=$(echo "$MESSAGE"| sed 's/\\n/ /g')

test -n "$SYSLOG"  && actionSyslog 
test -n "$LOGFILE" && actionLogfile 
test -n "$ECHO"    && actionEcho
test -n "$DFADMIN" && actionEmail 

exit  0