#! /bin/sh

# @(#)oracle_db_restart.sho	1.12 97/12/01 SMI
# Copyright 12/01/97 Sun Microsystems, Inc.  All Rights Reserved.
#
# oracle_db_restart <parent_process_id> <instance> - script for internal use by
#    the HA DBMS fault	monitor to restart the database it is monitoring.
#
# Environment:
# HA_ENV - name of HA environment file
# HA_METASETSERVE - list of the meta hosts to run on the local host
#


# Copyright 12/01/97 Sun Microsystems, Inc.  All Rights Reserved.
# @(#)oracle_boiler	1.8 97/12/01 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 
  	}

#end of common ha-dbms boiler plate
#include_boiler


if [ $# -ne 2 ] ; then
  	logerr "usage: $argv0 <parent_process_id> <instance_name>"
  	exit 2
fi

# startup_oracle: db_owner oracle_home sid 

PPID=$1
ORACLE_SID=$2 ; export ORACLE_SID
find_oracle $ORACLE_SID
oracle_owner=`ls -ld ${ORACLE_HOME} | nawk '{print $3}'`


#      startup listner process if it is not started.
#      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"

        dbafile="$ORACLE_HOME/bin/svrmgrl"
        orasrv_file=""
        match_orasrv="orasrv"
        if [ $SQLDBA != $dbafile ] ; then
		orasrv_file=$ORACLE_HOME/bin/orasrv
                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

#------ end of starting up listener ---------

# Did we find the instance?
if [ "$ORACLE_HOME" != "" ] ; then
    	oracle_owner=`ls -ld ${ORACLE_HOME} | nawk '{print $3}'`
    	# check for clean shutdown
    	if [ -f ${ORACLE_HOME}/dbs/sgadef${ORACLE_SID}.dbf -o \
	     -f ${ORACLE_HOME}/dbs/sgadef${ORACLE_SID}.ora ] ; then

      		lognotice "\"${ORACLE_SID}\" possibly left running when system went down (system crash?)"
      		echo "connect internal
shutdown abort" | su $oracle_owner -c ${SQLDBA}
      		if [ $? -ne 0 ] ; then
			logerr "shutdown abort failed for \"${ORACLE_SID}\" - notify Database Administrator"
			logerr "Oracle database \"${ORACLE_SID}\" NOT started"
			exit 1
      		fi
    	fi

      	if [ -r "$PFILE" ] ; then
		# make sure fault monitoring did not get stopped in the meantime
		if `kill -0 $PPID > /dev/null` ; then
			lognotice "Starting up instance $ORACLE_SID PFILE=$PFILE"
		      	echo "connect internal
startup PFILE=$PFILE" | su $oracle_owner -c $SQLDBA
      			if [ $? -ne 0 ] ; then
				logerr "Oracle database \"${ORACLE_SID}\" NOT started"
      			fi
		else
			lognotice "Re-start aborted because fault monitor terminated"
		fi
	else
		logerr "Oracle parameter file $PFILE does not exist or is not readable!"
	fi
	  
else
    	logerr "Oracle database \"${ORACLE_SID}\" NOT started"
fi


