~ubuntu-branches/debian/stretch/resource-agents/stretch

« back to all changes in this revision

Viewing changes to rgmanager/src/resources/orainstance.sh

  • Committer: Bazaar Package Importer
  • Author(s): Andres Rodriguez
  • Date: 2011-06-10 16:26:35 UTC
  • Revision ID: james.westby@ubuntu.com-20110610162635-yiy0vfopqw4trzgx
Tags: upstream-3.9.0
ImportĀ upstreamĀ versionĀ 3.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
#
 
3
# Copyright 2003-2004, 2006-2011 Red Hat, Inc.
 
4
#
 
5
# Author(s):
 
6
#     Hardy Merrill <hmerrill at redhat.com>
 
7
#     Lon Hohberger <lhh at redhat.com>
 
8
#     Michael Moon <Michael dot Moon at oracle.com>
 
9
#
 
10
# This program is Open Source software.  You may modify and/or redistribute
 
11
# it persuant to the terms of the Open Software License version 2.1, which
 
12
# is available from the following URL and is included herein by reference:
 
13
#
 
14
#       http://opensource.org/licenses/osl-2.1.php
 
15
#
 
16
# chkconfig: 345 99 01
 
17
# description: Service script for starting/stopping      \
 
18
#              Oracle(R) Database 10g on                 \
 
19
#                       Red Hat Enterprise Linux 5
 
20
#
 
21
# NOTES:
 
22
#
 
23
# (1) You can comment out the LOCKFILE declaration below.  This will prevent
 
24
# the need for this script to access anything outside of the ORACLE_HOME 
 
25
# path.
 
26
#
 
27
# (2) You MUST customize ORACLE_USER, ORACLE_HOME, ORACLE_SID, and
 
28
# ORACLE_HOSTNAME to match your installation if not running from within
 
29
# rgmanager.
 
30
#
 
31
# (3) Do NOT place this script in shared storage; place it in ORACLE_USER's
 
32
# home directory in non-clustered environments and /usr/share/cluster
 
33
# in rgmanager/Red Hat cluster environments.
 
34
#
 
35
# Oracle is a registered trademark of Oracle Corporation.
 
36
# Oracle9i is a trademark of Oracle Corporation.
 
37
# Oracle10g is a trademark of Oracle Corporation.
 
38
# All other trademarks are property of their respective owners.
 
39
#
 
40
#
 
41
# $Id: orainstance.sh 127 2009-08-21 09:17:52Z hevirtan $
 
42
#
 
43
# Original version is distributed with RHCS. The modifications include
 
44
# the following minor changes:
 
45
# - Meta-data moved to a dedicated file
 
46
# - Support for multiple listeners
 
47
# - Disabled EM
 
48
# - SysV init support removed. Only usable with rgmanager
 
49
#
 
50
 
 
51
. /etc/init.d/functions
 
52
 
 
53
declare SCRIPT="`basename $0`"
 
54
declare SCRIPTDIR="`dirname $0`"
 
55
 
 
56
# Required parameters from rgmanager
 
57
ORACLE_USER=$OCF_RESKEY_user
 
58
ORACLE_HOME=$OCF_RESKEY_home
 
59
ORACLE_SID=$OCF_RESKEY_name
 
60
 
 
61
# Optional parameters with default values
 
62
LISTENERS=$OCF_RESKEY_listeners
 
63
LOCKFILE="/tmp/.oracle10g-${ORACLE_SID}.lock"
 
64
[ -n "$OCF_RESKEY_lockfile" ] && LOCKFILE=$OCF_RESKEY_lockfile
 
65
 
 
66
export LISTENERS ORACLE_USER ORACLE_HOME ORACLE_SID LOCKFILE
 
67
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
 
68
export PATH=$ORACLE_HOME/bin:$PATH
 
69
 
 
70
declare -i      RESTART_RETRIES=3
 
71
declare -r      DB_PROCNAMES="pmon"
 
72
declare -r      LSNR_PROCNAME="tnslsnr"
 
73
 
 
74
 
 
75
#
 
76
# Start Oracle (database portion)
 
77
#
 
