1 # Hey Emacs, this is a -*- shell-script -*- !!!
3 # utility functions for ctdb event scripts
5 [ -z "$CTDB_VARDIR" ] && {
6 if [ -d "/var/lib/ctdb" ] ; then
7 export CTDB_VARDIR="/var/lib/ctdb"
9 export CTDB_VARDIR="/var/ctdb"
12 [ -z "$CTDB_ETCDIR" ] && {
13 export CTDB_ETCDIR="/etc"
16 #######################################
17 # pull in a system config file, if any
21 foo="${service_config:-${service_name}}"
22 if [ -n "$foo" ] ; then
28 if [ "$1" != "ctdb" ] ; then
36 if [ -f $CTDB_ETCDIR/sysconfig/$1 ]; then
37 . $CTDB_ETCDIR/sysconfig/$1
38 elif [ -f $CTDB_ETCDIR/default/$1 ]; then
39 . $CTDB_ETCDIR/default/$1
40 elif [ -f $CTDB_BASE/sysconfig/$1 ]; then
41 . $CTDB_BASE/sysconfig/$1
44 if [ "$1" = "ctdb" ] ; then
45 _config="${CTDB_BASE}/ctdbd.conf"
46 if [ -r "$_config" ] ; then
56 ##############################################################
58 # CTDB_SCRIPT_DEBUGLEVEL can be overwritten by setting it in a
62 if [ ${CTDB_SCRIPT_DEBUGLEVEL:-2} -ge 4 ] ; then
63 # If there are arguments then echo them. Otherwise expect to
64 # use stdin, which allows us to pass lots of debug using a
87 # Log given message or stdin to either syslog or a CTDB log file
88 # $1 is the tag passed to logger if syslog is in use.
93 case "$CTDB_LOGGING" in
95 if [ -n "$CTDB_LOGGING" ] ; then
96 _file="${CTDB_LOGGING#file:}"
98 _file="/var/log/log.ctdb"
101 if [ -n "$*" ] ; then
109 # Handle all syslog:* variants here too. There's no tool to do
110 # the lossy things, so just use logger.
111 logger -t "ctdbd: ${_tag}" $*
116 # When things are run in the background in an eventscript then logging
117 # output might get lost. This is the "solution". :-)
118 background_with_logging ()
121 "$@" 2>&1 </dev/null |
122 script_log "${script_name}&"
128 ##############################################################
129 # check number of args for different events
135 echo "ERROR: must supply interface, IP and maskbits"
141 echo "ERROR: must supply old interface, new interface, IP and maskbits"
148 ##############################################################
149 # determine on what type of system (init style) we are running
152 # only do detection if not already set:
153 [ -z "$CTDB_INIT_STYLE" ] || return
155 if [ -x /sbin/startproc ]; then
156 CTDB_INIT_STYLE="suse"
157 elif [ -x /sbin/start-stop-daemon ]; then
158 CTDB_INIT_STYLE="debian"
160 CTDB_INIT_STYLE="redhat"
164 ######################################################
165 # simulate /sbin/service on platforms that don't have it
166 # _service() makes it easier to hook the service() function for
173 # do nothing, when no service was specified
174 [ -z "$_service_name" ] && return
176 if [ -x /sbin/service ]; then
177 $_nice /sbin/service "$_service_name" "$_op"
178 elif [ -x /usr/sbin/service ]; then
179 $_nice /usr/sbin/service "$_service_name" "$_op"
180 elif [ -x $CTDB_ETCDIR/init.d/$_service_name ]; then
181 $_nice $CTDB_ETCDIR/init.d/$_service_name "$_op"
182 elif [ -x $CTDB_ETCDIR/rc.d/init.d/$_service_name ]; then
183 $_nice $CTDB_ETCDIR/rc.d/init.d/$_service_name "$_op"
193 ######################################################
194 # simulate /sbin/service (niced) on platforms that don't have it
201 ######################################################
202 # Cached retrieval of PNN from local node. This never changes so why
203 # open a client connection to the server each time this is needed?
204 # This sets $pnn - this avoid an unnecessary subprocess.
207 _pnn_file="$CTDB_VARDIR/state/my-pnn"
208 if [ ! -f "$_pnn_file" ] ; then
209 ctdb pnn | sed -e 's@.*:@@' >"$_pnn_file"
212 read pnn <"$_pnn_file"
215 ######################################################
216 # wrapper around /proc/ settings to allow them to be hooked
218 # 1st arg is relative path under /proc/, 2nd arg is value to set
221 echo "$2" >"/proc/$1"
224 ######################################################
225 # wrapper around getting file contents from /proc/ to allow
226 # this to be hooked for testing
227 # 1st arg is relative path under /proc/
233 ######################################################
234 # Print up to $_max kernel stack traces for processes named $_program
235 program_stack_traces ()
241 for _pid in $(pidof "$_prog") ; do
242 [ $_count -le $_max ] || break
244 # Do this first to avoid racing with process exit
245 _stack=$(get_proc "${_pid}/stack" 2>/dev/null)
246 if [ -n "$_stack" ] ; then
247 echo "Stack trace for ${_prog}[${_pid}]:"
249 _count=$(($_count + 1))
254 ######################################################
255 # Check that an RPC service is healthy -
256 # this includes allowing a certain number of failures
257 # before marking the NFS service unhealthy.
259 # usage: nfs_check_rpc_service SERVICE_NAME [ triple ...]
261 # each triple is a set of 3 arguments: an operator, a
262 # fail count limit and an action string.
266 # nfs_check_rpc_service "lockd" \
267 # -ge 15 "verbose restart unhealthy" \
268 # -eq 10 "restart:bs"
270 # says that if lockd is down for 15 iterations then do
271 # a verbose restart of lockd and mark the node unhealthy.
272 # Before this, after 10 iterations of failure, the
273 # service is restarted silently in the background.
274 # Order is important: the number of failures need to be
275 # specified in reverse order because processing stops
276 # after the first condition that is true.
277 ######################################################
278 nfs_check_rpc_service ()
280 _prog_name="$1" ; shift
282 if _nfs_check_rpc_common "$_prog_name" ; then
286 while [ -n "$3" ] ; do
287 if _nfs_check_rpc_action "$1" "$2" "$3" ; then
294 # The new way of doing things...
295 nfs_check_rpc_services ()
297 # Files must end with .check - avoids editor backups, RPM fu, ...
298 for _f in "${CTDB_BASE}/nfs-rpc-checks.d/"[0-9][0-9].*.check ; do
300 _prog_name="${_t##*/[0-9][0-9].}"
302 if _nfs_check_rpc_common "$_prog_name" ; then
303 # This RPC service is up, check next service...
307 # Check each line in the file in turn until one of the limit
309 while read _cmp _lim _rest ; do
315 if _nfs_check_rpc_action "$_cmp" "$_lim" "$_rest" ; then
316 # Limit was hit on this line, no further checking...
323 _nfs_check_rpc_common ()
327 # Some platforms don't have separate programs for all services.
328 case "$_prog_name" in
330 which "rpc.${_prog_name}" >/dev/null 2>&1 || return 0
333 case "$_prog_name" in
355 echo "Internal error: unknown RPC program \"$_prog_name\"."
359 _service_name="nfs_${_prog_name}"
361 if ctdb_check_rpc "$_rpc_prog" $_version >/dev/null ; then
362 ctdb_counter_init "$_service_name"
366 ctdb_counter_incr "$_service_name"
371 _nfs_check_rpc_action ()
377 if ctdb_check_counter "quiet" "$_cmp" "$_limit" "$_service_name" ; then
381 for _action in $_actions ; do
384 echo "$ctdb_check_rpc_out"
387 _nfs_restart_rpc_service "$_prog_name"
390 _nfs_restart_rpc_service "$_prog_name" true
396 echo "Internal error: unknown action \"$_action\"."
404 _nfs_restart_rpc_service ()
407 _background="${2:-false}"
409 if $_background ; then
410 _maybe_background="background_with_logging"
415 _p="rpc.${_prog_name}"
417 case "$_prog_name" in
419 echo "Trying to restart NFS service"
420 $_maybe_background startstop_nfs restart
423 echo "Trying to restart $_prog_name [${_p}]"
425 nfs_dump_some_threads "$_p"
426 $_maybe_background $_p $RPCMOUNTDOPTS \
427 ${MOUNTD_PORT:+-p} $MOUNTD_PORT
430 echo "Trying to restart $_prog_name [${_p}]"
432 nfs_dump_some_threads "$_p"
433 $_maybe_background $_p ${RQUOTAD_PORT:+-p} $RQUOTAD_PORT
436 echo "Trying to restart lock manager service"
437 $_maybe_background startstop_nfslock restart
440 echo "Trying to restart $_prog_name [${_p}]"
442 nfs_dump_some_threads "$_p"
443 $_maybe_background $_p \
444 ${STATD_HOSTNAME:+-n} $STATD_HOSTNAME \
445 ${STATD_PORT:+-p} $STATD_PORT \
446 ${STATD_OUTGOING_PORT:+-o} $STATD_OUTGOING_PORT
449 echo "Internal error: unknown RPC program \"$_prog_name\"."
454 ######################################################
455 # check that a rpc server is registered with portmap
456 # and responding to requests
457 # usage: ctdb_check_rpc SERVICE_NAME VERSION
458 ######################################################
464 _localhost="${CTDB_RPCINFO_LOCALHOST:-127.0.0.1}"
466 if ! ctdb_check_rpc_out=$(rpcinfo -T tcp $_localhost $progname $version 2>&1) ; then
467 ctdb_check_rpc_out="ERROR: $progname failed RPC check:
469 echo "$ctdb_check_rpc_out"
474 ######################################################
475 # Ensure $service_name is set
476 assert_service_name ()
478 [ -n "$service_name" ] || die "INTERNAL ERROR: \$service_name not set"
481 ######################################################
482 # check a set of directories is available
483 # return 1 on a missing directory
484 # directories are read from stdin
485 ######################################################
486 ctdb_check_directories_probe()
488 while IFS="" read d ; do
494 [ -d "${d}/." ] || return 1
499 ######################################################
500 # check a set of directories is available
501 # directories are read from stdin
502 ######################################################
503 ctdb_check_directories()
505 ctdb_check_directories_probe || {
506 echo "ERROR: $service_name directory \"$d\" not available"
511 ######################################################
512 # check a set of tcp ports
513 # usage: ctdb_check_tcp_ports <ports...>
514 ######################################################
516 # This flag file is created when a service is initially started. It
517 # is deleted the first time TCP port checks for that service succeed.
518 # Until then ctdb_check_tcp_ports() prints a more subtle "error"
519 # message if a port check fails.
520 _ctdb_check_tcp_common ()
523 _ctdb_service_started_file="$ctdb_fail_dir/$service_name.started"
526 ctdb_check_tcp_init ()
528 _ctdb_check_tcp_common
529 mkdir -p "${_ctdb_service_started_file%/*}" # dirname
530 touch "$_ctdb_service_started_file"
533 # Check whether something is listening on all of the given TCP ports
534 # using the "ctdb checktcpport" command.
535 ctdb_check_tcp_ports()
537 if [ -z "$1" ] ; then
538 echo "INTERNAL ERROR: ctdb_check_tcp_ports - no ports specified"
542 for _p ; do # process each function argument (port)
543 _cmd="ctdb checktcpport $_p"
548 _ctdb_check_tcp_common
549 if [ ! -f "$_ctdb_service_started_file" ] ; then
550 echo "ERROR: $service_name tcp port $_p is not responding"
551 debug "\"ctdb checktcpport $_p\" was able to bind to port"
553 echo "INFO: $service_name tcp port $_p is not responding"
559 # Couldn't bind, something already listening, next port...
563 echo "ERROR: unexpected error running \"ctdb checktcpport\""
565 ctdb checktcpport (exited with $_ret) with output:
572 # All ports listening
573 _ctdb_check_tcp_common
574 rm -f "$_ctdb_service_started_file"
578 ######################################################
579 # check a unix socket
580 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
581 ######################################################
582 ctdb_check_unix_socket() {
584 [ -z "$socket_path" ] && return
586 if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
587 echo "ERROR: $service_name socket $socket_path not found"
592 ######################################################
593 # check a command returns zero status
594 # usage: ctdb_check_command <command>
595 ######################################################
596 ctdb_check_command ()
598 _out=$("$@" 2>&1) || {
599 echo "ERROR: $* returned error"
605 ################################################
606 # kill off any TCP connections with the given IP
607 ################################################
608 kill_tcp_connections ()
613 if [ "$2" = "oneway" ] ; then
617 get_tcp_connections_for_ip "$_ip" | {
622 while read _dst _src; do
623 _destport="${_dst##*:}"
626 # we only do one-way killtcp for CIFS
627 139|445) __oneway=true ;;
630 echo "Killing TCP connection $_src $_dst"
631 _connections="${_connections}${_nl}${_src} ${_dst}"
632 if ! $__oneway ; then
633 _connections="${_connections}${_nl}${_dst} ${_src}"
636 _killcount=$(($_killcount + 1))
639 if [ $_killcount -eq 0 ] ; then
643 echo "$_connections" | ctdb killtcp || {
644 echo "Failed to send killtcp control"
650 _remaining=$(get_tcp_connections_for_ip $_ip | wc -l)
652 if [ $_remaining -eq 0 ] ; then
653 echo "Killed $_killcount TCP connections to released IP $_ip"
657 _count=$(($_count + 1))
658 if [ $_count -gt 3 ] ; then
659 echo "Timed out killing tcp connections for IP $_ip ($_remaining remaining)"
663 echo "Waiting for $_remaining connections to be killed for IP $_ip"
669 ##################################################################
670 # kill off the local end for any TCP connections with the given IP
671 ##################################################################
672 kill_tcp_connections_local_only ()
674 kill_tcp_connections "$1" "oneway"
677 ##################################################################
678 # tickle any TCP connections with the given IP
679 ##################################################################
680 tickle_tcp_connections ()
684 get_tcp_connections_for_ip "$_ip" |
688 while read dest src; do
689 echo "Tickle TCP connection $src $dest"
690 ctdb tickle $src $dest >/dev/null 2>&1 || _failed=true
691 echo "Tickle TCP connection $dest $src"
692 ctdb tickle $dest $src >/dev/null 2>&1 || _failed=true
696 echo "Failed to send tickle control"
701 get_tcp_connections_for_ip ()
705 netstat -tn | awk -v ip=$_ip \
706 'index($1, "tcp") == 1 && \
707 (index($4, ip ":") == 1 || index($4, "::ffff:" ip ":") == 1) \
708 && $6 == "ESTABLISHED" \
712 ##################################################################
713 # use statd-callout to update NFS lock info
714 ##################################################################
715 nfs_update_lock_info ()
717 if [ -x "$CTDB_BASE/statd-callout" ] ; then
718 "$CTDB_BASE/statd-callout" update
722 ########################################################
723 # start/stop the Ganesha nfs service
724 ########################################################
727 _service_name="nfs-ganesha-$CTDB_CLUSTER_FILESYSTEM_TYPE"
730 service "$_service_name" start
733 service "$_service_name" stop
736 service "$_service_name" stop
737 nfs_dump_some_threads "rpc.statd"
738 service "$_service_name" start
743 ########################################################
744 # start/stop the nfs service on different platforms
745 ########################################################
748 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
751 [ -x $CTDB_ETCDIR/init.d/nfslock -o \
752 -r /usr/lib/systemd/system/nfs-lock.service ] && {
760 service nfsserver start
763 service nfsserver stop > /dev/null 2>&1
766 set_proc "fs/nfsd/threads" 0
767 service nfsserver stop > /dev/null 2>&1
769 nfs_dump_some_threads
770 service nfsserver start
777 service nfslock start
785 set_proc "fs/nfsd/threads" 0
786 service nfs stop > /dev/null 2>&1
787 service nfslock stop > /dev/null 2>&1
789 nfs_dump_some_threads
790 service nfslock start
796 echo "Unknown platform. NFS is not supported with ctdb"
802 # Dump up to the configured number of nfsd thread backtraces.
803 nfs_dump_some_threads ()
807 _num="${CTDB_NFS_DUMP_STUCK_THREADS:-5}"
808 [ $_num -gt 0 ] || return 0
810 program_stack_traces "$_prog" $_num
813 ########################################################
814 # start/stop the nfs lockmanager service on different platforms
815 ########################################################
816 startstop_nfslock() {
818 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
821 [ -x $CTDB_ETCDIR/init.d/nfslock -o \
822 -r /usr/lib/systemd/system/nfs-lock.service ] && {
828 # for sles there is no service for lockmanager
829 # so we instead just shutdown/restart nfs
832 service nfsserver start
835 service nfsserver stop > /dev/null 2>&1
838 service nfsserver stop > /dev/null 2>&1
839 service nfsserver start
846 service nfslock start
849 service nfslock stop > /dev/null 2>&1
852 service nfslock stop > /dev/null 2>&1
853 service nfslock start
858 echo "Unknown platform. NFS locking is not supported with ctdb"
864 ########################################################
872 # Ensure interface is up
873 ip link set "$_iface" up || \
874 die "Failed to bringup interface $_iface"
876 # Only need to define broadcast for IPv4
882 ip addr add "$_ip/$_maskbits" $_bcast dev "$_iface" || {
883 echo "Failed to add $_ip/$_maskbits on dev $_iface"
887 # Wait 5 seconds for IPv6 addresses to stop being tentative...
888 if [ -z "$_bcast" ] ; then
889 for _x in $(seq 1 10) ; do
890 ip addr show to "${_ip}/128" | grep -q "tentative" || break
894 # If the address was a duplicate then it won't be on the
895 # interface so flag an error.
896 _t=$(ip addr show to "${_ip}/128")
899 echo "Failed to add $_ip/$_maskbits on dev $_iface"
902 *tentative*|*dadfailed*)
903 echo "Failed to add $_ip/$_maskbits on dev $_iface"
904 ip addr del "$_ip/$_maskbits" dev "$_iface"
911 delete_ip_from_iface()
917 # This could be set globally for all interfaces but it is probably
918 # better to avoid surprises, so limit it the interfaces where CTDB
919 # has public IP addresses. There isn't anywhere else convenient
920 # to do this so just set it each time. This is much cheaper than
921 # remembering and re-adding secondaries.
922 set_proc "sys/net/ipv4/conf/${_iface}/promote_secondaries" 1
924 ip addr del "$_ip/$_maskbits" dev "$_iface" || {
925 echo "Failed to del $_ip on dev $_iface"
930 # If the given IP is hosted then print 2 items: maskbits and iface
936 *:*) _family="inet6" ; _bits=128 ;;
937 *) _family="inet" ; _bits=32 ;;
940 ip addr show to "${_addr}/${_bits}" 2>/dev/null | \
941 awk -v family="${_family}" \
942 'NR == 1 { iface = $2; sub(":$", "", iface) } \
943 $1 ~ /inet/ { mask = $2; sub(".*/", "", mask); \
944 print mask, iface, family }'
949 _addr="${1%/*}" # Remove optional maskbits
951 set -- $(ip_maskbits_iface $_addr)
952 if [ -n "$1" ] ; then
955 echo "Removing public address $_addr/$_maskbits from device $_iface"
956 delete_ip_from_iface $_iface $_addr $_maskbits >/dev/null 2>&1
960 drop_all_public_ips ()
962 while read _ip _x ; do
964 done <"${CTDB_PUBLIC_ADDRESSES:-/dev/null}"
969 set_proc sys/net/ipv4/route/flush 1
970 set_proc sys/net/ipv6/route/flush 1
973 ########################################################
975 _ctdb_counter_common () {
976 _service_name="${1:-${service_name:-${script_name}}}"
977 _counter_file="$ctdb_fail_dir/$_service_name"
978 mkdir -p "${_counter_file%/*}" # dirname
980 ctdb_counter_init () {
981 _ctdb_counter_common "$1"
985 ctdb_counter_incr () {
986 _ctdb_counter_common "$1"
989 echo -n 1 >> "$_counter_file"
991 ctdb_check_counter () {
992 _msg="${1:-error}" # "error" - anything else is silent on fail
993 _op="${2:--ge}" # an integer operator supported by test
994 _limit="${3:-${service_fail_limit}}"
996 _ctdb_counter_common "$1"
999 _size=$(stat -c "%s" "$_counter_file" 2>/dev/null || echo 0)
1001 if [ "$_op" != "%" ] ; then
1002 if [ $_size $_op $_limit ] ; then
1006 if [ $(($_size $_op $_limit)) -eq 0 ] ; then
1011 if [ "$_msg" = "error" ] ; then
1012 echo "ERROR: $_size consecutive failures for $_service_name, marking node unhealthy"
1020 ########################################################
1022 ctdb_status_dir="$CTDB_VARDIR/state/service_status"
1023 ctdb_fail_dir="$CTDB_VARDIR/state/failcount"
1025 ctdb_setup_service_state_dir ()
1027 service_state_dir="$CTDB_VARDIR/state/service_state/${1:-${service_name}}"
1028 mkdir -p "$service_state_dir" || {
1029 echo "Error creating state dir \"$service_state_dir\""
1034 ########################################################
1035 # Managed status history, for auto-start/stop
1037 ctdb_managed_dir="$CTDB_VARDIR/state/managed_history"
1039 _ctdb_managed_common ()
1041 _ctdb_managed_file="$ctdb_managed_dir/$service_name"
1044 ctdb_service_managed ()
1046 _ctdb_managed_common
1047 mkdir -p "$ctdb_managed_dir"
1048 touch "$_ctdb_managed_file"
1051 ctdb_service_unmanaged ()
1053 _ctdb_managed_common
1054 rm -f "$_ctdb_managed_file"
1057 is_ctdb_previously_managed_service ()
1059 _ctdb_managed_common
1060 [ -f "$_ctdb_managed_file" ]
1063 ########################################################
1064 # Check and set status
1068 echo "node is \"$1\", \"${script_name}\" reports problem: $(cat $2)"
1073 if [ -r "$ctdb_status_dir/$script_name/unhealthy" ] ; then
1074 log_status_cat "unhealthy" "$ctdb_status_dir/$script_name/unhealthy"
1076 elif [ -r "$ctdb_status_dir/$script_name/banned" ] ; then
1077 log_status_cat "banned" "$ctdb_status_dir/$script_name/banned"
1086 d="$ctdb_status_dir/$script_name"
1093 for i in "banned" "unhealthy" ; do
1100 ##################################################################
1101 # Reconfigure a service on demand
1103 _ctdb_service_reconfigure_common ()
1105 _d="$ctdb_status_dir/${service_name}"
1107 _ctdb_service_reconfigure_flag="$_d/reconfigure"
1110 ctdb_service_needs_reconfigure ()
1112 _ctdb_service_reconfigure_common
1113 [ -e "$_ctdb_service_reconfigure_flag" ]
1116 ctdb_service_set_reconfigure ()
1118 _ctdb_service_reconfigure_common
1119 >"$_ctdb_service_reconfigure_flag"
1122 ctdb_service_unset_reconfigure ()
1124 _ctdb_service_reconfigure_common
1125 rm -f "$_ctdb_service_reconfigure_flag"
1128 ctdb_service_reconfigure ()
1130 echo "Reconfiguring service \"${service_name}\"..."
1131 ctdb_service_unset_reconfigure
1132 service_reconfigure || return $?
1136 # Default service_reconfigure() function does nothing.
1137 service_reconfigure ()
1142 ctdb_reconfigure_take_lock ()
1144 _ctdb_service_reconfigure_common
1145 _lock="${_d}/reconfigure_lock"
1146 mkdir -p "${_lock%/*}" # dirname
1151 # This is overkill but will work if we need to extend this to
1152 # allow certain events to run multiple times in parallel
1153 # (e.g. takeip) and write multiple PIDs to the file.
1155 if [ -n "$_locker_event" ] ; then
1156 while read _pid ; do
1157 if [ -n "$_pid" -a "$_pid" != $$ ] && \
1158 kill -0 "$_pid" 2>/dev/null ; then
1164 printf "%s\n%s\n" "$event_name" $$ >"$_lock"
1169 ctdb_reconfigure_release_lock ()
1171 _ctdb_service_reconfigure_common
1172 _lock="${_d}/reconfigure_lock"
1177 ctdb_replay_monitor_status ()
1179 echo "Replaying previous status for this script due to reconfigure..."
1180 # Leading separator ('|') is missing in some versions...
1181 _out=$(ctdb scriptstatus -X | grep -E "^\|?monitor\|${script_name}\|")
1182 # Output looks like this:
1183 # |monitor|60.nfs|1|ERROR|1314764004.030861|1314764004.035514|foo bar|
1184 # This is the cheapest way of getting fields in the middle.
1185 set -- $(IFS="|" ; echo $_out)
1188 # The error output field can include colons so we'll try to
1189 # preserve them. The weak checking at the beginning tries to make
1190 # this work for both broken (no leading '|') and fixed output.
1192 _err_out="${_out#*monitor|${script_name}|*|*|*|*|}"
1194 OK) : ;; # Do nothing special.
1196 # Recast this as an error, since we can't exit with the
1197 # correct negative number.
1199 _err_out="[Replay of TIMEDOUT scriptstatus - note incorrect return code.] ${_err_out}"
1202 # Recast this as an OK, since we can't exit with the
1203 # correct negative number.
1205 _err_out="[Replay of DISABLED scriptstatus - note incorrect return code.] ${_err_out}"
1207 *) : ;; # Must be ERROR, do nothing special.
1209 if [ -n "$_err_out" ] ; then
1215 ctdb_service_check_reconfigure ()
1219 # We only care about some events in this function. For others we
1221 case "$event_name" in
1222 monitor|ipreallocated|reconfigure) : ;;
1226 if ctdb_reconfigure_take_lock ; then
1227 # No events covered by this function are running, so proceed
1229 case "$event_name" in
1231 (ctdb_service_reconfigure)
1235 if ctdb_service_needs_reconfigure ; then
1236 ctdb_service_reconfigure
1241 ctdb_reconfigure_release_lock
1243 # Somebody else is running an event we don't want to collide
1244 # with. We proceed with caution.
1245 case "$event_name" in
1247 # Tell whoever called us to retry.
1251 # Defer any scheduled reconfigure and just run the
1252 # rest of the ipreallocated event, as per the
1253 # eventscript. There's an assumption here that the
1254 # event doesn't depend on any scheduled reconfigure.
1255 # This is true in the current code.
1259 # There is most likely a reconfigure in progress so
1260 # the service is possibly unstable. As above, we
1261 # defer any scheduled reconfigured. We also replay
1262 # the previous monitor status since that's the best
1263 # information we have.
1264 ctdb_replay_monitor_status
1270 ##################################################################
1271 # Does CTDB manage this service? - and associated auto-start/stop
1273 ctdb_compat_managed_service ()
1275 if [ "$1" = "yes" -a "$2" = "$service_name" ] ; then
1276 CTDB_MANAGED_SERVICES="$CTDB_MANAGED_SERVICES $2"
1280 is_ctdb_managed_service ()
1284 # $t is used just for readability and to allow better accurate
1285 # matching via leading/trailing spaces
1286 t=" $CTDB_MANAGED_SERVICES "
1288 # Return 0 if "<space>$service_name<space>" appears in $t
1289 if [ "${t#* ${service_name} }" != "${t}" ] ; then
1293 # If above didn't match then update $CTDB_MANAGED_SERVICES for
1294 # backward compatibility and try again.
1295 ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD" "vsftpd"
1296 ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA" "samba"
1297 ctdb_compat_managed_service "$CTDB_MANAGES_WINBIND" "winbind"
1298 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "apache2"
1299 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "httpd"
1300 ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI" "iscsi"
1301 ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD" "clamd"
1302 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs"
1303 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs-ganesha-gpfs"
1305 t=" $CTDB_MANAGED_SERVICES "
1307 # Return 0 if "<space>$service_name<space>" appears in $t
1308 [ "${t#* ${service_name} }" != "${t}" ]
1311 ctdb_start_stop_service ()
1315 # Allow service-start/service-stop pseudo-events to start/stop
1316 # services when we're not auto-starting/stopping and we're not
1318 case "$event_name" in
1320 if is_ctdb_managed_service ; then
1321 die 'service-start event not permitted when service is managed'
1323 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1324 die 'service-start event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1330 if is_ctdb_managed_service ; then
1331 die 'service-stop event not permitted when service is managed'
1333 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1334 die 'service-stop event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1341 # Do nothing unless configured to...
1342 [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] || return 0
1344 [ "$event_name" = "monitor" ] || return 0
1346 if is_ctdb_managed_service ; then
1347 if ! is_ctdb_previously_managed_service ; then
1348 echo "Starting service \"$service_name\" - now managed"
1349 background_with_logging ctdb_service_start
1353 if is_ctdb_previously_managed_service ; then
1354 echo "Stopping service \"$service_name\" - no longer managed"
1355 background_with_logging ctdb_service_stop
1361 ctdb_service_start ()
1363 # The service is marked managed if we've ever tried to start it.
1364 ctdb_service_managed
1366 service_start || return $?
1372 ctdb_service_stop ()
1374 ctdb_service_unmanaged
1378 # Default service_start() and service_stop() functions.
1380 # These may be overridden in an eventscript.
1383 service "$service_name" start
1388 service "$service_name" stop
1391 ##################################################################
1393 ctdb_standard_event_handler ()
1410 _family="$1" ; shift
1411 if [ "$_family" = "inet6" ] ; then
1412 _iptables_cmd="ip6tables"
1414 _iptables_cmd="iptables"
1417 # iptables doesn't like being re-entered, so flock-wrap it.
1418 flock -w 30 "${CTDB_VARDIR}/iptables-ctdb.flock" "$_iptables_cmd" "$@"
1421 # AIX (and perhaps others?) doesn't have mktemp
1422 if ! which mktemp >/dev/null 2>&1 ; then
1426 if [ "$1" = "-d" ] ; then
1430 _d="${TMPDIR:-/tmp}"
1431 _hex10=$(dd if=/dev/urandom count=20 2>/dev/null | \
1433 sed -e 's@\(..........\).*@\1@')
1434 _t="${_d}/tmp.${_hex10}"
1447 ########################################################
1449 ########################################################
1455 tickledir="$CTDB_VARDIR/state/tickles"
1456 mkdir -p "$tickledir"
1459 _pnn=$(ctdb pnn) ; _pnn=${_pnn#PNN:}
1461 # What public IPs do I hold?
1462 _ips=$(ctdb -X ip | awk -F'|' -v pnn=$_pnn '$3 == pnn {print $2}')
1464 # IPs as a regexp choice
1465 _ipschoice="($(echo $_ips | sed -e 's/ /|/g' -e 's/\./\\\\./g'))"
1467 # Record connections to our public IPs in a temporary file
1468 _my_connections="${tickledir}/${_port}.connections"
1469 rm -f "$_my_connections"
1471 awk -v destpat="^${_ipschoice}:${_port}\$" \
1472 '$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ destpat {print $5, $4}' |
1473 sort >"$_my_connections"
1475 # Record our current tickles in a temporary file
1476 _my_tickles="${tickledir}/${_port}.tickles"
1477 rm -f "$_my_tickles"
1478 for _i in $_ips ; do
1479 ctdb -X gettickles $_i $_port |
1480 awk -F'|' 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
1482 sort >"$_my_tickles"
1484 # Add tickles for connections that we haven't already got tickles for
1485 comm -23 "$_my_connections" "$_my_tickles" |
1486 while read _src _dst ; do
1487 ctdb addtickle $_src $_dst
1490 # Remove tickles for connections that are no longer there
1491 comm -13 "$_my_connections" "$_my_tickles" |
1492 while read _src _dst ; do
1493 ctdb deltickle $_src $_dst
1496 rm -f "$_my_connections" "$_my_tickles"
1499 ########################################################
1500 # load a site local config file
1501 ########################################################
1503 [ -n "$CTDB_RC_LOCAL" -a -x "$CTDB_RC_LOCAL" ] && {
1507 [ -x $CTDB_BASE/rc.local ] && {
1508 . $CTDB_BASE/rc.local
1511 [ -d $CTDB_BASE/rc.local.d ] && {
1512 for i in $CTDB_BASE/rc.local.d/* ; do
1513 [ -x "$i" ] && . "$i"
1517 script_name="${0##*/}" # basename
1518 service_fail_limit=1