#!/bin/sh
# Author : Shashi Bhusan
# Date : 18/09/2001
#This script will start eG DB2 monitoring script. 
# The following steps are carried out while excuting this script
# It checks whether the monitor switches of DB2 are ON or OFF
# If the switches are in OFF condition the prompt user to switch it ON and 
# restart DB
# If everything OK get port no associated to this instance
# If there are multiple ports associated with one instance
# loop to check which port is  is working
#  Echo message to user to telll which port has been taken for monitoring

PATH=$PATH:/bin:/usr/bin
export PATH

# Assign eG Home

EG_HOME=/opt/egurkha
export EG_HOME
echo ""
echo ""
echo " Starting the eG DB2 PerfMonitor ........"
echo ""
switch=1
# Check for switch of default monitors
# Before proceeding lets us check whether this user's environment is set correctly or not ?

if [ -z "$DB2INSTANCE" ]
then
 echo "*************************************************************"
        echo "The eG DB2 Perfmonitor not started"
        echo "Possible reasons may be :"
        echo "1. User's environment variables are not set properly"
        echo "2. User may not belong to DB2 admin/maint/cntrl group"
        echo
        echo "*************************************************************"
        exit

fi	
export switch
bufpool=`db2 "get database manager config" |grep "(DFT_MON_BUFPOOL)"|awk '{print $5}'`
export bufpool
sort=`db2 "get database manager config" |grep "(DFT_MON_SORT)"|awk '{print $4}'`
export sort
lock=`db2 "get database manager config" |grep "(DFT_MON_LOCK)"|awk '{print $4}'`
export lock
table=`db2 "get database manager config" |grep "(DFT_MON_TABLE)"|awk '{print $4}'`
export table
uow=`db2 "get database manager config" |grep "(DFT_MON_UOW)"|awk '{print $6}'`
export uow
stmt=`db2 "get database manager config" |grep "(DFT_MON_STMT)"|awk '{print $4}'`
export stmt


if [ $bufpool != 'ON' ]
# switch is off
then
        switch=0
        echo " DB2 default monitor DFT_MON_BUFPOOL switch is OFF !"
fi


if [ $lock != 'ON' ]
then
        switch=0
        echo " DB2 default monitor DFT_MON_LOCK switch is OFF !"
fi
if [ $table != "ON" ]
then
        switch=0
        echo " DB2 default monitor DFT_MON_TABLE switch is OFF !"
fi
if [ $uow != "ON" ]
then
        switch=0
        echo " DB2 default monitor DFT_MON_UOW switch is OFF !"
fi
if [ $sort != "ON" ]
then
        switch=0
        echo " DB2 default monitor DFT_MON_SORT switch is OFF !"
fi
if [ $stmt != "ON" ]
then
        switch=0
        echo " DB2 default monitor DFT_MON_STMT switch is OFF !"
fi

if [ $switch = 0 ]
then 
# Tell user about this condition !!
	echo ""
	echo " You need to switch ON the above DB2 default monitors and restart the Database"
	echo " Command for switching ON the monitor is:" 
	echo " db2 \"update database manager config using {monitor_name} ON\""
	echo ""
fi

# Check whether runEgPerfmonitor is already up ?
db2user=`id |awk '{print$1}'|cut -d "(" -f2|cut -d ")" -f1`
val100=`ps -e -o pid,user,args| grep runEgPerfmonitor |grep $db2user| grep -v grep | wc -l`
if [ $val100 -gt 0 ]
then
	
# It is already running ... Tell user 

		echo "***************************************************************"
	echo "The eG DB2 PerfMonitor is already running ..."
	echo "***************************************************************"
	# Ooops ! its running, Lets get out of here.
	exit
else
# Nope its not running ! We have lot of work to do , eh !

	#get port associated with instance
        #get the instance associated with this user
        db2instance=$DB2INSTANCE
	
        portval=`cat /etc/services |grep db2 |grep Connection |grep $db2instance |awk '{print $2 }' |wc -l`

        if [ $portval -gt 1 ]
        then
		portval1=`cat /etc/services |grep db2 |grep Connection |grep $db2instance |awk '{print $2}'`
        	#portval3=`echo $portval1 |cut -d "/" -f1`
		for var in  $portval1
		do
        		mport=`echo $var |cut -d "/" -f1`

        		netout=`netstat -na |grep "$mport"`
        		if [ "$netout" ]
        		then
                		port=$mport
        		fi
		done
		if [ ! "$port" ]
		then
			echo "DB2 Server may not be running !!"
		fi
        	echo "You have more than one ports associated with this Instance "
        	echo "Starting monitoring on Port no $port"
        	echo "While configuring the port in eG's User Interface use $port as port"
		export port

        else

        	port=`cat /etc/services |grep db2 |grep Connection |grep $db2instance|awk '{print $2}' |cut -d '/' -f1`
        	export port
        fi
	fnetout=`netstat -na |grep "$port"`
        if [ "$fnetout" ]
	then
		nohup $EG_HOME/bin/runEgPerfmonitor.sh $port >/dev/null 2>/dev/null &
		echo "**********************************************************"
		echo "The eG DB2 PerfMonitor started ..."
		echo "*********************************************************"
	else
		echo " DB2 server is not running or no port has been configured for this Instance."
		echo " The eG DB2 Perfmonitor was not started !"
		echo
		echo "**********************************************************"
	
	fi	
	
fi