78
start_db() {
 
79
        declare tmpfile
 
80
        declare logfile
 
81
        declare -i rv
 
82
 
 
83
        tmpfile=/tmp/$SCRIPT-start.$$
 
84
        logfile=/tmp/$SCRIPT-start.log
 
85
 
 
86
        # Set up our sqlplus script.  Basically, we're trying to 
 
87
        # capture output in the hopes that it's useful in the case
 
88
        # that something doesn't work properly.
 
89
        echo "startup" > $tmpfile
 
90
        echo "quit" >> $tmpfile
 
91
 
 
92
        sqlplus "/ as sysdba" < $tmpfile > $logfile
 
93
        rv=$?
 
94
 
 
95
        rm -f $tmpfile
 
96
 
 
97
        # Dump logfile to /var/log/messages
 
98
        initlog -q -c "cat $logfile"
 
99
        
 
100
        if [ $rv -ne 0 ]; then
 
101
        rm -f $logfile
 
102
        initlog -n $SCRIPT -q -s "sqlplus returned 1, failed"
 
103
                return 1
 
104
        fi
 
105
 
 
106
        # If we see:
 
107
        # ORA-.....: failure, we failed
 
108
        grep -q "failure" $logfile
 
109
        rv=$?
 
110
 
 
111
    rm -f $logfile
 
112
        if [ $rv -eq 0 ]; then
 
113
        initlog -n $SCRIPT -q -s "found failure in stdout, returning 1"
 
114
                return 1
 
115
        fi
 
116
 
 
117
        return 0
 
118
}
 
119
 
 
120
 
 
121
#
 
122
# Stop Oracle (database portion)
 
123
#
 
124
stop_db() {
 
125
        declare tmpfile
 
126
        declare logfile
 
127
        declare -i rv
 
128
 
 
129
        tmpfile=/tmp/$SCRIPT-stop.$$
 
130
        logfile=/tmp/$SCRIPT-stop.log
 
131
 
 
132
    ora_procname="ora_${DB_PROCNAMES}_${ORACLE_SID}"
 
133
    status $ora_procname
 
134
    if [ $? -ne 0 ]; then
 
135
        # No pmon process found, db already down
 
136
        return 0
 
137
    fi
 
138
 
 
139
        # Setup for Stop ...
 
140
        echo "shutdown immediate" > $tmpfile
 
141
        echo "quit" >> $tmpfile
 
142
 
 
143
        sqlplus "/ as sysdba" < $tmpfile > $logfile
 
144
        rv=$?
 
145
 
 
146
        rm -f $tmpfile
 
147
 
 
148
        # Dump logfile to /var/log/messages
 
149
        initlog -q -c "cat $logfile"
 
150
        
 
151
    # sqlplus returned failure. We'll return failed to rhcs
 
152
        if [ $rv -ne 0 ]; then
 
153
        rm -f $logfile
 
154
        initlog -n $SCRIPT -q -s "sqlplus returned 1, failed"
 
155
                return 1
 
156
        fi
 
157
 
 
158
        grep -q failure $logfile
 
159
    rv=$?
 
160
    rm -f $logfile
 
161
 
 
162
        # If we see 'failure' in the log, we're done.
 
163
        if [ $rv -eq 0 ]; then
 
164
        initlog -n $SCRIPT -q -s "found failure in stdout, returning 1"
 
165
                return 1
 
166
        fi
 
167
 
 
168
        return 0
 
169
}
 
170
 
 
171
 
 
172
#
 
173
# Destroy any remaining processes with refs to $ORACLE_SID
 
174
#
 
175
force_cleanup() {
 
176
        declare pids
 
177
        declare pid
 
178
 
 
179
        pids=`ps ax | grep $ORACLE_SID | grep -v grep | awk '{print $1}'`
 
180
 
 
181
        initlog -n $SCRIPT -s "<err> Not all Oracle processes exited cleanly, killing"
 
182
        
 
183
        for pid in $pids; do
 
184
                kill -9 $pid
 
185
                if [ $? -eq 0 ]; then
 
186
                        initlog -n $SCRIPT -s "Killed $pid"
 
187
                fi
 
188
        done
 
189
 
 
190
        return 0
 
191
}
 
192
 
 
193
 
 
194
#
 
195
# Wait for oracle processes to exit.  Time out after 60 seconds
 
196
#
 
