1 # Hey Emacs, this is a -*- shell-script -*- !!!
3 # utility functions for ctdb event scripts
5 if [ -z "$CTDB_BASE" ] ; then
6 echo 'CTDB_BASE unset in CTDB functions file'
10 CTDB_VARDIR="/usr/local/var/lib/ctdb"
11 ctdb_rundir="/usr/local/var/run/ctdb"
13 # Only (and always) override these variables in test code
15 if [ -z "$CTDB_SCRIPT_VARDIR" ] ; then
16 CTDB_SCRIPT_VARDIR="/usr/local/var/lib/ctdb/state"
19 if [ -z "$CTDB_SYS_ETCDIR" ] ; then
20 CTDB_SYS_ETCDIR="/etc"
23 if [ -z "$CTDB_HELPER_BINDIR" ] ; then
24 CTDB_HELPER_BINDIR="/usr/local/libexec/ctdb"
27 #######################################
28 # pull in a system config file, if any
30 rewrite_ctdb_options ()
34 _opts_defaults="mode=700"
35 # Get any extra options specified after colon
36 if [ "$CTDB_DBDIR" = "tmpfs" ] ; then
39 _opts="${CTDB_DBDIR#tmpfs:}"
41 # This is an internal variable, only used by ctdbd_wrapper.
42 # It is OK to repeat mount options - last value wins
43 CTDB_DBDIR_TMPFS_OPTIONS="${_opts_defaults}${_opts:+,}${_opts}"
45 CTDB_DBDIR="${ctdb_rundir}/CTDB_DBDIR"
48 CTDB_DBDIR_TMPFS_OPTIONS=""
55 foo="${service_config:-${service_name}}"
56 if [ -n "$foo" ] ; then
62 if [ "$1" != "ctdb" ] ; then
70 if [ -f $CTDB_SYS_ETCDIR/sysconfig/$1 ]; then
71 . $CTDB_SYS_ETCDIR/sysconfig/$1
72 elif [ -f $CTDB_SYS_ETCDIR/default/$1 ]; then
73 . $CTDB_SYS_ETCDIR/default/$1
74 elif [ -f $CTDB_BASE/sysconfig/$1 ]; then
75 . $CTDB_BASE/sysconfig/$1
78 if [ "$1" = "ctdb" ] ; then
79 _config="${CTDBD_CONF:-${CTDB_BASE}/ctdbd.conf}"
80 if [ -r "$_config" ] ; then
91 ##############################################################
93 # CTDB_SCRIPT_DEBUGLEVEL can be overwritten by setting it in a
97 if [ ${CTDB_SCRIPT_DEBUGLEVEL:-2} -ge 4 ] ; then
98 # If there are arguments then echo them. Otherwise expect to
99 # use stdin, which allows us to pass lots of debug using a
101 if [ -n "$1" ] ; then
104 sed -e 's@^@DEBUG: @'
107 if [ -z "$1" ] ; then
122 # Log given message or stdin to either syslog or a CTDB log file
123 # $1 is the tag passed to logger if syslog is in use.
128 case "$CTDB_LOGGING" in
130 if [ -n "$CTDB_LOGGING" ] ; then
131 _file="${CTDB_LOGGING#file:}"
133 _file="/usr/local/var/log/log.ctdb"
136 if [ -n "$*" ] ; then
144 # Handle all syslog:* variants here too. There's no tool to do
145 # the lossy things, so just use logger.
146 logger -t "ctdbd: ${_tag}" $*
151 # When things are run in the background in an eventscript then logging
152 # output might get lost. This is the "solution". :-)
153 background_with_logging ()
156 "$@" 2>&1 </dev/null |
157 script_log "${script_name}&"
163 ##############################################################
164 # check number of args for different events
170 echo "ERROR: must supply interface, IP and maskbits"
176 echo "ERROR: must supply old interface, new interface, IP and maskbits"
183 ##############################################################
184 # determine on what type of system (init style) we are running
187 # only do detection if not already set:
188 [ -z "$CTDB_INIT_STYLE" ] || return
190 if [ -x /sbin/startproc ]; then
191 CTDB_INIT_STYLE="suse"
192 elif [ -x /sbin/start-stop-daemon ]; then
193 CTDB_INIT_STYLE="debian"
195 CTDB_INIT_STYLE="redhat"
199 ######################################################
200 # simulate /sbin/service on platforms that don't have it
201 # _service() makes it easier to hook the service() function for
208 # do nothing, when no service was specified
209 [ -z "$_service_name" ] && return
211 if [ -x /sbin/service ]; then
212 $_nice /sbin/service "$_service_name" "$_op"
213 elif [ -x /usr/sbin/service ]; then
214 $_nice /usr/sbin/service "$_service_name" "$_op"
215 elif [ -x $CTDB_SYS_ETCDIR/init.d/$_service_name ]; then
216 $_nice $CTDB_SYS_ETCDIR/init.d/$_service_name "$_op"
217 elif [ -x $CTDB_SYS_ETCDIR/rc.d/init.d/$_service_name ]; then
218 $_nice $CTDB_SYS_ETCDIR/rc.d/init.d/$_service_name "$_op"
228 ######################################################
229 # simulate /sbin/service (niced) on platforms that don't have it
236 ######################################################
237 # Cached retrieval of PNN from local node. This never changes so why
238 # open a client connection to the server each time this is needed?
239 # This sets $pnn - this avoid an unnecessary subprocess.
242 _pnn_file="${CTDB_SCRIPT_VARDIR}/my-pnn"
243 if [ ! -f "$_pnn_file" ] ; then
244 ctdb pnn | sed -e 's@.*:@@' >"$_pnn_file"
247 read pnn <"$_pnn_file"
250 ######################################################
251 # wrapper around /proc/ settings to allow them to be hooked
253 # 1st arg is relative path under /proc/, 2nd arg is value to set
256 echo "$2" >"/proc/$1"
261 if [ -w "/proc/$1" ] ; then
266 ######################################################
267 # wrapper around getting file contents from /proc/ to allow
268 # this to be hooked for testing
269 # 1st arg is relative path under /proc/
275 ######################################################
276 # Print up to $_max kernel stack traces for processes named $_program
277 program_stack_traces ()
283 for _pid in $(pidof "$_prog") ; do
284 [ $_count -le $_max ] || break
286 # Do this first to avoid racing with process exit
287 _stack=$(get_proc "${_pid}/stack" 2>/dev/null)
288 if [ -n "$_stack" ] ; then
289 echo "Stack trace for ${_prog}[${_pid}]:"
291 _count=$(($_count + 1))
296 ######################################################
297 # Ensure $service_name is set
298 assert_service_name ()
300 [ -n "$service_name" ] || die "INTERNAL ERROR: \$service_name not set"
303 ######################################################
304 # check a set of directories is available
305 # return 1 on a missing directory
306 # directories are read from stdin
307 ######################################################
308 ctdb_check_directories_probe()
310 while IFS="" read d ; do
316 [ -d "${d}/." ] || return 1
321 ######################################################
322 # check a set of directories is available
323 # directories are read from stdin
324 ######################################################
325 ctdb_check_directories()
327 ctdb_check_directories_probe || {
328 echo "ERROR: $service_name directory \"$d\" not available"
333 ######################################################
334 # check a set of tcp ports
335 # usage: ctdb_check_tcp_ports <ports...>
336 ######################################################
338 # This flag file is created when a service is initially started. It
339 # is deleted the first time TCP port checks for that service succeed.
340 # Until then ctdb_check_tcp_ports() prints a more subtle "error"
341 # message if a port check fails.
342 _ctdb_check_tcp_common ()
345 _d="${CTDB_SCRIPT_VARDIR}/failcount"
346 _ctdb_service_started_file="${_d}/${service_name}.started"
349 ctdb_check_tcp_init ()
351 _ctdb_check_tcp_common
352 mkdir -p "${_ctdb_service_started_file%/*}" # dirname
353 touch "$_ctdb_service_started_file"
356 # Check whether something is listening on all of the given TCP ports
357 # using the "ctdb checktcpport" command.
358 ctdb_check_tcp_ports()
360 if [ -z "$1" ] ; then
361 echo "INTERNAL ERROR: ctdb_check_tcp_ports - no ports specified"
365 for _p ; do # process each function argument (port)
366 _cmd="ctdb checktcpport $_p"
371 _ctdb_check_tcp_common
372 if [ ! -f "$_ctdb_service_started_file" ] ; then
373 echo "ERROR: $service_name tcp port $_p is not responding"
374 debug "\"ctdb checktcpport $_p\" was able to bind to port"
376 echo "INFO: $service_name tcp port $_p is not responding"
382 # Couldn't bind, something already listening, next port...
386 echo "ERROR: unexpected error running \"ctdb checktcpport\""
388 ctdb checktcpport (exited with $_ret) with output:
395 # All ports listening
396 _ctdb_check_tcp_common
397 rm -f "$_ctdb_service_started_file"
401 ######################################################
402 # check a unix socket
403 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
404 ######################################################
405 ctdb_check_unix_socket() {
407 [ -z "$socket_path" ] && return
409 if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
410 echo "ERROR: $service_name socket $socket_path not found"
415 ######################################################
416 # check a command returns zero status
417 # usage: ctdb_check_command <command>
418 ######################################################
419 ctdb_check_command ()
421 _out=$("$@" 2>&1) || {
422 echo "ERROR: $* returned error"
428 ################################################
429 # kill off any TCP connections with the given IP
430 ################################################
431 kill_tcp_connections ()
437 if [ "$3" = "oneway" ] ; then
441 get_tcp_connections_for_ip "$_ip" | {
446 while read _dst _src; do
447 _destport="${_dst##*:}"
450 # we only do one-way killtcp for CIFS
451 139|445) __oneway=true ;;
454 echo "Killing TCP connection $_src $_dst"
455 _connections="${_connections}${_nl}${_src} ${_dst}"
456 if ! $__oneway ; then
457 _connections="${_connections}${_nl}${_dst} ${_src}"
460 _killcount=$(($_killcount + 1))
463 if [ $_killcount -eq 0 ] ; then
467 echo "$_connections" | \
468 "${CTDB_HELPER_BINDIR}/ctdb_killtcp" "$_iface" || {
469 echo "Failed to kill TCP connections"
473 _remaining=$(get_tcp_connections_for_ip $_ip | wc -l)
475 if [ $_remaining -eq 0 ] ; then
476 echo "Killed $_killcount TCP connections to released IP $_ip"
480 _t="${_remaining}/${_killcount}"
481 echo "Failed to kill TCP connections for IP $_ip (${_t} remaining)"
485 ##################################################################
486 # kill off the local end for any TCP connections with the given IP
487 ##################################################################
488 kill_tcp_connections_local_only ()
490 kill_tcp_connections "$@" "oneway"
493 ##################################################################
494 # tickle any TCP connections with the given IP
495 ##################################################################
496 tickle_tcp_connections ()
500 get_tcp_connections_for_ip "$_ip" |
504 while read dest src; do
505 echo "Tickle TCP connection $src $dest"
506 ctdb tickle $src $dest >/dev/null 2>&1 || _failed=true
507 echo "Tickle TCP connection $dest $src"
508 ctdb tickle $dest $src >/dev/null 2>&1 || _failed=true
512 echo "Failed to send tickle control"
517 get_tcp_connections_for_ip ()
521 netstat -tn | awk -v ip=$_ip \
522 'index($1, "tcp") == 1 && \
523 (index($4, ip ":") == 1 || index($4, "::ffff:" ip ":") == 1) \
524 && $6 == "ESTABLISHED" \
528 ########################################################
536 # Ensure interface is up
537 ip link set "$_iface" up || \
538 die "Failed to bringup interface $_iface"
540 # Only need to define broadcast for IPv4
546 ip addr add "$_ip/$_maskbits" $_bcast dev "$_iface" || {
547 echo "Failed to add $_ip/$_maskbits on dev $_iface"
551 # Wait 5 seconds for IPv6 addresses to stop being tentative...
552 if [ -z "$_bcast" ] ; then
553 for _x in $(seq 1 10) ; do
554 ip addr show to "${_ip}/128" | grep -q "tentative" || break
558 # If the address was a duplicate then it won't be on the
559 # interface so flag an error.
560 _t=$(ip addr show to "${_ip}/128")
563 echo "Failed to add $_ip/$_maskbits on dev $_iface"
566 *tentative*|*dadfailed*)
567 echo "Failed to add $_ip/$_maskbits on dev $_iface"
568 ip addr del "$_ip/$_maskbits" dev "$_iface"
575 delete_ip_from_iface()
581 # This could be set globally for all interfaces but it is probably
582 # better to avoid surprises, so limit it the interfaces where CTDB
583 # has public IP addresses. There isn't anywhere else convenient
584 # to do this so just set it each time. This is much cheaper than
585 # remembering and re-adding secondaries.
586 set_proc "sys/net/ipv4/conf/${_iface}/promote_secondaries" 1
588 ip addr del "$_ip/$_maskbits" dev "$_iface" || {
589 echo "Failed to del $_ip on dev $_iface"
594 # If the given IP is hosted then print 2 items: maskbits and iface
600 *:*) _family="inet6" ; _bits=128 ;;
601 *) _family="inet" ; _bits=32 ;;
604 ip addr show to "${_addr}/${_bits}" 2>/dev/null | \
605 awk -v family="${_family}" \
606 'NR == 1 { iface = $2; sub(":$", "", iface) ; \
607 sub("@.*", "", iface) } \
608 $1 ~ /inet/ { mask = $2; sub(".*/", "", mask); \
609 print mask, iface, family }'
614 _addr="${1%/*}" # Remove optional maskbits
616 set -- $(ip_maskbits_iface $_addr)
617 if [ -n "$1" ] ; then
620 echo "Removing public address $_addr/$_maskbits from device $_iface"
621 delete_ip_from_iface $_iface $_addr $_maskbits >/dev/null 2>&1
625 drop_all_public_ips ()
627 while read _ip _x ; do
629 done <"${CTDB_PUBLIC_ADDRESSES:-/dev/null}"
634 set_proc_maybe sys/net/ipv4/route/flush 1
635 set_proc_maybe sys/net/ipv6/route/flush 1
638 ########################################################
639 # Interface monitoring
641 # If the interface is a virtual one (e.g. VLAN) then get the
642 # underlying interface
643 interface_get_real ()
645 # Output of "ip link show <iface>"
648 # Extract the full interface description to see if it is a VLAN
649 _t=$(echo "$_iface_info" |
650 awk 'NR == 1 { iface = $2; sub(":$", "", iface) ; \
654 # VLAN: use the underlying interface, after the '@'
658 # Not a regular VLAN. For backward compatibility, assume
659 # there is some other sort of VLAN that doesn't have the
660 # '@' in the output and only use what is before a '.'. If
661 # there is no '.' then this will be the whole interface
667 # Check whether an interface is operational
672 _iface_info=$(ip link show "$_iface" 2>&1) || {
673 echo "ERROR: Monitored interface ${_iface} does not exist"
678 # If the interface is a virtual one (e.g. VLAN) then get the
679 # underlying interface.
680 _realiface=$(interface_get_real "$_iface_info")
682 if _bi=$(get_proc "net/bonding/${_realiface}" 2>/dev/null) ; then
683 # This is a bond: various monitoring strategies
684 echo "$_bi" | grep -q 'Currently Active Slave: None' && {
685 echo "ERROR: No active slaves for bond device ${_realiface}"
688 echo "$_bi" | grep -q '^MII Status: up' || {
689 echo "ERROR: public network interface ${_realiface} is down"
692 echo "$_bi" | grep -q '^Bonding Mode: IEEE 802.3ad Dynamic link aggregation' && {
693 # This works around a bug in the driver where the
694 # overall bond status can be up but none of the actual
695 # physical interfaces have a link.
696 echo "$_bi" | grep 'MII Status:' | tail -n +2 | grep -q '^MII Status: up' || {
697 echo "ERROR: No active slaves for 802.ad bond device ${_realiface}"
707 # loopback is always working
711 # we don't know how to test ib links
715 ethtool "$_iface" | grep -q 'Link detected: yes' || {
716 # On some systems, this is not successful when a
717 # cable is plugged but the interface has not been
718 # brought up previously. Bring the interface up
720 ip link set "$_iface" up
721 ethtool "$_iface" | grep -q 'Link detected: yes' || {
722 echo "ERROR: No link on the public network interface ${_iface}"
732 ########################################################
734 _ctdb_counter_common () {
735 _service_name="${1:-${service_name:-${script_name}}}"
736 _counter_file="${CTDB_SCRIPT_VARDIR}/failcount/${_service_name}"
737 mkdir -p "${_counter_file%/*}" # dirname
739 ctdb_counter_init () {
740 _ctdb_counter_common "$1"
744 ctdb_counter_incr () {
745 _ctdb_counter_common "$1"
748 echo -n 1 >> "$_counter_file"
750 ctdb_counter_get () {
751 _ctdb_counter_common "$1"
753 stat -c "%s" "$_counter_file" 2>/dev/null || echo 0
755 ctdb_check_counter () {
756 _msg="${1:-error}" # "error" - anything else is silent on fail
757 _op="${2:--ge}" # an integer operator supported by test
758 _limit="${3:-${service_fail_limit}}"
761 _size=$(ctdb_counter_get "$1")
764 if [ "$_op" != "%" ] ; then
765 if [ $_size $_op $_limit ] ; then
769 if [ $(($_size $_op $_limit)) -eq 0 ] ; then
774 if [ "$_msg" = "error" ] ; then
775 echo "ERROR: $_size consecutive failures for $_service_name, marking node unhealthy"
783 ########################################################
785 ctdb_setup_service_state_dir ()
787 service_state_dir="${CTDB_SCRIPT_VARDIR}/service_state/${1:-${service_name}}"
788 mkdir -p "$service_state_dir" || {
789 echo "Error creating state dir \"$service_state_dir\""
794 ########################################################
795 # Managed status history, for auto-start/stop
797 _ctdb_managed_common ()
799 _ctdb_managed_file="${CTDB_SCRIPT_VARDIR}/managed_history/${service_name}"
802 ctdb_service_managed ()
805 mkdir -p "${_ctdb_managed_file%/*}" # dirname
806 touch "$_ctdb_managed_file"
809 ctdb_service_unmanaged ()
812 rm -f "$_ctdb_managed_file"
815 is_ctdb_previously_managed_service ()
818 [ -f "$_ctdb_managed_file" ]
821 ##################################################################
822 # Reconfigure a service on demand
824 _ctdb_service_reconfigure_common ()
826 _d="${CTDB_SCRIPT_VARDIR}/service_status/${service_name}"
828 _ctdb_service_reconfigure_flag="$_d/reconfigure"
831 ctdb_service_needs_reconfigure ()
833 _ctdb_service_reconfigure_common
834 [ -e "$_ctdb_service_reconfigure_flag" ]
837 ctdb_service_set_reconfigure ()
839 _ctdb_service_reconfigure_common
840 >"$_ctdb_service_reconfigure_flag"
843 ctdb_service_unset_reconfigure ()
845 _ctdb_service_reconfigure_common
846 rm -f "$_ctdb_service_reconfigure_flag"
849 ctdb_service_reconfigure ()
851 echo "Reconfiguring service \"${service_name}\"..."
852 ctdb_service_unset_reconfigure
853 service_reconfigure || return $?
857 # Default service_reconfigure() function does nothing.
858 service_reconfigure ()
863 ctdb_reconfigure_take_lock ()
865 _ctdb_service_reconfigure_common
866 _lock="${_d}/reconfigure_lock"
867 mkdir -p "${_lock%/*}" # dirname
872 # This is overkill but will work if we need to extend this to
873 # allow certain events to run multiple times in parallel
874 # (e.g. takeip) and write multiple PIDs to the file.
876 if [ -n "$_locker_event" ] ; then
878 if [ -n "$_pid" -a "$_pid" != $$ ] && \
879 kill -0 "$_pid" 2>/dev/null ; then
885 printf "%s\n%s\n" "$event_name" $$ >"$_lock"
890 ctdb_reconfigure_release_lock ()
892 _ctdb_service_reconfigure_common
893 _lock="${_d}/reconfigure_lock"
898 ctdb_replay_monitor_status ()
900 echo "Replaying previous status for this script due to reconfigure..."
901 # Leading separator ('|') is missing in some versions...
902 _out=$(ctdb scriptstatus -X | grep -E "^\|?monitor\|${script_name}\|")
903 # Output looks like this:
904 # |monitor|60.nfs|1|ERROR|1314764004.030861|1314764004.035514|foo bar|
905 # This is the cheapest way of getting fields in the middle.
906 set -- $(IFS="|" ; echo $_out)
909 # The error output field can include colons so we'll try to
910 # preserve them. The weak checking at the beginning tries to make
911 # this work for both broken (no leading '|') and fixed output.
913 _err_out="${_out#*monitor|${script_name}|*|*|*|*|}"
915 OK) : ;; # Do nothing special.
917 # Recast this as an error, since we can't exit with the
918 # correct negative number.
920 _err_out="[Replay of TIMEDOUT scriptstatus - note incorrect return code.] ${_err_out}"
923 # Recast this as an OK, since we can't exit with the
924 # correct negative number.
926 _err_out="[Replay of DISABLED scriptstatus - note incorrect return code.] ${_err_out}"
928 *) : ;; # Must be ERROR, do nothing special.
930 if [ -n "$_err_out" ] ; then
936 ctdb_service_check_reconfigure ()
940 # We only care about some events in this function. For others we
942 case "$event_name" in
943 monitor|ipreallocated|reconfigure) : ;;
947 if ctdb_reconfigure_take_lock ; then
948 # No events covered by this function are running, so proceed
950 case "$event_name" in
952 (ctdb_service_reconfigure)
956 if ctdb_service_needs_reconfigure ; then
957 ctdb_service_reconfigure
962 ctdb_reconfigure_release_lock
964 # Somebody else is running an event we don't want to collide
965 # with. We proceed with caution.
966 case "$event_name" in
968 # Tell whoever called us to retry.
972 # Defer any scheduled reconfigure and just run the
973 # rest of the ipreallocated event, as per the
974 # eventscript. There's an assumption here that the
975 # event doesn't depend on any scheduled reconfigure.
976 # This is true in the current code.
980 # There is most likely a reconfigure in progress so
981 # the service is possibly unstable. As above, we
982 # defer any scheduled reconfigured. We also replay
983 # the previous monitor status since that's the best
984 # information we have.
985 ctdb_replay_monitor_status
991 ##################################################################
992 # Does CTDB manage this service? - and associated auto-start/stop
994 ctdb_compat_managed_service ()
996 if [ "$1" = "yes" -a "$2" = "$service_name" ] ; then
997 CTDB_MANAGED_SERVICES="$CTDB_MANAGED_SERVICES $2"
1001 is_ctdb_managed_service ()
1005 # $t is used just for readability and to allow better accurate
1006 # matching via leading/trailing spaces
1007 t=" $CTDB_MANAGED_SERVICES "
1009 # Return 0 if "<space>$service_name<space>" appears in $t
1010 if [ "${t#* ${service_name} }" != "${t}" ] ; then
1014 # If above didn't match then update $CTDB_MANAGED_SERVICES for
1015 # backward compatibility and try again.
1016 ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD" "vsftpd"
1017 ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA" "samba"
1018 ctdb_compat_managed_service "$CTDB_MANAGES_WINBIND" "winbind"
1019 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "apache2"
1020 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "httpd"
1021 ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI" "iscsi"
1022 ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD" "clamd"
1023 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs"
1025 t=" $CTDB_MANAGED_SERVICES "
1027 # Return 0 if "<space>$service_name<space>" appears in $t
1028 [ "${t#* ${service_name} }" != "${t}" ]
1031 ctdb_start_stop_service ()
1035 # Allow service-start/service-stop pseudo-events to start/stop
1036 # services when we're not auto-starting/stopping and we're not
1038 case "$event_name" in
1040 if is_ctdb_managed_service ; then
1041 die 'service-start event not permitted when service is managed'
1043 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1044 die 'service-start event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1050 if is_ctdb_managed_service ; then
1051 die 'service-stop event not permitted when service is managed'
1053 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1054 die 'service-stop event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1061 # Do nothing unless configured to...
1062 [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] || return 0
1064 [ "$event_name" = "monitor" ] || return 0
1066 if is_ctdb_managed_service ; then
1067 if ! is_ctdb_previously_managed_service ; then
1068 echo "Starting service \"$service_name\" - now managed"
1069 background_with_logging ctdb_service_start
1073 if is_ctdb_previously_managed_service ; then
1074 echo "Stopping service \"$service_name\" - no longer managed"
1075 background_with_logging ctdb_service_stop
1081 ctdb_service_start ()
1083 # The service is marked managed if we've ever tried to start it.
1084 ctdb_service_managed
1086 service_start || return $?
1092 ctdb_service_stop ()
1094 ctdb_service_unmanaged
1098 # Default service_start() and service_stop() functions.
1100 # These may be overridden in an eventscript.
1103 service "$service_name" start
1108 service "$service_name" stop
1111 ##################################################################
1113 ctdb_standard_event_handler ()
1120 _family="$1" ; shift
1121 if [ "$_family" = "inet6" ] ; then
1122 _iptables_cmd="ip6tables"
1124 _iptables_cmd="iptables"
1127 # iptables doesn't like being re-entered, so flock-wrap it.
1128 flock -w 30 "${CTDB_SCRIPT_VARDIR}/iptables.flock" "$_iptables_cmd" "$@"
1131 # AIX (and perhaps others?) doesn't have mktemp
1132 if ! type mktemp >/dev/null 2>&1 ; then
1136 if [ "$1" = "-d" ] ; then
1140 _d="${TMPDIR:-/tmp}"
1141 _hex10=$(dd if=/dev/urandom count=20 2>/dev/null | \
1143 sed -e 's@\(..........\).*@\1@')
1144 _t="${_d}/tmp.${_hex10}"
1157 ########################################################
1159 ########################################################
1165 tickledir="${CTDB_SCRIPT_VARDIR}/tickles"
1166 mkdir -p "$tickledir"
1170 # What public IPs do I hold?
1171 _ips=$(ctdb -X ip | awk -F'|' -v pnn=$pnn '$3 == pnn {print $2}')
1173 # IPs as a regexp choice
1174 _ipschoice="($(echo $_ips | sed -e 's/ /|/g' -e 's/\./\\\\./g'))"
1176 # Record connections to our public IPs in a temporary file.
1177 # This temporary file is in CTDB's private state directory and
1178 # $$ is used to avoid a very rare race involving CTDB's script
1179 # debugging. No security issue, nothing to see here...
1180 _my_connections="${tickledir}/${_port}.connections.$$"
1182 awk -v destpat="^${_ipschoice}:${_port}\$" \
1183 '$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ destpat {print $5, $4}' |
1184 sort >"$_my_connections"
1186 # Record our current tickles in a temporary file
1187 _my_tickles="${tickledir}/${_port}.tickles.$$"
1188 for _i in $_ips ; do
1189 ctdb -X gettickles $_i $_port |
1190 awk -F'|' 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
1192 sort >"$_my_tickles"
1194 # Add tickles for connections that we haven't already got tickles for
1195 comm -23 "$_my_connections" "$_my_tickles" |
1196 while read _src _dst ; do
1197 ctdb addtickle $_src $_dst
1200 # Remove tickles for connections that are no longer there
1201 comm -13 "$_my_connections" "$_my_tickles" |
1202 while read _src _dst ; do
1203 ctdb deltickle $_src $_dst
1206 rm -f "$_my_connections" "$_my_tickles"
1208 # Remove stale files from killed scripts
1209 find "$tickledir" -type f -mmin +10 | xargs -r rm
1212 ########################################################
1213 # load a site local config file
1214 ########################################################
1216 [ -n "$CTDB_RC_LOCAL" -a -x "$CTDB_RC_LOCAL" ] && {
1220 [ -x $CTDB_BASE/rc.local ] && {
1221 . $CTDB_BASE/rc.local
1224 [ -d $CTDB_BASE/rc.local.d ] && {
1225 for i in $CTDB_BASE/rc.local.d/* ; do
1226 [ -x "$i" ] && . "$i"
1230 script_name="${0##*/}" # basename
1231 service_fail_limit=1