#!/bin/ksh
#
#ident	"@(#)m2	1.9	04/12/08 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Title: Load monitor for disks and CPU's
# Author: Liam Grogan
# Purpose: Figure out if more disk and or CPU tests should be kicked off
# 
# A return of 100 means kick of more tests
# A return of 200 means subystem is fully queued
#

#############################
# get command line arguments
#############################

BLOCK=""
aflag=
bflag=
TYPE=
MAX_TESTS=100
OLD_IDLE_TIME=-1
OLD_WAIT_TIME=-1
OLD_BUSY_TIME=-1
SET_BUSY=
SET_WAIT=

USAGE="Usage: mon -x [Subsystem] -d [disk]"

while getopts x:d: name
do
    case $name in
	  x)      TYPE=$OPTARG;;
	  d)	  DISK=$OPTARG;;
          \?)     echo $USAGE
                 exit 2;;
    esac
done
shift $(($OPTIND - 1))

if [ $# -eq 2 ]
then
	SET_BUSY=$1
	SET_WAIT=$2
fi

#############################
# subsystem monitor functions
#############################

more_cpu() {
#set -x

if [[ -f "/tmp/mcpu" ]]
then 
. /tmp/mcpu
else
	echo "T_COUNT=0" > /tmp/mcpu
fi

	
NUM_CPUS=`psrinfo | wc -l`
mpstat 5 5 | grep -v CPU > /tmp/mpstat.op.$$

VALUE=200
COUNT=1
while (( "$COUNT" <= "$NUM_CPUS" ))
do
echo $COUNT
	IDLE_TIME=`cat /tmp/mpstat.op.$$ | tail -$COUNT |head -1 | awk '{print $16}'`
	WAIT_TIME=`cat /tmp/mpstat.op.$$ | tail -$COUNT |head -1 | awk '{print $15}'`
	set_block_value cpu $IDLE_TIME $WAIT_TIME $1 $2

	if [ "$IDLE_TIME" -ne 0 -o "$WAIT_TIME" -ne 100 ] && [[ "$BLOCK" != "yes" ]]
	then
        	VALUE=100
	fi
		COUNT=`expr $COUNT + 1`
done
rm /tmp/mpstat.op.$$
return $VALUE


}

more_disk(){

#set -x

if [[ -f "/tmp/mdisk" ]]
then
. /tmp/mdisk
else
        echo "T_COUNT=0" > /tmp/mdisk
fi

iostat -xpn 5 2 | grep "\<$DISK\>" > /tmp/iostat.$$
#lines=`wc -l /tmp/iostat.$$ | awk '{ print $1 }'`
#a_line=`expr $lines / 2`
#a_line=`expr $a_line + 1`

#BUSY_TIME=`sed -n "${a_line}p" /tmp/iostat.$$ | awk '{print $10}'`
#WAIT_TIME=`sed -n "${a_line}p" /tmp/iostat.$$ | awk '{print $9}'`
#echo `sed -n "${a_line}p" /tmp/iostat.$$ | awk '{print $10}'`
#echo `sed -n "${a_line}p" /tmp/iostat.$$ | awk '{print $9}'`
BUSY_TIME=`cat /tmp/iostat.$$ | tail -1 | awk '{print $10}'`
WAIT_TIME=`cat /tmp/iostat.$$ | tail -1 | awk '{print $9}'`
#echo "BUSY_TIME=$BUSY_TIME"
#echo "WAIT_TIME=$WAIT_TIME"

set_block_value disk $BUSY_TIME $WAIT_TIME
#echo "BLOCK?=$BLOCK"

#if [ "$BUSY_TIME" -ne 100 -o "$WAIT_TIME" -ne 100 ] && [[ "$BLOCK" != "yes" ]]
if [ "$BUSY_TIME" -lt 90 ] && [[ "$BLOCK" != "yes" ]]
then
	rm /tmp/iostat.$$
        return 100
else
	rm /tmp/iostat.$$
        return 200
fi

}



########################
# misc functions - none at the moment
########################

set_block_value() {
#set -x

DEV=$1
if [[ $DEV == "cpu" ]]
then
	IDLE_TIME=$2
	WAIT_TIME=$3
	. /tmp/mcpu
	T_COUNT=`head -1 /tmp/mcpu | cut -f2 -d=`
	echo $T_COUNT

	DIFFERENCEI=`echo $OLD_IDLE_TIME - $IDLE_TIME | bc`
	DIFFERENCEW=`echo $OLD_WAIT_TIME - $WAIT_TIME | bc`
	if [[ $T_COUNT == $MAX_TESTS ]] || [ $DIFFERENCEI == "0" -a $DIFFERENCEW == "0" ]
	then
		BLOCK="yes"
	fi
	update_tag_files $DEV $T_COUNT $IDLE_TIME $WAIT_TIME
else 
	BUSY_TIME=$2
	WAIT_TIME=$3
	. /tmp/mdisk

	if [ -n "$SET_WAIT" -a -n "$SET_BUSY" ]
	then
		OLD_BUSY_TIME=$SET_BUSY
		OLD_WAIT_TIME=$SET_WAIT
	fi

	T_COUNT=`head -1 /tmp/mdisk | cut -f2 -d=`
	##
	#echo "T_COUNT=$T_COUNT"

	DIFFERENCEB=`echo $OLD_BUSY_TIME - $BUSY_TIME | bc`
        DIFFERENCEW=`echo $OLD_WAIT_TIME - $WAIT_TIME | bc`
	#echo "OLD_BUSY_TIME=$OLD_BUSY_TIME"
	#echo "OLD_WAIT_TIME=$OLD_WAIT_TIME"
	if [[ $T_COUNT == $MAX_TESTS ]] || [ $DIFFERENCEB == "0" -a $DIFFERENCEW == "0" ]
        then
		##
		#echo "DIFFERENCEB=$DIFFERENCEB"
		#echo "DIFFERENCEW=$DIFFERENCEW"
                BLOCK="yes"
	fi
	update_tag_files $DEV $T_COUNT $BUSY_TIME $WAIT_TIME
fi

}

update_tag_files(){
#set -x

if [[ $DEV == "cpu" ]]
then
	cat << _EOF > /tmp/mcpu
T_COUNT=$T_COUNT
OLD_IDLE_TIME=$IDLE_TIME
OLD_WAIT_TIME=$WAIT_TIME 
_EOF
	
else 
 	cat << _EOF > /tmp/mdisk
T_COUNT=$T_COUNT
OLD_BUSY_TIME=$BUSY_TIME
OLD_WAIT_TIME=$WAIT_TIME 
_EOF



fi



}	
 

#############
# Main
#############



if [[ $TYPE == "cpu" ]]
then
	more_cpu
	VALUE=$?
elif [[ $TYPE == "disk" ]]
then
        more_disk
        VALUE=$?
else
        echo $USAGE
	exit 1
fi

return $VALUE