197
exit_idle() {
 
198
        declare -i n=0
 
199
        
 
200
        while ps ax | grep $ORACLE_SID | grep -q -v $LSNR_PROCNAME | grep -q -v grep; do
 
201
                if [ $n -ge 90 ]; then
 
202
                        force_cleanup
 
203
                        return 0
 
204
                fi
 
205
                sleep 1
 
206
                ((n++))
 
207
        done
 
208
        return 0
 
209
}
 
210
 
 
211
 
 
212
#
 
213
# Get database background process status.  Restart it if it failed and
 
214
# we have seen the lock file.
 
215
#
 
216
get_db_status() {
 
217
        declare -i subsys_lock=$1
 
218
        declare -i i=0
 
219
        declare -i rv=0
 
220
        declare ora_procname
 
221
 
 
222
        for procname in $DB_PROCNAMES ; do
 
223
                ora_procname="ora_${procname}_${ORACLE_SID}"
 
224
                
 
225
                status $ora_procname
 
226
                if [ $? -eq 0 ] ; then
 
227
                        # This one's okay; go to the next one.
 
228
                        continue
 
229
                fi
 
230
 
 
231
                # We're not supposed to be running, and we are,
 
232
                # in fact, not running...
 
233
                if [ $subsys_lock -ne 0 ]; then
 
234
                        return 3
 
235
                fi
 
236
 
 
237
                for (( i=$RESTART_RETRIES ; i; i-- )) ; do
 
238
                        # this db process is down - stop and
 
239
                        # (re)start all ora_XXXX_$ORACLE_SID processes
 
240
                        initlog -q -n $SCRIPT -s "Restarting Oracle Database..."
 
241
                        stop_db
 
242
 
 
243
                        start_db
 
244
                        if [ $? == 0 ] ; then
 
245
                                # ora_XXXX_$ORACLE_SID processes started
 
246
                                # successfully, so break out of the
 
247
                                # stop/start # 'for' loop
 
248
                                break
 
249
                        fi
 
250
                done
 
251
 
 
252
                if [ $i -eq 0 ]; then
 
253
                        # stop/start's failed - return 1 (failure)
 
254
            initlog -q -n $SCRIPT -s "Restart failed, retuning 1"
 
255
                        return 1
 
256
                fi
 
257
        done
 
258
        return 0
 
259
}
 
260
 
 
261
 
 
262
#
 
263
# Get the status of the Oracle listener process
 
264
#
 
265
get_lsnr_status() {
 
266
        declare -i subsys_lock=$1
 
267
        declare -i rv
 
268
    declare -r LISTENER=$3
 
269
 
 
270
    lsnrctl status $LISTENER >& /dev/null
 
271
        rv=$?
 
272
        if [ $rv == 0 ] ; then
 
273
                return 0 # Listener is running fine
 
274
        fi
 
275
 
 
276
        # We're not supposed to be running, and we are,
 
277
        # in fact, not running.  Return 3
 
278
        if [ $subsys_lock -ne 0 ]; then
 
279
                return 3
 
280
        fi
 
281
 
 
282
        # Listener is NOT running (but should be) - try to restart
 
283
        for (( i=$RESTART_RETRIES ; i; i-- )) ; do
 
284
        initlog -n $SCRIPT -q -s "Restarting Oracle listener ($LISTENER)"
 
285
                lsnrctl start $LISTENER
 
286
                lsnrctl status $LISTENER >& /dev/null
 
287
                if [ $? == 0 ] ; then
 
288
                        break # Listener was (re)started and is running fine
 
289
                fi
 
290
        done
 
291
 
 
292
        if [ $i -eq 0 ]; then
 
293
                # stop/start's failed - return 1 (failure)
 
294
        initlog -n $SCRIPT -q -s "Listener restart failed, retuning 1"
 
295
                return 1
 
296
        fi
 
297
 
 
298
    lsnrctl status $LISTENER >& /dev/null
 
299
        if [ $? != 0 ] ; then
 
300
        initlog -n $SCRIPT -q -s "Listener status failed, retuning 1"
 
301
                return 1 # Problem restarting the Listener
 
302
        fi
 
303
        return 0 # Success restarting the Listener
 
304
}
 
305
 
 
306
 
 
307
#
 
