#! /bin/sh
#
#	Copyright 10/15/97 Sun Microsystems, Inc.  All Rights Reserved.
#
# @(#)haoracle_fmon_start.sho	1.20	97/10/15	SMI

# haoracle_fmon_start - start an Oracle fault monitor (haoracle_fmon) for the
#			instance #1
#
# Environment:
# HA_ENV - file name of HA environment file
# HA_METASETSERVE - list of the logical hosts to run on the local host
# HA_SIBLING_METASETSERVE - list of the logical hosts not on local host
#

# make sure we reread HA_ENV (might have changed)
# setting HA_CLUSTER to "" forces a re-read in oracle_boiler
HA_CLUSTER=""
# Copyright 02/12/97 Sun Microsystems, Inc.  All Rights Reserved.
# @(#)oracle_boiler	1.10 97/02/12 SMI

# begin of common ha-dbms boilerplate

# remember our name
argv0=`basename $0`


LOGGER=/usr/bin/logger

# if HA_CLUSTER is not set, read the environment
# NOTE: haoracle_fmon_start unsets HA_CLUSTER to force re-building the env.
if [ "$HA_CLUSTER" = "" ] ; then
	if [ ! -r "$HA_ENV" ] ; then
		$LOGGER -p local7.err -t "$HA_CLUSTER" "$argv0: Cannot determine correct HA environment"
		exit 1
	fi
	. $HA_ENV
	if [ $? -ne 0 ] ; then
		$LOGGER -p local7.err -t "$HA_CLUSTER" "$argv0: cannot include ha_env file $HA_ENV"
		exit 1	
	fi
fi

# include HA utilities library
. utilities
if [ $? -ne 0 ]; then
	$LOGGER -p local7.err -t "$HA_SLOGTAG" "$argv0: Cannot find HA utilities library"
	exit 1
fi

# usage
#if [ "$USAGE_PARMS" != "" ] ; then
#  	if [ $# -ne $USAGE_PARMS ] ; then
#    		$LOGGER -p local7.err -t "$HA_SLOGTAG" "$argv0: Incorrect number of command line parameters"
#    		exit 2
#  	fi
#fi


# some default files and locations
# don't move this up - it depends on HA_FILES, which gets set above
HA_DATABASES=${HA_FILES}/haoracle_databases
HA_DB_SUPPORT=${HA_FILES}/haoracle_support


# verify that we have the haoracle_databases files
if [ ! -r $HA_DATABASES ] ; then
  logerr "file ${HA_DATABASES} does not exist or is not readable!"
  exit 2
fi

# verify that we have the haoracle_support file
if [ ! -r $HA_DB_SUPPORT ] ; then
  logerr "file ${HA_DB_SUPPORT} does not exist or is not readable!"
  exit 2
fi


# talk_start ... - echo the args, run them, and evaluate return value
talk_start() {
  	lognotice "$$: $*"
  	if $* ; then
    		return 0
  	else
    		logerr "$$: $* ... failed"
    		return 1
  	fi
}


# talk_start_bg ... - print the args, then run them in the background
talk_start_bg() {
  	lognotice "$$: $*"
  	$* &
  	}


# get_pid [ -u userid ] pattern - find the process id of a running program
# if called with "-u userid", only process for that user will be considered
get_pid() {
	if [ "$1" = "-u" ] ; then
		ps_args="-f -u $2"
		shift
		shift
	else
		ps_args="-ef"
	fi
	PID=`/usr/bin/ps $ps_args | grep "\<$*\>" | nawk -v pat="$*" '
 BEGIN { 
   search_pat = substr(pat, 0, 79)
   }
 { if ( match($0, search_pat) )
     if ( ! match($0, "nawk -v pat=") )
       print $2
  }
'`
}


# kill_proc pattern - find and terminate a process
kill_proc() {
  	lognotice "Killing $*..." 
  	get_pid $*
  	if [ "$PID" ] ; then
    		for p in ${PID} ; do
      			lognotice "Killing process id $p"
      			kill $p
    		done
    		echo
  	else
    		lognotice "process \"$*\" could not be located..." 
  	fi
}

