Debian Codegraph @

heartbeat/rabbitmq-server-ha

raw
1 #!/bin/sh
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at
5 #
6 # https://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13 #
14 # See usage() function below for more details ...
15 #
16 # Note that the script uses an external file to setup RabbitMQ policies
17 # so make sure to create it from an example shipped with the package.
18 #
19 #######################################################################
20 # Initialization:
21
22 : ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
23 . ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
24
25 #######################################################################
26
27 # Fill in some defaults if no values are specified
28
29 PATH=/sbin:/usr/sbin:/bin:/usr/bin
30
31 OCF_RESKEY_binary_default="/usr/sbin/rabbitmq-server"
32 OCF_RESKEY_ctl_default="/usr/sbin/rabbitmqctl"
33 OCF_RESKEY_debug_default=false
34 OCF_RESKEY_username_default="rabbitmq"
35 OCF_RESKEY_groupname_default="rabbitmq"
36 OCF_RESKEY_admin_user_default="guest"
37 OCF_RESKEY_admin_password_default="guest"
38 OCF_RESKEY_definitions_dump_file_default="/etc/rabbitmq/definitions"
39 OCF_RESKEY_pid_file_default="/var/run/rabbitmq/pid"
40 OCF_RESKEY_log_dir_default="/var/log/rabbitmq"
41 OCF_RESKEY_mnesia_base_default="/var/lib/rabbitmq/mnesia"
42 OCF_RESKEY_mnesia_schema_base_default="/var/lib/rabbitmq"
43 OCF_RESKEY_host_ip_default="127.0.0.1"
44 OCF_RESKEY_node_port_default=5672
45 OCF_RESKEY_default_vhost_default="/"
46 OCF_RESKEY_erlang_cookie_default=false
47 OCF_RESKEY_erlang_cookie_file_default="/var/lib/rabbitmq/.erlang.cookie"
48 OCF_RESKEY_use_fqdn_default=false
49 OCF_RESKEY_fqdn_prefix_default=""
50 OCF_RESKEY_max_rabbitmqctl_timeouts_default=3
51 OCF_RESKEY_policy_file_default="/usr/local/sbin/set_rabbitmq_policy"
52 OCF_RESKEY_rmq_feature_health_check_default=true
53 OCF_RESKEY_rmq_feature_local_list_queues_default=true
54 OCF_RESKEY_limit_nofile_default=65535
55 OCF_RESKEY_avoid_using_iptables_default=false
56 OCF_RESKEY_allowed_cluster_nodes_default=""
57
58 : ${HA_LOGTAG="lrmd"}
59 : ${HA_LOGFACILITY="daemon"}
60 : ${OCF_RESKEY_binary=${OCF_RESKEY_binary_default}}
61 : ${OCF_RESKEY_ctl=${OCF_RESKEY_ctl_default}}
62 : ${OCF_RESKEY_debug=${OCF_RESKEY_debug_default}}
63 : ${OCF_RESKEY_username=${OCF_RESKEY_username_default}}
64 : ${OCF_RESKEY_groupname=${OCF_RESKEY_groupname_default}}
65 : ${OCF_RESKEY_admin_user=${OCF_RESKEY_admin_user_default}}
66 : ${OCF_RESKEY_admin_password=${OCF_RESKEY_admin_password_default}}
67 : ${OCF_RESKEY_definitions_dump_file=${OCF_RESKEY_definitions_dump_file_default}}
68 : ${OCF_RESKEY_log_dir=${OCF_RESKEY_log_dir_default}}
69 : ${OCF_RESKEY_mnesia_base=${OCF_RESKEY_mnesia_base_default}}
70 : ${OCF_RESKEY_mnesia_schema_base=${OCF_RESKEY_mnesia_schema_base_default}}
71 : ${OCF_RESKEY_pid_file=${OCF_RESKEY_pid_file_default}}
72 : ${OCF_RESKEY_node_port=${OCF_RESKEY_node_port_default}}
73 : ${OCF_RESKEY_default_vhost=${OCF_RESKEY_default_vhost_default}}
74 : ${OCF_RESKEY_erlang_cookie=${OCF_RESKEY_erlang_cookie_default}}
75 : ${OCF_RESKEY_erlang_cookie_file=${OCF_RESKEY_erlang_cookie_file_default}}
76 : ${OCF_RESKEY_use_fqdn=${OCF_RESKEY_use_fqdn_default}}
77 : ${OCF_RESKEY_fqdn_prefix=${OCF_RESKEY_fqdn_prefix_default}}
78 : ${OCF_RESKEY_max_rabbitmqctl_timeouts=${OCF_RESKEY_max_rabbitmqctl_timeouts_default}}
79 : ${OCF_RESKEY_policy_file=${OCF_RESKEY_policy_file_default}}
80 : ${OCF_RESKEY_rmq_feature_health_check=${OCF_RESKEY_rmq_feature_health_check_default}}
81 : ${OCF_RESKEY_rmq_feature_local_list_queues=${OCF_RESKEY_rmq_feature_local_list_queues_default}}
82 : ${OCF_RESKEY_limit_nofile=${OCF_RESKEY_limit_nofile_default}}
83 : ${OCF_RESKEY_avoid_using_iptables=${OCF_RESKEY_avoid_using_iptables_default}}
84 : ${OCF_RESKEY_allowed_cluster_nodes=${OCF_RESKEY_allowed_cluster_nodes_default}}
85
86 #######################################################################
87
88 OCF_RESKEY_CRM_meta_timeout_default=30000
89 : ${OCF_RESKEY_CRM_meta_timeout=${OCF_RESKEY_CRM_meta_timeout_default}}
90 OCF_RESKEY_start_time_default=$((OCF_RESKEY_CRM_meta_timeout / 6000 + 2))
91 : ${OCF_RESKEY_start_time=${OCF_RESKEY_start_time_default}}
92 OCF_RESKEY_stop_time_default=${OCF_RESKEY_start_time_default}
93 : ${OCF_RESKEY_stop_time=${OCF_RESKEY_start_time_default}}
94 OCF_RESKEY_command_timeout_default=""
95 : ${OCF_RESKEY_command_timeout=${OCF_RESKEY_command_timeout_default}}
96 TIMEOUT_ARG=$((OCF_RESKEY_CRM_meta_timeout / 6000 + 30))
97 COMMAND_TIMEOUT="/usr/bin/timeout ${OCF_RESKEY_command_timeout} ${TIMEOUT_ARG}"
98 RESOURCE_NAME=`echo $OCF_RESOURCE_INSTANCE | cut -d ":" -f 1`
99
100 #######################################################################
101
102 usage() {
103 cat <<UEND
104 usage: $0 (start|stop|validate-all|meta-data|status|monitor)
105
106 $0 manages an ${OCF_RESKEY_binary} process as an HA resource
107
108 The 'start' operation starts the networking service.
109 The 'stop' operation stops the networking service.
110 The 'validate-all' operation reports whether the parameters are valid
111 The 'meta-data' operation reports this RA's meta-data information
112 The 'status' operation reports whether the networking service is running
113 The 'monitor' operation reports whether the networking service seems to be working
114
115 UEND
116 }
117
118 meta_data() {
119 # The EXTENDED_OCF_PARAMS parameter below does not exist by default
120 # and hence converted to an empty string unless overridden. It
121 # could be used by an extention script to add new parameters. For
122 # example see https://review.openstack.org/#/c/249180/10
123
124 cat <<END
125 <?xml version="1.0"?>
126 <!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
127 <resource-agent name="rabbitmq-server-ha">
128 <version>1.0</version>
129
130 <longdesc lang="en">
131 Resource agent for RabbitMQ promotes a node, then cluster nodes can join it
132 </longdesc>
133 <shortdesc lang="en">Resource agent for RabbitMQ HA cluster</shortdesc>
134 <parameters>
135
136 <parameter name="binary" unique="0" required="0">
137 <longdesc lang="en">
138 RabbitMQ binary
139 </longdesc>
140 <shortdesc lang="en">RabbitMQ binary</shortdesc>
141 <content type="string" default="${OCF_RESKEY_binary_default}" />
142 </parameter>
143
144 <parameter name="ctl" unique="0" required="0">
145 <longdesc lang="en">
146 rabbitctl binary
147 </longdesc>
148 <shortdesc lang="en">rabbitctl binary binary</shortdesc>
149 <content type="string" default="${OCF_RESKEY_ctl_default}" />
150 </parameter>
151
152 <parameter name="pid_file" unique="0" required="0">
153 <longdesc lang="en">
154 RabbitMQ PID file
155 </longdesc>
156 <shortdesc lang="en">RabbitMQ PID file</shortdesc>
157 <content type="string" default="${OCF_RESKEY_pid_file_default}" />
158 </parameter>
159
160 <parameter name="log_dir" unique="0" required="0">
161 <longdesc lang="en">
162 RabbitMQ log directory
163 </longdesc>
164 <shortdesc lang="en">RabbitMQ log directory</shortdesc>
165 <content type="string" default="${OCF_RESKEY_log_dir_default}" />
166 </parameter>
167
168 <parameter name="username" unique="0" required="0">
169 <longdesc lang="en">
170 RabbitMQ user name
171 </longdesc>
172 <shortdesc lang="en">RabbitMQ user name</shortdesc>
173 <content type="string" default="${OCF_RESKEY_username_default}" />
174 </parameter>
175
176 <parameter name="groupname" unique="0" required="0">
177 <longdesc lang="en">
178 RabbitMQ group name
179 </longdesc>
180 <shortdesc lang="en">RabbitMQ group name</shortdesc>
181 <content type="string" default="${OCF_RESKEY_groupname_default}" />
182 </parameter>
183
184 <parameter name="admin_user" unique="0" required="0">
185 <longdesc lang="en">
186 RabbitMQ default admin user for API
187 </longdesc>
188 <shortdesc lang="en">RabbitMQ admin user</shortdesc>
189 <content type="string" default="${OCF_RESKEY_admin_user_default}" />
190 </parameter>
191
192 <parameter name="admin_password" unique="0" required="0">
193 <longdesc lang="en">
194 RabbitMQ default admin user password for API
195 </longdesc>
196 <shortdesc lang="en">RabbitMQ admin password</shortdesc>
197 <content type="string" default="${OCF_RESKEY_admin_password_default}" />
198 </parameter>
199
200 <parameter name="definitions_dump_file" unique="0" required="0">
201 <longdesc lang="en">
202 RabbitMQ default definitions dump file
203 </longdesc>
204 <shortdesc lang="en">RabbitMQ definitions dump file</shortdesc>
205 <content type="string" default="${OCF_RESKEY_definitions_dump_file}" />
206 </parameter>
207
208 <parameter name="command_timeout" unique="0" required="0">
209 <longdesc lang="en">
210 Timeout command arguments for issued commands termination (value is auto evaluated)
211 </longdesc>
212 <shortdesc lang="en">Arguments for timeout wrapping command</shortdesc>
213 <content type="string" default="${OCF_RESKEY_command_timeout_default}" />
214 </parameter>
215
216 <parameter name="start_time" unique="0" required="0">
217 <longdesc lang="en">
218 Timeout for start rabbitmq server
219 </longdesc>
220 <shortdesc lang="en">Timeout for start rabbitmq server</shortdesc>
221 <content type="string" default="${OCF_RESKEY_start_time_default}" />
222 </parameter>
223
224 <parameter name="stop_time" unique="0" required="0">
225 <longdesc lang="en">
226 Timeout for stopping rabbitmq server
227 </longdesc>
228 <shortdesc lang="en">Timeout for stopping rabbitmq server</shortdesc>
229 <content type="string" default="${OCF_RESKEY_stop_time_default}" />
230 </parameter>
231
232 <parameter name="debug" unique="0" required="0">
233 <longdesc lang="en">
234 The debug flag for agent (${OCF_RESKEY_binary}) instance.
235 In the /tmp/ directory will be created rmq-* files for log
236 some operations and ENV values inside OCF-script.
237 </longdesc>
238 <shortdesc lang="en">AMQP server (${OCF_RESKEY_binary}) debug flag</shortdesc>
239 <content type="boolean" default="${OCF_RESKEY_debug_default}" />
240 </parameter>
241
242 <parameter name="mnesia_base" unique="0" required="0">
243 <longdesc lang="en">
244 Base directory for storing Mnesia files
245 </longdesc>
246 <shortdesc lang="en">Base directory for storing Mnesia files</shortdesc>
247 <content type="boolean" default="${OCF_RESKEY_mnesia_base_default}" />
248 </parameter>
249
250 <parameter name="mnesia_schema_base" unique="0" required="0">
251 <longdesc lang="en">
252 Parent directory for Mnesia schema directory
253 </longdesc>
254 <shortdesc lang="en">Parent directory for Mnesia schema directory</shortdesc>
255 <content type="string" default="${OCF_RESKEY_mnesia_schema_base_default}" />
256 </parameter>
257
258 <parameter name="host_ip" unique="0" required="0">
259 <longdesc lang="en">
260 ${OCF_RESKEY_binary} should listen on this IP address
261 </longdesc>
262 <shortdesc lang="en">${OCF_RESKEY_binary} should listen on this IP address</shortdesc>
263 <content type="boolean" default="${OCF_RESKEY_host_ip_default}" />
264 </parameter>
265
266 <parameter name="node_port" unique="0" required="0">
267 <longdesc lang="en">
268 ${OCF_RESKEY_binary} should listen on this port
269 </longdesc>
270 <shortdesc lang="en">${OCF_RESKEY_binary} should listen on this port</shortdesc>
271 <content type="boolean" default="${OCF_RESKEY_node_port_default}" />
272 </parameter>
273
274 <parameter name="default_vhost" unique="0" required="0">
275 <longdesc lang="en">
276 Default virtual host used for monitoring if a node is fully synchronized with
277 the rest of the cluster. In normal operation, the resource agent will wait for
278 queues from this virtual host on this node to be synchronized elsewhere before
279 stopping RabbitMQ. This also means queues in other virtual hosts may not be
280 fully synchronized on stop operations.
281 </longdesc>
282 <shortdesc lang="en">Default virtual host used for waiting for synchronization</shortdesc>
283 <content type="string" default="${OCF_RESKEY_default_vhost_default}" />
284 </parameter>
285
286 <parameter name="erlang_cookie" unique="0" required="0">
287 <longdesc lang="en">
288 Erlang cookie for clustering. If specified, will be updated at the mnesia reset
289 </longdesc>
290 <shortdesc lang="en">Erlang cookie</shortdesc>
291 <content type="boolean" default="${OCF_RESKEY_erlang_cookie_default}" />
292 </parameter>
293
294 <parameter name="erlang_cookie_file" unique="0" required="0">
295 <longdesc lang="en">
296 Erlang cookie file path where the cookie will be put, if requested
297 </longdesc>
298 <shortdesc lang="en">Erlang cookie file</shortdesc>
299 <content type="boolean" default="${OCF_RESKEY_erlang_cookie_file_default}" />
300 </parameter>
301
302 <parameter name="use_fqdn" unique="0" required="0">
303 <longdesc lang="en">
304 Either to use FQDN or a shortname for the rabbitmq node
305 </longdesc>
306 <shortdesc lang="en">Use FQDN</shortdesc>
307 <content type="boolean" default="${OCF_RESKEY_use_fqdn_default}" />
308 </parameter>
309
310 <parameter name="fqdn_prefix" unique="0" required="0">
311 <longdesc lang="en">
312 Optional FQDN prefix for RabbitMQ nodes in cluster.
313 FQDN prefix can be specified to host multiple RabbitMQ instances on a node or
314 in case of RabbitMQ running in dedicated network/interface.
315 </longdesc>
316 <shortdesc lang="en">FQDN prefix</shortdesc>
317 <content type="string" default="${OCF_RESKEY_fqdn_prefix_default}" />
318 </parameter>
319
320 <parameter name="max_rabbitmqctl_timeouts" unique="0" required="0">
321 <longdesc lang="en">
322 If during monitor call rabbitmqctl times out, the timeout is ignored
323 unless it is Nth timeout in a row. Here N is the value of the current parameter.
324 If too many timeouts happen in a raw, the monitor call will return with error.
325 </longdesc>
326 <shortdesc lang="en">Fail only if that many rabbitmqctl timeouts in a row occurred</shortdesc>
327 <content type="string" default="${OCF_RESKEY_max_rabbitmqctl_timeouts_default}" />
328 </parameter>
329
330 <parameter name="policy_file" unique="0" required="0">
331 <longdesc lang="en">
332 A path to the shell script to setup RabbitMQ policies
333 </longdesc>
334 <shortdesc lang="en">A policy file path</shortdesc>
335 <content type="string" default="${OCF_RESKEY_policy_file_default}" />
336 </parameter>
337
338 <parameter name="rmq_feature_health_check" unique="0" required="0">
339 <longdesc lang="en">
340 Since rabbit 3.6.4 list_queues/list_channels-based monitoring should
341 be replaced with "node_health_check" command, as it creates no network
342 load at all.
343 </longdesc>
344 <shortdesc lang="en">Use node_health_check for monitoring</shortdesc>
345 <content type="boolean" default="${OCF_RESKEY_rmq_feature_health_check_default}" />
346 </parameter>
347
348 <parameter name="rmq_feature_local_list_queues" unique="0" required="0">
349 <longdesc lang="en">
350 For rabbit version that implements --local flag for list_queues, this
351 can greatly reduce network overhead in cases when node is
352 stopped/demoted.
353 </longdesc>
354 <shortdesc lang="en">Use --local option for list_queues</shortdesc>
355 <content type="boolean" default="${OCF_RESKEY_rmq_feature_local_list_queues_default}" />
356 </parameter>
357
358 <parameter name="limit_nofile" unique="0" required="0">
359 <longdesc lang="en">
360 Soft and hard limit for NOFILE
361 </longdesc>
362 <shortdesc lang="en">NOFILE limit</shortdesc>
363 <content type="string" default="${OCF_RESKEY_limit_nofile_default}" />
364 </parameter>
365
366 <parameter name="avoid_using_iptables" unique="0" required="0">
367 <longdesc lang="en">
368 When set to true the iptables calls to block client access become
369 noops. This is useful when we run inside containers.
370 </longdesc>
371 <shortdesc lang="en">Disable iptables use entirely</shortdesc>
372 <content type="boolean" default="${OCF_RESKEY_avoid_using_iptables_default}" />
373 </parameter>
374
375 <parameter name="allowed_cluster_nodes" unique="0" required="0">
376 <longdesc lang="en">
377 When set to anything other than the empty string it must container the list of
378 cluster node names, separated by spaces, where the rabbitmq resource is allowed to run.
379 Tis is needed when rabbitmq is running on a subset of nodes part of a larger
380 cluster. The default ("") is to assume that all nodes part of the cluster will
381 run the rabbitmq resource.
382 </longdesc>
383 <shortdesc lang="en">List of cluster nodes where rabbitmq is allowed to run</shortdesc>
384 <content type="string" default="${OCF_RESKEY_allowed_cluster_nodes}" />
385 </parameter>
386
387 $EXTENDED_OCF_PARAMS
388
389 </parameters>
390
391 <actions>
392 <action name="start" timeout="20s" />
393 <action name="stop" timeout="20s" />
394 <action name="status" timeout="20s" />
395 <action name="monitor" depth="0" timeout="30s" interval="5s" />
396 <action name="monitor" depth="0" timeout="30s" interval="3s" role="Promoted"/>
397 <action name="promote" timeout="30s" />
398 <action name="demote" timeout="30s" />
399 <action name="notify" timeout="20s" />
400 <action name="validate-all" timeout="5s" />
401 <action name="meta-data" timeout="5s" />
402 </actions>
403 </resource-agent>
404 END
405 }
406
407
408 MIN_MASTER_SCORE=100
409 BEST_MASTER_SCORE=1000
410
411
412 #######################################################################
413 # Functions invoked by resource manager actions
414
415 #TODO(bogdando) move proc_kill, proc_stop to shared OCF functions
416 # to be shipped with HA cluster packages
417 ###########################################################
418 # Attempts to kill a process with retries and checks procfs
419 # to make sure the process is stopped.
420 #
421 # Globals:
422 # LL
423 # Arguments:
424 # $1 - pid of the process to try and kill
425 # $2 - service name used for logging and match-based kill, if the pid is "none"
426 # $3 - signal to use, defaults to SIGTERM
427 # $4 - number of retries, defaults to 5
428 # $5 - time to sleep between retries, defaults to 2
429 # Returns:
430 # 0 - if successful
431 # 1 - if process is still running according to procfs
432 # 2 - if invalid parameters passed in
433 ###########################################################
434 proc_kill()
435 {
436 local pid="${1}"
437 local service_name="${2}"
438 local signal="${3:-SIGTERM}"
439 local count="${4:-5}"
440 local process_sleep="${5:-2}"
441 local LH="${LL} proc_kill():"
442 local pgrp="$(ps -o pgid= ${pid} 2>/dev/null | tr -d '[[:space:]]')"
443
444 if [ "${pid}" ] && [ "${pgrp}" = "1" ] ; then
445 ocf_log err "${LH} shall not kill by the bad pid 1 (init)!"
446 return 2
447 fi
448
449 if [ "${pid}" = "none" ]; then
450 local matched
451 matched="$(pgrep -fla ${service_name})"
452 if [ -z "${matched}" ] ; then
453 ocf_log info "${LH} cannot find any processes matching the ${service_name}, considering target process to be already dead"
454 return 0
455 fi
456 ocf_log debug "${LH} no pid provided, will try the ${service_name}, matched list: ${matched}"
457 while [ $count -gt 0 ]; do
458 if [ -z "${matched}" ]; then
459 break
460 else
461 matched="$(pgrep -fla ${service_name})"
462 ocf_log debug "${LH} Stopping ${service_name} with ${signal}..."
463 ocf_run pkill -f -"${signal}" "${service_name}"
464 fi
465 sleep $process_sleep
466 count=$(( count-1 ))
467 done
468 pgrep -f "${service_name}" > /dev/null
469 if [ $? -ne 0 ] ; then
470 ocf_log debug "${LH} Stopped ${service_name} with ${signal}"
471 return 0
472 else
473 ocf_log warn "${LH} Failed to stop ${service_name} with ${signal}"
474 return 1
475 fi
476 else
477 # pid is not none
478 while [ $count -gt 0 ]; do
479 if [ ! -d "/proc/${pid}" ]; then
480 break
481 else
482 ocf_log debug "${LH} Stopping ${service_name} with ${signal}..."
483 ocf_run pkill -"${signal}" -g "${pgrp}"
484 fi
485 sleep $process_sleep
486 count=$(( count-1 ))
487 done
488
489 # Check if the process ended after the last sleep
490 if [ ! -d "/proc/${pid}" ] ; then
491 ocf_log debug "${LH} Stopped ${service_name} with ${signal}"
492 return 0
493 fi
494
495 ocf_log warn "${LH} Failed to stop ${service_name} with ${signal}"
496 return 1
497 fi
498 }
499
500 ###########################################################
501 # Attempts to kill a process with the given pid or pid file
502 # using proc_kill and will retry with sigkill if sigterm is
503 # unsuccessful.
504 #
505 # Globals:
506 # OCF_ERR_GENERIC
507 # OCF_SUCCESS
508 # LL
509 # Arguments:
510 # $1 - pidfile or pid or 'none', if stopping by the name matching
511 # $2 - service name used for logging or for the failback stopping method
512 # $3 - stop process timeout (in sec), used to determine how many times we try
513 # SIGTERM and an upper limit on how long this function should try and
514 # stop the process. Defaults to 15.
515 # Returns:
516 # OCF_SUCCESS - if successful
517 # OCF_ERR_GENERIC - if process is still running according to procfs
518 ###########################################################
519 proc_stop()
520 {
521 local pid_param="${1}"
522 local service_name="${2}"
523 local timeout="${3:-15}"
524 local LH="${LL} proc_stop():"
525 local i
526 local pid
527 local pidfile
528 if [ "${pid_param}" = "none" ] ; then
529 pid="none"
530 else
531 # check if provide just a number
532 echo "${pid_param}" | $EGREP -q '^[0-9]+$'
533 if [ $? -eq 0 ]; then
534 pid="${pid_param}"
535 elif [ -e "${pid_param}" ]; then # check if passed in a pid file
536 pidfile="${pid_param}"
537 pid=$(cat "${pidfile}" 2>/dev/null | tr -s " " "\n" | sort -u)
538 else
539 ocf_log warn "${LH} pid param ${pid_param} is not a file or a number, try match by ${service_name}"
540 pid="none"
541 fi
542 fi
543 # number of times to try a SIGTEM is (timeout - 5 seconds) / 2 seconds
544 local stop_count=$(( ($timeout-5)/2 ))
545
546 # make sure we stop at least once
547 if [ $stop_count -le 0 ]; then
548 stop_count=1
549 fi
550
551 if [ -z "${pid}" ] ; then
552 ocf_log warn "${LH} unable to get PID from ${pidfile}, try match by ${service_name}"
553 pid="none"
554 fi
555
556 if [ -n "${pid}" ]; then
557 for i in ${pid} ; do
558 [ "${i}" ] || break
559 ocf_log info "${LH} Stopping ${service_name} by PID ${i}"
560 proc_kill "${i}" "${service_name}" SIGTERM $stop_count
561 if [ $? -ne 0 ]; then
562 # SIGTERM failed, send a single SIGKILL
563 proc_kill "${i}" "${service_name}" SIGKILL 1 2
564 if [ $? -ne 0 ]; then
565 ocf_log err "${LH} ERROR: could not stop ${service_name}"
566 return "${OCF_ERR_GENERIC}"
567 fi
568 fi
569 done
570 fi
571
572 # Remove the pid file here which will remove empty pid files as well
573 if [ -n "${pidfile}" ]; then
574 rm -f "${pidfile}"
575 fi
576
577 ocf_log info "${LH} Stopped ${service_name}"
578 return "${OCF_SUCCESS}"
579 }
580
581 # Invokes the given command as a rabbitmq user and wrapped in the
582 # timeout command.
583 su_rabbit_cmd() {
584 local timeout
585 if [ "$1" = "-t" ]; then
586 timeout="/usr/bin/timeout ${OCF_RESKEY_command_timeout} $2"
587 shift 2
588 else
589 timeout=$COMMAND_TIMEOUT
590 fi
591 local cmd="${1:-status}"
592 local LH="${LL} su_rabbit_cmd():"
593 local rc=1
594 local user=$OCF_RESKEY_username
595 local mail=/var/spool/mail/rabbitmq
596 local pwd=/var/lib/rabbitmq
597 local home=/var/lib/rabbitmq
598
599 ocf_log debug "${LH} invoking a command: ${cmd}"
600 su $user -s /bin/sh -c "USER=${user} MAIL=${mail} PWD=${pwd} HOME=${home} LOGNAME=${user} \
601 ${timeout} ${cmd}"
602 rc=$?
603 ocf_log info "${LH} the invoked command exited ${rc}: ${cmd}"
604 return $rc
605 }
606
607 now() {
608 date -u +%s
609 }
610
611 set_limits() {
612 local current_limit=$(su $OCF_RESKEY_username -s /bin/sh -c "ulimit -n")
613 if [ ! -z $OCF_RESKEY_limit_nofile ] && [ $OCF_RESKEY_limit_nofile -gt $current_limit ] ; then
614 ulimit -n $OCF_RESKEY_limit_nofile
615 fi
616 }
617
618 master_score() {
619 local LH="${LL} master_score():"
620 local score=$1
621 if [ -z $score ] ; then
622 score=0
623 fi
624 ocf_log info "${LH} Updating master score attribute with ${score}"
625 ocf_run crm_master -N $THIS_PCMK_NODE -l reboot -v $score || return $OCF_ERR_GENERIC
626 return $OCF_SUCCESS
627 }
628
629 # Return either FQDN or shortname, depends on the OCF_RESKEY_use_fqdn.
630 get_hostname() {
631 local os=$(uname -s)
632 if ! ocf_is_true "${OCF_RESKEY_use_fqdn}"; then
633 if [ "$os" = "SunOS" ]; then
634 echo "$(hostname | sed 's@\..*@@')"
635 else
636 echo "$(hostname -s)"
637 fi
638 else
639 if [ "$os" = "SunOS" ]; then
640 echo "$(hostname)"
641 else
642 echo "$(hostname -f)"
643 fi
644 fi
645 }
646
647 # Strip the FQDN to the shortname, if OCF_RESKEY_use_fqdn was set;
648 # Prepend prefix to the hostname
649 process_fqdn() {
650 if ! ocf_is_true "${OCF_RESKEY_use_fqdn}"; then
651 echo "${OCF_RESKEY_fqdn_prefix}$1" | awk -F. '{print $1}'
652 else
653 echo "${OCF_RESKEY_fqdn_prefix}$1"
654 fi
655 }
656
657 # Return OCF_SUCCESS, if current host is in the list of given hosts.
658 # Otherwise, return 10
659 my_host() {
660 local hostlist="$1"
661 local hostname
662 local hn
663 local rc=10
664 local LH="${LL} my_host():"
665
666 hostname=$(process_fqdn $(get_hostname))
667 ocf_log debug "${LH} hostlist is: $hostlist"
668 for host in $hostlist ; do
669 hn=$(process_fqdn "${host}")
670 ocf_log debug "${LH} comparing '$hostname' with '$hn'"
671 if [ "${hostname}" = "${hn}" ] ; then
672 rc=$OCF_SUCCESS
673 break
674 fi
675 done
676
677 return $rc
678 }
679
680 get_integer_node_attr() {
681 local value
682 value=$(crm_attribute -N $1 -l reboot --name "$2" --query 2>/dev/null | awk '{ split($3, vals, "="); if (vals[2] != "(null)") print vals[2] }')
683 if [ $? -ne 0 ] || [ -z "$value" ] ; then
684 value=0
685 fi
686 echo $value
687 }
688
689 get_node_start_time() {
690 get_integer_node_attr $1 'rabbit-start-time'
691 }
692
693 get_node_master_score() {
694 get_integer_node_attr $1 "master-${RESOURCE_NAME}"
695 }
696
697 # Return either rabbit node name as FQDN or shortname, depends on the OCF_RESKEY_use_fqdn.
698 rabbit_node_name() {
699 echo "rabbit@$(process_fqdn $(ocf_attribute_target $1))"
700 }
701
702 rmq_setup_env() {
703 local H
704 local dir
705 local name
706 H="$(get_hostname)"
707 export RABBITMQ_NODENAME=$(rabbit_node_name $H)
708 if [ "$OCF_RESKEY_node_port" != "$OCF_RESKEY_node_port_default" ]; then
709 export RABBITMQ_NODE_PORT=$OCF_RESKEY_node_port
710 fi
711 export RABBITMQ_PID_FILE=$OCF_RESKEY_pid_file
712 MNESIA_FILES="${OCF_RESKEY_mnesia_base}/$(rabbit_node_name $H)"
713 if ! ocf_is_true "${OCF_RESKEY_use_fqdn}"; then
714 name="-sname"
715 else
716 name="-name"
717 fi
718 export RABBITMQ_SERVER_START_ARGS="${RABBITMQ_SERVER_START_ARGS} -mnesia dir \"${MNESIA_FILES}\" ${name} $(rabbit_node_name $H)"
719 RMQ_START_TIME="${MNESIA_FILES}/ocf_server_start_time.txt"
720 MASTER_FLAG_FILE="${MNESIA_FILES}/ocf_master_for_${OCF_RESOURCE_INSTANCE}"
721 THIS_PCMK_NODE=$(ocf_attribute_target)
722 TOTALVMEM=`free -mt | awk '/Total:/ {print $2}'`
723 # check and make PID file dir
724 local PID_DIR=$( dirname $OCF_RESKEY_pid_file )
725 if [ ! -d ${PID_DIR} ] ; then
726 mkdir -p ${PID_DIR}
727 chown -R ${OCF_RESKEY_username}:${OCF_RESKEY_groupname} ${PID_DIR}
728 chmod 755 ${PID_DIR}
729 fi
730
731 # Regardless of whether we just created the directory or it
732 # already existed, check whether it is writable by the configured
733 # user
734 for dir in ${PID_DIR} "${OCF_RESKEY_mnesia_base}" "${OCF_RESKEY_log_dir}"; do
735 if test -e ${dir}; then
736 local files
737 files=$(su -s /bin/sh - $OCF_RESKEY_username -c "find ${dir} ! -writable")
738 if [ "${files}" ]; then
739 ocf_log warn "Directory ${dir} is not writable by ${OCF_RESKEY_username}, chowning."
740 chown -R ${OCF_RESKEY_username}:${OCF_RESKEY_groupname} "${dir}"
741 fi
742 fi
743 done
744
745 export LL="${OCF_RESOURCE_INSTANCE}[$$]:"
746 update_cookie
747 }
748
749 # Return a RabbitMQ node to its virgin state.
750 # For reset and force_reset to succeed the RabbitMQ application must have been stopped.
751 # If the app cannot be stopped, beam will be killed and mnesia files will be removed.
752 reset_mnesia() {
753 local LH="${LL} reset_mnesia():"
754 local make_amnesia=false
755 local rc=$OCF_ERR_GENERIC
756
757 # check status of a beam process
758 get_status
759 rc=$?
760 if [ $rc -eq 0 ] ; then
761 # beam is running
762 # check status of rabbit app and stop it, if it is running
763 get_status rabbit
764 rc=$?
765 if [ $rc -eq 0 ] ; then
766 # rabbit app is running, have to stop it
767 ocf_log info "${LH} Stopping RMQ-app prior to reset the mnesia."
768 stop_rmq_server_app
769 rc=$?
770 if [ $rc -ne 0 ] ; then
771 ocf_log warn "${LH} RMQ-app can't be stopped."
772 make_amnesia=true
773 fi
774 fi
775
776 if ! $make_amnesia ; then
777 # rabbit app is not running, reset mnesia
778 ocf_log info "${LH} Execute reset with timeout: ${TIMEOUT_ARG}"
779 su_rabbit_cmd "${OCF_RESKEY_ctl} reset"
780 rc=$?
781 if [ $rc -ne 0 ] ; then
782 ocf_log info "${LH} Execute force_reset with timeout: ${TIMEOUT_ARG}"
783 su_rabbit_cmd "${OCF_RESKEY_ctl} force_reset"
784 rc=$?
785 if [ $rc -ne 0 ] ; then
786 ocf_log warn "${LH} Mnesia couldn't cleaned, even by force-reset command."
787 make_amnesia=true
788 fi
789 fi
790 fi
791 else
792 # there is no beam running
793 make_amnesia=true
794 ocf_log warn "${LH} There is no Beam process running."
795 fi
796
797 # remove mnesia files, if required
798 if $make_amnesia ; then
799 kill_rmq_and_remove_pid
800 ocf_run rm -rf "${MNESIA_FILES}"
801 mnesia_schema_location="${OCF_RESKEY_mnesia_schema_base}/Mnesia.$(rabbit_node_name $(get_hostname))"
802 ocf_run rm -rf "$mnesia_schema_location"
803 ocf_log warn "${LH} Mnesia files appear corrupted and have been removed from ${MNESIA_FILES} and $mnesia_schema_location"
804 fi
805 # always return OCF SUCCESS
806 return $OCF_SUCCESS
807 }
808
809
810 block_client_access()
811 {
812 # When OCF_RESKEY_avoid_using_iptables is true iptables calls are noops
813 if ocf_is_true "${OCF_RESKEY_avoid_using_iptables}"; then
814 return $OCF_SUCCESS
815 fi
816 # do not add temporary RMQ blocking rule, if it is already exist
817 # otherwise, try to add a blocking rule with max of 5 retries
818 local tries=5
819 until $(iptables -nvL --wait | grep -q 'temporary RMQ block') || [ $tries -eq 0 ]; do
820 tries=$((tries-1))
821 iptables --wait -I INPUT -p tcp -m tcp --dport ${OCF_RESKEY_node_port} -m state --state NEW,RELATED,ESTABLISHED \
822 -m comment --comment 'temporary RMQ block' -j REJECT --reject-with tcp-reset
823 sleep 1
824 done
825 if [ $tries -eq 0 ]; then
826 return $OCF_ERR_GENERIC
827 else
828 return $OCF_SUCCESS
829 fi
830 }
831
832 unblock_client_access()
833 {
834 local lhtext="none"
835 if [ -z $1 ] ; then
836 lhtext=$1
837 fi
838 # When OCF_RESKEY_avoid_using_iptables is true iptables calls are noops
839 if ocf_is_true "${OCF_RESKEY_avoid_using_iptables}"; then
840 return
841 fi
842 # remove all temporary RMQ blocking rules, if there are more than one exist
843 for i in $(iptables -nvL --wait --line-numbers | awk '/temporary RMQ block/ {print $1}'); do
844 iptables --wait -D INPUT -p tcp -m tcp --dport ${OCF_RESKEY_node_port} -m state --state NEW,RELATED,ESTABLISHED \
845 -m comment --comment 'temporary RMQ block' -j REJECT --reject-with tcp-reset
846 done
847 ocf_log info "${lhtext} unblocked access to RMQ port"
848 }
849
850 get_nodes__base(){
851 local infotype=''
852 local rc=$OCF_ERR_GENERIC
853 local c_status
854
855 if [ "$1" = 'nodes' ]
856 then
857 infotype='db_nodes'
858 elif [ "$1" = 'running' ]
859 then
860 infotype='running_db_nodes'
861 fi
862 c_status=`${OCF_RESKEY_ctl} eval "mnesia:system_info(${infotype})." 2>/dev/null`
863 rc=$?
864 if [ $rc -ne 0 ] ; then
865 echo ''
866 return $OCF_ERR_GENERIC
867 fi
868 # translate line like '{running_nodes,['rabbit@node-1','rabbit@node-2','rabbit@node-3']},' to node_list
869 echo $(echo "${c_status}" | awk -F, '{ for (i=1;i<=NF;i++) { if ($i ~ /@/) { gsub(/[\[\]}{]/,"",$i); print $i; } }}' | tr -d "\'")
870 return $OCF_SUCCESS
871 }
872
873 get_nodes() {
874 echo $(get_nodes__base nodes)
875 return $?
876 }
877
878 get_running_nodes() {
879 echo $(get_nodes__base running)
880 return $?
881 }
882
883 # Get alive cluster nodes in visible partition, but the specified one
884 get_alive_pacemaker_nodes_but()
885 {
886 if [ -z "$1" ]; then
887 tmp_pcmk_node_list=`crm_node -l -p | sed -e '/(null)/d'`
888 else
889 tmp_pcmk_node_list=`crm_node -l -p | sed -e "s/${1}//g" | sed -e '/(null)/d'`
890 fi
891 # If OCF_RESKEY_allowed_cluster_nodes is set then we only want the intersection
892 # of the cluster node output and the allowed_cluster_nodes list
893 if [ -z "${OCF_RESKEY_allowed_cluster_nodes}" ]; then
894 pcmk_node_list=$tmp_pcmk_node_list
895 else
896 pcmk_node_list=`for i in $tmp_pcmk_node_list ${OCF_RESKEY_allowed_cluster_nodes}; do echo $i; done | sort | uniq -d`
897 fi
898 echo $pcmk_node_list
899 }
900
901 # Get current master. If a parameter is provided,
902 # do not check node with that name
903 get_master_name_but()
904 {
905 local node
906 for node in $(get_alive_pacemaker_nodes_but "$@")
907 do
908 ocf_log info "${LH} looking if $node is master"
909
910 if is_master $node; then
911 ocf_log info "${LH} master is $node"
912 echo $node
913 break
914 fi
915 done
916 }
917
918 # Evals some erlang code on current node
919 erl_eval() {
920 local fmt="${1:?}"
921 shift
922
923 $COMMAND_TIMEOUT ${OCF_RESKEY_ctl} eval "$(printf "$fmt" "$@")" 2>/dev/null
924 }
925
926 # Returns 0 if we are clustered with provideded node
927 is_clustered_with()
928 {
929 local LH="${LH}: is_clustered_with: "
930 local node_name
931 local rc
932 node_name=$(rabbit_node_name $1)
933
934 local seen_as_running
935 seen_as_running=$(erl_eval "lists:member('%s', rabbit_mnesia:cluster_nodes(running))." "$node_name")
936 rc=$?
937 if [ "$rc" -ne 0 ]; then
938 ocf_log err "${LH} Failed to check whether '$node_name' is considered running by us"
939 # We had a transient local error; that doesn't mean the remote node is
940 # not part of the cluster, so ignore this
941 elif [ "$seen_as_running" != true ]; then
942 ocf_log info "${LH} Node $node_name is not running, considering it not clustered with us"
943 return 1
944 fi
945
946 local seen_as_partitioned
947 seen_as_partitioned=$(erl_eval "lists:member('%s', rabbit_node_monitor:partitions())." "$node_name")
948 rc=$?
949 if [ "$rc" -ne 0 ]; then
950 ocf_log err "${LH} Failed to check whether '$node_name' is partitioned with us"
951 # We had a transient local error; that doesn't mean the remote node is
952 # partitioned with us, so ignore this
953 elif [ "$seen_as_partitioned" != false ]; then
954 ocf_log info "${LH} Node $node_name is partitioned from us"
955 return 1
956 fi
957
958 return $?
959 }
960
961
962 check_need_join_to() {
963 local join_to
964 local node
965 local running_nodes
966 local rc=$OCF_ERR_GENERIC
967
968 rc=0
969 join_to=$(rabbit_node_name $1)
970 running_nodes=$(get_running_nodes)
971 for node in $running_nodes ; do
972 if [ "${join_to}" = "${node}" ] ; then
973 rc=1
974 break
975 fi
976 done
977
978 return $rc
979 }
980
981 # Update erlang cookie, if it has been specified
982 update_cookie() {
983 local cookie_file_content
984 if [ "${OCF_RESKEY_erlang_cookie}" != 'false' ] ; then
985 if [ -f "${OCF_RESKEY_erlang_cookie_file}" ]; then
986 # First line of cookie file without newline
987 cookie_file_content=$(head -n1 "${OCF_RESKEY_erlang_cookie_file}" | perl -pe chomp)
988 fi
989 # As there is a brief period of time when the file is empty
990 # (shell redirection has already opened and truncated file,
991 # and echo hasn't finished its job), we are doing this write
992 # only when cookie has changed.
993 if [ "${OCF_RESKEY_erlang_cookie}" != "${cookie_file_content}" ]; then
994 echo "${OCF_RESKEY_erlang_cookie}" > "${OCF_RESKEY_erlang_cookie_file}"
995 fi
996 # And this are idempotent operations, so we don't have to
997 # check any preconditions for running them.
998 chown ${OCF_RESKEY_username}:${OCF_RESKEY_groupname} "${OCF_RESKEY_erlang_cookie_file}"
999 chmod 600 "${OCF_RESKEY_erlang_cookie_file}"
1000 fi
1001 return $OCF_SUCCESS
1002 }
1003
1004 # Stop rmq beam process by pid and by rabbit node name match. Returns SUCCESS/ERROR
1005 kill_rmq_and_remove_pid() {
1006 local LH="${LL} kill_rmq_and_remove_pid():"
1007 # Stop the rabbitmq-server by its pidfile, use the name matching as a fallback,
1008 # and ignore the exit code
1009 proc_stop "${OCF_RESKEY_pid_file}" "beam.*${RABBITMQ_NODENAME}" "${OCF_RESKEY_stop_time}"
1010 # Ensure the beam.smp stopped by the rabbit node name matching as well
1011 proc_stop none "beam.*${RABBITMQ_NODENAME}" "${OCF_RESKEY_stop_time}"
1012 if [ $? -eq 0 ] ; then
1013 return $OCF_SUCCESS
1014 else
1015 return $OCF_ERR_GENERIC
1016 fi
1017 }
1018
1019 trim_var(){
1020 local string="$*"
1021 echo ${string%% }
1022 }
1023
1024 action_validate() {
1025 # todo(sv): validate some incoming parameters
1026 OCF_RESKEY_CRM_meta_notify_post=$(trim_var $OCF_RESKEY_CRM_meta_notify_post)
1027 OCF_RESKEY_CRM_meta_notify_pre=$(trim_var $OCF_RESKEY_CRM_meta_notify_pre)
1028 OCF_RESKEY_CRM_meta_notify_start=$(trim_var $OCF_RESKEY_CRM_meta_notify_start)
1029 OCF_RESKEY_CRM_meta_notify_stop=$(trim_var $OCF_RESKEY_CRM_meta_notify_stop)
1030 OCF_RESKEY_CRM_meta_notify_start_resource=$(trim_var $OCF_RESKEY_CRM_meta_notify_start_resource)
1031 OCF_RESKEY_CRM_meta_notify_stop_resource=$(trim_var $OCF_RESKEY_CRM_meta_notify_stop_resource)
1032 OCF_RESKEY_CRM_meta_notify_active_resource=$(trim_var $OCF_RESKEY_CRM_meta_notify_active_resource)
1033 OCF_RESKEY_CRM_meta_notify_inactive_resource=$(trim_var $OCF_RESKEY_CRM_meta_notify_inactive_resource)
1034 OCF_RESKEY_CRM_meta_notify_start_uname=$(trim_var $OCF_RESKEY_CRM_meta_notify_start_uname)
1035 OCF_RESKEY_CRM_meta_notify_stop_uname=$(trim_var $OCF_RESKEY_CRM_meta_notify_stop_uname)
1036 OCF_RESKEY_CRM_meta_notify_active_uname=$(trim_var $OCF_RESKEY_CRM_meta_notify_active_uname)
1037 OCF_RESKEY_CRM_meta_notify_master_resource=$(trim_var $OCF_RESKEY_CRM_meta_notify_master_resource)
1038 OCF_RESKEY_CRM_meta_notify_master_uname=$(trim_var $OCF_RESKEY_CRM_meta_notify_master_uname)
1039 OCF_RESKEY_CRM_meta_notify_demote_resource=$(trim_var $OCF_RESKEY_CRM_meta_notify_demote_resource)
1040 OCF_RESKEY_CRM_meta_notify_demote_uname=$(trim_var $OCF_RESKEY_CRM_meta_notify_demote_uname)
1041 OCF_RESKEY_CRM_meta_notify_slave_resource=$(trim_var $OCF_RESKEY_CRM_meta_notify_slave_resource)
1042 OCF_RESKEY_CRM_meta_notify_slave_uname=$(trim_var $OCF_RESKEY_CRM_meta_notify_slave_uname)
1043 OCF_RESKEY_CRM_meta_notify_promote_resource=$(trim_var $OCF_RESKEY_CRM_meta_notify_promote_resource)
1044 OCF_RESKEY_CRM_meta_notify_promote_uname=$(trim_var $OCF_RESKEY_CRM_meta_notify_promote_uname)
1045 return $OCF_SUCCESS
1046 }
1047
1048 update_rabbit_start_time_if_rc() {
1049 local nowtime
1050 local rc=$1
1051 if [ $rc -eq 0 ]; then
1052 nowtime="$(now)"
1053 ocf_log info "${LH} Rabbit app started successfully. Updating start time attribute with ${nowtime}"
1054 ocf_run crm_attribute -N $THIS_PCMK_NODE -l reboot --name 'rabbit-start-time' --update "${nowtime}"
1055 fi
1056 }
1057
1058 join_to_cluster() {
1059 local node="$1"
1060 local rmq_node
1061 local rc=$OCF_ERR_GENERIC
1062 local LH="${LL} join_to_cluster():"
1063
1064 ocf_log info "${LH} start."
1065
1066 rmq_node=$(rabbit_node_name $node)
1067 ocf_log info "${LH} Joining to cluster by node '${rmq_node}'."
1068 get_status rabbit
1069 rc=$?
1070 if [ $rc -eq $OCF_SUCCESS ] ; then
1071 ocf_log info "${LH} rabbitmq app will be stopped."
1072 stop_rmq_server_app
1073 rc=$?
1074 if [ $rc -ne 0 ] ; then
1075 ocf_log err "${LH} Can't stop rabbitmq app by stop_app command. Stopping."
1076 action_stop
1077 return $OCF_ERR_GENERIC
1078 fi
1079 fi
1080 ocf_log info "${LH} Execute join_cluster with timeout: ${TIMEOUT_ARG}"
1081 su_rabbit_cmd "${OCF_RESKEY_ctl} join_cluster $rmq_node"
1082 rc=$?
1083 if [ $rc -ne 0 ] ; then
1084 ocf_log err "${LH} Can't join to cluster by node '${rmq_node}'. Stopping."
1085 action_stop
1086 return $OCF_ERR_GENERIC
1087 fi
1088 sleep 2
1089 try_to_start_rmq_app
1090 rc=$?
1091 if [ $rc -ne 0 ] ; then
1092 ocf_log err "${LH} Can't start RMQ app after join to cluster. Stopping."
1093 action_stop
1094 return $OCF_ERR_GENERIC
1095 else
1096 update_rabbit_start_time_if_rc 0
1097 ocf_log info "${LH} Joined to cluster succesfully."
1098 fi
1099
1100 ocf_log info "${LH} end."
1101 return $rc
1102 }
1103
1104 unjoin_nodes_from_cluster() {
1105 # node names of the nodes where the pcs resource is being stopped
1106 local nodelist="$1"
1107 local hostname
1108 local nodename
1109 local rc=$OCF_ERR_GENERIC
1110 local rnode
1111 # nodes in rabbit cluster db
1112 local nodes_in_cluster
1113 local LH="${LL} unjoin_nodes_from_cluster():"
1114
1115 nodes_in_cluster=$(get_nodes)
1116 rc=$?
1117 if [ $rc -ne 0 ] ; then
1118 # no nodes in node list, nothing to do
1119 return $OCF_SUCCESS
1120 fi
1121
1122 # unjoin all cluster nodes which are being stopped (i.e. recieved post-stop notify), except *this* node
1123 # before to unjoin the nodes, make sure they were disconnected from *this* node
1124 for hostname in $nodelist ; do
1125 nodename=$(rabbit_node_name $hostname)
1126 if [ "${nodename}" = "${RABBITMQ_NODENAME}" ] ; then
1127 continue
1128 fi
1129 for rnode in $nodes_in_cluster ; do
1130 if [ "${nodename}" = "${rnode}" ] ; then
1131 # disconnect node being unjoined from this node
1132 ocf_run ${OCF_RESKEY_ctl} eval "disconnect_node(list_to_atom(\"${nodename}\"))." 2>&1
1133 rc=$?
1134 if [ $rc -eq $OCF_SUCCESS ] ; then
1135 ocf_log info "${LH} node '${nodename}' disconnected succesfully."
1136 else
1137 ocf_log info "${LH} disconnecting node '${nodename}' failed."
1138 fi
1139
1140 # unjoin node
1141 # when the rabbit node went down, its status
1142 # remains 'running' for a while, so few retries are required
1143 local tries=0
1144 until [ $tries -eq 5 ]; do
1145 tries=$((tries+1))
1146 if is_clustered_with $nodename; then
1147 ocf_log info "${LH} the ${nodename} is alive and cannot be kicked from the cluster yet"
1148 else
1149 break
1150 fi
1151 sleep 10
1152 done
1153 ocf_log info "${LH} Execute forget_cluster_node with timeout: ${TIMEOUT_ARG}"
1154 su_rabbit_cmd "${OCF_RESKEY_ctl} forget_cluster_node ${nodename}"
1155 rc=$?
1156 if [ $rc -eq 0 ] ; then
1157 ocf_log info "${LH} node '${nodename}' unjoined succesfully."
1158 else
1159 ocf_log warn "${LH} unjoining node '${nodename}' failed."
1160 fi
1161 fi
1162 done
1163 done
1164 return $OCF_SUCCESS
1165 }
1166
1167 # Stop RMQ beam server process. Returns SUCCESS/ERROR
1168 stop_server_process() {
1169 local pid
1170 local rc=$OCF_ERR_GENERIC
1171 local LH="${LL} stop_server_process():"
1172
1173 pid=$(cat ${OCF_RESKEY_pid_file})
1174 rc=$?
1175 if [ $rc -ne 0 ] ; then
1176 # Try to stop without known PID
1177 ocf_log err "${LH} RMQ-server process PIDFILE was not found!"
1178 su_rabbit_cmd "${OCF_RESKEY_ctl} stop >> \"${OCF_RESKEY_log_dir}/shutdown_log\" 2>&1"
1179 if [ $? -eq 0 ] ; then
1180 ocf_log info "${LH} RMQ-server process stopped succesfully, although there was no PIDFILE found."
1181 ocf_log info "${LH} grant a graceful termintation window ${OCF_RESKEY_stop_time} to end its beam"
1182 sleep "${OCF_RESKEY_stop_time}"
1183 else
1184 kill_rmq_and_remove_pid
1185 fi
1186 elif [ "${pid}" ] ; then
1187 # Try to stop gracefully by known PID
1188 ocf_log info "${LH} Execute stop with timeout: ${TIMEOUT_ARG}"
1189 su_rabbit_cmd "${OCF_RESKEY_ctl} stop ${OCF_RESKEY_pid_file} >> \"${OCF_RESKEY_log_dir}/shutdown_log\" 2>&1"
1190 [ $? -eq 0 ] && ocf_log info "${LH} RMQ-server process (PID=${pid}) stopped succesfully."
1191 fi
1192
1193 # Ensure there is no beam process and pidfile left
1194 pgrep -f "beam.*${RABBITMQ_NODENAME}" > /dev/null
1195 rc=$?
1196 if [ -f ${OCF_RESKEY_pid_file} ] || [ $rc -eq 0 ] ; then
1197 ocf_log warn "${LH} The pidfile or beam's still exist, forcing the RMQ-server cleanup"
1198 kill_rmq_and_remove_pid
1199 return $?
1200 else
1201 return $OCF_SUCCESS
1202 fi
1203 }
1204
1205 # Stop RMQ-app. Return OCF_SUCCESS, if the app was stopped,
1206 # otherwise return OCF_ERR_GENERIC
1207 stop_rmq_server_app() {
1208 local rc=$OCF_ERR_GENERIC
1209
1210 # if the beam process isn't running, then rabbit app is stopped as well
1211 get_status
1212 rc=$?
1213 if [ $rc -ne 0 ] ; then
1214 return $OCF_SUCCESS
1215 fi
1216
1217 # stop the app
1218 ocf_log info "${LH} Execute stop_app with timeout: ${TIMEOUT_ARG}"
1219 su_rabbit_cmd "${OCF_RESKEY_ctl} stop_app >> \"${OCF_RESKEY_log_dir}/shutdown_log\" 2>&1"
1220 rc=$?
1221 if [ $rc -ne 0 ] ; then
1222 ocf_log err "${LH} RMQ-server app cannot be stopped."
1223 return $OCF_ERR_GENERIC
1224 fi
1225
1226 get_status rabbit
1227 rc=$?
1228 if [ $rc -ne $OCF_SUCCESS ] ; then
1229 ocf_log info "${LH} RMQ-server app stopped succesfully."
1230 rc=$OCF_SUCCESS
1231 else
1232 ocf_log err "${LH} RMQ-server app cannot be stopped."
1233 rc=$OCF_ERR_GENERIC
1234 fi
1235
1236 return $rc
1237 }
1238
1239 start_beam_process() {
1240 local command
1241 local rc=$OCF_ERR_GENERIC
1242 local ts_end
1243 local pf_end
1244 local pid
1245 local LH="${LL} start_beam_process():"
1246
1247 # remove old PID-file if it exists
1248 if [ -f "${OCF_RESKEY_pid_file}" ] ; then
1249 ocf_log warn "${LH} found old PID-file '${OCF_RESKEY_pid_file}'."
1250 pid=$(cat ${OCF_RESKEY_pid_file})
1251 if [ "${pid}" ] && [ -d "/proc/${pid}" ] ; then
1252 ocf_run cat /proc/${pid}/cmdline | grep -c 'bin/beam' > /dev/null 2>&1
1253 rc=$?
1254 if [ $rc -eq $OCF_SUCCESS ] ; then
1255 ocf_log warn "${LH} found beam process with PID=${pid}, killing...'."
1256 ocf_run kill -TERM $pid
1257 else
1258 ocf_log err "${LH} found unknown process with PID=${pid} from '${OCF_RESKEY_pid_file}'."
1259 return $OCF_ERR_GENERIC
1260 fi
1261 fi
1262 ocf_run rm -f $OCF_RESKEY_pid_file
1263 fi
1264
1265 [ -f /etc/default/rabbitmq-server ] && . /etc/default/rabbitmq-server
1266
1267 # RabbitMQ requires high soft and hard limits for NOFILE
1268 set_limits
1269
1270 # run beam process
1271 command="${OCF_RESKEY_binary} >> \"${OCF_RESKEY_log_dir}/startup_log\" 2>/dev/null"
1272 RABBITMQ_NODE_ONLY=1 su rabbitmq -s /bin/sh -c "${command}"&
1273 ts_end=$(( $(now) + ${OCF_RESKEY_start_time} ))
1274 sleep 3 # give it some time, before attempting to start_app
1275 # PID-file is now created later, if the application started successfully
1276 # So assume beam.smp is started, and defer errors handling for start_app
1277 return $OCF_SUCCESS
1278 }
1279
1280 check_plugins() {
1281 # Check if it's safe to load plugins and if we need to do so. Logic is:
1282 # if (EnabledPlugins > 0) and (ActivePlugins == 0) ; then it's safe to load
1283 # If we have at least one active plugin, then it's not safe to re-load them
1284 # because plugins:setup() would remove existing dependency plugins in plugins_expand_dir.
1285 ${OCF_RESKEY_ctl} eval '{ok, EnabledFile} = application:get_env(rabbit, enabled_plugins_file), EnabledPlugins = rabbit_plugins:read_enabled(EnabledFile), ActivePlugins = rabbit_plugins:active(), if length(EnabledPlugins)>0 -> if length(ActivePlugins)==0 -> erlang:error("need_to_load_plugins"); true -> false end; true -> false end.'
1286 return $?
1287 }
1288
1289 load_plugins() {
1290 check_plugins
1291 local rc=$?
1292 if [ $rc -eq 0 ] ; then
1293 return 0
1294 else
1295 ${OCF_RESKEY_ctl} eval 'ToBeLoaded = rabbit_plugins:setup(), ok = app_utils:load_applications(ToBeLoaded), StartupApps = app_utils:app_dependency_order(ToBeLoaded,false), app_utils:start_applications(StartupApps).'
1296 return $?
1297 fi
1298 }
1299
1300 list_active_plugins() {
1301 local list
1302 list=`${OCF_RESKEY_ctl} eval 'rabbit_plugins:active().' 2>/dev/null`
1303 echo "${list}"
1304 }
1305
1306 try_to_start_rmq_app() {
1307 local startup_log="${1:-${OCF_RESKEY_log_dir}/startup_log}"
1308 local rc=$OCF_ERR_GENERIC
1309 local LH="${LL} try_to_start_rmq_app():"
1310
1311 get_status
1312 rc=$?
1313 if [ $rc -ne $OCF_SUCCESS ] ; then
1314 ocf_log info "${LH} RMQ-runtime (beam) not started, starting..."
1315 start_beam_process
1316 rc=$?
1317 if [ $rc -ne $OCF_SUCCESS ]; then
1318 ocf_log err "${LH} Failed to start beam - returning from the function"
1319 return $OCF_ERR_GENERIC
1320 fi
1321 fi
1322
1323
1324 if [ -z "${startup_log}" ] ; then
1325 startup_log="${OCF_RESKEY_log_dir}/startup_log"
1326 fi
1327
1328 ocf_log info "${LH} begin."
1329 ocf_log info "${LH} Execute start_app with timeout: ${TIMEOUT_ARG}"
1330 su_rabbit_cmd "${OCF_RESKEY_ctl} start_app >>${startup_log} 2>&1"
1331 rc=$?
1332 if [ $rc -eq 0 ] ; then
1333 ocf_log info "${LH} start_app was successful."
1334 ocf_log info "${LH} waiting for start to finish with timeout: ${TIMEOUT_ARG}"
1335 su_rabbit_cmd "${OCF_RESKEY_ctl} wait ${OCF_RESKEY_pid_file}"
1336 rc=$?
1337 if [ $rc -ne 0 ] ; then
1338 ocf_log err "${LH} RMQ-server app failed to wait for start."
1339 return $OCF_ERR_GENERIC
1340 fi
1341 rc=$OCF_SUCCESS
1342 # Loading enabled modules
1343 ocf_log info "${LH} start plugins."
1344 load_plugins
1345 local mrc=$?
1346 if [ $mrc -eq 0 ] ; then
1347 local mlist
1348 mlist=`list_active_plugins`
1349 ocf_log info "${LH} Starting plugins: ${mlist}"
1350 else
1351 ocf_log info "${LH} Starting plugins: failed."
1352 fi
1353 else
1354 ocf_log info "${LH} start_app failed."
1355 rc=$OCF_ERR_GENERIC
1356 fi
1357 return $rc
1358 }
1359
1360 start_rmq_server_app() {
1361 local rc=$OCF_ERR_GENERIC
1362 local startup_log="${OCF_RESKEY_log_dir}/startup_log"
1363 local startup_output
1364 local LH="${LL} start_rmq_server_app():"
1365 local a
1366
1367 #We are performing initial start check.
1368 #We are not ready to provide service.
1369 #Clients should not have access.
1370
1371
1372 ocf_log info "${LH} begin."
1373 # Safe-unblock the rules, if there are any
1374 unblock_client_access "${LH}"
1375 # Apply the blocking rule
1376 block_client_access
1377 rc=$?
1378 if [ $rc -eq $OCF_SUCCESS ]; then
1379 ocf_log info "${LH} blocked access to RMQ port"
1380 else
1381 ocf_log err "${LH} cannot block access to RMQ port!"
1382 return $OCF_ERR_GENERIC
1383 fi
1384 get_status
1385 rc=$?
1386 if [ $rc -ne $OCF_SUCCESS ] ; then
1387 ocf_log info "${LH} RMQ-runtime (beam) not started, starting..."
1388 start_beam_process
1389 rc=$?
1390 if [ $rc -ne $OCF_SUCCESS ]; then
1391 unblock_client_access "${LH}"
1392 return $OCF_ERR_GENERIC
1393 fi
1394 fi
1395
1396 ocf_log info "${LH} RMQ-server app not started, starting..."
1397 try_to_start_rmq_app "$startup_log"
1398 rc=$?
1399 if [ $rc -eq $OCF_SUCCESS ] ; then
1400 # rabbitmq-server started successfuly as master of cluster
1401 master_score $MIN_MASTER_SCORE
1402 stop_rmq_server_app
1403 rc=$?
1404 if [ $rc -ne 0 ] ; then
1405 ocf_log err "${LH} RMQ-server app can't be stopped. Beam will be killed."
1406 kill_rmq_and_remove_pid
1407 unblock_client_access "${LH}"
1408 return $OCF_ERR_GENERIC
1409 fi
1410 else
1411 # error at start RMQ-server
1412 ocf_log warn "${LH} RMQ-server app can't start without Mnesia cleaning."
1413 for a in $(seq 1 10) ; do
1414 rc=$OCF_ERR_GENERIC
1415 reset_mnesia || break
1416 try_to_start_rmq_app "$startup_log"
1417 rc=$?
1418 if [ $rc -eq $OCF_SUCCESS ]; then
1419 stop_rmq_server_app
1420 rc=$?
1421 if [ $rc -eq $OCF_SUCCESS ]; then
1422 ocf_log info "${LH} RMQ-server app Mnesia cleaned successfully."
1423 rc=$OCF_SUCCESS
1424 master_score $MIN_MASTER_SCORE
1425 break
1426 else
1427 ocf_log err "${LH} RMQ-server app can't be stopped during Mnesia cleaning. Beam will be killed."
1428 kill_rmq_and_remove_pid
1429 unblock_client_access "${LH}"
1430 return $OCF_ERR_GENERIC
1431 fi
1432 fi
1433 done
1434 fi
1435 if [ $rc -eq $OCF_ERR_GENERIC ] ; then
1436 ocf_log err "${LH} RMQ-server can't be started while many tries. Beam will be killed."
1437 kill_rmq_and_remove_pid
1438 fi
1439 ocf_log info "${LH} end."
1440 unblock_client_access "${LH}"
1441 return $rc
1442 }
1443
1444 # check status of rabbit beam process or a rabbit app, if rabbit arg specified
1445 # by default, test if the kernel app is running, otherwise consider it is "not running"
1446 get_status() {
1447 local what="${1:-kernel}"
1448 local rc=$OCF_NOT_RUNNING
1449 local LH="${LL} get_status():"
1450 local body
1451 local beam_running
1452
1453 body=$( ${COMMAND_TIMEOUT} ${OCF_RESKEY_ctl} eval 'rabbit_misc:which_applications().' 2>&1 )
1454 rc=$?
1455
1456 pgrep -f "beam.*${RABBITMQ_NODENAME}" > /dev/null
1457 beam_running=$?
1458 # report not running only if the which_applications() reported an error AND the beam is not running
1459 if [ $rc -ne 0 ] && [ $beam_running -ne 0 ] ; then
1460 ocf_log info "${LH} failed with code ${rc}. Command output: ${body}"
1461 return $OCF_NOT_RUNNING
1462 # return a generic error, if there were errors and beam is found running
1463 elif [ $rc -ne 0 ] ; then
1464 ocf_log info "${LH} found the beam process running but failed with code ${rc}. Command output: ${body}"
1465 return $OCF_ERR_GENERIC
1466 fi
1467
1468 # try to parse the which_applications() output only if it exited w/o errors
1469 if [ "${what}" ] && [ $rc -eq 0 ] ; then
1470 rc=$OCF_NOT_RUNNING
1471 echo "$body" | grep "\{${what}," > /dev/null 2>&1 && rc=$OCF_SUCCESS
1472
1473 if [ $rc -ne $OCF_SUCCESS ] ; then
1474 ocf_log info "${LH} app ${what} was not found in command output: ${body}"
1475 fi
1476 fi
1477
1478 [ $rc -ne $OCF_SUCCESS ] && rc=$OCF_NOT_RUNNING
1479 return $rc
1480 }
1481
1482 action_status() {
1483 local rc=$OCF_ERR_GENERIC
1484
1485 get_status
1486 rc=$?
1487 return $rc
1488 }
1489
1490 # return 0, if given node has a master attribute in CIB,
1491 # otherwise, return 1
1492 is_master() {
1493 local result
1494 result=`crm_attribute -N "${1}" -l reboot --name 'rabbit-master' --query 2>/dev/null |\
1495 awk '{print $3}' | awk -F "=" '{print $2}' | sed -e '/(null)/d'`
1496 if [ "${result}" != 'true' ] ; then
1497 return 1
1498 fi
1499 return 0
1500 }
1501
1502 # Verify if su_rabbit_cmd exited by timeout by checking its return code.
1503 # If it did not, return 0. If it did AND it is
1504 # $OCF_RESKEY_max_rabbitmqctl_timeouts'th timeout in a row,
1505 # return 2 to signal get_monitor that it should
1506 # exit with error. Otherwise return 1 to signal that there was a timeout,
1507 # but it should be ignored. Timeouts for different operations are tracked
1508 # separately. The second argument is used to distingush them.
1509 check_timeouts() {
1510 local op_rc=$1
1511 local timeouts_attr_name=$2
1512 local op_name=$3
1513
1514 # 75 is EX_TEMPFAIL from sysexits, and is used by rabbitmqctl to signal about
1515 # timeout.
1516 if [ $op_rc -ne 124 ] && [ $op_rc -ne 137 ] && [ $op_rc -ne 75 ]; then
1517 ocf_update_private_attr $timeouts_attr_name 0
1518 return 0
1519 fi
1520
1521 local count
1522 count=$(ocf_get_private_attr $timeouts_attr_name 0)
1523
1524 count=$((count+1))
1525 # There is a slight chance that this piece of code will be executed twice simultaneously.
1526 # As a result, $timeouts_attr_name's value will be one less than it should be. But we don't need
1527 # precise calculation here.
1528 ocf_update_private_attr $timeouts_attr_name $count
1529
1530 if [ $count -lt $OCF_RESKEY_max_rabbitmqctl_timeouts ]; then
1531 ocf_log warn "${LH} 'rabbitmqctl $op_name' timed out $count of max. $OCF_RESKEY_max_rabbitmqctl_timeouts time(s) in a row. Doing nothing for now."
1532 return 1
1533 else
1534 ocf_log err "${LH} 'rabbitmqctl $op_name' timed out $count of max. $OCF_RESKEY_max_rabbitmqctl_timeouts time(s) in a row and is not responding. The resource is failed."
1535 return 2
1536 fi
1537 }
1538
1539 wait_sync() {
1540 local wait_time=$1
1541 local queues
1542 local opt_arg=""
1543
1544 if ocf_is_true "$OCF_RESKEY_rmq_feature_local_list_queues"; then
1545 opt_arg="--local"
1546 fi
1547
1548 queues="${COMMAND_TIMEOUT} ${OCF_RESKEY_ctl} -p ${OCF_RESKEY_default_vhost} list_queues $opt_arg name state"
1549
1550 su_rabbit_cmd -t "${wait_time}" "sh -c \"while ${queues} | grep -q 'syncing,'; \
1551 do sleep 2; done\""
1552
1553 return $?
1554 }
1555
1556 get_monitor() {
1557 local rc=$OCF_ERR_GENERIC
1558 local LH="${LL} get_monitor():"
1559 local status_master=1
1560 local rabbit_running
1561 local name
1562 local node
1563 local node_start_time
1564 local nowtime
1565 local partitions_report
1566 local node_partitions
1567
1568 ocf_log info "${LH} CHECK LEVEL IS: ${OCF_CHECK_LEVEL}"
1569 get_status
1570 rc=$?
1571 if [ $rc -eq $OCF_NOT_RUNNING ] ; then
1572 ocf_log info "${LH} get_status() returns ${rc}."
1573 ocf_log info "${LH} ensuring this slave does not get promoted."
1574 master_score 0
1575 return $OCF_NOT_RUNNING
1576 elif [ $rc -eq $OCF_SUCCESS ] ; then
1577 ocf_log info "${LH} get_status() returns ${rc}."
1578 ocf_log info "${LH} also checking if we are master."
1579 get_status rabbit
1580 rabbit_running=$?
1581 is_master $THIS_PCMK_NODE
1582 status_master=$?
1583 ocf_log info "${LH} master attribute is ${status_master}"
1584 if [ $status_master -eq 0 ] && [ $rabbit_running -eq $OCF_SUCCESS ]
1585 then
1586 ocf_log info "${LH} We are the running master"
1587 rc=$OCF_RUNNING_MASTER
1588 elif [ $status_master -eq 0 ] && [ $rabbit_running -ne $OCF_SUCCESS ] ; then
1589 ocf_log err "${LH} We are the master and RMQ-runtime (beam) is not running. this is a failure"
1590 exit $OCF_FAILED_MASTER
1591 fi
1592 fi
1593 get_status rabbit
1594 rabbit_running=$?
1595 ocf_log info "${LH} checking if rabbit app is running"
1596
1597 if [ $rc -eq $OCF_RUNNING_MASTER ]; then
1598 if [ $rabbit_running -eq $OCF_SUCCESS ]; then
1599 ocf_log info "${LH} rabbit app is running and is master of cluster"
1600 else
1601 ocf_log err "${LH} we are the master and rabbit app is not running. This is a failure"
1602 exit $OCF_FAILED_MASTER
1603 fi
1604 else
1605 start_time=$((180 + $(ocf_get_private_attr 'rabbit-start-phase-1-time' 0)))
1606 restart_order_time=$((60 + $(ocf_get_private_attr 'rabbit-ordered-to-restart' 0)))
1607 nowtime=$(now)
1608
1609 # If we started more than 3 minutes ago, and
1610 # we got order to restart less than 1 minute ago
1611 if [ $nowtime -lt $restart_order_time ]; then
1612 if [ $nowtime -gt $start_time ]; then
1613 ocf_log err "${LH} failing because we have received an order to restart from the master"
1614 stop_server_process
1615 rc=$OCF_ERR_GENERIC
1616 else
1617 ocf_log warn "${LH} received an order to restart from the master, ignoring it because we have just started"
1618 fi
1619 fi
1620 fi
1621
1622 if [ $rc -eq $OCF_ERR_GENERIC ]; then
1623 ocf_log err "${LH} get_status() returns generic error ${rc}"
1624 ocf_log info "${LH} ensuring this slave does not get promoted."
1625 master_score 0
1626 return $OCF_ERR_GENERIC
1627 fi
1628
1629 # Recounting our master score
1630 ocf_log info "${LH} preparing to update master score for node"
1631 local our_start_time
1632 local new_score
1633 local node_start_time
1634 local node_score
1635
1636 our_start_time=$(get_node_start_time $THIS_PCMK_NODE)
1637
1638 if [ $our_start_time -eq 0 ]; then
1639 new_score=$MIN_MASTER_SCORE
1640 else
1641 new_score=$BEST_MASTER_SCORE
1642 for node in $(get_alive_pacemaker_nodes_but $THIS_PCMK_NODE)
1643 do
1644 node_start_time=$(get_node_start_time $node)
1645 node_score=$(get_node_master_score $node)
1646
1647 ocf_log info "${LH} comparing us (start time: $our_start_time, score: $new_score) with $node (start time: $node_start_time, score: $node_score)"
1648 if [ $node_start_time -ne 0 ] && [ $node_score -ne 0 ] && [ $node_start_time -lt $our_start_time ]; then
1649 new_score=$((node_score - 10 < new_score ? node_score - 10 : new_score ))
1650 elif [ $node_start_time -ne 0 ] && [ $node_score -ne 0 ] && [ $node_start_time -eq $our_start_time ]; then
1651 # Do not get promoted if the other node is already master and we have the same start time
1652 if is_master $node; then
1653 new_score=$((node_score - 10 < new_score ? node_score - 10 : new_score ))
1654 fi
1655 fi
1656 done
1657 fi
1658
1659 if [ "$new_score" -ne "$(get_node_master_score $THIS_PCMK_NODE)" ]; then
1660 master_score $new_score
1661 fi
1662 ocf_log info "${LH} our start time is $our_start_time and score is $new_score"
1663
1664 # Skip all other checks if rabbit app is not running
1665 if [ $rabbit_running -ne $OCF_SUCCESS ]; then
1666 ocf_log info "${LH} RabbitMQ is not running, get_monitor function ready to return ${rc}"
1667 return $rc
1668 fi
1669
1670 # rc can be SUCCESS or RUNNING_MASTER, don't touch it unless there
1671 # is some error uncovered by node_health_check
1672 if ! node_health_check; then
1673 rc=$OCF_ERR_GENERIC
1674 fi
1675
1676 if [ $rc -eq $OCF_RUNNING_MASTER ] ; then
1677 # If we are the master and healthy, perform various
1678 # connectivity checks for other nodes in the cluster.
1679 # Order a member to restart if something fishy happens with it.
1680 # All cross-node checks MUST happen only here.
1681
1682 partitions_report="$(partitions_report)"
1683
1684 for node in $(get_alive_pacemaker_nodes_but $THIS_PCMK_NODE); do
1685 # Restart node if we don't consider ourselves clustered with it
1686 if ! is_clustered_with $node; then
1687 ocf_log warn "${LH} node $node is not connected with us"
1688 order_node_restart "$node"
1689 continue
1690 fi
1691
1692 # Restart node if it has any unresolved partitions
1693 node_partitions=$(grep_partitions_report $node "$partitions_report")
1694 if [ ! -z "$node_partitions" ]; then
1695 ocf_log warn "${LH} Node $node thinks that it is partitoned with $node_partitions"
1696 order_node_restart "$node"
1697 continue
1698 fi
1699 done
1700 fi
1701
1702 ocf_log info "${LH} get_monitor function ready to return ${rc}"
1703 return $rc
1704 }
1705
1706 order_node_restart() {
1707 local node=${1:?}
1708 ocf_log warn "${LH} Ordering node '$node' to restart"
1709 ocf_update_private_attr 'rabbit-ordered-to-restart' "$(now)" "$node"
1710 }
1711
1712 # Checks whether node is mentioned somewhere in report returned by
1713 # partitions_report()
1714 grep_partitions_report() {
1715 local node="${1:?}"
1716 local report="${2:?}"
1717 local rabbit_node
1718 rabbit_node=$(rabbit_node_name "$node")
1719 echo "$report" | grep "PARTITIONED $rabbit_node:" | sed -e 's/^[^:]\+: //'
1720 }
1721
1722 # Report partitions (if any) from viewpoint of every running node in cluster.
1723 # It is parseable/grepable version of `rabbitmqctl cluster_status`.
1724 #
1725 # If node sees partition, report will contain the line like:
1726 # PARTITIONED node-name: list-of-nodes, which-node-name-considers, itself-partitioned-with
1727 partitions_report() {
1728 $COMMAND_TIMEOUT xargs -0 ${OCF_RESKEY_ctl} eval <<EOF
1729 RpcTimeout = 10,
1730
1731 Nodes = rabbit_mnesia:cluster_nodes(running),
1732
1733 {Replies, _BadNodes} = gen_server:multi_call(Nodes, rabbit_node_monitor, partitions, RpcTimeout * 1000),
1734
1735 lists:foreach(fun ({_, []}) -> ok;
1736 ({Node, Partitions}) ->
1737 PartitionsStr = string:join([atom_to_list(Part) || Part <- Partitions],
1738 ", "),
1739 io:format("PARTITIONED ~s: ~s~n",
1740 [Node, PartitionsStr])
1741 end, Replies),
1742
1743 ok.
1744 EOF
1745 }
1746
1747 # Check if the rabbitmqctl control plane is alive.
1748 node_health_check() {
1749 local rc
1750 if [ "$OCF_RESKEY_rmq_feature_health_check" = true ]; then
1751 node_health_check_local
1752 rc=$?
1753 else
1754 node_health_check_legacy
1755 rc=$?
1756 fi
1757 return $rc
1758 }
1759
1760 node_health_check_local() {
1761 local LH="${LH} node_health_check_local():"
1762 local rc
1763 local rc_timeouts
1764
1765 # Give node_health_check some time to handle timeout by itself.
1766 # By using internal rabbitmqctl timeouts, we allow it to print
1767 # more useful diagnostics
1768 local timeout=$((TIMEOUT_ARG - 2))
1769 su_rabbit_cmd "${OCF_RESKEY_ctl} node_health_check -t $timeout"
1770 rc=$?
1771
1772 check_timeouts $rc "rabbit_node_health_check_timeouts" "node_health_check"
1773 rc_timeouts=$?
1774
1775 if [ "$rc_timeouts" -eq 2 ]; then
1776 master_score 0
1777 ocf_log info "${LH} node_health_check timed out, retry limit reached"
1778 return $OCF_ERR_GENERIC
1779 elif [ "$rc_timeouts" -eq 1 ]; then
1780 ocf_log info "${LH} node_health_check timed out, going to retry"
1781 return $OCF_SUCCESS
1782 fi
1783
1784 if [ "$rc" -ne 0 ]; then
1785 ocf_log err "${LH} rabbitmqctl node_health_check exited with errors."
1786 return $OCF_ERR_GENERIC
1787 else
1788 return $OCF_SUCCESS
1789 fi
1790 }
1791
1792 node_health_check_legacy() {
1793 local rc_alive
1794 local timeout_alive
1795 su_rabbit_cmd "${OCF_RESKEY_ctl} list_channels > /dev/null 2>&1"
1796 rc_alive=$?
1797 { [ $rc_alive -eq 137 ] || [ $rc_alive -eq 124 ] ; } && ocf_log err "${LH} 'rabbitmqctl list_channels' timed out, per-node explanation: $(enhanced_list_channels)"
1798 check_timeouts $rc_alive "rabbit_list_channels_timeouts" "list_channels"
1799 timeout_alive=$?
1800
1801 if [ $timeout_alive -eq 2 ]; then
1802 master_score 0
1803 return $OCF_ERR_GENERIC
1804 elif [ $timeout_alive -eq 0 ]; then
1805 if [ $rc_alive -ne 0 ]; then
1806 ocf_log err "${LH} rabbitmqctl list_channels exited with errors."
1807 rc=$OCF_ERR_GENERIC
1808 fi
1809 fi
1810
1811 # Check for memory alarms for this Master or Slave node.
1812 # If alert found, reset the alarm
1813 # and restart the resource as it likely means a dead end situation
1814 # when rabbitmq cluster is running with blocked publishing due
1815 # to high memory watermark exceeded.
1816 local alarms
1817 local rc_alarms
1818 local timeout_alarms
1819 alarms=`su_rabbit_cmd "${OCF_RESKEY_ctl} -q eval 'rabbit_alarm:get_alarms().'" 2>/dev/null`
1820 rc_alarms=$?
1821 check_timeouts $rc_alarms "rabbit_get_alarms_timeouts" "get_alarms"
1822 timeout_alarms=$?
1823
1824 if [ $timeout_alarms -eq 2 ]; then
1825 master_score 0
1826 return $OCF_ERR_GENERIC
1827
1828 elif [ $timeout_alarms -eq 0 ]; then
1829 if [ $rc_alarms -ne 0 ]; then
1830 ocf_log err "${LH} rabbitmqctl get_alarms exited with errors."
1831 rc=$OCF_ERR_GENERIC
1832
1833 elif [ -n "${alarms}" ]; then
1834 for node in ${alarms}; do
1835 name=`echo ${node} | perl -n -e "m/memory,'(?<n>\S+)+'/ && print \"$+{n}\n\""`
1836 if [ "${name}" = "${RABBITMQ_NODENAME}" ] ; then
1837 ocf_log err "${LH} Found raised memory alarm. Erasing the alarm and restarting."
1838 su_rabbit_cmd "${OCF_RESKEY_ctl} set_vm_memory_high_watermark 10 > /dev/null 2>&1"
1839 rc=$OCF_ERR_GENERIC
1840 break
1841 fi
1842 done
1843 fi
1844 fi
1845
1846 if ! is_cluster_status_ok ; then
1847 rc=$OCF_ERR_GENERIC
1848 fi
1849
1850 # Check if the list of all queues is available,
1851 # Also report some queues stats and total virtual memory.
1852 local queues
1853 local rc_queues
1854 local timeout_queues
1855 queues=`su_rabbit_cmd "${OCF_RESKEY_ctl} -q -p ${OCF_RESKEY_default_vhost} list_queues memory messages consumer_utilisation"`
1856 rc_queues=$?
1857 check_timeouts $rc_queues "rabbit_list_queues_timeouts" "list_queues"
1858 timeout_queues=$?
1859
1860 if [ $timeout_queues -eq 2 ]; then
1861 master_score 0
1862 return $OCF_ERR_GENERIC
1863
1864 elif [ $timeout_queues -eq 0 ]; then
1865 if [ $rc_queues -ne 0 ]; then
1866 ocf_log err "${LH} rabbitmqctl list_queues exited with errors."
1867 rc=$OCF_ERR_GENERIC
1868
1869 elif [ -n "${queues}" ]; then
1870 local q_c
1871 q_c=`printf %b "${queues}\n" | wc -l`
1872 local mem
1873 mem=`printf %b "${queues}\n" | awk -v sum=0 '{sum+=$1} END {print (sum/1048576)}'`
1874 local mes
1875 mes=`printf %b "${queues}\n" | awk -v sum=0 '{sum+=$2} END {print sum}'`
1876 local c_u
1877 c_u=`printf %b "${queues}\n" | awk -v sum=0 -v cnt=${q_c} '{sum+=$3} END {print (sum+1)/(cnt+1)}'`
1878 local status
1879 status=`echo $(su_rabbit_cmd "${OCF_RESKEY_ctl} -q status")`
1880 ocf_log info "${LH} RabbitMQ is running ${q_c} queues consuming ${mem}m of ${TOTALVMEM}m total, with ${mes} queued messages, average consumer utilization ${c_u}"
1881 ocf_log info "${LH} RabbitMQ status: ${status}"
1882 fi
1883 fi
1884
1885 return $rc
1886 }
1887
1888 ocf_get_private_attr() {
1889 local attr_name="${1:?}"
1890 local attr_default_value="${2:?}"
1891 local nodename="${3:-$THIS_PCMK_NODE}"
1892 local count
1893 count=$(attrd_updater -p --name "$attr_name" --node "$nodename" --query)
1894 if [ $? -ne 0 ]; then
1895 echo $attr_default_value
1896 else
1897 echo "$count" | awk -vdef_val="$attr_default_value" '{ gsub(/"/, "", $3); split($3, vals, "="); if (vals[2] != "") print vals[2]; else print def_val }'
1898 fi
1899 }
1900
1901 ocf_update_private_attr() {
1902 local attr_name="${1:?}"
1903 local attr_value="${2:?}"
1904 local nodename="${3:-$THIS_PCMK_NODE}"
1905 ocf_run attrd_updater -p --name "$attr_name" --node "$nodename" --update "$attr_value"
1906 }
1907
1908 rabbitmqctl_with_timeout_check() {
1909 local command="${1:?}"
1910 local timeout_attr_name="${2:?}"
1911
1912 su_rabbit_cmd "${OCF_RESKEY_ctl} $command"
1913 local rc=$?
1914
1915 check_timeouts $rc $timeout_attr_name "$command"
1916 local has_timed_out=$?
1917
1918 case "$has_timed_out" in
1919 0)
1920 return $rc;;
1921 1)
1922 return 0;;
1923 2)
1924 return 1;;
1925 esac
1926 }
1927
1928 is_cluster_status_ok() {
1929 local LH="${LH}: is_cluster_status_ok:"
1930 rabbitmqctl_with_timeout_check cluster_status rabbit_cluster_status_timeouts > /dev/null 2>&1
1931 }
1932
1933 action_monitor() {
1934 local rc=$OCF_ERR_GENERIC
1935 local LH="${LL} monitor:"
1936 ocf_log debug "${LH} action start."
1937 if ocf_is_true "${OCF_RESKEY_debug}"; then
1938 d=`date '+%Y%m%d %H:%M:%S'`
1939 echo $d >> /tmp/rmq-monitor.log
1940 env >> /tmp/rmq-monitor.log
1941 echo "$d [monitor] start='${OCF_RESKEY_CRM_meta_notify_start_uname}' stop='${OCF_RESKEY_CRM_meta_notify_stop_uname}' active='${OCF_RESKEY_CRM_meta_notify_active_uname}' inactive='${OCF_RESKEY_CRM_meta_notify_inactive_uname}'" >> /tmp/rmq-ocf.log
1942 fi
1943 get_monitor
1944 rc=$?
1945 ocf_log debug "${LH} role: ${OCF_RESKEY_CRM_meta_role}"
1946 ocf_log debug "${LH} result: $rc"
1947 ocf_log debug "${LH} action end."
1948 return $rc
1949 }
1950
1951
1952 action_start() {
1953 local rc=$OCF_ERR_GENERIC
1954 local LH="${LL} start:"
1955 local nowtime
1956
1957 if ocf_is_true "${OCF_RESKEY_debug}"; then
1958 d=`date '+%Y%m%d %H:%M:%S'`
1959 echo $d >> /tmp/rmq-start.log
1960 env >> /tmp/rmq-start.log
1961 echo "$d [start] start='${OCF_RESKEY_CRM_meta_notify_start_uname}' stop='${OCF_RESKEY_CRM_meta_notify_stop_uname}' active='${OCF_RESKEY_CRM_meta_notify_active_uname}' inactive='${OCF_RESKEY_CRM_meta_notify_inactive_uname}'" >> /tmp/rmq-ocf.log
1962 fi
1963
1964 ocf_log info "${LH} action begin."
1965
1966 get_status
1967 rc=$?
1968 if [ $rc -eq $OCF_SUCCESS ] ; then
1969 ocf_log warn "${LH} RMQ-runtime (beam) already started."
1970 return $OCF_SUCCESS
1971 fi
1972
1973 local attrs_to_zero="rabbit_list_channels_timeouts rabbit_get_alarms_timeouts rabbit_list_queues_timeouts rabbit_cluster_status_timeouts rabbit_node_health_check_timeouts"
1974 local attr_name_to_reset
1975 for attr_name_to_reset in $attrs_to_zero; do
1976 ocf_update_private_attr $attr_name_to_reset 0
1977 done
1978
1979 nowtime=$(now)
1980 ocf_log info "${LH} Setting phase 1 one start time to $nowtime"
1981 ocf_update_private_attr 'rabbit-start-phase-1-time' "$nowtime"
1982 ocf_log info "${LH} Deleting start time attribute"
1983 ocf_run crm_attribute -N $THIS_PCMK_NODE -l reboot --name 'rabbit-start-time' --delete
1984 ocf_log info "${LH} Deleting master attribute"
1985 ocf_run crm_attribute -N $THIS_PCMK_NODE -l reboot --name 'rabbit-master' --delete
1986
1987 ocf_log info "${LH} RMQ going to start."
1988 start_rmq_server_app
1989 rc=$?
1990 if [ $rc -eq $OCF_SUCCESS ] ; then
1991 ocf_log info "${LH} RMQ prepared for start succesfully."
1992 fi
1993
1994 ocf_log info "${LH} action end."
1995 return $rc
1996 }
1997
1998
1999 action_stop() {
2000 local rc=$OCF_ERR_GENERIC
2001 local LH="${LL} stop:"
2002
2003 if ocf_is_true "${OCF_RESKEY_debug}"; then
2004 d=$(date '+%Y%m%d %H:%M:%S')
2005 echo $d >> /tmp/rmq-stop.log
2006 env >> /tmp/rmq-stop.log
2007 echo "$d [stop] start='${OCF_RESKEY_CRM_meta_notify_start_uname}' stop='${OCF_RESKEY_CRM_meta_notify_stop_uname}' active='${OCF_RESKEY_CRM_meta_notify_active_uname}' inactive='${OCF_RESKEY_CRM_meta_notify_inactive_uname}'" >> /tmp/rmq-ocf.log
2008 fi
2009
2010 ocf_log info "${LH} action begin."
2011
2012 ocf_log info "${LH} Deleting master attribute"
2013 ocf_run crm_attribute -N $THIS_PCMK_NODE -l reboot --name 'rabbit-master' --delete
2014 master_score 0
2015 ocf_log info "${LH} Deleting start time attribute"
2016 ocf_run crm_attribute -N $THIS_PCMK_NODE -l reboot --name 'rabbit-start-time' --delete
2017
2018 # Wait for synced state first
2019 ocf_log info "${LH} waiting $((OCF_RESKEY_stop_time/2)) to sync"
2020 wait_sync $((OCF_RESKEY_stop_time/2))
2021
2022 ocf_log info "${LH} RMQ-runtime (beam) going to down."
2023 stop_server_process
2024
2025 if [ $? -ne $OCF_SUCCESS ] ; then
2026 ocf_log err "RMQ-runtime (beam) couldn't be stopped and will likely became unmanaged. Take care of it manually!"
2027 ocf_log info "${LH} action end."
2028 exit $OCF_ERR_GENERIC
2029 fi
2030
2031 ocf_log info "${LH} RMQ-runtime (beam) not running."
2032 ocf_log info "${LH} action end."
2033 return $OCF_SUCCESS
2034 }
2035
2036 #######################################################################
2037 # Enhanced list_channels:
2038 # - nodes are processed in parallel
2039 # - report contains information about which nodes timed out
2040 #
2041 # 'list_channels' is used as a healh-check for current node, but it
2042 # actually checks overall health of all node in cluster. And there were
2043 # some bugs where only one (non-local) channel became stuck, but OCF
2044 # script was wrongfully killing local node.
2045 #
2046 # Hopefully all such bugs are fixed, but if not - it will allow to
2047 # detect such conditions.
2048 #
2049 # Somewhat strange implementation is due to the following reasons:
2050 # - ability to support older versions of RabbitMQ which have reached
2051 # end-of-life with single version of the script
2052 # - zero dependencies - for older versions this functionality could be
2053 # implemented as a plugin, but it'll require this plugin installation
2054 enhanced_list_channels() {
2055 # One second less than timeout of su_rabbit_cmd
2056 local timeout=$((${TIMEOUT_ARG:-5} - 1))
2057
2058 su_rabbit_cmd "xargs -0 ${OCF_RESKEY_ctl} eval" <<EOF
2059 SecondsToCompletion = $timeout,
2060
2061 %% Milliseconds since unix epoch
2062 Now = fun() ->
2063 {Mega, Secs, Micro} = os:timestamp(),
2064 Mili = Micro div 1000,
2065 Mili + 1000 * (Secs + 1000000 * Mega)
2066 end,
2067
2068 %% We shouldn't continue execution past this time
2069 ShouldEndAt = Now() + SecondsToCompletion * 1000,
2070
2071 %% How many milliseconds we still have
2072 Timeout = fun() ->
2073 case ShouldEndAt - Now() of
2074 Past when Past =< 0 ->
2075 0;
2076 Timeout ->
2077 Timeout
2078 end
2079 end,
2080
2081 %% Lambda combinator - for defining anonymous recursive functions
2082 Y = fun(F) ->
2083 (fun (X) -> F(fun(Y) -> (X(X))(Y) end) end)(
2084 fun (X) -> F(fun(Y) -> (X(X))(Y) end) end)
2085 end,
2086
2087 Parent = self(),
2088
2089 ListChannels = Y(fun(Rec) ->
2090 fun (({Node, [], OkChannelsCount})) ->
2091 Parent ! {Node, ok, OkChannelsCount};
2092 ({Node, [Chan|Rest], OkChannelsCount}) ->
2093 case catch rpc:call(Node, rabbit_channel, info, [Chan], Timeout()) of
2094 Infos when is_list(Infos) ->
2095 Rec({Node, Rest, OkChannelsCount + 1});
2096 {badrpc, {'EXIT', {noproc, _}}} ->
2097 %% Channel became dead before we could request it's status, don't care
2098 Rec({Node, Rest, OkChannelsCount});
2099 Err ->
2100 Parent ! {Node, Err, OkChannelsCount}
2101 end
2102 end
2103 end),
2104
2105 SingleNodeListing = fun(Node) ->
2106 case catch rpc:call(Node, pg_local, get_members, [rabbit_channels], Timeout()) of
2107 LocalChannels when is_list(LocalChannels) ->
2108 ListChannels({Node, LocalChannels, 0});
2109 Err ->
2110 Parent ! {Node, Err, 0}
2111 end
2112 end,
2113
2114 AllNodes = rabbit_mnesia:cluster_nodes(running),
2115 [ spawn(fun() -> SingleNodeListing(Node) end) || Node <- AllNodes ],
2116
2117 WaitForNodes = Y(fun(Rec) ->
2118 fun ({[], Acc}) ->
2119 Acc;
2120 ({RemainingNodes, Acc}) ->
2121 receive
2122 {Node, _Status, _ChannelCount} = Smth ->
2123 RemainingNodes1 = lists:delete(Node, RemainingNodes),
2124 Rec({RemainingNodes1, [Smth|Acc]})
2125 after Timeout() + 100 ->
2126 Acc
2127 end
2128 end
2129 end),
2130
2131 Result = WaitForNodes({AllNodes, []}),
2132
2133 ExpandedResult = [ case lists:keysearch(Node, 1, Result) of
2134 {value, NodeResult} ->
2135 NodeResult;
2136 false ->
2137 {Node, no_data_collected, 0}
2138 end || Node <- AllNodes ],
2139
2140 ExpandedResult.
2141 EOF
2142 }
2143
2144 #######################################################################
2145 # Join the cluster and return OCF_SUCCESS, if joined.
2146 # Return 10, if node is trying to join to itself or empty destination.
2147 # Return OCF_ERR_GENERIC, if cannot join.
2148 jjj_join () {
2149 local join_to="$1"
2150 local rc=$OCF_ERR_GENERIC
2151 local LH="${LL} jjj_join:"
2152
2153 my_host ${join_to}
2154 rc=$?
2155 ocf_log debug "${LH} node='${join_to}' rc='${rc}'"
2156
2157 # Check whether we are joining to ourselves
2158 # or master host is not given
2159 if [ $rc -ne 0 ] && [ "${join_to}" ] ; then
2160 ocf_log info "${LH} Joining to cluster by node '${join_to}'"
2161 join_to_cluster "${join_to}"
2162 rc=$?
2163 if [ $rc -ne $OCF_SUCCESS ] ; then
2164 ocf_log err "${LH} Failed to join the cluster. The mnesia will be reset."
2165 reset_mnesia
2166 rc=$OCF_ERR_GENERIC
2167 fi
2168 fi
2169 return $rc
2170 }
2171
2172 action_notify() {
2173 local rc_join=$OCF_SUCCESS
2174 local rc=$OCF_ERR_GENERIC
2175 local rc2=$OCF_ERR_GENERIC
2176 local LH="${LL} notify:"
2177 local nodelist
2178
2179 if ocf_is_true "${OCF_RESKEY_debug}"; then
2180 d=`date '+%Y%m%d %H:%M:%S'`
2181 echo $d >> /tmp/rmq-notify.log
2182 env >> /tmp/rmq-notify.log
2183 echo "$d [notify] ${OCF_RESKEY_CRM_meta_notify_type}-${OCF_RESKEY_CRM_meta_notify_operation} promote='${OCF_RESKEY_CRM_meta_notify_promote_uname}' demote='${OCF_RESKEY_CRM_meta_notify_demote_uname}' master='${OCF_RESKEY_CRM_meta_notify_master_uname}' slave='${OCF_RESKEY_CRM_meta_notify_slave_uname}' start='${OCF_RESKEY_CRM_meta_notify_start_uname}' stop='${OCF_RESKEY_CRM_meta_notify_stop_uname}' active='${OCF_RESKEY_CRM_meta_notify_active_uname}' inactive='${OCF_RESKEY_CRM_meta_notify_inactive_uname}'" >> /tmp/rmq-ocf.log
2184 fi
2185
2186 if [ "${OCF_RESKEY_CRM_meta_notify_type}" = 'post' ] ; then
2187 # POST- anything notify section
2188 case "$OCF_RESKEY_CRM_meta_notify_operation" in
2189 promote)
2190 ocf_log info "${LH} post-promote begin."
2191
2192 rc=$OCF_SUCCESS
2193
2194 # Do nothing, if the list of nodes being promoted reported empty.
2195 # Delegate recovery, if needed, to the "running out of the cluster" monitor's logic
2196 if [ -z "${OCF_RESKEY_CRM_meta_notify_promote_uname}" ] ; then
2197 ocf_log warn "${LH} there are no nodes to join to reported on post-promote. Nothing to do."
2198
2199 elif my_host "${OCF_RESKEY_CRM_meta_notify_promote_uname}"; then
2200 ocf_log info "${LH} ignoring post-promote of self"
2201
2202 elif is_clustered_with "${OCF_RESKEY_CRM_meta_notify_promote_uname}"; then
2203 if get_status rabbit; then
2204 ocf_log info "${LH} we are already clustered with master - ${OCF_RESKEY_CRM_meta_notify_promote_uname}. Nothing to do."
2205 else
2206 ocf_log info "${LH} we are already clustered with master - ${OCF_RESKEY_CRM_meta_notify_promote_uname}. We only need to start the app."
2207
2208 try_to_start_rmq_app
2209 rc2=$?
2210 update_rabbit_start_time_if_rc $rc2
2211 fi
2212
2213 else
2214 # Note, this should fail when the mnesia is inconsistent.
2215 # For example, when the "old" master processing the promition of the new one.
2216 # Later this ex-master node will rejoin the cluster at post-start.
2217 jjj_join "${OCF_RESKEY_CRM_meta_notify_promote_uname}"
2218 rc=$?
2219 if [ $rc -eq $OCF_ERR_GENERIC ] ; then
2220 ocf_log err "${LH} Failed to join the cluster on post-promote. The resource will be restarted."
2221 fi
2222 fi
2223
2224 ocf_log info "${LH} post-promote end."
2225 return $rc
2226 ;;
2227 start)
2228 ocf_log info "${LH} post-start begin."
2229 local nodes_list="${OCF_RESKEY_CRM_meta_notify_start_uname} ${OCF_RESKEY_CRM_meta_notify_active_uname}"
2230 # Do nothing, if the list of nodes being started or running reported empty
2231 # Delegate recovery, if needed, to the "running out of the cluster" monitor's logic
2232 if [ -z "${OCF_RESKEY_CRM_meta_notify_start_uname}" ] && [ -z "${OCF_RESKEY_CRM_meta_notify_active_uname}" ] ; then
2233 ocf_log warn "${LH} I'm a last man standing and I must survive!"
2234 ocf_log info "${LH} post-start end."
2235 return $OCF_SUCCESS
2236 fi
2237 # check did this event from this host
2238 my_host "${nodes_list}"
2239 rc=$?
2240 # Do nothing, if there is no master reported
2241 # Delegate recovery, if needed, to the "running out of the cluster" monitor's logic
2242 if [ -z "${OCF_RESKEY_CRM_meta_notify_master_uname}" ] ; then
2243 ocf_log warn "${LH} there are no nodes to join to reported on post-start. Nothing to do."
2244 ocf_log info "${LH} post-start end."
2245 return $OCF_SUCCESS
2246 fi
2247 if [ $rc -eq $OCF_SUCCESS ] ; then
2248 # Now we need to:
2249 # a. join to the cluster if we are not joined yet
2250 # b. start the RabbitMQ application, which is always
2251 # stopped after start action finishes
2252 check_need_join_to ${OCF_RESKEY_CRM_meta_notify_master_uname}
2253 rc_join=$?
2254 if [ $rc_join -eq $OCF_SUCCESS ]; then
2255 ocf_log warn "${LH} Going to join node ${OCF_RESKEY_CRM_meta_notify_master_uname}"
2256 jjj_join "${OCF_RESKEY_CRM_meta_notify_master_uname}"
2257 rc2=$?
2258 else
2259 ocf_log warn "${LH} We are already clustered with node ${OCF_RESKEY_CRM_meta_notify_master_uname}"
2260
2261 try_to_start_rmq_app
2262 rc2=$?
2263 update_rabbit_start_time_if_rc $rc2
2264 fi
2265 if [ -s "${OCF_RESKEY_definitions_dump_file}" ] ; then
2266 ocf_log info "File ${OCF_RESKEY_definitions_dump_file} exists"
2267 ocf_run curl --silent --show-error --request POST --user $OCF_RESKEY_admin_user:$OCF_RESKEY_admin_password $OCF_RESKEY_host_ip:15672/api/definitions --header "Content-Type:application/json" --data @$OCF_RESKEY_definitions_dump_file
2268 rc=$?
2269 if [ $rc -eq $OCF_SUCCESS ] ; then
2270 ocf_log info "RMQ definitions have imported succesfully."
2271 else
2272 ocf_log err "RMQ definitions have not imported."
2273 fi
2274 fi
2275 if [ $rc2 -eq $OCF_ERR_GENERIC ] ; then
2276 ocf_log warn "${LH} Failed to join the cluster on post-start. The resource will be restarted."
2277 ocf_log info "${LH} post-start end."
2278 return $OCF_ERR_GENERIC
2279 fi
2280 fi
2281 ocf_log info "${LH} post-start end."
2282 ;;
2283 stop)
2284 # if rabbitmq-server stops on any another node, we should remove it from cluster (as ordinary operation)
2285 ocf_log info "${LH} post-stop begin."
2286 # Report not running, if there are no nodes being stopped reported
2287 if [ -z "${OCF_RESKEY_CRM_meta_notify_stop_uname}" ] ; then
2288 ocf_log warn "${LH} there are no nodes being stopped reported on post-stop. The resource will be restarted."
2289 ocf_log info "${LH} post-stop end."
2290 return $OCF_ERR_GENERIC
2291 fi
2292 my_host "${OCF_RESKEY_CRM_meta_notify_stop_uname}"
2293 rc=$?
2294 if [ $rc -ne $OCF_SUCCESS ] ; then
2295 # Wait for synced state first
2296 ocf_log info "${LH} waiting $((OCF_RESKEY_stop_time/2)) to sync"
2297 wait_sync $((OCF_RESKEY_stop_time/2))
2298 # On other nodes processing the post-stop, make sure the stopped node will be forgotten
2299 unjoin_nodes_from_cluster "${OCF_RESKEY_CRM_meta_notify_stop_uname}"
2300 else
2301 # On the nodes being stopped, reset the master score
2302 ocf_log info "${LH} resetting the master score."
2303 master_score 0
2304 fi
2305 # always returns OCF_SUCCESS
2306 ocf_log info "${LH} post-stop end."
2307 ;;
2308 *) ;;
2309 esac
2310 fi
2311
2312 return $OCF_SUCCESS
2313 }
2314
2315
2316 action_promote() {
2317 local rc=$OCF_ERR_GENERIC
2318 local LH="${LL} promote:"
2319
2320 if ocf_is_true "${OCF_RESKEY_debug}"; then
2321 d=$(date '+%Y%m%d %H:%M:%S')
2322 echo $d >> /tmp/rmq-promote.log
2323 env >> /tmp/rmq-promote.log
2324 echo "$d [promote] start='${OCF_RESKEY_CRM_meta_notify_start_uname}' stop='${OCF_RESKEY_CRM_meta_notify_stop_uname}' active='${OCF_RESKEY_CRM_meta_notify_active_uname}' inactive='${OCF_RESKEY_CRM_meta_notify_inactive_uname}'" >> /tmp/rmq-ocf.log
2325 fi
2326
2327 ocf_log info "${LH} action begin."
2328
2329 get_monitor
2330 rc=$?
2331 ocf_log info "${LH} get_monitor returns ${rc}"
2332 case "$rc" in
2333 "$OCF_SUCCESS")
2334 # Running as slave. Normal, expected behavior.
2335 ocf_log info "${LH} Resource is currently running as Slave"
2336 # rabbitmqctl start_app if need
2337 get_status rabbit
2338 rc=$?
2339 ocf_log info "${LH} Updating cluster master attribute"
2340 ocf_run crm_attribute -N $THIS_PCMK_NODE -l reboot --name 'rabbit-master' --update 'true'
2341 if [ $rc -ne $OCF_SUCCESS ] ; then
2342 ocf_log info "${LH} RMQ app is not started. Starting..."
2343 start_rmq_server_app
2344 rc=$?
2345 if [ $rc -eq 0 ] ; then
2346 try_to_start_rmq_app
2347 rc=$?
2348 if [ $rc -ne 0 ] ; then
2349 ocf_log err "${LH} Can't start RMQ app. Master resource is failed."
2350 ocf_log info "${LH} action end."
2351 exit $OCF_FAILED_MASTER
2352 fi
2353
2354 [ -f "${OCF_RESKEY_policy_file}" ] && . "${OCF_RESKEY_policy_file}"
2355
2356 update_rabbit_start_time_if_rc $rc
2357
2358 ocf_log info "${LH} Checking master status"
2359 get_monitor
2360 rc=$?
2361 ocf_log info "${LH} Master status is $rc"
2362 if [ $rc = $OCF_RUNNING_MASTER ]
2363 then
2364 rc=$OCF_SUCCESS
2365 else
2366 ocf_log err "${LH} Master resource is failed."
2367 ocf_log info "${LH} action end."
2368 exit $OCF_FAILED_MASTER
2369 fi
2370 else
2371 ocf_log err "${LH} Can't start RMQ-runtime."
2372 rc=$OCF_ERR_GENERIC
2373 fi
2374 fi
2375 return $rc
2376 ;;
2377 "$OCF_RUNNING_MASTER")
2378 # Already a master. Unexpected, but not a problem.
2379 ocf_log warn "${LH} Resource is already running as Master"
2380 rc=$OCF_SUCCESS
2381 ;;
2382
2383 "$OCF_FAILED_MASTER")
2384 # Master failed.
2385 ocf_log err "${LH} Master resource is failed and not running"
2386 ocf_log info "${LH} action end."
2387 exit $OCF_FAILED_MASTER
2388 ;;
2389
2390 "$OCF_NOT_RUNNING")
2391 # Currently not running.
2392 ocf_log err "${LH} Resource is currently not running"
2393 rc=$OCF_NOT_RUNNING
2394 ;;
2395 *)
2396 # Failed resource. Let the cluster manager recover.
2397 ocf_log err "${LH} Unexpected error, cannot promote"
2398 ocf_log info "${LH} action end."
2399 exit $rc
2400 ;;
2401 esac
2402
2403 # transform slave RMQ-server to master
2404
2405 ocf_log info "${LH} action end."
2406 return $rc
2407 }
2408
2409
2410 action_demote() {
2411 local LH="${LL} demote:"
2412 ocf_log info "${LH} action begin."
2413 ocf_run crm_attribute -N $THIS_PCMK_NODE -l reboot --name 'rabbit-master' --delete
2414 ocf_log info "${LH} action end."
2415 return $OCF_SUCCESS
2416 }
2417 #######################################################################
2418
2419 case "$1" in
2420 meta-data) meta_data
2421 exit $OCF_SUCCESS;;
2422 usage|help) usage
2423 exit $OCF_SUCCESS;;
2424 esac
2425
2426 rmq_setup_env
2427
2428 # Anything except meta-data and help must pass validation
2429 action_validate || exit $?
2430
2431 # What kind of method was invoked?
2432 case "$1" in
2433 start) action_start;;
2434 stop) action_stop;;
2435 status) action_status;;
2436 monitor) action_monitor;;
2437 validate) action_validate;;
2438 promote) action_promote;;
2439 demote) action_demote;;
2440 notify) action_notify;;
2441 validate-all) action_validate;;
2442 *) usage;;
2443 esac
2444 ###