308
# Helps us keep a running status so we know what our ultimate return
 
309
# code will be.  Returns 1 if the $1 and $2 are not equivalent, otherwise
 
310
# returns $1.  The return code is meant to be the next $1 when this is
 
311
# called, so, for example:
 
312
#
 
313
# update_status 0   <-- returns 0
 
314
# update_status $? 0 <-- returns 0
 
315
# update_status $? 3 <-- returns 1 (values different - error condition)
 
316
# update_status $? 1 <-- returns 1 (same, but happen to be error state!)
 
317
#
 
318
# update_status 3
 
319
# update_status $? 3 <-- returns 3
 
320
#
 
321
# (and so forth...)
 
322
#
 
323
update_status() {
 
324
        declare -i old_status=$1
 
325
        declare -i new_status=$2
 
326
 
 
327
        if [ -z "$2" ]; then
 
328
                return $old_status
 
329
        fi
 
330
 
 
331
        if [ $old_status -ne $new_status ]; then
 
332
        initlog -n $SCRIPT -q -s "$old_status vs $new_status - returning 1"
 
333
                return 1
 
334
        fi
 
335
 
 
336
        return $old_status
 
337
}
 
338
 
 
339
 
 
340
#
 
341
# Print an error message to the user and exit.
 
342
#
 
343
oops() {
 
344
        #echo "Please configure this script ($0) to"
 
345
        #echo "match your installation."
 
346
        #echo 
 
347
        #echo "    $1 failed validation checks."
 
348
    initlog -n $SCRIPT -q -s "$1 failed validation checks"
 
349
        exit 1
 
350
}
 
351
 
 
352
 
 
353
#
 
354
# Do some validation on the user-configurable stuff at the beginning of the
 
355
# script.
 
356
#
 
357
validation_checks() {
 
358
        # If the oracle user doesn't exist, we're done.
 
359
        [ -n "$ORACLE_USER" ] || oops "ORACLE_USER"
 
360
        id -u $ORACLE_USER > /dev/null || oops "ORACLE_USER"
 
361
        id -g $ORACLE_USER > /dev/null || oops "ORACLE_USER"
 
362
 
 
363
        # If the oracle home isn't a directory, we're done
 
364
        [ -n "$ORACLE_HOME" ] || oops ORACLE_HOME
 
365
 
 
366
        # If the oracle SID is NULL, we're done
 
367
        [ -n "$ORACLE_SID" ] || oops ORACLE_SID
 
368
 
 
369
        # Super user? Automatically change UID and exec as oracle user.
 
370
        # Oracle needs to be run as the Oracle user, not root!
 
371
        if [ "`id -u`" = "0" ]; then
 
372
                su $ORACLE_USER -c "$0 $*"
 
373
                exit $?
 
374
        fi
 
375
 
 
376
        # If we're not root and not the Oracle user, we're done.
 
377
        [ "`id -u`" = "`id -u $ORACLE_USER`" ] || exit 1
 
378
        [ "`id -g`" = "`id -g $ORACLE_USER`" ] || exit 1
 
379
 
 
380
        # Go home.
 
381
        cd $ORACLE_HOME
 
382
 
 
383
        return 0
 
384
}
 
385
 
 
386
 
 
387
#
 
388
# Start Oracle
 
389
#
 
390
start_oracle() {
 
391
    initlog -n $SCRIPT -q -s "Starting Oracle Database"
 
392
        start_db || return 1
 
393
        
 
394
    for LISTENER in ${LISTENERS}; do
 
395
        logfile=/tmp/$SCRIPT-lsn-$$.log
 
396
        initlog -n $SCRIPT -q -s "Starting Oracle Listener $LISTENER"
 
397
        lsnrctl start $LISTENER > $logfile
 
398
        initlog -q -c "cat $logfile"
 
399
        rm -f $logfile
 
400
    done
 
401
 
 
402
        if [ -n "$LOCKFILE" ]; then
 
403
                touch $LOCKFILE
 
404
        fi
 
405
        return 0
 
406
}
 
407
 
 
408
 
 
409
#
 
410
# Stop Oracle
 
411
#
 