# read_ha_databases instance - find an entry in the HA_DATABASES file,
# 	parse it apart, and return in variables. Assumes that oracle_home
#	is set!
read_ha_databases() {
	conf_line=`grep "^o[nf][f]*	$1	" $HA_DATABASES`
	if [ "$conf_line" = "" ] ; then
		logerr "$1 missing from $HA_DATABASES!"
		return 1
	fi
	logical_host=`echo "$conf_line" | cut -s -f 3`
	poll_cycle=`echo "$conf_line" | cut -s -f 4`
	connect_cycle=`echo "$conf_line" | cut -s -f 5`
	timeout=`echo "$conf_line" | cut -s -f 6`
	restart_delay=`echo "$conf_line" | cut -s -f 7`
	db_login=`echo "$conf_line" | cut -s -f 8`
	init_ora=`echo "$conf_line" | cut -s -f 9`
        listener_name=`echo "$conf_line" | cut -s -f 10`
	return 0
}

# find_oracle instance - finds and sets various Oracle related variables:
#	ORACLE_HOME SQLDBA PFILE
# in case of a problem, it returns with an empty ORACLE_HOME variable
find_oracle() {
  	ORATAB=/var/opt/oracle/oratab

  	ORACLE_HOME=""
	SQLDBA=""
	if oratab_line=`grep "^[	 ]*$1:" $ORATAB` ; then
		oracle_home=`echo $oratab_line | awk -F: '{print $2}' -`
		if [ -d $oracle_home ] ; then
			if [ ! -f $oracle_home/orainst/RELVER ] ; then
				logerr "cannot find Oracle release number; RELVER file is missing"
				exit 1
			fi

			ora_version=`grep RELEASE_VERSION $oracle_home/orainst/RELVER | cut -d= -f2`
			if [ "$ora_version" = "" ] ; then
				logerr "RELEASE_VERSION missing from RELVER file"
				exit 1
			fi

			ora_subversion=`echo $ora_version | cut -d. -f2`
			ora_rel=`echo $ora_version | cut -d. -f3`
			if [ "$ora_rel" = "" ] ; then
				ora_rel=0
			fi

			# check if the oracle version is supported
			# The versions we will support are:  7.3.[>=2] ,
			# 7.2.[>=3] , and 7.1.[>=6].
			if [ $ora_subversion -eq 3 -a $ora_rel -lt 2 -o \
			     $ora_subversion -eq 2 -a $ora_rel -lt 3 -o \
			     $ora_subversion -eq 1 -a $ora_rel -lt 6 ] ; then
				logerr "Oracle $ora_version not supported!"
				exit 1;
			fi

			# release 7.3 use svrmgrl to connect to the instance
			if [ $ora_subversion -ge 3 ] ; then
				SQLDBA="${oracle_home}/bin/svrmgrl"
			else
				SQLDBA="${oracle_home}/bin/sqldba"
			fi

			if [ -x ${SQLDBA} ] ; then
				read_ha_databases $1
				ORACLE_HOME="$oracle_home"
				PFILE="$init_ora"
			else
				logerr "${SQLDBA} does not exist or is not executable!"
			fi
		else
			logerr "Invalid value for ORACLE_HOME: ${oracle_home}"
		fi
	else
		logerr "Database '$1' not found in ${ORATAB}"
	fi
    	export ORACLE_HOME SQLDBA PFILE
}


make_rpc_call() {
  	lognotice "Calling $3 for instance $2 on host $1..."
  	ha_dbms_call $1 $2 $3 
  	}


get_remote_host(){
        RSHHOST=""
        for X in `haget  -f private_links -h $HA_REMOTEHOST` ; do
          net_pinghost $X > /dev/null 2>&1
          if [ $? -eq 0 ] ; then
                RSHHOST=$X
                export RSHHOST
                return 0
          fi
        done
        return 1
        }



#end of common ha-dbms boiler plate
#include_boiler

# process_parm_file - process an Oracle parameter file $1, follow ifile
#    references, and echo the value for background_dump_dest
process_parm_file() {
	if [ -r "$1" ] ; then
		# Note: the following [ ]'s contain one space and on tab
		line=`grep '^[ 	]*background_dump_dest[	 ]*=' $1`
		if [ "$line" != "" ] ; then
			# found
			echo `echo $line | cut -d= -f 2`
		else
			# check include files
			ifile_list=`grep '^[ 	]*ifile[ 	]*=' $1 | cut -d= -f 2`
			for ifile in $ifile_list ; do
				process_parm_file $ifile
			done
		fi
	else
		echo "Oracle parameter file $1 does not exist or is not readable!"
        fi
}


