#!/bin/sh
#
# 
# oralsnr
#
# Description:	Manages an Oracle Listener as a High-Availability
#		resource
#
#
# Author:	Dejan Muhamedagic
# Support:	linux-ha@lists.linux-ha.org
# License:	GNU General Public License (GPL)
# Copyright:	(C) 2006 International Business Machines, Inc.
#
#		This code inspired by the DB2 resource script
#		written by Alan Robertson
#
# An example usage in /etc/ha.d/haresources: 
#       node1  10.0.0.170 oralsnr::sid::home::user::listener
#
# See usage() function below for more details...
#
# OCF instance parameters:
#	OCF_RESKEY_sid (mandatory; for the monitor op)
#	OCF_RESKEY_home (optional; else read it from /etc/oratab)
#	OCF_RESKEY_user (optional; user to run the listener)
#	OCF_RESKEY_listener (optional; defaults to LISTENER)
#
# Initialization:

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat}
. ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs

#######################################################################

SH=/bin/sh

usage() {
  methods=`oralsnr_methods`
  methods=`echo $methods | tr ' ' '|'`
  cat <<-!
	usage: $0 ($methods)

	$0 manages an Oracle Database instance as an HA resource.

	The 'start' operation starts the database.
	The 'stop' operation stops the database.
	The 'status' operation reports whether the database is running
	The 'monitor' operation reports whether the database seems to be working
	The 'validate-all' operation reports whether the parameters are valid
	The 'methods' operation reports on the methods $0 supports

	!
}

meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="oralsnr">
<version>1.0</version>

<longdesc lang="en">
Resource script for Oracle Listener. It manages an
Oracle Listener instance as an HA resource.
</longdesc>
<shortdesc lang="en">Manages an Oracle TNS listener</shortdesc>

<parameters>

<parameter name="sid" unique="1" required="1">
<longdesc lang="en">
The Oracle SID (aka ORACLE_SID). Necessary for the monitor op,
i.e. to do tnsping SID.
</longdesc>
<shortdesc lang="en">sid</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="home" unique="0">
<longdesc lang="en">
The Oracle home directory (aka ORACLE_HOME).
If not specified, then the SID should be listed in /etc/oratab.
</longdesc>
<shortdesc lang="en">home</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="user" unique="0">
<longdesc lang="en">
Run the listener as this user.
</longdesc>
<shortdesc lang="en">user</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="listener" unique="1">
<longdesc lang="en">
Listener instance to be started (as defined in listener.ora).
Defaults to LISTENER.
</longdesc>
<shortdesc lang="en">listener</shortdesc>
<content type="string" default="" />
</parameter>

</parameters>

<actions>
<action name="start" timeout="120" />
<action name="stop" timeout="120" />
<action name="status" timeout="60" />
<action name="monitor" depth="0" timeout="30" interval="10" />
<action name="validate-all" timeout="5" />
<action name="meta-data" timeout="5" />
<action name="methods" timeout="5" />
</actions>
</resource-agent>
END
}


#
# methods: What methods/operations do we support?
#
oralsnr_methods() {
  cat <<-!
	start
	stop
	status
	monitor
	validate-all
	methods
	meta-data
	usage
	!
}


#	Gather up information about our oralsnr instance

ora_info() {
	ORACLE_SID=$1
	ORACLE_HOME=$2
	ORACLE_OWNER=$3

	# get ORACLE_HOME from /etc/oratab if not set
	[ x = "x$ORACLE_HOME" ] &&
		ORACLE_HOME=`awk -F: "/^$ORACLE_SID:/"'{print $2}' /etc/oratab`

	# there a better way to find out ORACLE_OWNER?
	[ x = "x$ORACLE_OWNER" ] &&
		ORACLE_OWNER=`ls -ld $ORACLE_HOME/. 2>/dev/null | awk 'NR==1{print $3}'`

	sqlplus=$ORACLE_HOME/bin/sqlplus
	lsnrctl=$ORACLE_HOME/bin/lsnrctl
	tnsping=$ORACLE_HOME/bin/tnsping
}

