summaryrefslogtreecommitdiffstats
path: root/heartbeat/sapdb.sh
blob: 66e9854b6c21991714199235d436a79cd809b2c5 (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#
# sapdb.sh - for systems having SAPHostAgent installed
# (sourced by SAPDatabase)
#
# Description:	This code is separated from the SAPDatabase agent to
#               introduce new functions for systems which having
#               SAPHostAgent installed.
#               Someday it might be merged back into SAPDatabase agein.
#
# Author:       Alexander Krauth, September 2010
# Support:      linux@sap.com
# License:      GNU General Public License (GPL)
# Copyright:    (c) 2010, 2012 Alexander Krauth
#


#
# background_check_saphostexec : Run a request to saphostexec in a separate task, to be able to react to a hanging process
#
background_check_saphostexec() {
  timeout=600
  count=0

  $SAPHOSTCTRL -function ListDatabases >/dev/null 2>&1 &
  pid=$!

  while kill -0 $pid > /dev/null 2>&1
  do
    sleep 0.1
    count=$(( $count + 1 ))
    if [ $count -ge $timeout ]; then
      kill -9 $pid >/dev/null 2>&1
      ocf_log warn "saphostexec did not respond to the method 'ListDatabases' within 60 seconds"
      return $OCF_ERR_GENERIC                # Timeout
    fi
  done

  # child has already finished, now evaluate its returncode
  wait $pid
}

#
# cleanup_saphostexec : make sure to cleanup the SAPHostAgent in case of any
#                       misbehavior
#
cleanup_saphostexec() {
  pkill -9 -f "$SAPHOSTEXEC"
  pkill -9 -f "$SAPHOSTSRV"
  oscolpid=$(pgrep -f "$SAPHOSTOSCOL")      # we check saposcol pid, because it
                                            # might not run under control of
                                            # saphostexec

  # cleanup saposcol shared memory, otherwise it will not start again
  if [ -n "$oscolpid" ];then
    kill -9 $oscolpid
    oscolipc=$(ipcs -m | grep "4dbe " | awk '{print $2}')
    if [ -n "$oscolipc" ]; then
      ipcrm -m $oscolipc
    fi
  fi

  # removing the unix domain socket file as it might have wrong permissions or
  # ownership - it will be recreated by saphostexec during next start
  [ -r /tmp/.sapstream1128 ] && rm -f /tmp/.sapstream1128
}

#
# check_saphostexec : Before using saphostctrl we make sure that the
#                     saphostexec is running on the current node.
#
check_saphostexec() {
  chkrc=$OCF_SUCCESS
  running=$(pgrep -f "$SAPHOSTEXEC" | wc -l)

  if [ $running -gt 0 ]; then
    if background_check_saphostexec; then
      return $OCF_SUCCESS
    else
      ocf_log warn "saphostexec did not respond to the method 'ListDatabases' correctly (rc=$?), it will be killed now"
      running=0
    fi
  fi

  if [ $running -eq 0 ]; then
    ocf_log warn "saphostexec is not running on node `hostname`, it will be started now"
    cleanup_saphostexec
    output=`$SAPHOSTEXEC -restart 2>&1`

    # now make sure the daemon has been started and is able to respond
    srvrc=1
    while [ $srvrc -ne 0 ] && [ "$(pgrep -f "$SAPHOSTEXEC" | wc -l)" -gt 0 ]
    do
      sleep 1
      background_check_saphostexec
      srvrc=$?
    done

    if [ $srvrc -eq 0 ]
    then
      ocf_log info "saphostexec on node $(hostname) was restarted !"
      chkrc=$OCF_SUCCESS
    else
      ocf_log error "saphostexec on node $(hostname) could not be started! - $output"
      chkrc=$OCF_ERR_GENERIC
    fi
  fi

  return $chkrc
}


#
# sapdatabase_start : Start the SAP database
#
sapdatabase_start() {

  check_saphostexec
  rc=$?

  if [ $rc -eq $OCF_SUCCESS ]
  then
    sapuserexit PRE_START_USEREXIT "$OCF_RESKEY_PRE_START_USEREXIT"

    DBINST=""
    if [ -n "$OCF_RESKEY_DBINSTANCE" ]
    then
      DBINST="-dbinstance $OCF_RESKEY_DBINSTANCE "
    fi
    FORCE=""
    if ocf_is_true $OCF_RESKEY_AUTOMATIC_RECOVER
    then
      FORCE="-force"
    fi
    DBOSUSER=""
    if [ -n "$OCF_RESKEY_DBOSUSER" ]
    then
      DBOSUSER="-dbuser $OCF_RESKEY_DBOSUSER "
    fi
    output=`$SAPHOSTCTRL -function StartDatabase -dbname $SID -dbtype $DBTYPE $DBINST $DBOSUSER $FORCE -service`

    sapdatabase_monitor 1
    rc=$?

    if [ $rc -eq 0 ]
    then
      ocf_log info "SAP database $SID started: $output"
      rc=$OCF_SUCCESS

      sapuserexit POST_START_USEREXIT "$OCF_RESKEY_POST_START_USEREXIT"
    else
      ocf_log err "SAP database $SID start failed: $output"
      rc=$OCF_ERR_GENERIC
    fi
  fi

  return $rc
}

#
# sapdatabase_stop: Stop the SAP database
#
sapdatabase_stop() {

  check_saphostexec
  rc=$?

  if [ $rc -eq $OCF_SUCCESS ]
  then
    sapuserexit PRE_STOP_USEREXIT "$OCF_RESKEY_PRE_STOP_USEREXIT"

    DBINST=""
    if [ -n "$OCF_RESKEY_DBINSTANCE" ]
    then
      DBINST="-dbinstance $OCF_RESKEY_DBINSTANCE "
    fi
    DBOSUSER=""
    if [ -n "$OCF_RESKEY_DBOSUSER" ]
    then
      DBOSUSER="-dbuser $OCF_RESKEY_DBOSUSER "
    fi
    output=`$SAPHOSTCTRL -function StopDatabase -dbname $SID -dbtype $DBTYPE $DBINST $DBOSUSER -force -service`

    if [ $? -eq 0 ]
    then
      ocf_log info "SAP database $SID stopped: $output"
      rc=$OCF_SUCCESS
    else
      ocf_log err "SAP database $SID stop failed: $output"
      rc=$OCF_ERR_GENERIC
    fi
  fi

  sapuserexit POST_STOP_USEREXIT "$OCF_RESKEY_POST_STOP_USEREXIT"

  return $rc
}


#
# sapdatabase_monitor: Can the given database instance do anything useful?
#
sapdatabase_monitor() {
  strict=$1
  rc=$OCF_SUCCESS

  if ! ocf_is_true $strict
  then
    sapdatabase_status
    rc=$?
  else
    check_saphostexec
    rc=$?

    if [ $rc -eq $OCF_SUCCESS ]
    then
      count=0

      DBINST=""
      if [ -n "$OCF_RESKEY_DBINSTANCE" ]
      then
        DBINST="-dbinstance $OCF_RESKEY_DBINSTANCE "
      fi
      if [ -n "$OCF_RESKEY_DBOSUSER" ]
      then
        DBOSUSER="-dbuser $OCF_RESKEY_DBOSUSER "
      fi
      output=`$SAPHOSTCTRL -function GetDatabaseStatus -dbname $SID -dbtype $DBTYPE $DBINST $DBOSUSER`

      # we have to parse the output, because the returncode doesn't tell anything about the instance status
      for SERVICE in `echo "$output" | grep -i 'Component[ ]*Name *[:=] [A-Za-z][A-Za-z0-9_]* (' | sed 's/^.*Component[ ]*Name *[:=] *\([A-Za-z][A-Za-z0-9_]*\).*$/\1/i'`
      do
        COLOR=`echo "$output" | grep -i "Component[ ]*Name *[:=] *$SERVICE (" | sed 's/^.*Status *[:=] *\([A-Za-z][A-Za-z0-9_]*\).*$/\1/i' | uniq`
        STATE=0

        case $COLOR in
          Running)       STATE=$OCF_SUCCESS;;
          *)             STATE=$OCF_NOT_RUNNING;;
        esac

        SEARCH=`echo "$OCF_RESKEY_MONITOR_SERVICES" | sed 's/\+/\\\+/g' | sed 's/\./\\\./g'`
        if [ `echo "$SERVICE" | egrep -c "$SEARCH"` -eq 1 ]
        then
            if [ $STATE -eq $OCF_NOT_RUNNING ]
            then
              ocf_log err "SAP database service $SERVICE is not running with status $COLOR !"
              rc=$STATE
            fi
            count=1
        fi
      done

      if [ $count -eq 0 -a $rc -eq $OCF_SUCCESS ]
      then
        ocf_log err "The resource does not run any services which this RA could monitor!"
        rc=$OCF_ERR_ARGS
      fi

      if [ $rc -ne $OCF_SUCCESS ]
      then
        ocf_log err "The SAP database $SID is not running: $output"
      fi
    fi
  fi
  return $rc
}