# ############# Main ########################## Main ##########################
if [ $# -ne 1 ] ; then
	logerr "usage: $argv0 instance"
	exit 2
fi

ORACLE_SID=$1
read_ha_databases $1 
DB_LOGIN=$db_login
export DB_LOGIN


find_logical_host=`/opt/SUNWhadf/bin/haget -f all_logical_hosts | grep $logical_host`
if [ "$find_logical_host" = "" ] ; then
        logerr "logical host $logical_host is not in the cluster configuration"
        logerr "Monitors for oracle database \"$ORACLE_SID\" NOT started"
        exit 1
fi

remote_args=""
matches=`expr " $HA_METASETSERVE " : ".* $logical_host "`
if [ "$matches" -eq 0 ] ; then
    	remote_args="-r $logical_host"
fi

if [ "$remote_args" != "" ] ; then
        ORATAB=/var/opt/oracle/oratab
        if oratab_line=`grep "^[	 ]*$1:" $ORATAB` ; then
                oracle_home=`echo $oratab_line | awk -F: '{print $2}' -`
	fi
	binaries_on_logical=`echo "$oracle_home" | grep "$logical_host"`
fi

# starting up remote fault monitor when the Oracle binaries are
# installed on the logical host then the remote server does not
# have access to version and error log, thus,they are placed in $HA_VAR. 
# The remote fmon will be started and exit from here
if [ "$remote_args" != "" -a "$binaries_on_logical" != "" ] ; then
        alert_file="none"
        $oracle_home="/var/opt/oracle"
        ORACLE_HOME="/var/opt/oracle"
        release_file=$HA_VAR/RELVER.$ORACLE_SID
	sleep 10
        if [ ! -f $release_file ] ; then
           logerr "cannot find Oracle release number; RELVER file is missing"
           exit 1
        fi
 
        ora_version=`grep RELEASE_VERSION $release_file | cut -d= -f2`
        if [ "$ora_version" = "" ] ; then
           logerr "RELEASE_VERSION missing from RELVER file"
           exit 1
        fi
        ora_subversion=`echo $ora_version | cut -d. -f2`

        grep -v "^#" ${HA_DB_SUPPORT} | \
        while read support_line ; do
                pattern="\("`echo "$support_line" | cut -f 1`"\)"
                result=`expr "$ora_version" : ${pattern}`
                if [ "$result" != "" ] ; then
                        ha_executable=`echo "$support_line" | cut -f 2`
                        action_file=`echo "$support_line" | cut -f 3`
                        if [ "$ha_executable" = "" ] ; then
                           logerr "instance ${ORACLE_SID}: executable file name missing in line '$support_line' in file $HA_DB_SUPPORT"
                                exit 1
                        fi
                        if [ "$action_file" = "" ] ; then
                           logerr "instance ${ORACLE_SID}: action file name missing in line '$support_line' in file $HA_DB_SUPPORT"
                                exit 1
                        fi
                        talk_start ${ha_executable} $remote_args $ORACLE_SID $poll_cycle $connect_cycle $timeout $restart_delay $HA_FILES/$action_file $alert_file
                        exit 3
		fi
	done
	[ $? = "3" ] && exit 0
fi
#---------- end of remote fault monitor for binaries on logical host-----------

find_oracle $ORACLE_SID
oracle_owner=`ls -ld ${ORACLE_HOME} | nawk '{print $3}'`
binaries_on_logical=`echo "$ORACLE_HOME" | grep "$logical_host"`

# copy the RELVER file to be used by fault monitor, especially
# when oracle binaries are installed on the logical host,
# then RELVER need to be copied to the private disk

if [ "$binaries_on_logical" != "" ] ; then
	relfile=$HA_VAR/RELVER.$ORACLE_SID
	cp  -p $ORACLE_HOME/orainst/RELVER $relfile
	if [ $? -eq 0 ] ; then
		get_remote_host
		if [ $? -eq 0 ] ; then
			rcp -p $ORACLE_HOME/orainst/RELVER $RSHHOST:$relfile
			if [ $? -ne 0 ] ; then
				logerr "Failed to copy $relfile to remote host; $ORACLE_SID not started"
				exit 1
 			fi
		fi
	else
		logerr "Failed to copy $relfile to local host; $ORACLE_SID not started"
		exit 1
	fi
fi
#----- end of copy RELVER file -----

# startup listner process if it is not started for local host only. 
# if oracle version is < 7.3 then need to start both
# v1 listener using "orasrv" and v2 listener using "lsnrctl"
# otherwise, start up v2 listener using "lsnrctl"

if [ "$remote_args" = "" ] ; then
        dbafile="$ORACLE_HOME/bin/svrmgrl"
        orasrv_file=""
        match_orasrv="orasrv"
        if [ $SQLDBA != $dbafile ] ; then
                # if oracle installed on logical host, and it's 7.1.x or 7.2.x
                # then, move orasrv executable to physical host and
                # start orasrv from physical host
                if [ "$binaries_on_logical" != "" ] ; then
                        orasrv_file=$HA_VAR/orasrv
                        if [ ! -f $orasrv_file ] ; then
                           cp  -p $ORACLE_HOME/bin/orasrv $orasrv_file
			   get_remote_host
			   if [ $? -eq 0 ] ; then 
                        	remote_orasrv=$RSHHOST:$orasrv_file
                                rcp -p $ORACLE_HOME/bin/orasrv $remore_orasrv
		           fi		
                        fi
                else
                        orasrv_file=$ORACLE_HOME/bin/orasrv
                fi
                OPID="`getpids2 orasrv`"

                if  [ "${OPID}" = "" ] ; then
                        lognotice "starting up SQL*Net V1 listener"
                        $orasrv_file & >/dev/console 2>&1
                fi
        fi
        #---end of ORASRV process ----

        if [ "$listener_name" = "" ] ; then
                match_string="tnslsnr LISTENER"
        else
                match_string="tnslsnr $listener_name"
        fi

        LPID="`getpids2 $match_string`"
        if [ "${LPID}" = "" ] ; then
           lognotice "starting up SQL*Net V2 listener"
           su  $oracle_owner -c "$ORACLE_HOME/bin/lsnrctl start $listener_name &
" >/dev/console 2>&1
        fi
fi
#------ end of starting up listener ---------



if [ "$ORACLE_HOME" != "" ] ; then
      	grep -v "^#" ${HA_DB_SUPPORT} | \
      	while read support_line ; do
        	pattern="\("`echo "$support_line" | cut -f 1`"\)"
        	result=`expr "$ora_version" : ${pattern}`
        	if [ "$result" != "" ] ; then
	  		ha_executable=`echo "$support_line" | cut -f 2`
	  		action_file=`echo "$support_line" | cut -f 3`
	  		if [ "$ha_executable" = "" ] ; then
				logerr "instance ${ORACLE_SID}: executable file name missing in line '$support_line' in file $HA_DB_SUPPORT"
				exit 1
			fi
	  		if [ "$action_file" = "" ] ; then
				logerr "instance ${ORACLE_SID}: action file name missing in line '$support_line' in file $HA_DB_SUPPORT"
				exit 1
			fi
      			if [ "$remote_args" = "" ] ; then
				alert_file=`process_parm_file $PFILE`
            			if [ "$alert_file" = "" ] ; then
              				logerr "Could not locate background_dump_dest value in parameter file"
					exit 1
				else
					alert_file=${alert_file}/alert_${ORACLE_SID}.log
            			fi
          		else
            			alert_file="none"
          		fi
	  		talk_start ${ha_executable} $remote_args $ORACLE_SID $poll_cycle $connect_cycle $timeout $restart_delay $HA_FILES/$action_file $alert_file 
          		exit 3
        	fi
      	done
        case $? in
                1) ;;
                3) exit 0 ;;
                *) logerr "Oracle ${ora_version} not supported!" ;;
        esac

fi

# if we reach here, then a fault monitor did not get started
# the reason did already get logged in the various logerr calls above
# Note: The "exit" statements in the while statement above do not
#       cause an exit of the entire script, but just the subshell that
#	executes the while command as part of a pipe!
logerr "Monitors for Oracle database \"${ORACLE_SID}\" NOT started"
exit 1