testoraenv() {
	#	Let's make sure a few important things are set...
	if [ x == "x$ORACLE_HOME" ]; then
		ocf_log info "ORACLE_HOME not set"
		return $OCF_ERR_CONFIGURED
	fi
	if [ x == "x$ORACLE_OWNER" ]; then
		ocf_log info "ORACLE_OWNER not set"
		return $OCF_ERR_CONFIGURED
	fi
	#	and some important things are there
	if [ ! -x "$sqlplus" ]; then
		ocf_log info "$sqlplus does not exist"
		return $OCF_ERR_INSTALLED
	fi
	if [ ! -x "$lsnrctl" ]; then
		ocf_log err "$lsnrctl does not exist"
		return $OCF_ERR_INSTALLED
	fi
	if [ ! -x "$tnsping" ]; then
		ocf_log err "$tnsping does not exist"
		return $OCF_ERR_INSTALLED
	fi
	return 0
}

setoraenv() {
	LD_LIBRARY_PATH=$ORACLE_HOME/lib
	LIBPATH=$ORACLE_HOME/lib
	TNS_ADMIN=$ORACLE_HOME/network/admin
	PATH=$ORACLE_HOME/bin:$ORACLE_HOME/dbs:$PATH
	export ORACLE_SID ORACLE_HOME ORACLE_OWNER TNS_ADMIN
	export LD_LIBRARY_PATH LIBPATH
}
dumporaenv() {
cat<<EOF
PATH=$ORACLE_HOME/bin:$ORACLE_HOME/dbs:$PATH
ORACLE_SID=$ORACLE_SID
ORACLE_HOME=$ORACLE_HOME
ORACLE_OWNER=$ORACLE_OWNER
LD_LIBRARY_PATH=$ORACLE_HOME/lib
LIBPATH=$ORACLE_HOME/lib
TNS_ADMIN=$ORACLE_HOME/network/admin
export ORACLE_SID ORACLE_HOME ORACLE_OWNER TNS_ADMIN
export LD_LIBRARY_PATH LIBPATH
EOF
}

#
#	Run commands as the Oracle owner...
#
runasdba() {
	if [ "$US" = "$ORACLE_OWNER" ]; then
		$SH
	else
		(
		echo ". $envtmpf"
		cat
		) | su - $ORACLE_OWNER
	fi
}

#
# oralsnr_start: Start the Oracle listener instance
#

oralsnr_start() {
	if tnsping; then
		: nothing to be done, we can leave right now
		ocf_log info "Listener $listener already running"
		return $OCF_SUCCESS
	fi
	output=`echo $lsnrctl start $listener | runasdba`
	if tnsping; then
		: cool, we are up and running
		ocf_log info "Listener $listener running: $output"
		return $OCF_SUCCESS
	else
		ocf_log err "Listener $listener appears to have started, but is not running properly: $output"
		ocf_log err "Probable Oracle configuration error"
		ocf_log err "Oracle $tnsping output: `$tnsping $ORACLE_SID`"
		return $OCF_ERR_GENERIC
	fi
}

#
# oralsnr_stop: Stop the Oracle instance
#
oralsnr_stop() {
	if is_oralsnr_up; then
		output=`echo $lsnrctl stop $listener | runasdba`
	else
		ocf_log info "Listener $listener already stopped"
		return $OCF_SUCCESS
	fi
	oralsnr_kill  # kill the procs if they hanged
	if is_oralsnr_up; then
		ocf_log err "Listener $listener not stopped: $output"
		return $OCF_ERR_GENERIC
	else
		ocf_log info "Listener $listener stopped: $output"
		return $OCF_SUCCESS
	fi
}
# kill the listener procs
# give them 10 secs to exit cleanly (5 times 2)
oralsnr_kill() {
	oraprocs=`eval $procs | awk '{print $1}'`
	if [ -z "$oraprocs" ]; then
		ocf_log debug "All oralsnr processes are already stopped."
		return
	fi
	killprocs TERM $oraprocs
	for i in 1 2 3 4 5; do
		if [ -z "`eval $procs | awk '{print $1}'`" ]; then
			ocf_log debug "All oralsnr processes are killed."
			return
		fi
		sleep 2
	done
	killprocs KILL `eval $procs | awk '{print $1}'`
}
killprocs() {
	sig=$1
	shift 1
	# Record stderr
	kill -$sig $* >/dev/null
}

