#! /bin/sh
#
#	Copyright 10/15/97 Sun Microsystems, Inc.  All Rights Reserved.
#
#ident	"@(#)oracle_svc_start_net.sho	1.12	97/10/15 SMI"
#
# oracle_svc_start_net - start all HA DBMS data services on hosts in
#  HA_METASETSERVE
#
# Environment:
# HA_ENV should be set, and point to the appropriate file
# Usage: oracle_svc_start_net
#

# 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


# startup_oracle - runs Oracle startup command for one instance
#   uses variables ORACLE_SID, init_ora, SQLDBA, oracle_onwer, PFILE
startup_oracle() {
        lognotice "Starting up instance $ORACLE_SID, PFILE=${init_ora}"
        echo "connect internal
startup PFILE=$PFILE" | su $oracle_owner -c $SQLDBA
        if [ $? -ne 0 ] ; then
          	logerr "Oracle database \"${ORACLE_SID}\" NOT started"
        fi
  }


# startup_instance - start up instance $ORACLE_SID
startup_instance() {
	export ORACLE_SID

  	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
			if [ "$HA_MEMBERSHIP" != "JUSTME" ]; 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"
						return
					fi
				fi
			fi
		else
			logerr "Failed to copy $relfile to local host; $ORACLE_SID not started"
			return
 		fi
	fi

#    startup listener 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
		# 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
			   if [ "$HA_MEMBERSHIP" != "JUSTME" ]; then
				   get_remote_host
				   if [ $? -eq 0 ] ; then
					remote_orasrv=$RSHHOST:$orasrv_file
					rcp -p $ORACLE_HOME/bin/orasrv $remore_orasrv
				   fi			
			   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

#---- end of starting up listener -------
	
# starting up Oracle instance
  	if [ "$ORACLE_HOME" != "" ] ; then
      		# check for clean shutdown
      		if [ -f ${ORACLE_HOME}/dbs/sgadef${ORACLE_SID}.dbf -o \
           	     -f ${ORACLE_HOME}/dbs/sgadef${ORACLE_SID}.ora ] ; then
  			get_pid -u $oracle_owner ora_smon_${ORACLE_SID}
			if [ "$PID" ] ; then
	  			lognotice "Oracle instance ${ORACLE_SID} already running!"
	  			return
			fi

			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"
	  			return
			fi
      		fi

        	if [ -r "$PFILE" ] ; then
			startup_oracle &
		else
			logerr "Oracle parameter file $PFILE does not exist or is not readable!"
		fi
  	else
    		logerr "Oracle database \"${ORACLE_SID}\" NOT started"
  	fi
}
  
# get the list of databases, and then start them up
for ORACLE_SID in `oracle_status_svcs -on` ; do
 	startup_instance 
done