#
# sapdatabase_status: Are there any database processes on this host ?
#
sapdatabase_status() {
  sid=`echo $SID | tr '[:upper:]' '[:lower:]'`

  SUSER=${OCF_RESKEY_DBOSUSER:-""}

  case $DBTYPE in
    ADA) SEARCH="$SID/db/pgm/kernel"
         [ -z "$SUSER" ] && SUSER=`grep "^SdbOwner" /etc/opt/sdb | awk -F'=' '{print $2}'`
         SNUM=2
         ;;
    ORA) DBINST=${OCF_RESKEY_DBINSTANCE}
          DBINST=${OCF_RESKEY_DBINSTANCE:-${SID}}
          SEARCH="ora_[a-z][a-z][a-z][a-z]_$DBINST"

          if [ -z "$SUSER" ]; then
            id "oracle" > /dev/null 2> /dev/null && SUSER="oracle"
            id "ora${sid}" > /dev/null 2> /dev/null && SUSER="${SUSER:+${SUSER},}ora${sid}"
          fi

          SNUM=4
         ;;
    DB6) SEARCH="db2[a-z][a-z][a-z]"
         [ -z "$SUSER" ] && SUSER="db2${sid}"
         SNUM=2
         ;;
    SYB) SEARCH="dataserver"
         [ -z "$SUSER" ] && SUSER="syb${sid}"
         SNUM=1
		 ;;
    HDB) SEARCH="hdb[a-z]*server"
         [ -z "$SUSER" ] && SUSER="${sid}adm"
         SNUM=1
		 ;;
  esac

  [ -z "$SUSER" ] && return $OCF_ERR_INSTALLED

  cnt=`ps -u $SUSER -o args 2> /dev/null | grep -v grep | grep -c $SEARCH`
  [ $cnt -ge $SNUM ] && return $OCF_SUCCESS
  return $OCF_NOT_RUNNING
}