#
# is_oralsnr_up: is listener process running?
# oralsnr_status: is the listener running?
#
is_oralsnr_up() {
	[ x != "x`eval $procs`" ]
}
oralsnr_status() {
	output=`$lsnrctl status $listener`
	echo "$output" | tail -1 | grep -qs 'completed successfully'
	RET=$?
	if [ $RET -ne 0 ]; then
		ocf_log info "$listener status failed: $output"
	fi
	return $RET
}
# and does it work?
tnsping() {
	output=`$tnsping $ORACLE_SID`
	echo "$output" | tail -1 | grep -qs '^OK'
	RET=$?
	if [ $RET -ne 0 ]; then
		ocf_log info "$tnsping $ORACLE_SID failed: $output"
	fi
	return $RET
}

#
# oralsnr_monitor: Can we connect to the listener?
#
oralsnr_monitor() {
	if oralsnr_status && tnsping
	then
		: good
		#ocf_log info "Listener $listener running"
		return $OCF_SUCCESS
	else
		ocf_log info "Listener $listener not running"
		return $OCF_NOT_RUNNING
	fi
}

#
#	'main' starts here...
#

if [ $# -ne 1 ]
then
  usage
  exit $OCF_ERR_ARGS
fi

# These operations don't require OCF instance parameters to be set
case "$1" in
  meta-data)	meta_data
		exit $OCF_SUCCESS;;

  usage) 	usage
		exit $OCF_SUCCESS;;

  methods)	oralsnr_methods
		exit $?;;

  *);;
esac

if [ x = "x$OCF_RESKEY_sid" ]
then
  ocf_log err "Please set OCF_RESKEY_sid to the Oracle SID !"
  exit $OCF_ERR_ARGS
fi

ora_info "$OCF_RESKEY_sid" "$OCF_RESKEY_home" "$OCF_RESKEY_user"

LSB_STATUS_STOPPED=3
testoraenv
rc=$?
if [ $rc -ne 0 ]; then
	ocf_log info "Oracle environment for SID $ORACLE_SID does not exist"
	case "$1" in
		stop) exit $OCF_SUCCESS;;
		monitor) exit $OCF_NOT_RUNNING;;
		status) exit $LSB_STATUS_STOPPED;;
		*)
			ocf_log err "Oracle environment for SID $ORACLE_SID broken"
			exit $rc
		;;
	esac
fi

setoraenv # important: set the environment for the SID
envtmpf=`mktemp`
dumporaenv > $envtmpf
chmod 644 $envtmpf
trap "rm -f $envtmpf" EXIT

#
# default listener is "LISTENER"
#
listener=${OCF_RESKEY_listener:-"LISTENER"}
# how to get listener processes
procs="ps -e -o pid,args | grep '[t]nslsnr' | grep -w $listener"

US=`id -u -n`
if [ $US != root -a $US != $ORACLE_OWNER ]
then
  ocf_log err "$0 must be run as root or $ORACLE_OWNER"
  exit $OCF_ERR_PERM
fi

# What kind of method was invoked?
case "$1" in

  start)	oralsnr_start
		exit $?;;

  stop)		oralsnr_stop
		exit $?;;

  status)	if oralsnr_status
		then
		  echo Listener $listener is running
		  exit $OCF_SUCCESS
		else
		  echo Listener $listener is stopped
		  exit $OCF_NOT_RUNNING
		fi
		;;

  monitor)	oralsnr_monitor
		exit $?;;

  validate-all)	# OCF_RESKEY_sid was already checked by ora_info(),
		# just exit successfully here.
		exit $OCF_SUCCESS;;

  *)		oralsnr_methods
		exit $OCF_ERR_UNIMPLEMENTED;;
esac

#
# vim:tabstop=4:shiftwidth=4:textwidth=0:wrapmargin=0
