#! /bin/ksh
#
# @(#)oracle_monitor_process.sho        1.8     99/06/09     SMI
# Copyright 07/01/97 Sun Microsystems, Inc.  All Rights Reserved.
#
# oracle_monitor_process - monitors oracle listener process and restarts it
#			if it is not running
#	Syntax: oracle_monitor_process <sid> <listener> 
#

monitor_listener()
{
  typeset status
  typeset oracle_owner
  lsnr_timeout=30
  # IMPORTANT: if you want to modify lsnr_timeout, make sure to modify
  # listener_timeout in file oracle_boiler too.

   oracle_owner=`ls -ld ${ORACLE_HOME} | nawk '{print $3}'`

export LISTENER_NAME=${LISTENER_NAME:="LISTENER"}
 /usr/bin/ps -e -u $oracle_owner -o args | grep -w "tnslsnr $LISTENER_NAME " | grep -v "grep" > /dev/null
   if [ $? -ne 0 ]; then
        su  $oracle_owner -c sh << EOF > /dev/console 2>&1
                hatimerun -t $lsnr_timeout $ORACLE_HOME/bin/lsnrctl start $LISTENER_NAME
EOF
    fi
    return
}


# ############# Main ########################## Main ##########################
export ORACLE_SID=${1:-""}
export LISTENER_NAME=${2:-""}

ORATAB=/var/opt/oracle/oratab
ORACLE_HOME=""

if oratab_line=`grep "^[         ]*$ORACLE_SID:" $ORATAB` ; then
	ORACLE_HOME=`echo $oratab_line | awk -F: '{print $2}' -`
fi

[ -z "$ORACLE_HOME" ] && exit 1
[ -d "$ORACLE_HOME" ] || exit 1
[ -z "$ORACLE_SID" ] && exit 1

export LD_LIBRARY_PATH=${ORACLE_HOME}/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export ORACLE_HOME LISTENER_NAME

monitor_listener & 

exit 0

