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
83 # Log given message or stdin to either syslog or a CTDB log file
84 # $1 is the tag passed to logger if syslog is in use.
89 case "$CTDB_LOGGING" in
91 if [ -n "$CTDB_LOGGING" ] ; then
92 _file="${CTDB_LOGGING#file:}"
94 _file="/var/log/log.ctdb"
105 # Handle all syslog:* variants here too. There's no tool to do
106 # the lossy things, so just use logger.
107 logger -t "ctdbd: ${_tag}" $*
112 # When things are run in the background in an eventscript then logging
113 # output might get lost. This is the "solution". :-)
114 background_with_logging ()
117 "$@" 2>&1 </dev/null |
118 script_log "${script_name}&"
124 ##############################################################
125 # check number of args for different events
131 echo "ERROR: must supply interface, IP and maskbits"
137 echo "ERROR: must supply old interface, new interface, IP and maskbits"
144 ##############################################################
145 # determine on what type of system (init style) we are running
148 # only do detection if not already set:
149 [ -z "$CTDB_INIT_STYLE" ] || return
151 if [ -x /sbin/startproc ]; then
152 CTDB_INIT_STYLE="suse"
153 elif [ -x /sbin/start-stop-daemon ]; then
154 CTDB_INIT_STYLE="debian"
156 CTDB_INIT_STYLE="redhat"
160 ######################################################
161 # simulate /sbin/service on platforms that don't have it
162 # _service() makes it easier to hook the service() function for
169 # do nothing, when no service was specified
170 [ -z "$_service_name" ] && return
172 if [ -x /sbin/service ]; then
173 $_nice /sbin/service "$_service_name" "$_op"
174 elif [ -x /usr/sbin/service ]; then
175 $_nice /usr/sbin/service "$_service_name" "$_op"
176 elif [ -x $CTDB_ETCDIR/init.d/$_service_name ]; then
177 $_nice $CTDB_ETCDIR/init.d/$_service_name "$_op"
178 elif [ -x $CTDB_ETCDIR/rc.d/init.d/$_service_name ]; then
179 $_nice $CTDB_ETCDIR/rc.d/init.d/$_service_name "$_op"
189 ######################################################
190 # simulate /sbin/service (niced) on platforms that don't have it
197 ######################################################
198 # wrapper around /proc/ settings to allow them to be hooked
200 # 1st arg is relative path under /proc/, 2nd arg is value to set
203 echo "$2" >"/proc/$1"
206 ######################################################
207 # wrapper around getting file contents from /proc/ to allow
208 # this to be hooked for testing
209 # 1st arg is relative path under /proc/
215 ######################################################
216 # Print up to $_max kernel stack traces for processes named $_program
217 program_stack_traces ()
223 for _pid in $(pidof "$_prog") ; do
224 [ $_count -le $_max ] || break
226 # Do this first to avoid racing with process exit
227 _stack=$(get_proc "${_pid}/stack" 2>/dev/null)
228 if [ -n "$_stack" ] ; then
229 echo "Stack trace for ${_prog}[${_pid}]:"
231 _count=$(($_count + 1))
236 ######################################################
237 # Check that an RPC service is healthy -
238 # this includes allowing a certain number of failures
239 # before marking the NFS service unhealthy.
241 # usage: nfs_check_rpc_service SERVICE_NAME [ triple ...]
243 # each triple is a set of 3 arguments: an operator, a
244 # fail count limit and an action string.
248 # nfs_check_rpc_service "lockd" \
249 # -ge 15 "verbose restart unhealthy" \
250 # -eq 10 "restart:bs"
252 # says that if lockd is down for 15 iterations then do
253 # a verbose restart of lockd and mark the node unhealthy.
254 # Before this, after 10 iterations of failure, the
255 # service is restarted silently in the background.
256 # Order is important: the number of failures need to be
257 # specified in reverse order because processing stops
258 # after the first condition that is true.
259 ######################################################
260 nfs_check_rpc_service ()
262 _prog_name="$1" ; shift
264 if _nfs_check_rpc_common "$_prog_name" ; then
268 while [ -n "$3" ] ; do
269 if _nfs_check_rpc_action "$1" "$2" "$3" ; then
276 # The new way of doing things...
277 nfs_check_rpc_services ()
279 # Files must end with .check - avoids editor backups, RPM fu, ...
280 for _f in "${CTDB_BASE}/nfs-rpc-checks.d/"[0-9][0-9].*.check ; do
282 _prog_name="${_t##*/[0-9][0-9].}"
284 if _nfs_check_rpc_common "$_prog_name" ; then
285 # This RPC service is up, check next service...
289 # Check each line in the file in turn until one of the limit
291 while read _cmp _lim _rest ; do
297 if _nfs_check_rpc_action "$_cmp" "$_lim" "$_rest" ; then
298 # Limit was hit on this line, no further checking...
305 _nfs_check_rpc_common ()
309 # Some platforms don't have separate programs for all services.
310 case "$_prog_name" in
312 which "rpc.${_prog_name}" >/dev/null 2>&1 || return 0
315 case "$_prog_name" in
337 echo "Internal error: unknown RPC program \"$_prog_name\"."
341 _service_name="nfs_${_prog_name}"
343 if ctdb_check_rpc "$_rpc_prog" $_version >/dev/null ; then
344 ctdb_counter_init "$_service_name"
348 ctdb_counter_incr "$_service_name"
353 _nfs_check_rpc_action ()
359 if ctdb_check_counter "quiet" "$_cmp" "$_limit" "$_service_name" ; then
363 for _action in $_actions ; do
366 echo "$ctdb_check_rpc_out"
369 _nfs_restart_rpc_service "$_prog_name"
372 _nfs_restart_rpc_service "$_prog_name" true
378 echo "Internal error: unknown action \"$_action\"."
386 _nfs_restart_rpc_service ()
389 _background="${2:-false}"
391 if $_background ; then
392 _maybe_background="background_with_logging"
397 _p="rpc.${_prog_name}"
399 case "$_prog_name" in
401 echo "Trying to restart NFS service"
402 $_maybe_background startstop_nfs restart
405 echo "Trying to restart $_prog_name [${_p}]"
407 nfs_dump_some_threads "$_p"
408 $_maybe_background $_p ${MOUNTD_PORT:+-p} $MOUNTD_PORT
411 echo "Trying to restart $_prog_name [${_p}]"
413 nfs_dump_some_threads "$_p"
414 $_maybe_background $_p ${RQUOTAD_PORT:+-p} $RQUOTAD_PORT
417 echo "Trying to restart lock manager service"
418 $_maybe_background startstop_nfslock restart
421 echo "Trying to restart $_prog_name [${_p}]"
423 nfs_dump_some_threads "$_p"
424 $_maybe_background $_p \
425 ${STATD_HOSTNAME:+-n} $STATD_HOSTNAME \
426 ${STATD_PORT:+-p} $STATD_PORT \
427 ${STATD_OUTGOING_PORT:+-o} $STATD_OUTGOING_PORT
430 echo "Internal error: unknown RPC program \"$_prog_name\"."
435 ######################################################
436 # check that a rpc server is registered with portmap
437 # and responding to requests
438 # usage: ctdb_check_rpc SERVICE_NAME VERSION
439 ######################################################
445 _localhost="${CTDB_RPCINFO_LOCALHOST:-127.0.0.1}"
447 if ! ctdb_check_rpc_out=$(rpcinfo -u $_localhost $progname $version 2>&1) ; then
448 ctdb_check_rpc_out="ERROR: $progname failed RPC check:
450 echo "$ctdb_check_rpc_out"
455 ######################################################
456 # Ensure $service_name is set
457 assert_service_name ()
459 [ -n "$service_name" ] || die "INTERNAL ERROR: \$service_name not set"
462 ######################################################
463 # check a set of directories is available
464 # return 1 on a missing directory
465 # directories are read from stdin
466 ######################################################
467 ctdb_check_directories_probe()
469 while IFS="" read d ; do
475 [ -d "${d}/." ] || return 1
480 ######################################################
481 # check a set of directories is available
482 # directories are read from stdin
483 ######################################################
484 ctdb_check_directories()
486 ctdb_check_directories_probe || {
487 echo "ERROR: $service_name directory \"$d\" not available"
492 ######################################################
493 # check a set of tcp ports
494 # usage: ctdb_check_tcp_ports <ports...>
495 ######################################################
497 # This flag file is created when a service is initially started. It
498 # is deleted the first time TCP port checks for that service succeed.
499 # Until then ctdb_check_tcp_ports() prints a more subtle "error"
500 # message if a port check fails.
501 _ctdb_check_tcp_common ()
504 _ctdb_service_started_file="$ctdb_fail_dir/$service_name.started"
507 ctdb_check_tcp_init ()
509 _ctdb_check_tcp_common
510 mkdir -p "${_ctdb_service_started_file%/*}" # dirname
511 touch "$_ctdb_service_started_file"
514 # Check whether something is listening on all of the given TCP ports
515 # using the "ctdb checktcpport" command.
516 ctdb_check_tcp_ports()
518 if [ -z "$1" ] ; then
519 echo "INTERNAL ERROR: ctdb_check_tcp_ports - no ports specified"
523 for _p ; do # process each function argument (port)
524 _cmd="ctdb checktcpport $_p"
529 _ctdb_check_tcp_common
530 if [ ! -f "$_ctdb_service_started_file" ] ; then
531 echo "ERROR: $service_name tcp port $_p is not responding"
532 debug "\"ctdb checktcpport $_p\" was able to bind to port"
534 echo "INFO: $service_name tcp port $_p is not responding"
540 # Couldn't bind, something already listening, next port...
544 echo "ERROR: unexpected error running \"ctdb checktcpport\""
546 ctdb checktcpport (exited with $_ret) with output:
553 # All ports listening
554 _ctdb_check_tcp_common
555 rm -f "$_ctdb_service_started_file"
559 ######################################################
560 # check a unix socket
561 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
562 ######################################################
563 ctdb_check_unix_socket() {
565 [ -z "$socket_path" ] && return
567 if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
568 echo "ERROR: $service_name socket $socket_path not found"
573 ######################################################
574 # check a command returns zero status
575 # usage: ctdb_check_command <command>
576 ######################################################
577 ctdb_check_command ()
579 _out=$("$@" 2>&1) || {
580 echo "ERROR: $* returned error"
586 ################################################
587 # kill off any TCP connections with the given IP
588 ################################################
589 kill_tcp_connections ()
594 if [ "$2" = "oneway" ] ; then
598 get_tcp_connections_for_ip "$_ip" | {
603 while read _dst _src; do
604 _destport="${_dst##*:}"
607 # we only do one-way killtcp for CIFS
608 139|445) __oneway=true ;;
611 echo "Killing TCP connection $_src $_dst"
612 _connections="${_connections}${_nl}${_src} ${_dst}"
613 if ! $__oneway ; then
614 _connections="${_connections}${_nl}${_dst} ${_src}"
617 _killcount=$(($_killcount + 1))
620 if [ $_killcount -eq 0 ] ; then
624 echo "$_connections" | ctdb killtcp || {
625 echo "Failed to send killtcp control"
631 _remaining=$(get_tcp_connections_for_ip $_ip | wc -l)
633 if [ $_remaining -eq 0 ] ; then
634 echo "Killed $_killcount TCP connections to released IP $_ip"
638 _count=$(($_count + 1))
639 if [ $_count -gt 3 ] ; then
640 echo "Timed out killing tcp connections for IP $_ip ($_remaining remaining)"
644 echo "Waiting for $_remaining connections to be killed for IP $_ip"
650 ##################################################################
651 # kill off the local end for any TCP connections with the given IP
652 ##################################################################
653 kill_tcp_connections_local_only ()
655 kill_tcp_connections "$1" "oneway"
658 ##################################################################
659 # tickle any TCP connections with the given IP
660 ##################################################################
661 tickle_tcp_connections ()
665 get_tcp_connections_for_ip "$_ip" |
669 while read dest src; do
670 echo "Tickle TCP connection $src $dest"
671 ctdb tickle $src $dest >/dev/null 2>&1 || _failed=true
672 echo "Tickle TCP connection $dest $src"
673 ctdb tickle $dest $src >/dev/null 2>&1 || _failed=true
677 echo "Failed to send tickle control"
682 get_tcp_connections_for_ip ()
686 netstat -tn | awk -v ip=$_ip \
687 'index($1, "tcp") == 1 && \
688 (index($4, ip ":") == 1 || index($4, "::ffff:" ip ":") == 1) \
689 && $6 == "ESTABLISHED" \
693 ##################################################################
694 # use statd-callout to update NFS lock info
695 ##################################################################
696 nfs_update_lock_info ()
698 if [ -x "$CTDB_BASE/statd-callout" ] ; then
699 "$CTDB_BASE/statd-callout" update
703 ########################################################
704 # start/stop the Ganesha nfs service
705 ########################################################
708 _service_name="nfs-ganesha-$CTDB_CLUSTER_FILESYSTEM_TYPE"
711 service "$_service_name" start
714 service "$_service_name" stop
717 service "$_service_name" stop
718 nfs_dump_some_threads "rpc.statd"
719 service "$_service_name" start
724 ########################################################
725 # start/stop the nfs service on different platforms
726 ########################################################
729 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
732 [ -x $CTDB_ETCDIR/init.d/nfslock -o \
733 -r /usr/lib/systemd/system/nfs-lock.service ] && {
741 service nfsserver start
744 service nfsserver stop > /dev/null 2>&1
747 set_proc "fs/nfsd/threads" 0
748 service nfsserver stop > /dev/null 2>&1
750 nfs_dump_some_threads
751 service nfsserver start
758 service nfslock start
766 set_proc "fs/nfsd/threads" 0
767 service nfs stop > /dev/null 2>&1
768 service nfslock stop > /dev/null 2>&1
770 nfs_dump_some_threads
771 service nfslock start
777 echo "Unknown platform. NFS is not supported with ctdb"
783 # Dump up to the configured number of nfsd thread backtraces.
784 nfs_dump_some_threads ()
788 _num="${CTDB_NFS_DUMP_STUCK_THREADS:-5}"
789 [ $_num -gt 0 ] || return 0
791 program_stack_traces "$_prog" $_num
794 ########################################################
795 # start/stop the nfs lockmanager service on different platforms
796 ########################################################
797 startstop_nfslock() {
799 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
802 [ -x $CTDB_ETCDIR/init.d/nfslock -o \
803 -r /usr/lib/systemd/system/nfs-lock.service ] && {
809 # for sles there is no service for lockmanager
810 # so we instead just shutdown/restart nfs
813 service nfsserver start
816 service nfsserver stop > /dev/null 2>&1
819 service nfsserver stop > /dev/null 2>&1
820 service nfsserver start
827 service nfslock start
830 service nfslock stop > /dev/null 2>&1
833 service nfslock stop > /dev/null 2>&1
834 service nfslock start
839 echo "Unknown platform. NFS locking is not supported with ctdb"
845 # Periodically update the statd database
850 _statd_update_trigger="$service_state_dir/update-trigger"
851 [ -f "$_statd_update_trigger" ] || touch "$_statd_update_trigger"
853 _last_update=$(stat --printf="%Y" "$_statd_update_trigger")
854 _current_time=$(date +"%s")
855 if [ $(( $_current_time - $_last_update)) -ge $_update_period ] ; then
856 touch "$_statd_update_trigger"
857 $CTDB_BASE/statd-callout updatelocal &
858 $CTDB_BASE/statd-callout updateremote &
862 ########################################################
870 # Ensure interface is up
871 ip link set "$_iface" up || \
872 die "Failed to bringup interface $_iface"
874 # Only need to define broadcast for IPv4
880 ip addr add "$_ip/$_maskbits" $_bcast dev "$_iface" || {
881 echo "Failed to add $_ip/$_maskbits on dev $_iface"
885 # Wait 5 seconds for IPv6 addresses to stop being tentative...
886 if [ -z "$_bcast" ] ; then
887 for _x in $(seq 1 10) ; do
888 ip addr show to "${_ip}/128" | grep -q "tentative" || break
892 # If the address was a duplicate then it won't be on the
893 # interface so flag an error.
894 _t=$(ip addr show to "${_ip}/128")
897 echo "Failed to add $_ip/$_maskbits on dev $_iface"
900 *tentative*|*dadfailed*)
901 echo "Failed to add $_ip/$_maskbits on dev $_iface"
902 ip addr del "$_ip/$_maskbits" dev "$_iface"
909 delete_ip_from_iface()
915 # This could be set globally for all interfaces but it is probably
916 # better to avoid surprises, so limit it the interfaces where CTDB
917 # has public IP addresses. There isn't anywhere else convenient
918 # to do this so just set it each time. This is much cheaper than
919 # remembering and re-adding secondaries.
920 set_proc "sys/net/ipv4/conf/${_iface}/promote_secondaries" 1
922 ip addr del "$_ip/$_maskbits" dev "$_iface" || {
923 echo "Failed to del $_ip on dev $_iface"
928 # If the given IP is hosted then print 2 items: maskbits and iface
934 *:*) _family="inet6" ; _bits=128 ;;
935 *) _family="inet" ; _bits=32 ;;
938 ip addr show to "${_addr}/${_bits}" 2>/dev/null | \
939 awk -v family="${_family}" \
940 'NR == 1 { iface = $2; sub(":$", "", iface) } \
941 $1 ~ /inet/ { mask = $2; sub(".*/", "", mask); \
942 print mask, iface, family }'
947 _addr="${1%/*}" # Remove optional maskbits
949 set -- $(ip_maskbits_iface $_addr)
950 if [ -n "$1" ] ; then
953 echo "Removing public address $_addr/$_maskbits from device $_iface"
954 delete_ip_from_iface $_iface $_addr $_maskbits >/dev/null 2>&1
958 drop_all_public_ips ()
960 while read _ip _x ; do
962 done <"${CTDB_PUBLIC_ADDRESSES:-/dev/null}"
967 set_proc sys/net/ipv4/route/flush 1
968 set_proc sys/net/ipv6/route/flush 1
971 ########################################################
973 _ctdb_counter_common () {
974 _service_name="${1:-${service_name:-${script_name}}}"
975 _counter_file="$ctdb_fail_dir/$_service_name"
976 mkdir -p "${_counter_file%/*}" # dirname
978 ctdb_counter_init () {
979 _ctdb_counter_common "$1"
983 ctdb_counter_incr () {
984 _ctdb_counter_common "$1"
987 echo -n 1 >> "$_counter_file"
989 ctdb_check_counter () {
990 _msg="${1:-error}" # "error" - anything else is silent on fail
991 _op="${2:--ge}" # an integer operator supported by test
992 _limit="${3:-${service_fail_limit}}"
994 _ctdb_counter_common "$1"
997 _size=$(stat -c "%s" "$_counter_file" 2>/dev/null || echo 0)
999 if [ "$_op" != "%" ] ; then
1000 if [ $_size $_op $_limit ] ; then
1004 if [ $(($_size $_op $_limit)) -eq 0 ] ; then
1009 if [ "$_msg" = "error" ] ; then
1010 echo "ERROR: $_size consecutive failures for $_service_name, marking node unhealthy"
1018 ########################################################
1020 ctdb_status_dir="$CTDB_VARDIR/state/service_status"
1021 ctdb_fail_dir="$CTDB_VARDIR/state/failcount"
1023 ctdb_setup_service_state_dir ()
1025 service_state_dir="$CTDB_VARDIR/state/service_state/${1:-${service_name}}"
1026 mkdir -p "$service_state_dir" || {
1027 echo "Error creating state dir \"$service_state_dir\""
1032 ########################################################
1033 # Managed status history, for auto-start/stop
1035 ctdb_managed_dir="$CTDB_VARDIR/state/managed_history"
1037 _ctdb_managed_common ()
1039 _ctdb_managed_file="$ctdb_managed_dir/$service_name"
1042 ctdb_service_managed ()
1044 _ctdb_managed_common
1045 mkdir -p "$ctdb_managed_dir"
1046 touch "$_ctdb_managed_file"
1049 ctdb_service_unmanaged ()
1051 _ctdb_managed_common
1052 rm -f "$_ctdb_managed_file"
1055 is_ctdb_previously_managed_service ()
1057 _ctdb_managed_common
1058 [ -f "$_ctdb_managed_file" ]
1061 ########################################################
1062 # Check and set status
1066 echo "node is \"$1\", \"${script_name}\" reports problem: $(cat $2)"
1071 if [ -r "$ctdb_status_dir/$script_name/unhealthy" ] ; then
1072 log_status_cat "unhealthy" "$ctdb_status_dir/$script_name/unhealthy"
1074 elif [ -r "$ctdb_status_dir/$script_name/banned" ] ; then
1075 log_status_cat "banned" "$ctdb_status_dir/$script_name/banned"
1084 d="$ctdb_status_dir/$script_name"
1091 for i in "banned" "unhealthy" ; do
1098 ##################################################################
1099 # Reconfigure a service on demand
1101 _ctdb_service_reconfigure_common ()
1103 _d="$ctdb_status_dir/${service_name}"
1105 _ctdb_service_reconfigure_flag="$_d/reconfigure"
1108 ctdb_service_needs_reconfigure ()
1110 _ctdb_service_reconfigure_common
1111 [ -e "$_ctdb_service_reconfigure_flag" ]
1114 ctdb_service_set_reconfigure ()
1116 _ctdb_service_reconfigure_common
1117 >"$_ctdb_service_reconfigure_flag"
1120 ctdb_service_unset_reconfigure ()
1122 _ctdb_service_reconfigure_common
1123 rm -f "$_ctdb_service_reconfigure_flag"
1126 ctdb_service_reconfigure ()
1128 echo "Reconfiguring service \"${service_name}\"..."
1129 ctdb_service_unset_reconfigure
1130 service_reconfigure || return $?
1134 # Default service_reconfigure() function does nothing.
1135 service_reconfigure ()
1140 ctdb_reconfigure_take_lock ()
1142 _ctdb_service_reconfigure_common
1143 _lock="${_d}/reconfigure_lock"
1144 mkdir -p "${_lock%/*}" # dirname
1149 # This is overkill but will work if we need to extend this to
1150 # allow certain events to run multiple times in parallel
1151 # (e.g. takeip) and write multiple PIDs to the file.
1153 if [ -n "$_locker_event" ] ; then
1154 while read _pid ; do
1155 if [ -n "$_pid" -a "$_pid" != $$ ] && \
1156 kill -0 "$_pid" 2>/dev/null ; then
1162 printf "%s\n%s\n" "$event_name" $$ >"$_lock"
1167 ctdb_reconfigure_release_lock ()
1169 _ctdb_service_reconfigure_common
1170 _lock="${_d}/reconfigure_lock"
1175 ctdb_replay_monitor_status ()
1177 echo "Replaying previous status for this script due to reconfigure..."
1178 # Leading separator ('|') is missing in some versions...
1179 _out=$(ctdb scriptstatus -X | grep -E "^\|?monitor\|${script_name}\|")
1180 # Output looks like this:
1181 # |monitor|60.nfs|1|ERROR|1314764004.030861|1314764004.035514|foo bar|
1182 # This is the cheapest way of getting fields in the middle.
1183 set -- $(IFS="|" ; echo $_out)
1186 # The error output field can include colons so we'll try to
1187 # preserve them. The weak checking at the beginning tries to make
1188 # this work for both broken (no leading '|') and fixed output.
1190 _err_out="${_out#*monitor|${script_name}|*|*|*|*|}"
1192 OK) : ;; # Do nothing special.
1194 # Recast this as an error, since we can't exit with the
1195 # correct negative number.
1197 _err_out="[Replay of TIMEDOUT scriptstatus - note incorrect return code.] ${_err_out}"
1200 # Recast this as an OK, since we can't exit with the
1201 # correct negative number.
1203 _err_out="[Replay of DISABLED scriptstatus - note incorrect return code.] ${_err_out}"
1205 *) : ;; # Must be ERROR, do nothing special.
1207 if [ -n "$_err_out" ] ; then
1213 ctdb_service_check_reconfigure ()
1217 # We only care about some events in this function. For others we
1219 case "$event_name" in
1220 monitor|ipreallocated|reconfigure) : ;;
1224 if ctdb_reconfigure_take_lock ; then
1225 # No events covered by this function are running, so proceed
1227 case "$event_name" in
1229 (ctdb_service_reconfigure)
1233 if ctdb_service_needs_reconfigure ; then
1234 ctdb_service_reconfigure
1239 ctdb_reconfigure_release_lock
1241 # Somebody else is running an event we don't want to collide
1242 # with. We proceed with caution.
1243 case "$event_name" in
1245 # Tell whoever called us to retry.
1249 # Defer any scheduled reconfigure and just run the
1250 # rest of the ipreallocated event, as per the
1251 # eventscript. There's an assumption here that the
1252 # event doesn't depend on any scheduled reconfigure.
1253 # This is true in the current code.
1257 # There is most likely a reconfigure in progress so
1258 # the service is possibly unstable. As above, we
1259 # defer any scheduled reconfigured. We also replay
1260 # the previous monitor status since that's the best
1261 # information we have.
1262 ctdb_replay_monitor_status
1268 ##################################################################
1269 # Does CTDB manage this service? - and associated auto-start/stop
1271 ctdb_compat_managed_service ()
1273 if [ "$1" = "yes" -a "$2" = "$service_name" ] ; then
1274 CTDB_MANAGED_SERVICES="$CTDB_MANAGED_SERVICES $2"
1278 is_ctdb_managed_service ()
1282 # $t is used just for readability and to allow better accurate
1283 # matching via leading/trailing spaces
1284 t=" $CTDB_MANAGED_SERVICES "
1286 # Return 0 if "<space>$service_name<space>" appears in $t
1287 if [ "${t#* ${service_name} }" != "${t}" ] ; then
1291 # If above didn't match then update $CTDB_MANAGED_SERVICES for
1292 # backward compatibility and try again.
1293 ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD" "vsftpd"
1294 ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA" "samba"
1295 ctdb_compat_managed_service "$CTDB_MANAGES_WINBIND" "winbind"
1296 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "apache2"
1297 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "httpd"
1298 ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI" "iscsi"
1299 ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD" "clamd"
1300 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs"
1301 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs-ganesha-gpfs"
1303 t=" $CTDB_MANAGED_SERVICES "
1305 # Return 0 if "<space>$service_name<space>" appears in $t
1306 [ "${t#* ${service_name} }" != "${t}" ]
1309 ctdb_start_stop_service ()
1313 # Allow service-start/service-stop pseudo-events to start/stop
1314 # services when we're not auto-starting/stopping and we're not
1316 case "$event_name" in
1318 if is_ctdb_managed_service ; then
1319 die 'service-start event not permitted when service is managed'
1321 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1322 die 'service-start event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1328 if is_ctdb_managed_service ; then
1329 die 'service-stop event not permitted when service is managed'
1331 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1332 die 'service-stop event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1339 # Do nothing unless configured to...
1340 [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] || return 0
1342 [ "$event_name" = "monitor" ] || return 0
1344 if is_ctdb_managed_service ; then
1345 if ! is_ctdb_previously_managed_service ; then
1346 echo "Starting service \"$service_name\" - now managed"
1347 background_with_logging ctdb_service_start
1351 if is_ctdb_previously_managed_service ; then
1352 echo "Stopping service \"$service_name\" - no longer managed"
1353 background_with_logging ctdb_service_stop
1359 ctdb_service_start ()
1361 # The service is marked managed if we've ever tried to start it.
1362 ctdb_service_managed
1364 service_start || return $?
1370 ctdb_service_stop ()
1372 ctdb_service_unmanaged
1376 # Default service_start() and service_stop() functions.
1378 # These may be overridden in an eventscript.
1381 service "$service_name" start
1386 service "$service_name" stop
1389 ##################################################################
1391 ctdb_standard_event_handler ()
1408 _family="$1" ; shift
1409 if [ "$_family" = "inet6" ] ; then
1410 _iptables_cmd="ip6tables"
1412 _iptables_cmd="iptables"
1415 # iptables doesn't like being re-entered, so flock-wrap it.
1416 flock -w 30 "${CTDB_VARDIR}/iptables-ctdb.flock" "$_iptables_cmd" "$@"
1419 # AIX (and perhaps others?) doesn't have mktemp
1420 if ! which mktemp >/dev/null 2>&1 ; then
1424 if [ "$1" = "-d" ] ; then
1428 _d="${TMPDIR:-/tmp}"
1429 _hex10=$(dd if=/dev/urandom count=20 2>/dev/null | \
1431 sed -e 's@\(..........\).*@\1@')
1432 _t="${_d}/tmp.${_hex10}"
1445 ########################################################
1447 ########################################################
1453 tickledir="$CTDB_VARDIR/state/tickles"
1454 mkdir -p "$tickledir"
1457 _pnn=$(ctdb pnn) ; _pnn=${_pnn#PNN:}
1459 # What public IPs do I hold?
1460 _ips=$(ctdb -X ip | awk -F'|' -v pnn=$_pnn '$3 == pnn {print $2}')
1462 # IPs as a regexp choice
1463 _ipschoice="($(echo $_ips | sed -e 's/ /|/g' -e 's/\./\\\\./g'))"
1465 # Record connections to our public IPs in a temporary file
1466 _my_connections="${tickledir}/${_port}.connections"
1467 rm -f "$_my_connections"
1469 awk -v destpat="^${_ipschoice}:${_port}\$" \
1470 '$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ destpat {print $5, $4}' |
1471 sort >"$_my_connections"
1473 # Record our current tickles in a temporary file
1474 _my_tickles="${tickledir}/${_port}.tickles"
1475 rm -f "$_my_tickles"
1476 for _i in $_ips ; do
1477 ctdb -X gettickles $_i $_port |
1478 awk -F'|' 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
1480 sort >"$_my_tickles"
1482 # Add tickles for connections that we haven't already got tickles for
1483 comm -23 "$_my_connections" "$_my_tickles" |
1484 while read _src _dst ; do
1485 ctdb addtickle $_src $_dst
1488 # Remove tickles for connections that are no longer there
1489 comm -13 "$_my_connections" "$_my_tickles" |
1490 while read _src _dst ; do
1491 ctdb deltickle $_src $_dst
1494 rm -f "$_my_connections" "$_my_tickles"
1497 ########################################################
1498 # load a site local config file
1499 ########################################################
1501 [ -n "$CTDB_RC_LOCAL" -a -x "$CTDB_RC_LOCAL" ] && {
1505 [ -x $CTDB_BASE/rc.local ] && {
1506 . $CTDB_BASE/rc.local
1509 [ -d $CTDB_BASE/rc.local.d ] && {
1510 for i in $CTDB_BASE/rc.local.d/* ; do
1511 [ -x "$i" ] && . "$i"
1515 script_name="${0##*/}" # basename
1516 service_fail_limit=1