summaryrefslogtreecommitdiffstats
path: root/heartbeat/oralsnr
blob: dd0df1c614c63d836cae53410802fdaf11af56b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/bin/sh
#
# 
# oralsnr
#
# Description:	Manages an Oracle Listener as a High-Availability
#		resource
#
#
# Author:	Dejan Muhamedagic
# Support:	users@clusterlabs.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 oralsnr_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}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
. ${OCF_FUNCTIONS_DIR}/ora-common.sh

# Parameter defaults

OCF_RESKEY_sid_default=""
OCF_RESKEY_home_default=""
OCF_RESKEY_user_default=""
OCF_RESKEY_listener_default="LISTENER"

: ${OCF_RESKEY_sid=${OCF_RESKEY_sid_default}}
: ${OCF_RESKEY_home=${OCF_RESKEY_home_default}}
: ${OCF_RESKEY_user=${OCF_RESKEY_user_default}}
: ${OCF_RESKEY_listener=${OCF_RESKEY_listener_default}}

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

SH=/bin/sh

oralsnr_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

	!
}

oralsnr_meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="oralsnr" version="1.0">
<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="${OCF_RESKEY_sid_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="${OCF_RESKEY_home_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="${OCF_RESKEY_user_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="${OCF_RESKEY_listener_default}" />
</parameter>

<parameter name="tns_admin" required="0" unique="0">
<longdesc lang="en">
	Full path to the directory that contains the Oracle
	listener tnsnames.ora configuration file.  The shell
	variable TNS_ADMIN is set to the value provided.
</longdesc>
<shortdesc lang="en">
	Full path to the directory containing tnsnames.ora
</shortdesc>
<content type="string"/>
</parameter>

</parameters>

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


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

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

#
# oralsnr_start: Start the Oracle listener instance
#

oralsnr_start() {
	if is_proc_running && test_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 test_tnsping; then
		: cool, we are up and running
		ocf_log info "Listener $listener running: $output"
		return $OCF_SUCCESS
	else
		ocf_exit_reason "Listener $listener appears to have started, but is not running properly: $output"
		ocf_log err "Probable Oracle configuration error"
		return $OCF_ERR_GENERIC
	fi
}

#
# oralsnr_stop: Stop the Oracle instance
#
oralsnr_stop() {
	if is_proc_running; then
		output=`echo lsnrctl stop $listener | runasdba`
	else
		ocf_log info "Listener $listener already stopped"
		return $OCF_SUCCESS
	fi
	ocf_stop_processes TERM $PROCS_CLEANUP_TIME `proc_pids`  # kill the procs if they hanged
	if is_proc_running; then
		ocf_exit_reason "Listener $listener not stopped: $output"
		return $OCF_ERR_GENERIC
	else
		ocf_log info "Listener $listener stopped: $output"
		return $OCF_SUCCESS
	fi
}

#
# is_proc_running: is the listener running?
#
is_proc_running() {
	show_procs | grep "." > /dev/null
}
# the following two should be run only if the process is running
test_listener() {
	local output
	output=`lsnrctl status $listener`
	if echo "$output" | tail -1 | grep -qs 'completed successfully'
	then
		return $OCF_SUCCESS
	else
		ocf_exit_reason "$listener status failed: $output"
		return $OCF_ERR_GENERIC
	fi
}
# and does it work?
test_tnsping() {
	local output
	output=`tnsping $ORACLE_SID`
	if echo "$output" | tail -1 | grep -qs '^OK'; then
		return $OCF_SUCCESS
	else
		ocf_exit_reason "tnsping $ORACLE_SID failed: $output"
		return $OCF_ERR_GENERIC
	fi
}

#
# oralsnr_monitor: Can we connect to the listener?
#
oralsnr_monitor() {
	if is_proc_running; then
		test_listener && test_tnsping
	else
		return $OCF_NOT_RUNNING
	fi
}

oralsnr_status() {
	if is_proc_running
	then
	  echo Listener $listener is running
	  exit $OCF_SUCCESS
	else
	  echo Listener $listener is stopped
	  exit $OCF_NOT_RUNNING
	fi
}

oralsnr_getconfig() {
	ora_common_getconfig "$OCF_RESKEY_sid" "$OCF_RESKEY_home" "$OCF_RESKEY_user" "$OCF_RESKEY_tns_admin"
	listener=${OCF_RESKEY_listener}
}

oralsnr_validate_all() {
	ora_common_validate_all
}

# used in ora-common.sh
show_procs() {
	ps -U "$ORACLE_OWNER" -o pid,user,args |
		grep '[t]nslsnr' | grep -i -w "$listener"
}
proc_pids() { show_procs | awk '{print $1}'; }
PROCS_CLEANUP_TIME="10"

OCF_REQUIRED_PARAMS="sid"
OCF_REQUIRED_BINARIES="lsnrctl tnsping"
ocf_rarun $*

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