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'
11 CTDB_VARDIR="/usr/local/var/lib/ctdb"
12 ctdb_rundir="/usr/local/var/run/ctdb"
14 CTDB="${CTDB:-/usr/local/bin/ctdb}"
16 # Only (and always) override these variables in test code
18 if [ -z "$CTDB_SCRIPT_VARDIR" ] ; then
19 CTDB_SCRIPT_VARDIR="/usr/local/var/lib/ctdb/state"
22 if [ -z "$CTDB_SYS_ETCDIR" ] ; then
23 CTDB_SYS_ETCDIR="/etc"
26 if [ -z "$CTDB_HELPER_BINDIR" ] ; then
27 CTDB_HELPER_BINDIR="/usr/local/libexec/ctdb"
30 #######################################
31 # pull in a system config file, if any
33 rewrite_ctdb_options ()
37 _opts_defaults="mode=700"
38 # Get any extra options specified after colon
39 if [ "$CTDB_DBDIR" = "tmpfs" ] ; then
42 _opts="${CTDB_DBDIR#tmpfs:}"
44 # This is an internal variable, only used by ctdbd_wrapper.
45 # It is OK to repeat mount options - last value wins
46 CTDB_DBDIR_TMPFS_OPTIONS="${_opts_defaults}${_opts:+,}${_opts}"
48 CTDB_DBDIR="${ctdb_rundir}/CTDB_DBDIR"
51 CTDB_DBDIR_TMPFS_OPTIONS=""
58 foo="${service_config:-${service_name}}"
59 if [ -n "$foo" ] ; then
65 if [ "$1" != "ctdb" ] ; then
73 if [ -f "${CTDB_SYS_ETCDIR}/sysconfig/$1" ]; then
74 . "${CTDB_SYS_ETCDIR}/sysconfig/$1"
75 elif [ -f "${CTDB_SYS_ETCDIR}/default/$1" ]; then
76 . "${CTDB_SYS_ETCDIR}/default/$1"
77 elif [ -f "${CTDB_BASE}/sysconfig/$1" ]; then
78 . "${CTDB_BASE}/sysconfig/$1"
81 if [ "$1" = "ctdb" ] ; then
82 _config="${CTDBD_CONF:-${CTDB_BASE}/ctdbd.conf}"
83 if [ -r "$_config" ] ; then
94 ##############################################################
96 # CTDB_SCRIPT_DEBUGLEVEL can be overwritten by setting it in a
100 if [ "${CTDB_SCRIPT_DEBUGLEVEL:-2}" -ge 4 ] ; then
101 # If there are arguments then echo them. Otherwise expect to
102 # use stdin, which allows us to pass lots of debug using a
104 if [ -n "$1" ] ; then
107 sed -e 's@^@DEBUG: @'
110 if [ -z "$1" ] ; then
125 # Log given message or stdin to either syslog or a CTDB log file
126 # $1 is the tag passed to logger if syslog is in use.
131 case "$CTDB_LOGGING" in
133 if [ -n "$CTDB_LOGGING" ] ; then
134 _file="${CTDB_LOGGING#file:}"
136 _file="/usr/local/var/log/log.ctdb"
139 if [ -n "$*" ] ; then
147 # Handle all syslog:* variants here too. There's no tool to do
148 # the lossy things, so just use logger.
149 logger -t "ctdbd: ${_tag}" "$*"
154 # When things are run in the background in an eventscript then logging
155 # output might get lost. This is the "solution". :-)
156 background_with_logging ()
159 "$@" 2>&1 </dev/null |
160 script_log "${script_name}&"
166 ##############################################################
167 # check number of args for different events
173 echo "ERROR: must supply interface, IP and maskbits"
179 echo "ERROR: must supply old interface, new interface, IP and maskbits"
186 ##############################################################
187 # determine on what type of system (init style) we are running
190 # only do detection if not already set:
191 [ -z "$CTDB_INIT_STYLE" ] || return
193 if [ -x /sbin/startproc ]; then
194 CTDB_INIT_STYLE="suse"
195 elif [ -x /sbin/start-stop-daemon ]; then
196 CTDB_INIT_STYLE="debian"
198 CTDB_INIT_STYLE="redhat"
202 ######################################################
203 # simulate /sbin/service on platforms that don't have it
204 # _service() makes it easier to hook the service() function for
211 # do nothing, when no service was specified
212 [ -z "$_service_name" ] && return
214 if [ -x /sbin/service ]; then
215 $_nice /sbin/service "$_service_name" "$_op"
216 elif [ -x /usr/sbin/service ]; then
217 $_nice /usr/sbin/service "$_service_name" "$_op"
218 elif [ -x /bin/systemctl ]; then
219 $_nice /bin/systemctl "$_op" "$_service_name"
220 elif [ -x "${CTDB_SYS_ETCDIR}/init.d/${_service_name}" ]; then
221 $_nice "${CTDB_SYS_ETCDIR}/init.d/${_service_name}" "$_op"
222 elif [ -x "${CTDB_SYS_ETCDIR}/rc.d/init.d/${_service_name}" ]; then
223 $_nice "${CTDB_SYS_ETCDIR}/rc.d/init.d/${_service_name}" "$_op"
233 ######################################################
234 # simulate /sbin/service (niced) on platforms that don't have it
241 ######################################################
242 # Cached retrieval of PNN from local node. This never changes so why
243 # open a client connection to the server each time this is needed?
244 # This sets $pnn - this avoid an unnecessary subprocess.
247 _pnn_file="${CTDB_SCRIPT_VARDIR}/my-pnn"
248 if [ ! -f "$_pnn_file" ] ; then
249 $CTDB pnn | sed -e 's@.*:@@' >"$_pnn_file"
252 read pnn <"$_pnn_file"
255 # Cached retrieval of private IP address from local node. This never
256 # changes. Sets $ip_address to avoid an unnecessary subprocess.
257 ctdb_get_ip_address ()
259 _ip_addr_file="${CTDB_SCRIPT_VARDIR}/my-ip-address"
260 if [ ! -f "$_ip_addr_file" ] ; then
261 $CTDB -X nodestatus |
262 awk -F '|' 'NR == 2 { print $3 }' >"$_ip_addr_file"
265 read ip_address <"$_ip_addr_file"
268 ######################################################
269 # wrapper around /proc/ settings to allow them to be hooked
271 # 1st arg is relative path under /proc/, 2nd arg is value to set
274 echo "$2" >"/proc/$1"
279 if [ -w "/proc/$1" ] ; then
284 ######################################################
285 # wrapper around getting file contents from /proc/ to allow
286 # this to be hooked for testing
287 # 1st arg is relative path under /proc/
293 ######################################################
294 # Print up to $_max kernel stack traces for processes named $_program
295 program_stack_traces ()
301 for _pid in $(pidof "$_prog") ; do
302 [ $_count -le $_max ] || break
304 # Do this first to avoid racing with process exit
305 _stack=$(get_proc "${_pid}/stack" 2>/dev/null)
306 if [ -n "$_stack" ] ; then
307 echo "Stack trace for ${_prog}[${_pid}]:"
309 _count=$(($_count + 1))
314 ######################################################
315 # Ensure $service_name is set
316 assert_service_name ()
318 [ -n "$service_name" ] || die "INTERNAL ERROR: \$service_name not set"
321 ######################################################
322 # check a set of directories is available
323 # return 1 on a missing directory
324 # directories are read from stdin
325 ######################################################
326 ctdb_check_directories_probe()
328 while IFS="" read d ; do
334 [ -d "${d}/." ] || return 1
339 ######################################################
340 # check a set of directories is available
341 # directories are read from stdin
342 ######################################################
343 ctdb_check_directories()
345 ctdb_check_directories_probe || {
346 echo "ERROR: $service_name directory \"$d\" not available"
351 ######################################################
352 # check a set of tcp ports
353 # usage: ctdb_check_tcp_ports <ports...>
354 ######################################################
356 # This flag file is created when a service is initially started. It
357 # is deleted the first time TCP port checks for that service succeed.
358 # Until then ctdb_check_tcp_ports() prints a more subtle "error"
359 # message if a port check fails.
360 _ctdb_check_tcp_common ()
363 _d="${CTDB_SCRIPT_VARDIR}/failcount"
364 _ctdb_service_started_file="${_d}/${service_name}.started"
367 ctdb_check_tcp_init ()
369 _ctdb_check_tcp_common
370 mkdir -p "${_ctdb_service_started_file%/*}" # dirname
371 touch "$_ctdb_service_started_file"
374 # Check whether something is listening on all of the given TCP ports
375 # using the "ctdb checktcpport" command.
376 ctdb_check_tcp_ports()
378 if [ -z "$1" ] ; then
379 echo "INTERNAL ERROR: ctdb_check_tcp_ports - no ports specified"
383 for _p ; do # process each function argument (port)
384 _cmd="$CTDB checktcpport $_p"
389 _ctdb_check_tcp_common
390 if [ ! -f "$_ctdb_service_started_file" ] ; then
391 echo "ERROR: $service_name tcp port $_p is not responding"
392 debug "\"ctdb checktcpport $_p\" was able to bind to port"
394 echo "INFO: $service_name tcp port $_p is not responding"
400 # Couldn't bind, something already listening, next port...
404 echo "ERROR: unexpected error running \"ctdb checktcpport\""
406 $CTDB checktcpport (exited with $_ret) with output:
413 # All ports listening
414 _ctdb_check_tcp_common
415 rm -f "$_ctdb_service_started_file"
419 ######################################################
420 # check a unix socket
421 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
422 ######################################################
423 ctdb_check_unix_socket() {
425 [ -z "$socket_path" ] && return
427 if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
428 echo "ERROR: $service_name socket $socket_path not found"
433 ######################################################
434 # check a command returns zero status
435 # usage: ctdb_check_command <command>
436 ######################################################
437 ctdb_check_command ()
439 _out=$("$@" 2>&1) || {
440 echo "ERROR: $* returned error"
446 ################################################
447 # kill off any TCP connections with the given IP
448 ################################################
449 kill_tcp_connections ()
455 if [ "$3" = "oneway" ] ; then
459 get_tcp_connections_for_ip "$_ip" | {
464 while read _dst _src; do
465 _destport="${_dst##*:}"
468 # we only do one-way killtcp for CIFS
469 139|445) __oneway=true ;;
472 echo "Killing TCP connection $_src $_dst"
473 _connections="${_connections}${_nl}${_src} ${_dst}"
474 if ! $__oneway ; then
475 _connections="${_connections}${_nl}${_dst} ${_src}"
478 _killcount=$(($_killcount + 1))
481 if [ $_killcount -eq 0 ] ; then
485 echo "$_connections" | \
486 "${CTDB_HELPER_BINDIR}/ctdb_killtcp" "$_iface" || {
487 echo "Failed to kill TCP connections"
491 _remaining=$(get_tcp_connections_for_ip "$_ip" | wc -l)
493 if [ $_remaining -eq 0 ] ; then
494 echo "Killed $_killcount TCP connections to released IP $_ip"
498 _t="${_remaining}/${_killcount}"
499 echo "Failed to kill TCP connections for IP $_ip (${_t} remaining)"
503 ##################################################################
504 # kill off the local end for any TCP connections with the given IP
505 ##################################################################
506 kill_tcp_connections_local_only ()
508 kill_tcp_connections "$@" "oneway"
511 ##################################################################
512 # tickle any TCP connections with the given IP
513 ##################################################################
514 tickle_tcp_connections ()
518 get_tcp_connections_for_ip "$_ip" |
522 while read dest src; do
523 echo "Tickle TCP connection $src $dest"
524 $CTDB tickle "$src" "$dest" >/dev/null 2>&1 || _failed=true
525 echo "Tickle TCP connection $dest $src"
526 $CTDB tickle "$dest" "$src" >/dev/null 2>&1 || _failed=true
530 echo "Failed to send tickle control"
535 get_tcp_connections_for_ip ()
539 ss -tn state established "src [$_ip]" | awk 'NR > 1 {print $3, $4}'
542 ########################################################
550 # Ensure interface is up
551 ip link set "$_iface" up || \
552 die "Failed to bringup interface $_iface"
554 # Only need to define broadcast for IPv4
560 ip addr add "$_ip/$_maskbits" $_bcast dev "$_iface" || {
561 echo "Failed to add $_ip/$_maskbits on dev $_iface"
565 # Wait 5 seconds for IPv6 addresses to stop being tentative...
566 if [ -z "$_bcast" ] ; then
567 for _x in $(seq 1 10) ; do
568 ip addr show to "${_ip}/128" | grep -q "tentative" || break
572 # If the address was a duplicate then it won't be on the
573 # interface so flag an error.
574 _t=$(ip addr show to "${_ip}/128")
577 echo "Failed to add $_ip/$_maskbits on dev $_iface"
580 *tentative*|*dadfailed*)
581 echo "Failed to add $_ip/$_maskbits on dev $_iface"
582 ip addr del "$_ip/$_maskbits" dev "$_iface"
589 delete_ip_from_iface()
595 # This could be set globally for all interfaces but it is probably
596 # better to avoid surprises, so limit it the interfaces where CTDB
597 # has public IP addresses. There isn't anywhere else convenient
598 # to do this so just set it each time. This is much cheaper than
599 # remembering and re-adding secondaries.
600 set_proc "sys/net/ipv4/conf/${_iface}/promote_secondaries" 1
602 ip addr del "$_ip/$_maskbits" dev "$_iface" || {
603 echo "Failed to del $_ip on dev $_iface"
608 # If the given IP is hosted then print 2 items: maskbits and iface
618 ip addr show to "${_addr}/${_bits}" 2>/dev/null | \
619 awk 'NR == 1 { iface = $2; sub(":$", "", iface) ; \
620 sub("@.*", "", iface) } \
621 $1 ~ /inet/ { mask = $2; sub(".*/", "", mask); \
627 _addr="${1%/*}" # Remove optional maskbits
629 set -- $(ip_maskbits_iface "$_addr")
630 if [ -n "$1" ] ; then
633 echo "Removing public address $_addr/$_maskbits from device $_iface"
634 delete_ip_from_iface "$_iface" "$_addr" "$_maskbits" >/dev/null 2>&1
638 drop_all_public_ips ()
640 while read _ip _x ; do
642 done <"${CTDB_PUBLIC_ADDRESSES:-/dev/null}"
647 set_proc_maybe sys/net/ipv4/route/flush 1
648 set_proc_maybe sys/net/ipv6/route/flush 1
651 ########################################################
652 # Interface monitoring
654 # If the interface is a virtual one (e.g. VLAN) then get the
655 # underlying interface
656 interface_get_real ()
658 # Output of "ip link show <iface>"
661 # Extract the full interface description to see if it is a VLAN
662 _t=$(echo "$_iface_info" |
663 awk 'NR == 1 { iface = $2; sub(":$", "", iface) ; \
667 # VLAN: use the underlying interface, after the '@'
671 # Not a regular VLAN. For backward compatibility, assume
672 # there is some other sort of VLAN that doesn't have the
673 # '@' in the output and only use what is before a '.'. If
674 # there is no '.' then this will be the whole interface
680 # Check whether an interface is operational
685 _iface_info=$(ip link show "$_iface" 2>&1) || {
686 echo "ERROR: Monitored interface ${_iface} does not exist"
691 # If the interface is a virtual one (e.g. VLAN) then get the
692 # underlying interface.
693 _realiface=$(interface_get_real "$_iface_info")
695 if _bi=$(get_proc "net/bonding/${_realiface}" 2>/dev/null) ; then
696 # This is a bond: various monitoring strategies
697 echo "$_bi" | grep -q 'Currently Active Slave: None' && {
698 echo "ERROR: No active slaves for bond device ${_realiface}"
701 echo "$_bi" | grep -q '^MII Status: up' || {
702 echo "ERROR: public network interface ${_realiface} is down"
705 echo "$_bi" | grep -q '^Bonding Mode: IEEE 802.3ad Dynamic link aggregation' && {
706 # This works around a bug in the driver where the
707 # overall bond status can be up but none of the actual
708 # physical interfaces have a link.
709 echo "$_bi" | grep 'MII Status:' | tail -n +2 | grep -q '^MII Status: up' || {
710 echo "ERROR: No active slaves for 802.ad bond device ${_realiface}"
720 # loopback is always working
724 # we don't know how to test ib links
728 ethtool "$_iface" | grep -q 'Link detected: yes' || {
729 # On some systems, this is not successful when a
730 # cable is plugged but the interface has not been
731 # brought up previously. Bring the interface up
733 ip link set "$_iface" up
734 ethtool "$_iface" | grep -q 'Link detected: yes' || {
735 echo "ERROR: No link on the public network interface ${_iface}"
745 ########################################################
747 _ctdb_counter_common () {
748 _service_name="${1:-${service_name:-${script_name}}}"
749 _counter_file="${CTDB_SCRIPT_VARDIR}/failcount/${_service_name}"
750 mkdir -p "${_counter_file%/*}" # dirname
752 ctdb_counter_init () {
753 _ctdb_counter_common "$1"
757 ctdb_counter_incr () {
758 _ctdb_counter_common "$1"
761 echo -n 1 >> "$_counter_file"
763 ctdb_counter_get () {
764 _ctdb_counter_common "$1"
766 stat -c "%s" "$_counter_file" 2>/dev/null || echo 0
769 ########################################################
771 ctdb_setup_service_state_dir ()
773 service_state_dir="${CTDB_SCRIPT_VARDIR}/service_state/${1:-${service_name}}"
774 mkdir -p "$service_state_dir" || {
775 echo "Error creating state dir \"$service_state_dir\""
780 ########################################################
781 # Managed status history, for auto-start/stop
783 _ctdb_managed_common ()
785 _ctdb_managed_file="${CTDB_SCRIPT_VARDIR}/managed_history/${service_name}"
788 ctdb_service_managed ()
791 mkdir -p "${_ctdb_managed_file%/*}" # dirname
792 touch "$_ctdb_managed_file"
795 ctdb_service_unmanaged ()
798 rm -f "$_ctdb_managed_file"
801 is_ctdb_previously_managed_service ()
804 [ -f "$_ctdb_managed_file" ]
807 ##################################################################
808 # Reconfigure a service on demand
810 _ctdb_service_reconfigure_common ()
812 _d="${CTDB_SCRIPT_VARDIR}/service_status/${service_name}"
814 _ctdb_service_reconfigure_flag="$_d/reconfigure"
817 ctdb_service_needs_reconfigure ()
819 _ctdb_service_reconfigure_common
820 [ -e "$_ctdb_service_reconfigure_flag" ]
823 ctdb_service_set_reconfigure ()
825 _ctdb_service_reconfigure_common
826 >"$_ctdb_service_reconfigure_flag"
829 ctdb_service_unset_reconfigure ()
831 _ctdb_service_reconfigure_common
832 rm -f "$_ctdb_service_reconfigure_flag"
835 ctdb_service_reconfigure ()
837 echo "Reconfiguring service \"${service_name}\"..."
838 ctdb_service_unset_reconfigure
839 service_reconfigure || return $?
843 # Default service_reconfigure() function does nothing.
844 service_reconfigure ()
849 ctdb_reconfigure_take_lock ()
851 _ctdb_service_reconfigure_common
852 _lock="${_d}/reconfigure_lock"
853 mkdir -p "${_lock%/*}" # dirname
858 # This is overkill but will work if we need to extend this to
859 # allow certain events to run multiple times in parallel
860 # (e.g. takeip) and write multiple PIDs to the file.
862 if [ -n "$_locker_event" ] ; then
864 if [ -n "$_pid" -a "$_pid" != $$ ] && \
865 kill -0 "$_pid" 2>/dev/null ; then
871 printf "%s\n%s\n" "$event_name" $$ >"$_lock"
876 ctdb_reconfigure_release_lock ()
878 _ctdb_service_reconfigure_common
879 _lock="${_d}/reconfigure_lock"
884 ctdb_replay_monitor_status ()
886 echo "Replaying previous status for this script due to reconfigure..."
887 # Leading separator ('|') is missing in some versions...
888 _out=$($CTDB scriptstatus -X | grep -E "^\|?monitor\|${script_name}\|")
889 # Output looks like this:
890 # |monitor|60.nfs|1|ERROR|1314764004.030861|1314764004.035514|foo bar|
891 # This is the cheapest way of getting fields in the middle.
892 set -- $(IFS="|" ; echo $_out)
895 # The error output field can include colons so we'll try to
896 # preserve them. The weak checking at the beginning tries to make
897 # this work for both broken (no leading '|') and fixed output.
899 _err_out="${_out#*monitor|${script_name}|*|*|*|*|}"
901 OK) : ;; # Do nothing special.
903 # Recast this as an error, since we can't exit with the
904 # correct negative number.
906 _err_out="[Replay of TIMEDOUT scriptstatus - note incorrect return code.] ${_err_out}"
909 # Recast this as an OK, since we can't exit with the
910 # correct negative number.
912 _err_out="[Replay of DISABLED scriptstatus - note incorrect return code.] ${_err_out}"
914 *) : ;; # Must be ERROR, do nothing special.
916 if [ -n "$_err_out" ] ; then
922 ctdb_service_check_reconfigure ()
926 # We only care about some events in this function. For others we
928 case "$event_name" in
929 monitor|ipreallocated|reconfigure) : ;;
933 if ctdb_reconfigure_take_lock ; then
934 # No events covered by this function are running, so proceed
936 case "$event_name" in
938 (ctdb_service_reconfigure)
942 if ctdb_service_needs_reconfigure ; then
943 ctdb_service_reconfigure
948 ctdb_reconfigure_release_lock
950 # Somebody else is running an event we don't want to collide
951 # with. We proceed with caution.
952 case "$event_name" in
954 # Tell whoever called us to retry.
958 # Defer any scheduled reconfigure and just run the
959 # rest of the ipreallocated event, as per the
960 # eventscript. There's an assumption here that the
961 # event doesn't depend on any scheduled reconfigure.
962 # This is true in the current code.
966 # There is most likely a reconfigure in progress so
967 # the service is possibly unstable. As above, we
968 # defer any scheduled reconfigured. We also replay
969 # the previous monitor status since that's the best
970 # information we have.
971 ctdb_replay_monitor_status
977 ##################################################################
978 # Does CTDB manage this service? - and associated auto-start/stop
980 ctdb_compat_managed_service ()
982 if [ "$1" = "yes" -a "$2" = "$service_name" ] ; then
983 CTDB_MANAGED_SERVICES="$CTDB_MANAGED_SERVICES $2"
987 is_ctdb_managed_service ()
991 # $t is used just for readability and to allow better accurate
992 # matching via leading/trailing spaces
993 t=" $CTDB_MANAGED_SERVICES "
995 # Return 0 if "<space>$service_name<space>" appears in $t
996 if [ "${t#* ${service_name} }" != "${t}" ] ; then
1000 # If above didn't match then update $CTDB_MANAGED_SERVICES for
1001 # backward compatibility and try again.
1002 ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD" "vsftpd"
1003 ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA" "samba"
1004 ctdb_compat_managed_service "$CTDB_MANAGES_WINBIND" "winbind"
1005 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "apache2"
1006 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "httpd"
1007 ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI" "iscsi"
1008 ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD" "clamd"
1009 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs"
1011 t=" $CTDB_MANAGED_SERVICES "
1013 # Return 0 if "<space>$service_name<space>" appears in $t
1014 [ "${t#* ${service_name} }" != "${t}" ]
1017 ctdb_start_stop_service ()
1021 # Allow service-start/service-stop pseudo-events to start/stop
1022 # services when we're not auto-starting/stopping and we're not
1024 case "$event_name" in
1026 if is_ctdb_managed_service ; then
1027 die 'service-start event not permitted when service is managed'
1029 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1030 die 'service-start event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1036 if is_ctdb_managed_service ; then
1037 die 'service-stop event not permitted when service is managed'
1039 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1040 die 'service-stop event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1047 # Do nothing unless configured to...
1048 [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] || return 0
1050 [ "$event_name" = "monitor" ] || return 0
1052 if is_ctdb_managed_service ; then
1053 if ! is_ctdb_previously_managed_service ; then
1054 echo "Starting service \"$service_name\" - now managed"
1055 background_with_logging ctdb_service_start
1059 if is_ctdb_previously_managed_service ; then
1060 echo "Stopping service \"$service_name\" - no longer managed"
1061 background_with_logging ctdb_service_stop
1067 ctdb_service_start ()
1069 # The service is marked managed if we've ever tried to start it.
1070 ctdb_service_managed
1072 service_start || return $?
1078 ctdb_service_stop ()
1080 ctdb_service_unmanaged
1084 # Default service_start() and service_stop() functions.
1086 # These may be overridden in an eventscript.
1089 service "$service_name" start
1094 service "$service_name" stop
1097 ##################################################################
1099 # This exists only for backward compatibility with 3rd party scripts
1101 ctdb_standard_event_handler ()
1108 _family="$1" ; shift
1109 if [ "$_family" = "inet6" ] ; then
1110 _iptables_cmd="ip6tables"
1112 _iptables_cmd="iptables"
1115 # iptables doesn't like being re-entered, so flock-wrap it.
1116 flock -w 30 "${CTDB_SCRIPT_VARDIR}/iptables.flock" "$_iptables_cmd" "$@"
1119 # AIX (and perhaps others?) doesn't have mktemp
1120 if ! type mktemp >/dev/null 2>&1 ; then
1124 if [ "$1" = "-d" ] ; then
1128 _d="${TMPDIR:-/tmp}"
1129 _hex10=$(dd if=/dev/urandom count=20 2>/dev/null | \
1131 sed -e 's@\(..........\).*@\1@')
1132 _t="${_d}/tmp.${_hex10}"
1145 ######################################################################
1146 # NFS callout handling
1150 if [ -z "$CTDB_NFS_CALLOUT" ] ; then
1151 CTDB_NFS_CALLOUT="${CTDB_BASE}/nfs-linux-kernel-callout"
1153 # Always export, for statd callout
1154 export CTDB_NFS_CALLOUT
1156 # If the callout wants to use this then it must create it
1157 export CTDB_NFS_CALLOUT_STATE_DIR="${service_state_dir}/callout-state"
1159 # Export, if set, for use by clustered NFS callouts
1160 if [ -n "$CTDB_NFS_STATE_FS_TYPE" ] ; then
1161 export CTDB_NFS_STATE_FS_TYPE
1163 if [ -n "$CTDB_NFS_STATE_MNT" ] ; then
1164 export CTDB_NFS_STATE_MNT
1167 nfs_callout_cache="${service_state_dir}/nfs_callout_cache"
1168 nfs_callout_cache_callout="${nfs_callout_cache}/CTDB_NFS_CALLOUT"
1169 nfs_callout_cache_ops="${nfs_callout_cache}/ops"
1172 nfs_callout_register ()
1174 mkdir -p "$nfs_callout_cache_ops"
1175 rm -f "$nfs_callout_cache_ops"/*
1177 echo "$CTDB_NFS_CALLOUT" >"$nfs_callout_cache_callout"
1179 _t=$(eval "$CTDB_NFS_CALLOUT" "register")
1180 if [ -n "$_t" ] ; then
1182 while IFS="" read _op ; do
1183 touch "${nfs_callout_cache_ops}/${_op}"
1186 touch "${nfs_callout_cache_ops}/ALL"
1192 # Re-run registration if $CTDB_NFS_CALLOUT has changed
1194 if [ -r "$nfs_callout_cache_callout" ] ; then
1195 read _prev <"$nfs_callout_cache_callout"
1197 if [ "$CTDB_NFS_CALLOUT" != "$_prev" ] ; then
1198 nfs_callout_register
1201 # Run the operation if it is registered...
1202 if [ -e "${nfs_callout_cache_ops}/${1}" ] || \
1203 [ -e "${nfs_callout_cache_ops}/ALL" ]; then
1204 eval "$CTDB_NFS_CALLOUT" "$@"
1208 ########################################################
1210 ########################################################
1216 tickledir="${CTDB_SCRIPT_VARDIR}/tickles"
1217 mkdir -p "$tickledir"
1221 # What public IPs do I hold?
1222 _ips=$($CTDB -X ip | awk -F'|' -v pnn="$pnn" '$3 == pnn {print $2}')
1224 # IPs and port as ss filters
1226 for _ip in $_ips ; do
1227 _ip_filter="${_ip_filter}${_ip_filter:+ || }src [${_ip}]"
1229 _port_filter="sport == :${_port}"
1231 # Record connections to our public IPs in a temporary file.
1232 # This temporary file is in CTDB's private state directory and
1233 # $$ is used to avoid a very rare race involving CTDB's script
1234 # debugging. No security issue, nothing to see here...
1235 _my_connections="${tickledir}/${_port}.connections.$$"
1236 # Parentheses are needed around the filters for precedence but
1237 # the parentheses can't be empty!
1238 ss -tn state established \
1239 "${_ip_filter:+( ${_ip_filter} )}" \
1240 "${_port_filter:+( ${_port_filter} )}" |
1241 awk 'NR > 1 {print $4, $3}' |
1242 sort >"$_my_connections"
1244 # Record our current tickles in a temporary file
1245 _my_tickles="${tickledir}/${_port}.tickles.$$"
1246 for _i in $_ips ; do
1247 $CTDB -X gettickles "$_i" "$_port" |
1248 awk -F'|' 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
1250 sort >"$_my_tickles"
1252 # Add tickles for connections that we haven't already got tickles for
1253 comm -23 "$_my_connections" "$_my_tickles" |
1254 while read _src _dst ; do
1255 $CTDB addtickle "$_src" "$_dst"
1258 # Remove tickles for connections that are no longer there
1259 comm -13 "$_my_connections" "$_my_tickles" |
1260 while read _src _dst ; do
1261 $CTDB deltickle "$_src" "$_dst"
1264 rm -f "$_my_connections" "$_my_tickles"
1266 # Remove stale files from killed scripts
1267 find "$tickledir" -type f -mmin +10 | xargs -r rm
1270 ########################################################
1271 # load a site local config file
1272 ########################################################
1274 [ -n "$CTDB_RC_LOCAL" -a -x "$CTDB_RC_LOCAL" ] && {
1278 [ -x "${CTDB_BASE}/rc.local" ] && {
1279 . "${CTDB_BASE}/rc.local"
1282 [ -d "${CTDB_BASE}/rc.local.d" ] && {
1283 for i in "${CTDB_BASE}/rc.local.d"/* ; do
1284 [ -x "$i" ] && . "$i"
1288 script_name="${0##*/}" # basename