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 CTDB="${CTDB:-/usr/local/bin/ctdb}"
15 # Only (and always) override these variables in test code
17 if [ -z "$CTDB_SCRIPT_VARDIR" ] ; then
18 CTDB_SCRIPT_VARDIR="/usr/local/var/lib/ctdb/state"
21 if [ -z "$CTDB_SYS_ETCDIR" ] ; then
22 CTDB_SYS_ETCDIR="/etc"
25 if [ -z "$CTDB_HELPER_BINDIR" ] ; then
26 CTDB_HELPER_BINDIR="/usr/local/libexec/ctdb"
29 #######################################
30 # pull in a system config file, if any
32 rewrite_ctdb_options ()
36 _opts_defaults="mode=700"
37 # Get any extra options specified after colon
38 if [ "$CTDB_DBDIR" = "tmpfs" ] ; then
41 _opts="${CTDB_DBDIR#tmpfs:}"
43 # This is an internal variable, only used by ctdbd_wrapper.
44 # It is OK to repeat mount options - last value wins
45 CTDB_DBDIR_TMPFS_OPTIONS="${_opts_defaults}${_opts:+,}${_opts}"
47 CTDB_DBDIR="${ctdb_rundir}/CTDB_DBDIR"
50 CTDB_DBDIR_TMPFS_OPTIONS=""
57 foo="${service_config:-${service_name}}"
58 if [ -n "$foo" ] ; then
64 if [ "$1" != "ctdb" ] ; then
72 if [ -f $CTDB_SYS_ETCDIR/sysconfig/$1 ]; then
73 . $CTDB_SYS_ETCDIR/sysconfig/$1
74 elif [ -f $CTDB_SYS_ETCDIR/default/$1 ]; then
75 . $CTDB_SYS_ETCDIR/default/$1
76 elif [ -f $CTDB_BASE/sysconfig/$1 ]; then
77 . $CTDB_BASE/sysconfig/$1
80 if [ "$1" = "ctdb" ] ; then
81 _config="${CTDBD_CONF:-${CTDB_BASE}/ctdbd.conf}"
82 if [ -r "$_config" ] ; then
93 ##############################################################
95 # CTDB_SCRIPT_DEBUGLEVEL can be overwritten by setting it in a
99 if [ ${CTDB_SCRIPT_DEBUGLEVEL:-2} -ge 4 ] ; then
100 # If there are arguments then echo them. Otherwise expect to
101 # use stdin, which allows us to pass lots of debug using a
103 if [ -n "$1" ] ; then
106 sed -e 's@^@DEBUG: @'
109 if [ -z "$1" ] ; then
124 # Log given message or stdin to either syslog or a CTDB log file
125 # $1 is the tag passed to logger if syslog is in use.
130 case "$CTDB_LOGGING" in
132 if [ -n "$CTDB_LOGGING" ] ; then
133 _file="${CTDB_LOGGING#file:}"
135 _file="/usr/local/var/log/log.ctdb"
138 if [ -n "$*" ] ; then
146 # Handle all syslog:* variants here too. There's no tool to do
147 # the lossy things, so just use logger.
148 logger -t "ctdbd: ${_tag}" $*
153 # When things are run in the background in an eventscript then logging
154 # output might get lost. This is the "solution". :-)
155 background_with_logging ()
158 "$@" 2>&1 </dev/null |
159 script_log "${script_name}&"
165 ##############################################################
166 # check number of args for different events
172 echo "ERROR: must supply interface, IP and maskbits"
178 echo "ERROR: must supply old interface, new interface, IP and maskbits"
185 ##############################################################
186 # determine on what type of system (init style) we are running
189 # only do detection if not already set:
190 [ -z "$CTDB_INIT_STYLE" ] || return
192 if [ -x /sbin/startproc ]; then
193 CTDB_INIT_STYLE="suse"
194 elif [ -x /sbin/start-stop-daemon ]; then
195 CTDB_INIT_STYLE="debian"
197 CTDB_INIT_STYLE="redhat"
201 ######################################################
202 # simulate /sbin/service on platforms that don't have it
203 # _service() makes it easier to hook the service() function for
210 # do nothing, when no service was specified
211 [ -z "$_service_name" ] && return
213 if [ -x /sbin/service ]; then
214 $_nice /sbin/service "$_service_name" "$_op"
215 elif [ -x /usr/sbin/service ]; then
216 $_nice /usr/sbin/service "$_service_name" "$_op"
217 elif [ -x /bin/systemctl ]; then
218 $_nice /bin/systemctl "$_op" "$_service_name"
219 elif [ -x $CTDB_SYS_ETCDIR/init.d/$_service_name ]; then
220 $_nice $CTDB_SYS_ETCDIR/init.d/$_service_name "$_op"
221 elif [ -x $CTDB_SYS_ETCDIR/rc.d/init.d/$_service_name ]; then
222 $_nice $CTDB_SYS_ETCDIR/rc.d/init.d/$_service_name "$_op"
232 ######################################################
233 # simulate /sbin/service (niced) on platforms that don't have it
240 ######################################################
241 # Cached retrieval of PNN from local node. This never changes so why
242 # open a client connection to the server each time this is needed?
243 # This sets $pnn - this avoid an unnecessary subprocess.
246 _pnn_file="${CTDB_SCRIPT_VARDIR}/my-pnn"
247 if [ ! -f "$_pnn_file" ] ; then
248 ctdb pnn | sed -e 's@.*:@@' >"$_pnn_file"
251 read pnn <"$_pnn_file"
254 # Cached retrieval of private IP address from local node. This never
255 # changes. Sets $ip_address to avoid an unnecessary subprocess.
256 ctdb_get_ip_address ()
258 _ip_addr_file="${CTDB_SCRIPT_VARDIR}/my-ip-address"
259 if [ ! -f "$_ip_addr_file" ] ; then
261 awk -F '|' 'NR == 2 { print $3 }' >"$_ip_addr_file"
264 read ip_address <"$_ip_addr_file"
267 ######################################################
268 # wrapper around /proc/ settings to allow them to be hooked
270 # 1st arg is relative path under /proc/, 2nd arg is value to set
273 echo "$2" >"/proc/$1"
278 if [ -w "/proc/$1" ] ; then
283 ######################################################
284 # wrapper around getting file contents from /proc/ to allow
285 # this to be hooked for testing
286 # 1st arg is relative path under /proc/
292 ######################################################
293 # Print up to $_max kernel stack traces for processes named $_program
294 program_stack_traces ()
300 for _pid in $(pidof "$_prog") ; do
301 [ $_count -le $_max ] || break
303 # Do this first to avoid racing with process exit
304 _stack=$(get_proc "${_pid}/stack" 2>/dev/null)
305 if [ -n "$_stack" ] ; then
306 echo "Stack trace for ${_prog}[${_pid}]:"
308 _count=$(($_count + 1))
313 ######################################################
314 # Ensure $service_name is set
315 assert_service_name ()
317 [ -n "$service_name" ] || die "INTERNAL ERROR: \$service_name not set"
320 ######################################################
321 # check a set of directories is available
322 # return 1 on a missing directory
323 # directories are read from stdin
324 ######################################################
325 ctdb_check_directories_probe()
327 while IFS="" read d ; do
333 [ -d "${d}/." ] || return 1
338 ######################################################
339 # check a set of directories is available
340 # directories are read from stdin
341 ######################################################
342 ctdb_check_directories()
344 ctdb_check_directories_probe || {
345 echo "ERROR: $service_name directory \"$d\" not available"
350 ######################################################
351 # check a set of tcp ports
352 # usage: ctdb_check_tcp_ports <ports...>
353 ######################################################
355 # This flag file is created when a service is initially started. It
356 # is deleted the first time TCP port checks for that service succeed.
357 # Until then ctdb_check_tcp_ports() prints a more subtle "error"
358 # message if a port check fails.
359 _ctdb_check_tcp_common ()
362 _d="${CTDB_SCRIPT_VARDIR}/failcount"
363 _ctdb_service_started_file="${_d}/${service_name}.started"
366 ctdb_check_tcp_init ()
368 _ctdb_check_tcp_common
369 mkdir -p "${_ctdb_service_started_file%/*}" # dirname
370 touch "$_ctdb_service_started_file"
373 # Check whether something is listening on all of the given TCP ports
374 # using the "ctdb checktcpport" command.
375 ctdb_check_tcp_ports()
377 if [ -z "$1" ] ; then
378 echo "INTERNAL ERROR: ctdb_check_tcp_ports - no ports specified"
382 for _p ; do # process each function argument (port)
383 _cmd="ctdb checktcpport $_p"
388 _ctdb_check_tcp_common
389 if [ ! -f "$_ctdb_service_started_file" ] ; then
390 echo "ERROR: $service_name tcp port $_p is not responding"
391 debug "\"ctdb checktcpport $_p\" was able to bind to port"
393 echo "INFO: $service_name tcp port $_p is not responding"
399 # Couldn't bind, something already listening, next port...
403 echo "ERROR: unexpected error running \"ctdb checktcpport\""
405 ctdb checktcpport (exited with $_ret) with output:
412 # All ports listening
413 _ctdb_check_tcp_common
414 rm -f "$_ctdb_service_started_file"
418 ######################################################
419 # check a unix socket
420 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
421 ######################################################
422 ctdb_check_unix_socket() {
424 [ -z "$socket_path" ] && return
426 if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
427 echo "ERROR: $service_name socket $socket_path not found"
432 ######################################################
433 # check a command returns zero status
434 # usage: ctdb_check_command <command>
435 ######################################################
436 ctdb_check_command ()
438 _out=$("$@" 2>&1) || {
439 echo "ERROR: $* returned error"
445 ################################################
446 # kill off any TCP connections with the given IP
447 ################################################
448 kill_tcp_connections ()
454 if [ "$3" = "oneway" ] ; then
458 get_tcp_connections_for_ip "$_ip" | {
463 while read _dst _src; do
464 _destport="${_dst##*:}"
467 # we only do one-way killtcp for CIFS
468 139|445) __oneway=true ;;
471 echo "Killing TCP connection $_src $_dst"
472 _connections="${_connections}${_nl}${_src} ${_dst}"
473 if ! $__oneway ; then
474 _connections="${_connections}${_nl}${_dst} ${_src}"
477 _killcount=$(($_killcount + 1))
480 if [ $_killcount -eq 0 ] ; then
484 echo "$_connections" | \
485 "${CTDB_HELPER_BINDIR}/ctdb_killtcp" "$_iface" || {
486 echo "Failed to kill TCP connections"
490 _remaining=$(get_tcp_connections_for_ip $_ip | wc -l)
492 if [ $_remaining -eq 0 ] ; then
493 echo "Killed $_killcount TCP connections to released IP $_ip"
497 _t="${_remaining}/${_killcount}"
498 echo "Failed to kill TCP connections for IP $_ip (${_t} remaining)"
502 ##################################################################
503 # kill off the local end for any TCP connections with the given IP
504 ##################################################################
505 kill_tcp_connections_local_only ()
507 kill_tcp_connections "$@" "oneway"
510 ##################################################################
511 # tickle any TCP connections with the given IP
512 ##################################################################
513 tickle_tcp_connections ()
517 get_tcp_connections_for_ip "$_ip" |
521 while read dest src; do
522 echo "Tickle TCP connection $src $dest"
523 ctdb tickle $src $dest >/dev/null 2>&1 || _failed=true
524 echo "Tickle TCP connection $dest $src"
525 ctdb tickle $dest $src >/dev/null 2>&1 || _failed=true
529 echo "Failed to send tickle control"
534 get_tcp_connections_for_ip ()
538 ss -tn state established "src [$_ip]" | awk 'NR > 1 {print $3, $4}'
541 ########################################################
549 # Ensure interface is up
550 ip link set "$_iface" up || \
551 die "Failed to bringup interface $_iface"
553 # Only need to define broadcast for IPv4
559 ip addr add "$_ip/$_maskbits" $_bcast dev "$_iface" || {
560 echo "Failed to add $_ip/$_maskbits on dev $_iface"
564 # Wait 5 seconds for IPv6 addresses to stop being tentative...
565 if [ -z "$_bcast" ] ; then
566 for _x in $(seq 1 10) ; do
567 ip addr show to "${_ip}/128" | grep -q "tentative" || break
571 # If the address was a duplicate then it won't be on the
572 # interface so flag an error.
573 _t=$(ip addr show to "${_ip}/128")
576 echo "Failed to add $_ip/$_maskbits on dev $_iface"
579 *tentative*|*dadfailed*)
580 echo "Failed to add $_ip/$_maskbits on dev $_iface"
581 ip addr del "$_ip/$_maskbits" dev "$_iface"
588 delete_ip_from_iface()
594 # This could be set globally for all interfaces but it is probably
595 # better to avoid surprises, so limit it the interfaces where CTDB
596 # has public IP addresses. There isn't anywhere else convenient
597 # to do this so just set it each time. This is much cheaper than
598 # remembering and re-adding secondaries.
599 set_proc "sys/net/ipv4/conf/${_iface}/promote_secondaries" 1
601 ip addr del "$_ip/$_maskbits" dev "$_iface" || {
602 echo "Failed to del $_ip on dev $_iface"
607 # If the given IP is hosted then print 2 items: maskbits and iface
617 ip addr show to "${_addr}/${_bits}" 2>/dev/null | \
618 awk 'NR == 1 { iface = $2; sub(":$", "", iface) ; \
619 sub("@.*", "", iface) } \
620 $1 ~ /inet/ { mask = $2; sub(".*/", "", mask); \
626 _addr="${1%/*}" # Remove optional maskbits
628 set -- $(ip_maskbits_iface $_addr)
629 if [ -n "$1" ] ; then
632 echo "Removing public address $_addr/$_maskbits from device $_iface"
633 delete_ip_from_iface $_iface $_addr $_maskbits >/dev/null 2>&1
637 drop_all_public_ips ()
639 while read _ip _x ; do
641 done <"${CTDB_PUBLIC_ADDRESSES:-/dev/null}"
646 set_proc_maybe sys/net/ipv4/route/flush 1
647 set_proc_maybe sys/net/ipv6/route/flush 1
650 ########################################################
651 # Interface monitoring
653 # If the interface is a virtual one (e.g. VLAN) then get the
654 # underlying interface
655 interface_get_real ()
657 # Output of "ip link show <iface>"
660 # Extract the full interface description to see if it is a VLAN
661 _t=$(echo "$_iface_info" |
662 awk 'NR == 1 { iface = $2; sub(":$", "", iface) ; \
666 # VLAN: use the underlying interface, after the '@'
670 # Not a regular VLAN. For backward compatibility, assume
671 # there is some other sort of VLAN that doesn't have the
672 # '@' in the output and only use what is before a '.'. If
673 # there is no '.' then this will be the whole interface
679 # Check whether an interface is operational
684 _iface_info=$(ip link show "$_iface" 2>&1) || {
685 echo "ERROR: Monitored interface ${_iface} does not exist"
690 # If the interface is a virtual one (e.g. VLAN) then get the
691 # underlying interface.
692 _realiface=$(interface_get_real "$_iface_info")
694 if _bi=$(get_proc "net/bonding/${_realiface}" 2>/dev/null) ; then
695 # This is a bond: various monitoring strategies
696 echo "$_bi" | grep -q 'Currently Active Slave: None' && {
697 echo "ERROR: No active slaves for bond device ${_realiface}"
700 echo "$_bi" | grep -q '^MII Status: up' || {
701 echo "ERROR: public network interface ${_realiface} is down"
704 echo "$_bi" | grep -q '^Bonding Mode: IEEE 802.3ad Dynamic link aggregation' && {
705 # This works around a bug in the driver where the
706 # overall bond status can be up but none of the actual
707 # physical interfaces have a link.
708 echo "$_bi" | grep 'MII Status:' | tail -n +2 | grep -q '^MII Status: up' || {
709 echo "ERROR: No active slaves for 802.ad bond device ${_realiface}"
719 # loopback is always working
723 # we don't know how to test ib links
727 ethtool "$_iface" | grep -q 'Link detected: yes' || {
728 # On some systems, this is not successful when a
729 # cable is plugged but the interface has not been
730 # brought up previously. Bring the interface up
732 ip link set "$_iface" up
733 ethtool "$_iface" | grep -q 'Link detected: yes' || {
734 echo "ERROR: No link on the public network interface ${_iface}"
744 ########################################################
746 _ctdb_counter_common () {
747 _service_name="${1:-${service_name:-${script_name}}}"
748 _counter_file="${CTDB_SCRIPT_VARDIR}/failcount/${_service_name}"
749 mkdir -p "${_counter_file%/*}" # dirname
751 ctdb_counter_init () {
752 _ctdb_counter_common "$1"
756 ctdb_counter_incr () {
757 _ctdb_counter_common "$1"
760 echo -n 1 >> "$_counter_file"
762 ctdb_counter_get () {
763 _ctdb_counter_common "$1"
765 stat -c "%s" "$_counter_file" 2>/dev/null || echo 0
767 ctdb_check_counter () {
768 _msg="${1:-error}" # "error" - anything else is silent on fail
769 _op="${2:--ge}" # an integer operator supported by test
770 _limit="${3:-${service_fail_limit}}"
773 _size=$(ctdb_counter_get "$1")
776 if [ "$_op" != "%" ] ; then
777 if [ $_size $_op $_limit ] ; then
781 if [ $(($_size $_op $_limit)) -eq 0 ] ; then
786 if [ "$_msg" = "error" ] ; then
787 echo "ERROR: $_size consecutive failures for $_service_name, marking node unhealthy"
795 ########################################################
797 ctdb_setup_service_state_dir ()
799 service_state_dir="${CTDB_SCRIPT_VARDIR}/service_state/${1:-${service_name}}"
800 mkdir -p "$service_state_dir" || {
801 echo "Error creating state dir \"$service_state_dir\""
806 ########################################################
807 # Managed status history, for auto-start/stop
809 _ctdb_managed_common ()
811 _ctdb_managed_file="${CTDB_SCRIPT_VARDIR}/managed_history/${service_name}"
814 ctdb_service_managed ()
817 mkdir -p "${_ctdb_managed_file%/*}" # dirname
818 touch "$_ctdb_managed_file"
821 ctdb_service_unmanaged ()
824 rm -f "$_ctdb_managed_file"
827 is_ctdb_previously_managed_service ()
830 [ -f "$_ctdb_managed_file" ]
833 ##################################################################
834 # Reconfigure a service on demand
836 _ctdb_service_reconfigure_common ()
838 _d="${CTDB_SCRIPT_VARDIR}/service_status/${service_name}"
840 _ctdb_service_reconfigure_flag="$_d/reconfigure"
843 ctdb_service_needs_reconfigure ()
845 _ctdb_service_reconfigure_common
846 [ -e "$_ctdb_service_reconfigure_flag" ]
849 ctdb_service_set_reconfigure ()
851 _ctdb_service_reconfigure_common
852 >"$_ctdb_service_reconfigure_flag"
855 ctdb_service_unset_reconfigure ()
857 _ctdb_service_reconfigure_common
858 rm -f "$_ctdb_service_reconfigure_flag"
861 ctdb_service_reconfigure ()
863 echo "Reconfiguring service \"${service_name}\"..."
864 ctdb_service_unset_reconfigure
865 service_reconfigure || return $?
869 # Default service_reconfigure() function does nothing.
870 service_reconfigure ()
875 ctdb_reconfigure_take_lock ()
877 _ctdb_service_reconfigure_common
878 _lock="${_d}/reconfigure_lock"
879 mkdir -p "${_lock%/*}" # dirname
884 # This is overkill but will work if we need to extend this to
885 # allow certain events to run multiple times in parallel
886 # (e.g. takeip) and write multiple PIDs to the file.
888 if [ -n "$_locker_event" ] ; then
890 if [ -n "$_pid" -a "$_pid" != $$ ] && \
891 kill -0 "$_pid" 2>/dev/null ; then
897 printf "%s\n%s\n" "$event_name" $$ >"$_lock"
902 ctdb_reconfigure_release_lock ()
904 _ctdb_service_reconfigure_common
905 _lock="${_d}/reconfigure_lock"
910 ctdb_replay_monitor_status ()
912 echo "Replaying previous status for this script due to reconfigure..."
913 # Leading separator ('|') is missing in some versions...
914 _out=$(ctdb scriptstatus -X | grep -E "^\|?monitor\|${script_name}\|")
915 # Output looks like this:
916 # |monitor|60.nfs|1|ERROR|1314764004.030861|1314764004.035514|foo bar|
917 # This is the cheapest way of getting fields in the middle.
918 set -- $(IFS="|" ; echo $_out)
921 # The error output field can include colons so we'll try to
922 # preserve them. The weak checking at the beginning tries to make
923 # this work for both broken (no leading '|') and fixed output.
925 _err_out="${_out#*monitor|${script_name}|*|*|*|*|}"
927 OK) : ;; # Do nothing special.
929 # Recast this as an error, since we can't exit with the
930 # correct negative number.
932 _err_out="[Replay of TIMEDOUT scriptstatus - note incorrect return code.] ${_err_out}"
935 # Recast this as an OK, since we can't exit with the
936 # correct negative number.
938 _err_out="[Replay of DISABLED scriptstatus - note incorrect return code.] ${_err_out}"
940 *) : ;; # Must be ERROR, do nothing special.
942 if [ -n "$_err_out" ] ; then
948 ctdb_service_check_reconfigure ()
952 # We only care about some events in this function. For others we
954 case "$event_name" in
955 monitor|ipreallocated|reconfigure) : ;;
959 if ctdb_reconfigure_take_lock ; then
960 # No events covered by this function are running, so proceed
962 case "$event_name" in
964 (ctdb_service_reconfigure)
968 if ctdb_service_needs_reconfigure ; then
969 ctdb_service_reconfigure
974 ctdb_reconfigure_release_lock
976 # Somebody else is running an event we don't want to collide
977 # with. We proceed with caution.
978 case "$event_name" in
980 # Tell whoever called us to retry.
984 # Defer any scheduled reconfigure and just run the
985 # rest of the ipreallocated event, as per the
986 # eventscript. There's an assumption here that the
987 # event doesn't depend on any scheduled reconfigure.
988 # This is true in the current code.
992 # There is most likely a reconfigure in progress so
993 # the service is possibly unstable. As above, we
994 # defer any scheduled reconfigured. We also replay
995 # the previous monitor status since that's the best
996 # information we have.
997 ctdb_replay_monitor_status
1003 ##################################################################
1004 # Does CTDB manage this service? - and associated auto-start/stop
1006 ctdb_compat_managed_service ()
1008 if [ "$1" = "yes" -a "$2" = "$service_name" ] ; then
1009 CTDB_MANAGED_SERVICES="$CTDB_MANAGED_SERVICES $2"
1013 is_ctdb_managed_service ()
1017 # $t is used just for readability and to allow better accurate
1018 # matching via leading/trailing spaces
1019 t=" $CTDB_MANAGED_SERVICES "
1021 # Return 0 if "<space>$service_name<space>" appears in $t
1022 if [ "${t#* ${service_name} }" != "${t}" ] ; then
1026 # If above didn't match then update $CTDB_MANAGED_SERVICES for
1027 # backward compatibility and try again.
1028 ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD" "vsftpd"
1029 ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA" "samba"
1030 ctdb_compat_managed_service "$CTDB_MANAGES_WINBIND" "winbind"
1031 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "apache2"
1032 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "httpd"
1033 ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI" "iscsi"
1034 ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD" "clamd"
1035 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs"
1037 t=" $CTDB_MANAGED_SERVICES "
1039 # Return 0 if "<space>$service_name<space>" appears in $t
1040 [ "${t#* ${service_name} }" != "${t}" ]
1043 ctdb_start_stop_service ()
1047 # Allow service-start/service-stop pseudo-events to start/stop
1048 # services when we're not auto-starting/stopping and we're not
1050 case "$event_name" in
1052 if is_ctdb_managed_service ; then
1053 die 'service-start event not permitted when service is managed'
1055 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1056 die 'service-start event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1062 if is_ctdb_managed_service ; then
1063 die 'service-stop event not permitted when service is managed'
1065 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1066 die 'service-stop event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1073 # Do nothing unless configured to...
1074 [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] || return 0
1076 [ "$event_name" = "monitor" ] || return 0
1078 if is_ctdb_managed_service ; then
1079 if ! is_ctdb_previously_managed_service ; then
1080 echo "Starting service \"$service_name\" - now managed"
1081 background_with_logging ctdb_service_start
1085 if is_ctdb_previously_managed_service ; then
1086 echo "Stopping service \"$service_name\" - no longer managed"
1087 background_with_logging ctdb_service_stop
1093 ctdb_service_start ()
1095 # The service is marked managed if we've ever tried to start it.
1096 ctdb_service_managed
1098 service_start || return $?
1104 ctdb_service_stop ()
1106 ctdb_service_unmanaged
1110 # Default service_start() and service_stop() functions.
1112 # These may be overridden in an eventscript.
1115 service "$service_name" start
1120 service "$service_name" stop
1123 ##################################################################
1125 ctdb_standard_event_handler ()
1132 _family="$1" ; shift
1133 if [ "$_family" = "inet6" ] ; then
1134 _iptables_cmd="ip6tables"
1136 _iptables_cmd="iptables"
1139 # iptables doesn't like being re-entered, so flock-wrap it.
1140 flock -w 30 "${CTDB_SCRIPT_VARDIR}/iptables.flock" "$_iptables_cmd" "$@"
1143 # AIX (and perhaps others?) doesn't have mktemp
1144 if ! type mktemp >/dev/null 2>&1 ; then
1148 if [ "$1" = "-d" ] ; then
1152 _d="${TMPDIR:-/tmp}"
1153 _hex10=$(dd if=/dev/urandom count=20 2>/dev/null | \
1155 sed -e 's@\(..........\).*@\1@')
1156 _t="${_d}/tmp.${_hex10}"
1169 ######################################################################
1170 # NFS callout handling
1174 if [ -z "$CTDB_NFS_CALLOUT" ] ; then
1175 CTDB_NFS_CALLOUT="${CTDB_BASE}/nfs-linux-kernel-callout"
1177 # Always export, for statd callout
1178 export CTDB_NFS_CALLOUT
1180 # If the callout wants to use this then it must create it
1181 export CTDB_NFS_CALLOUT_STATE_DIR="${service_state_dir}/callout-state"
1183 # Export, if set, for use by clustered NFS callouts
1184 if [ -n "$CTDB_NFS_STATE_FS_TYPE" ] ; then
1185 export CTDB_NFS_STATE_FS_TYPE
1187 if [ -n "$CTDB_NFS_STATE_MNT" ] ; then
1188 export CTDB_NFS_STATE_MNT
1191 nfs_callout_cache="${service_state_dir}/nfs_callout_cache"
1192 nfs_callout_cache_callout="${nfs_callout_cache}/CTDB_NFS_CALLOUT"
1193 nfs_callout_cache_ops="${nfs_callout_cache}/ops"
1196 nfs_callout_register ()
1198 mkdir -p "$nfs_callout_cache_ops"
1199 rm -f "$nfs_callout_cache_ops"/*
1201 echo "$CTDB_NFS_CALLOUT" >"$nfs_callout_cache_callout"
1203 _t=$(eval "$CTDB_NFS_CALLOUT" "register")
1204 if [ -n "$_t" ] ; then
1206 while IFS="" read _op ; do
1207 touch "${nfs_callout_cache_ops}/${_op}"
1210 touch "${nfs_callout_cache_ops}/ALL"
1216 # Re-run registration if $CTDB_NFS_CALLOUT has changed
1218 if [ -r "$nfs_callout_cache_callout" ] ; then
1219 read _prev <"$nfs_callout_cache_callout"
1221 if [ "$CTDB_NFS_CALLOUT" != "$_prev" ] ; then
1222 nfs_callout_register
1225 # Run the operation if it is registered...
1226 if [ -e "${nfs_callout_cache_ops}/${1}" ] || \
1227 [ -e "${nfs_callout_cache_ops}/ALL" ]; then
1228 eval "$CTDB_NFS_CALLOUT" "$@"
1232 ########################################################
1234 ########################################################
1240 tickledir="${CTDB_SCRIPT_VARDIR}/tickles"
1241 mkdir -p "$tickledir"
1245 # What public IPs do I hold?
1246 _ips=$(ctdb -X ip | awk -F'|' -v pnn=$pnn '$3 == pnn {print $2}')
1248 # IPs and port as ss filters
1250 for _ip in $_ips ; do
1251 _ip_filter="${_ip_filter}${_ip_filter:+ || }src [${_ip}]"
1253 _port_filter="sport == :${_port}"
1255 # Record connections to our public IPs in a temporary file.
1256 # This temporary file is in CTDB's private state directory and
1257 # $$ is used to avoid a very rare race involving CTDB's script
1258 # debugging. No security issue, nothing to see here...
1259 _my_connections="${tickledir}/${_port}.connections.$$"
1260 # Parentheses are needed around the filters for precedence but
1261 # the parentheses can't be empty!
1262 ss -tn state established \
1263 "${_ip_filter:+( ${_ip_filter} )}" \
1264 "${_port_filter:+( ${_port_filter} )}" |
1265 awk 'NR > 1 {print $4, $3}' |
1266 sort >"$_my_connections"
1268 # Record our current tickles in a temporary file
1269 _my_tickles="${tickledir}/${_port}.tickles.$$"
1270 for _i in $_ips ; do
1271 ctdb -X gettickles $_i $_port |
1272 awk -F'|' 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
1274 sort >"$_my_tickles"
1276 # Add tickles for connections that we haven't already got tickles for
1277 comm -23 "$_my_connections" "$_my_tickles" |
1278 while read _src _dst ; do
1279 ctdb addtickle $_src $_dst
1282 # Remove tickles for connections that are no longer there
1283 comm -13 "$_my_connections" "$_my_tickles" |
1284 while read _src _dst ; do
1285 ctdb deltickle $_src $_dst
1288 rm -f "$_my_connections" "$_my_tickles"
1290 # Remove stale files from killed scripts
1291 find "$tickledir" -type f -mmin +10 | xargs -r rm
1294 ########################################################
1295 # load a site local config file
1296 ########################################################
1298 [ -n "$CTDB_RC_LOCAL" -a -x "$CTDB_RC_LOCAL" ] && {
1302 [ -x $CTDB_BASE/rc.local ] && {
1303 . $CTDB_BASE/rc.local
1306 [ -d $CTDB_BASE/rc.local.d ] && {
1307 for i in $CTDB_BASE/rc.local.d/* ; do
1308 [ -x "$i" ] && . "$i"
1312 script_name="${0##*/}" # basename
1313 service_fail_limit=1