#
# sapdatabase_recover:
#
sapdatabase_recover() {
  OCF_RESKEY_AUTOMATIC_RECOVER=1
  sapdatabase_stop
  sapdatabase_start
}


#
# sapdatabase_validate: Check the semantics of the input parameters
#
sapdatabase_validate() {
  rc=$OCF_SUCCESS
  if [ `echo "$SID" | grep -c '^[A-Z][A-Z0-9][A-Z0-9]$'` -ne 1 ]
  then
    ocf_log err "Parsing parameter SID: '$SID' is not a valid system ID!"
    rc=$OCF_ERR_ARGS
  fi

  case "$DBTYPE" in
   ORA|ADA|DB6|SYB|HDB) ;;
   *) ocf_log err "Parsing parameter DBTYPE: '$DBTYPE' is not a supported database type!"
      rc=$OCF_ERR_ARGS ;;
  esac

  return $rc
}

#
# sapdatabase_init: initialize global variables at the beginning
#
sapdatabase_init() {
OCF_RESKEY_AUTOMATIC_RECOVER_default=0
: ${OCF_RESKEY_AUTOMATIC_RECOVER=${OCF_RESKEY_AUTOMATIC_RECOVER_default}}

if [ -z "$OCF_RESKEY_MONITOR_SERVICES" ]
then
  case $DBTYPE in
    ORA) export OCF_RESKEY_MONITOR_SERVICES="Instance|Database|Listener"
         ;;
    ADA) export OCF_RESKEY_MONITOR_SERVICES="Database"
         ;;
    DB6) db2sid="db2`echo $SID | tr '[:upper:]' '[:lower:]'`"
         export OCF_RESKEY_MONITOR_SERVICES="${SID}|${db2sid}"
         ;;
    SYB) export OCF_RESKEY_MONITOR_SERVICES="Server"
         ;;
    HDB) export OCF_RESKEY_MONITOR_SERVICES="hdbindexserver|hdbnameserver"
         ;;
  esac
fi
}