412
stop_oracle() {
 
413
        if ! [ -e "$ORACLE_HOME/bin/lsnrctl" ]; then
 
414
                initlog -n $SCRIPT -q -s "Oracle Listener Control is not available ($ORACLE_HOME not mounted?)"
 
415
                return 0
 
416
        fi
 
417
 
 
418
    initlog -n $SCRIPT -q -s "Stopping Oracle Database"
 
419
    stop_db || return 1
 
420
 
 
421
        
 
422
    for LISTENER in ${LISTENERS}; do
 
423
        initlog -n $SCRIPT -q -s "Stopping Oracle Listener $LISTENER"
 
424
        lsnrctl stop $LISTENER
 
425
    done
 
426
 
 
427
    initlog -n $SCRIPT -q -s "Waiting for all Oracle processes to exit"
 
428
    exit_idle
 
429
 
 
430
        if [ $? -ne 0 ]; then
 
431
                initlog -n $SCRIPT -q -s "WARNING: Not all Oracle processes exited cleanly"
 
432
        fi
 
433
 
 
434
        if [ -n "$LOCKFILE" ]; then
 
435
                rm -f $LOCKFILE
 
436
        fi
 
437
        return 0
 
438
}
 
439
 
 
440
 
 
441
#
 
442
# Find and display the status of iAS infrastructure.
 
443
#
 
444
# This has three parts:
 
445
# (1) Oracle database itself
 
446
# (2) Oracle listener process
 
447
# (3) OPMN and OPMN-managed processes
 
448
#
 
449
# - If all are (cleanly) down, we return 3.  In order for this to happen,
 
450
# $LOCKFILE must not exist.  In this case, we try and restart certain parts
 
451
# of the service - as this may be running in a clustered environment.
 
452
#
 
453
# - If some but not all are running (and, if $LOCKFILE exists, we could not
 
454
# restart the failed portions), we return 1 (ERROR)
 
455
#
 
456
# - If all are running, return 0.  In the "all-running" case, we recreate
 
457
# $LOCKFILE if it does not exist.
 
458
#
 
459
status_oracle() {
 
460
        declare -i subsys_lock=1
 
461
        declare -i last 
 
462
        declare -i depth=$1
 
463
 
 
464
        # Check for lock file. Crude and rudimentary, but it works
 
465
        if [ -z "$LOCKFILE" ] || [ -f $LOCKFILE ]; then
 
466
                subsys_lock=0 
 
467
        fi
 
468
 
 
469
        # Check database status
 
470
        get_db_status $subsys_lock $depth
 
471
        update_status $? # Start
 
472
        last=$?
 
473
 
 
474
        # Check & report listener status
 
475
    for LISTENER in ${LISTENERS}; do
 
476
        get_lsnr_status $subsys_lock $depth $LISTENER
 
477
        update_status $? $last
 
478
        last=$?
 
479
    done
 
480
        
 
481
        # No lock file, but everything's running.  Put the lock
 
482
        # file back. XXX - this kosher?
 
483
        if [ $last -eq 0 ] && [ $subsys_lock -ne 0 ]; then
 
484
                touch $LOCKFILE
 
485
        fi
 
486
 
 
487
        return $last
 
488
}
 
489
 
 
490
 
 
491
########################
 
492
# Do some real work... #
 
493
########################
 
494
 
 
495
case $1 in
 
496
    meta-data)
 
497
        cat `echo $0 | sed 's/^\(.*\)\.sh$/\1.metadata/'`
 
498
        exit 0
 
499
        ;;
 
500
        start)
 
501
        validation_checks $*
 
502
                start_oracle
 
503
                exit $?
 
504
                ;;
 
505
        stop)
 
506
        validation_checks $*
 
507
                stop_oracle
 
508
                exit $?
 
509
                ;;
 
510
        status|monitor)
 
511
        validation_checks $*
 
512
                status_oracle $OCF_CHECK_LEVEL
 
513
                exit $?
 
514
                ;;
 
515
        restart)
 
516
                $0 stop || exit $?
 
517
                $0 start || exit $?
 
518
                exit 0
 
519
                ;;
 
520
        *)
 
521
                echo "usage: $SCRIPT {start|stop|restart|status|monitor|meta-data}"
 
522
                exit 1
 
523
                ;;
 
524
esac
 
525
 
 
526
exit 0