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 if [ "$CTDB_SYSLOG" = "yes" ] ; then
90 logger -t "ctdbd: ${_tag}" $*
98 } >>"${CTDB_LOGFILE:-/var/log/log.ctdb}"
102 # When things are run in the background in an eventscript then logging
103 # output might get lost. This is the "solution". :-)
104 background_with_logging ()
107 "$@" 2>&1 </dev/null |
108 script_log "${script_name}&"
114 ##############################################################
115 # check number of args for different events
121 echo "ERROR: must supply interface, IP and maskbits"
127 echo "ERROR: must supply old interface, new interface, IP and maskbits"
134 ##############################################################
135 # determine on what type of system (init style) we are running
136 detect_init_style() {
137 # only do detection if not already set:
138 test "x$CTDB_INIT_STYLE" != "x" && return
140 if [ -x /sbin/startproc ]; then
141 CTDB_INIT_STYLE="suse"
142 elif [ -x /sbin/start-stop-daemon ]; then
143 CTDB_INIT_STYLE="debian"
145 CTDB_INIT_STYLE="redhat"
149 ######################################################
150 # simulate /sbin/service on platforms that don't have it
151 # _service() makes it easier to hook the service() function for
158 # do nothing, when no service was specified
159 [ -z "$_service_name" ] && return
161 if [ -x /sbin/service ]; then
162 $_nice /sbin/service "$_service_name" "$_op"
163 elif [ -x $CTDB_ETCDIR/init.d/$_service_name ]; then
164 $_nice $CTDB_ETCDIR/init.d/$_service_name "$_op"
165 elif [ -x $CTDB_ETCDIR/rc.d/init.d/$_service_name ]; then
166 $_nice $CTDB_ETCDIR/rc.d/init.d/$_service_name "$_op"
176 ######################################################
177 # simulate /sbin/service (niced) on platforms that don't have it
184 ######################################################
185 # wrapper around /proc/ settings to allow them to be hooked
187 # 1st arg is relative path under /proc/, 2nd arg is value to set
190 echo "$2" >"/proc/$1"
193 ######################################################
194 # wrapper around getting file contents from /proc/ to allow
195 # this to be hooked for testing
196 # 1st arg is relative path under /proc/
202 ######################################################
203 # Check that an RPC service is healthy -
204 # this includes allowing a certain number of failures
205 # before marking the NFS service unhealthy.
207 # usage: nfs_check_rpc_service SERVICE_NAME [ triple ...]
209 # each triple is a set of 3 arguments: an operator, a
210 # fail count limit and an action string.
214 # nfs_check_rpc_service "lockd" \
215 # -ge 15 "verbose restart unhealthy" \
216 # -eq 10 "restart:bs"
218 # says that if lockd is down for 15 iterations then do
219 # a verbose restart of lockd and mark the node unhealthy.
220 # Before this, after 10 iterations of failure, the
221 # service is restarted silently in the background.
222 # Order is important: the number of failures need to be
223 # specified in reverse order because processing stops
224 # after the first condition that is true.
225 ######################################################
226 nfs_check_rpc_service ()
228 _prog_name="$1" ; shift
230 if _nfs_check_rpc_common "$_prog_name" ; then
234 while [ -n "$3" ] ; do
235 if _nfs_check_rpc_action "$1" "$2" "$3" ; then
242 # The new way of doing things...
243 nfs_check_rpc_services ()
245 # Files must end with .check - avoids editor backups, RPM fu, ...
246 for _f in "${CTDB_BASE}/nfs-rpc-checks.d/"[0-9][0-9].*.check ; do
248 _prog_name="${_t##*/[0-9][0-9].}"
250 if _nfs_check_rpc_common "$_prog_name" ; then
251 # This RPC service is up, check next service...
255 # Check each line in the file in turn until one of the limit
257 while read _cmp _lim _rest ; do
263 if _nfs_check_rpc_action "$_cmp" "$_lim" "$_rest" ; then
264 # Limit was hit on this line, no further checking...
271 _nfs_check_rpc_common ()
275 # Some platforms don't have separate programs for all services.
276 case "$_prog_name" in
278 which "rpc.${_prog_name}" >/dev/null 2>&1 || return 0
281 case "$_prog_name" in
303 echo "Internal error: unknown RPC program \"$_prog_name\"."
307 _service_name="nfs_${_prog_name}"
309 if ctdb_check_rpc "$_rpc_prog" $_version >/dev/null ; then
310 ctdb_counter_init "$_service_name"
314 ctdb_counter_incr "$_service_name"
319 _nfs_check_rpc_action ()
325 if ctdb_check_counter "quiet" "$_cmp" "$_limit" "$_service_name" ; then
329 for _action in $_actions ; do
332 echo "$ctdb_check_rpc_out"
335 _nfs_restart_rpc_service "$_prog_name"
338 _nfs_restart_rpc_service "$_prog_name" true
344 echo "Internal error: unknown action \"$_action\"."
352 _nfs_restart_rpc_service ()
355 _background="${2:-false}"
357 if $_background ; then
358 _maybe_background="background_with_logging"
363 _p="rpc.${_prog_name}"
365 case "$_prog_name" in
367 echo "Trying to restart NFS service"
368 $_maybe_background startstop_nfs restart
371 echo "Trying to restart $_prog_name [${_p}]"
373 $_maybe_background $_p ${MOUNTD_PORT:+-p} $MOUNTD_PORT
376 echo "Trying to restart $_prog_name [${_p}]"
378 $_maybe_background $_p ${RQUOTAD_PORT:+-p} $RQUOTAD_PORT
381 echo "Trying to restart lock manager service"
382 $_maybe_background startstop_nfslock restart
385 echo "Trying to restart $_prog_name [${_p}]"
387 $_maybe_background $_p \
388 ${STATD_HOSTNAME:+-n} $STATD_HOSTNAME \
389 ${STATD_PORT:+-p} $STATD_PORT \
390 ${STATD_OUTGOING_PORT:+-o} $STATD_OUTGOING_PORT
393 echo "Internal error: unknown RPC program \"$_prog_name\"."
398 ######################################################
399 # check that a rpc server is registered with portmap
400 # and responding to requests
401 # usage: ctdb_check_rpc SERVICE_NAME VERSION
402 ######################################################
408 _localhost="${CTDB_RPCINFO_LOCALHOST:-127.0.0.1}"
410 if ! ctdb_check_rpc_out=$(rpcinfo -u $_localhost $progname $version 2>&1) ; then
411 ctdb_check_rpc_out="ERROR: $progname failed RPC check:
413 echo "$ctdb_check_rpc_out"
418 ######################################################
419 # Ensure $service_name is set
420 assert_service_name ()
422 [ -n "$service_name" ] || die "INTERNAL ERROR: \$service_name not set"
425 ######################################################
426 # check a set of directories is available
427 # return 1 on a missing directory
428 # directories are read from stdin
429 ######################################################
430 ctdb_check_directories_probe()
432 while IFS="" read d ; do
438 [ -d "${d}/." ] || return 1
443 ######################################################
444 # check a set of directories is available
445 # directories are read from stdin
446 ######################################################
447 ctdb_check_directories()
449 ctdb_check_directories_probe || {
450 echo "ERROR: $service_name directory \"$d\" not available"
455 ######################################################
456 # check a set of tcp ports
457 # usage: ctdb_check_tcp_ports <ports...>
458 ######################################################
460 # This flag file is created when a service is initially started. It
461 # is deleted the first time TCP port checks for that service succeed.
462 # Until then ctdb_check_tcp_ports() prints a more subtle "error"
463 # message if a port check fails.
464 _ctdb_check_tcp_common ()
467 _ctdb_service_started_file="$ctdb_fail_dir/$service_name.started"
470 ctdb_check_tcp_init ()
472 _ctdb_check_tcp_common
473 mkdir -p "${_ctdb_service_started_file%/*}" # dirname
474 touch "$_ctdb_service_started_file"
477 ctdb_check_tcp_ports()
479 if [ -z "$1" ] ; then
480 echo "INTERNAL ERROR: ctdb_check_tcp_ports - no ports specified"
484 # Set default value for CTDB_TCP_PORT_CHECKERS if unset.
485 # If any of these defaults are unsupported then this variable can
486 # be overridden in /etc/sysconfig/ctdb or via a file in
487 # /etc/ctdb/rc.local.d/.
488 : ${CTDB_TCP_PORT_CHECKERS:=ctdb nmap netstat}
490 for _c in $CTDB_TCP_PORT_CHECKERS ; do
491 ctdb_check_tcp_ports_$_c "$@"
494 _ctdb_check_tcp_common
495 rm -f "$_ctdb_service_started_file"
499 _ctdb_check_tcp_common
500 if [ ! -f "$_ctdb_service_started_file" ] ; then
501 echo "ERROR: $service_name tcp port $_p is not responding"
503 $ctdb_check_tcp_ports_debug
506 echo "INFO: $service_name tcp port $_p is not responding"
513 ctdb_check_ports - checker $_c not implemented
514 output from checker was:
515 $ctdb_check_tcp_ports_debug
523 echo "INTERNAL ERROR: ctdb_check_ports - no working checkers in CTDB_TCP_PORT_CHECKERS=\"$CTDB_TCP_PORT_CHECKERS\""
528 ctdb_check_tcp_ports_netstat ()
530 _cmd='netstat -l -t -n'
532 if [ $? -eq 127 ] ; then
533 # netstat probably not installed - unlikely?
534 ctdb_check_tcp_ports_debug="$_ns"
538 for _p ; do # process each function argument (port)
539 for _a in '0\.0\.0\.0' '::' ; do
540 _pat="[[:space:]]${_a}:${_p}[[:space:]]+[^[:space:]]+[[:space:]]+LISTEN"
541 if echo "$_ns" | grep -E -q "$_pat" ; then
542 # We matched the port, so process next port
547 # We didn't match the port, so flag an error.
548 ctdb_check_tcp_ports_debug="$_cmd shows this output:
556 ctdb_check_tcp_ports_nmap ()
558 # nmap wants a comma-separated list of ports
561 _ports="${_ports}${_ports:+,}${_p}"
564 _cmd="nmap -n -oG - -PS 127.0.0.1 -p $_ports"
566 _nmap_out=$($_cmd 2>&1)
567 if [ $? -eq 127 ] ; then
568 # nmap probably not installed
569 ctdb_check_tcp_ports_debug="$_nmap_out"
573 # get the port-related output
574 _port_info=$(echo "$_nmap_out" | sed -n -r -e 's@^.*Ports:[[:space:]]@@p')
577 # looking for something like this:
578 # 445/open/tcp//microsoft-ds///
579 # possibly followed by a comma
581 case "$_port_info" in
582 # The info we're after must be either at the beginning of
583 # the string or it must follow a space.
586 # Nope, flag an error...
587 ctdb_check_tcp_ports_debug="$_cmd shows this output:
596 # Use the new "ctdb checktcpport" command to check the port.
597 # This is very cheap.
598 ctdb_check_tcp_ports_ctdb ()
600 for _p ; do # process each function argument (port)
601 _cmd="ctdb checktcpport $_p"
606 ctdb_check_tcp_ports_debug="\"$_cmd\" was able to bind to port"
610 # Couldn't bind, something already listening, next port...
614 ctdb_check_tcp_ports_debug="$_cmd (exited with $_ret) with output:
616 # assume not implemented
624 ######################################################
625 # check a unix socket
626 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
627 ######################################################
628 ctdb_check_unix_socket() {
630 [ -z "$socket_path" ] && return
632 if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
633 echo "ERROR: $service_name socket $socket_path not found"
638 ######################################################
639 # check a command returns zero status
640 # usage: ctdb_check_command <command>
641 ######################################################
642 ctdb_check_command ()
644 _out=$("$@" 2>&1) || {
645 echo "ERROR: $* returned error"
651 ################################################
652 # kill off any TCP connections with the given IP
653 ################################################
654 kill_tcp_connections ()
659 if [ "$2" = "oneway" ] ; then
663 get_tcp_connections_for_ip "$_ip" | {
668 while read _dst _src; do
669 _destport="${_dst##*:}"
672 # we only do one-way killtcp for CIFS
673 139|445) __oneway=true ;;
676 echo "Killing TCP connection $_src $_dst"
677 _connections="${_connections}${_nl}${_src} ${_dst}"
678 if ! $__oneway ; then
679 _connections="${_connections}${_nl}${_dst} ${_src}"
682 _killcount=$(($_killcount + 1))
685 if [ $_killcount -eq 0 ] ; then
689 echo "$_connections" | ctdb killtcp || {
690 echo "Failed to send killtcp control"
696 _remaining=$(get_tcp_connections_for_ip $_ip | wc -l)
698 if [ $_remaining -eq 0 ] ; then
699 echo "Killed $_killcount TCP connections to released IP $_ip"
703 _count=$(($_count + 1))
704 if [ $_count -gt 3 ] ; then
705 echo "Timed out killing tcp connections for IP $_ip"
709 echo "Waiting for $_remaining connections to be killed for IP $_ip"
715 ##################################################################
716 # kill off the local end for any TCP connections with the given IP
717 ##################################################################
718 kill_tcp_connections_local_only ()
720 kill_tcp_connections "$1" "oneway"
723 ##################################################################
724 # tickle any TCP connections with the given IP
725 ##################################################################
726 tickle_tcp_connections ()
730 get_tcp_connections_for_ip "$_ip" |
734 while read dest src; do
735 echo "Tickle TCP connection $src $dest"
736 ctdb tickle $src $dest >/dev/null 2>&1 || _failed=true
737 echo "Tickle TCP connection $dest $src"
738 ctdb tickle $dest $src >/dev/null 2>&1 || _failed=true
742 echo "Failed to send tickle control"
747 get_tcp_connections_for_ip ()
751 netstat -tn | awk -v ip=$_ip \
752 'index($1, "tcp") == 1 && \
753 (index($4, ip ":") == 1 || index($4, "::ffff:" ip ":") == 1) \
754 && $6 == "ESTABLISHED" \
758 ########################################################
759 # start/stop the Ganesha nfs service
760 ########################################################
763 _service_name="nfs-ganesha-$CTDB_CLUSTER_FILESYSTEM_TYPE"
766 service "$_service_name" start
769 service "$_service_name" stop
772 service "$_service_name" restart
777 ########################################################
778 # start/stop the nfs service on different platforms
779 ########################################################
782 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
785 [ -x $CTDB_ETCDIR/init.d/nfslock ] && {
793 service nfsserver start
796 service nfsserver stop > /dev/null 2>&1
799 set_proc "fs/nfsd/threads" 0
800 service nfsserver stop > /dev/null 2>&1
802 nfs_dump_some_threads
803 service nfsserver start
810 service nfslock start
818 set_proc "fs/nfsd/threads" 0
819 service nfs stop > /dev/null 2>&1
820 service nfslock stop > /dev/null 2>&1
822 nfs_dump_some_threads
823 service nfslock start
829 echo "Unknown platform. NFS is not supported with ctdb"
835 # Dump up to the configured number of nfsd thread backtraces.
836 nfs_dump_some_threads ()
838 [ -n "$CTDB_NFS_DUMP_STUCK_THREADS" ] || return 0
840 # Optimisation to avoid running an unnecessary pidof
841 [ $CTDB_NFS_DUMP_STUCK_THREADS -gt 0 ] || return 0
844 for _pid in $(pidof nfsd) ; do
845 [ $_count -le $CTDB_NFS_DUMP_STUCK_THREADS ] || break
847 # Do this first to avoid racing with thread exit
848 _stack=$(get_proc "${_pid}/stack" 2>/dev/null)
849 if [ -n "$_stack" ] ; then
850 echo "Stack trace for stuck nfsd thread [${_pid}]:"
852 _count=$(($_count + 1))
857 ########################################################
858 # start/stop the nfs lockmanager service on different platforms
859 ########################################################
860 startstop_nfslock() {
862 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
865 [ -x $CTDB_ETCDIR/init.d/nfslock ] && {
871 # for sles there is no service for lockmanager
872 # so we instead just shutdown/restart nfs
875 service nfsserver start
878 service nfsserver stop > /dev/null 2>&1
881 service nfsserver stop > /dev/null 2>&1
882 service nfsserver start
889 service nfslock start
892 service nfslock stop > /dev/null 2>&1
895 service nfslock stop > /dev/null 2>&1
896 service nfslock start
901 echo "Unknown platform. NFS locking is not supported with ctdb"
907 # Periodically update the statd database
912 _statd_update_trigger="$service_state_dir/update-trigger"
913 [ -f "$_statd_update_trigger" ] || touch "$_statd_update_trigger"
915 _last_update=$(stat --printf="%Y" "$_statd_update_trigger")
916 _current_time=$(date +"%s")
917 if [ $(( $_current_time - $_last_update)) -ge $_update_period ] ; then
918 touch "$_statd_update_trigger"
919 $CTDB_BASE/statd-callout updatelocal &
920 $CTDB_BASE/statd-callout updateremote &
930 _lockfile="${CTDB_VARDIR}/state/interface_modify_${_iface}.flock"
931 mkdir -p "${_lockfile%/*}" # dirname
932 [ -f "$_lockfile" ] || touch "$_lockfile"
935 # Note: use of return/exit/die() below only gets us out of the
936 # sub-shell, which is actually what we want. That is, the
937 # function should just return non-zero.
939 flock --timeout 30 0 || \
940 die "add_ip_to_iface: unable to get lock for ${_iface}"
942 # Ensure interface is up
943 ip link set "$_iface" up || \
944 die "Failed to bringup interface $_iface"
946 ip addr add "$_ip/$_maskbits" brd + dev "$_iface" || \
947 die "Failed to add $_ip/$_maskbits on dev $_iface"
950 # Do nothing here - return above only gets us out of the subshell
951 # and doing anything here will affect the return code.
954 delete_ip_from_iface()
960 _lockfile="${CTDB_VARDIR}/state/interface_modify_${_iface}.flock"
961 mkdir -p "${_lockfile%/*}" # dirname
962 [ -f "$_lockfile" ] || touch "$_lockfile"
965 # Note: use of return/exit/die() below only gets us out of the
966 # sub-shell, which is actually what we want. That is, the
967 # function should just return non-zero.
969 flock --timeout 30 0 || \
970 die "delete_ip_from_iface: unable to get lock for ${_iface}"
972 _im="$_ip/$_maskbits" # shorthand for readability
974 # "ip addr del" will delete all secondary IPs if this is the
975 # primary. To work around this _very_ annoying behaviour we
976 # have to keep a record of the secondaries and re-add them
980 if ip addr list dev "$_iface" primary | grep -Fq "inet $_im " ; then
981 _secondaries=$(ip addr list dev "$_iface" secondary | \
982 awk '$1 == "inet" { print $2 }')
986 ip addr del "$_im" dev "$_iface" || {
987 echo "Failed to del $_ip on dev $_iface"
991 if [ -n "$_secondaries" ] ; then
992 for _i in $_secondaries; do
993 if ip addr list dev "$_iface" | grep -Fq "inet $_i" ; then
994 echo "Kept secondary $_i on dev $_iface"
996 echo "Re-adding secondary address $_i to dev $_iface"
997 ip addr add $_i brd + dev $_iface || {
998 echo "Failed to re-add address $_i to dev $_iface"
1008 # Do nothing here - return above only gets us out of the subshell
1009 # and doing anything here will affect the return code.
1012 # If the given IP is hosted then print 2 items: maskbits and iface
1013 ip_maskbits_iface ()
1017 ip addr show to "${_addr}/32" 2>/dev/null | \
1018 awk '$1 == "inet" { print gensub(".*/", "", 1, $2), $NF }'
1023 _addr="${1%/*}" # Remove optional maskbits
1025 set -- $(ip_maskbits_iface $_addr)
1026 if [ -n "$1" ] ; then
1029 echo "Removing public address $_addr/$_maskbits from device $_iface"
1030 delete_ip_from_iface $_iface $_addr $_maskbits >/dev/null 2>&1
1034 drop_all_public_ips ()
1036 while read _ip _x ; do
1038 done <"${CTDB_PUBLIC_ADDRESSES:-/dev/null}"
1041 ########################################################
1043 _ctdb_counter_common () {
1044 _service_name="${1:-${service_name:-${script_name}}}"
1045 _counter_file="$ctdb_fail_dir/$_service_name"
1046 mkdir -p "${_counter_file%/*}" # dirname
1048 ctdb_counter_init () {
1049 _ctdb_counter_common "$1"
1053 ctdb_counter_incr () {
1054 _ctdb_counter_common "$1"
1057 echo -n 1 >> "$_counter_file"
1059 ctdb_check_counter () {
1060 _msg="${1:-error}" # "error" - anything else is silent on fail
1061 _op="${2:--ge}" # an integer operator supported by test
1062 _limit="${3:-${service_fail_limit}}"
1064 _ctdb_counter_common "$1"
1067 _size=$(stat -c "%s" "$_counter_file" 2>/dev/null || echo 0)
1069 if [ "$_op" != "%" ] ; then
1070 if [ $_size $_op $_limit ] ; then
1074 if [ $(($_size $_op $_limit)) -eq 0 ] ; then
1079 if [ "$_msg" = "error" ] ; then
1080 echo "ERROR: $_size consecutive failures for $_service_name, marking node unhealthy"
1088 ########################################################
1090 ctdb_status_dir="$CTDB_VARDIR/status"
1091 ctdb_fail_dir="$CTDB_VARDIR/failcount"
1093 ctdb_setup_service_state_dir ()
1095 service_state_dir="$CTDB_VARDIR/state/${1:-${service_name}}"
1096 mkdir -p "$service_state_dir" || {
1097 echo "Error creating state dir \"$service_state_dir\""
1102 ########################################################
1103 # Managed status history, for auto-start/stop
1105 ctdb_managed_dir="$CTDB_VARDIR/managed_history"
1107 _ctdb_managed_common ()
1109 _ctdb_managed_file="$ctdb_managed_dir/$service_name"
1112 ctdb_service_managed ()
1114 _ctdb_managed_common
1115 mkdir -p "$ctdb_managed_dir"
1116 touch "$_ctdb_managed_file"
1119 ctdb_service_unmanaged ()
1121 _ctdb_managed_common
1122 rm -f "$_ctdb_managed_file"
1125 is_ctdb_previously_managed_service ()
1127 _ctdb_managed_common
1128 [ -f "$_ctdb_managed_file" ]
1131 ########################################################
1132 # Check and set status
1136 echo "node is \"$1\", \"${script_name}\" reports problem: $(cat $2)"
1141 if [ -r "$ctdb_status_dir/$script_name/unhealthy" ] ; then
1142 log_status_cat "unhealthy" "$ctdb_status_dir/$script_name/unhealthy"
1144 elif [ -r "$ctdb_status_dir/$script_name/banned" ] ; then
1145 log_status_cat "banned" "$ctdb_status_dir/$script_name/banned"
1154 d="$ctdb_status_dir/$script_name"
1161 for i in "banned" "unhealthy" ; do
1168 ##################################################################
1169 # Reconfigure a service on demand
1171 _ctdb_service_reconfigure_common ()
1173 _d="$ctdb_status_dir/${service_name}"
1175 _ctdb_service_reconfigure_flag="$_d/reconfigure"
1178 ctdb_service_needs_reconfigure ()
1180 _ctdb_service_reconfigure_common
1181 [ -e "$_ctdb_service_reconfigure_flag" ]
1184 ctdb_service_set_reconfigure ()
1186 _ctdb_service_reconfigure_common
1187 >"$_ctdb_service_reconfigure_flag"
1190 ctdb_service_unset_reconfigure ()
1192 _ctdb_service_reconfigure_common
1193 rm -f "$_ctdb_service_reconfigure_flag"
1196 ctdb_service_reconfigure ()
1198 echo "Reconfiguring service \"${service_name}\"..."
1199 ctdb_service_unset_reconfigure
1200 service_reconfigure || return $?
1204 # Default service_reconfigure() function does nothing.
1205 service_reconfigure ()
1210 ctdb_reconfigure_try_lock ()
1212 _ctdb_service_reconfigure_common
1213 _lock="${_d}/reconfigure_lock"
1214 mkdir -p "${_lock%/*}" # dirname
1219 # This is overkill but will work if we need to extend this to
1220 # allow certain events to run multiple times in parallel
1221 # (e.g. takeip) and write multiple PIDs to the file.
1223 if [ -n "$_locker_event" ] ; then
1224 while read _pid ; do
1225 if [ -n "$_pid" -a "$_pid" != $$ ] && \
1226 kill -0 "$_pid" 2>/dev/null ; then
1232 printf "%s\n%s\n" "$event_name" $$ >"$_lock"
1237 ctdb_replay_monitor_status ()
1239 echo "Replaying previous status for this script due to reconfigure..."
1240 # Leading colon (':') is missing in some versions...
1241 _out=$(ctdb scriptstatus -Y | grep -E "^:?monitor:${script_name}:")
1242 # Output looks like this:
1243 # :monitor:60.nfs:1:ERROR:1314764004.030861:1314764004.035514:foo bar:
1244 # This is the cheapest way of getting fields in the middle.
1245 set -- $(IFS=":" ; echo $_out)
1248 # The error output field can include colons so we'll try to
1249 # preserve them. The weak checking at the beginning tries to make
1250 # this work for both broken (no leading ':') and fixed output.
1252 _err_out="${_out#*monitor:${script_name}:*:*:*:*:}"
1254 OK) : ;; # Do nothing special.
1256 # Recast this as an error, since we can't exit with the
1257 # correct negative number.
1259 _err_out="[Replay of TIMEDOUT scriptstatus - note incorrect return code.] ${_err_out}"
1262 # Recast this as an OK, since we can't exit with the
1263 # correct negative number.
1265 _err_out="[Replay of DISABLED scriptstatus - note incorrect return code.] ${_err_out}"
1267 *) : ;; # Must be ERROR, do nothing special.
1269 if [ -n "$_err_out" ] ; then
1275 ctdb_service_check_reconfigure ()
1279 # We only care about some events in this function. For others we
1281 case "$event_name" in
1282 monitor|ipreallocated|reconfigure) : ;;
1286 if ctdb_reconfigure_try_lock ; then
1287 # No events covered by this function are running, so proceed
1289 case "$event_name" in
1291 (ctdb_service_reconfigure)
1295 if ctdb_service_needs_reconfigure ; then
1296 ctdb_service_reconfigure
1300 if ctdb_service_needs_reconfigure ; then
1301 ctdb_service_reconfigure
1302 # Given that the reconfigure might not have
1303 # resulted in the service being stable yet, we
1304 # replay the previous status since that's the best
1305 # information we have.
1306 ctdb_replay_monitor_status
1311 # Somebody else is running an event we don't want to collide
1312 # with. We proceed with caution.
1313 case "$event_name" in
1315 # Tell whoever called us to retry.
1319 # Defer any scheduled reconfigure and just run the
1320 # rest of the ipreallocated event, as per the
1321 # eventscript. There's an assumption here that the
1322 # event doesn't depend on any scheduled reconfigure.
1323 # This is true in the current code.
1327 # There is most likely a reconfigure in progress so
1328 # the service is possibly unstable. As above, we
1329 # defer any scheduled reconfigured. We also replay
1330 # the previous monitor status since that's the best
1331 # information we have.
1332 ctdb_replay_monitor_status
1338 ##################################################################
1339 # Does CTDB manage this service? - and associated auto-start/stop
1341 ctdb_compat_managed_service ()
1343 if [ "$1" = "yes" -a "$2" = "$service_name" ] ; then
1344 CTDB_MANAGED_SERVICES="$CTDB_MANAGED_SERVICES $2"
1348 is_ctdb_managed_service ()
1352 # $t is used just for readability and to allow better accurate
1353 # matching via leading/trailing spaces
1354 t=" $CTDB_MANAGED_SERVICES "
1356 # Return 0 if "<space>$service_name<space>" appears in $t
1357 if [ "${t#* ${service_name} }" != "${t}" ] ; then
1361 # If above didn't match then update $CTDB_MANAGED_SERVICES for
1362 # backward compatibility and try again.
1363 ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD" "vsftpd"
1364 ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA" "samba"
1365 ctdb_compat_managed_service "$CTDB_MANAGES_WINBIND" "winbind"
1366 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "apache2"
1367 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "httpd"
1368 ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI" "iscsi"
1369 ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD" "clamd"
1370 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs"
1371 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs-ganesha-gpfs"
1373 t=" $CTDB_MANAGED_SERVICES "
1375 # Return 0 if "<space>$service_name<space>" appears in $t
1376 [ "${t#* ${service_name} }" != "${t}" ]
1379 ctdb_start_stop_service ()
1383 # Allow service-start/service-stop pseudo-events to start/stop
1384 # services when we're not auto-starting/stopping and we're not
1386 case "$event_name" in
1388 if is_ctdb_managed_service ; then
1389 die 'service-start event not permitted when service is managed'
1391 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1392 die 'service-start event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1398 if is_ctdb_managed_service ; then
1399 die 'service-stop event not permitted when service is managed'
1401 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1402 die 'service-stop event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1409 # Do nothing unless configured to...
1410 [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] || return 0
1412 [ "$event_name" = "monitor" ] || return 0
1414 if is_ctdb_managed_service ; then
1415 if ! is_ctdb_previously_managed_service ; then
1416 echo "Starting service \"$service_name\" - now managed"
1417 background_with_logging ctdb_service_start
1421 if is_ctdb_previously_managed_service ; then
1422 echo "Stopping service \"$service_name\" - no longer managed"
1423 background_with_logging ctdb_service_stop
1429 ctdb_service_start ()
1431 # The service is marked managed if we've ever tried to start it.
1432 ctdb_service_managed
1434 service_start || return $?
1440 ctdb_service_stop ()
1442 ctdb_service_unmanaged
1446 # Default service_start() and service_stop() functions.
1448 # These may be overridden in an eventscript. When overriding, the
1449 # following convention must be followed. If these functions are
1450 # called with no arguments then they may use internal logic to
1451 # determine whether the service is managed and, therefore, whether
1452 # they should take any action. However, if the service name is
1453 # specified as an argument then an attempt must be made to start or
1454 # stop the service. This is because the auto-start/stop code calls
1455 # them with the service name as an argument.
1458 service "$service_name" start
1463 service "$service_name" stop
1466 ##################################################################
1468 ctdb_standard_event_handler ()
1483 # iptables doesn't like being re-entered, so flock-wrap it.
1486 flock -w 30 $CTDB_VARDIR/iptables-ctdb.flock /sbin/iptables "$@"
1489 # AIX (and perhaps others?) doesn't have mktemp
1490 if ! which mktemp >/dev/null 2>&1 ; then
1494 if [ "$1" = "-d" ] ; then
1498 _d="${TMPDIR:-/tmp}"
1499 _hex10=$(dd if=/dev/urandom count=20 2>/dev/null | \
1501 sed -e 's@\(..........\).*@\1@')
1502 _t="${_d}/tmp.${_hex10}"
1515 ########################################################
1517 ########################################################
1523 tickledir="$CTDB_VARDIR/state/tickles"
1524 mkdir -p "$tickledir"
1527 _pnn=$(ctdb pnn) ; _pnn=${_pnn#PNN:}
1529 # What public IPs do I hold?
1530 _ips=$(ctdb -Y ip | awk -F: -v pnn=$_pnn '$3 == pnn {print $2}')
1532 # IPs as a regexp choice
1533 _ipschoice="($(echo $_ips | sed -e 's/ /|/g' -e 's/\./\\\\./g'))"
1535 # Record connections to our public IPs in a temporary file
1536 _my_connections="${tickledir}/${_port}.connections"
1537 rm -f "$_my_connections"
1539 awk -v destpat="^${_ipschoice}:${_port}\$" \
1540 '$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ destpat {print $5, $4}' |
1541 sort >"$_my_connections"
1543 # Record our current tickles in a temporary file
1544 _my_tickles="${tickledir}/${_port}.tickles"
1545 rm -f "$_my_tickles"
1546 for _i in $_ips ; do
1547 ctdb -Y gettickles $_i $_port |
1548 awk -F: 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
1550 sort >"$_my_tickles"
1552 # Add tickles for connections that we haven't already got tickles for
1553 comm -23 "$_my_connections" "$_my_tickles" |
1554 while read _src _dst ; do
1555 ctdb addtickle $_src $_dst
1558 # Remove tickles for connections that are no longer there
1559 comm -13 "$_my_connections" "$_my_tickles" |
1560 while read _src _dst ; do
1561 ctdb deltickle $_src $_dst
1564 rm -f "$_my_connections" "$_my_tickles"
1567 ########################################################
1568 # load a site local config file
1569 ########################################################
1571 [ -n "$CTDB_RC_LOCAL" -a -x "$CTDB_RC_LOCAL" ] && {
1575 [ -x $CTDB_BASE/rc.local ] && {
1576 . $CTDB_BASE/rc.local
1579 [ -d $CTDB_BASE/rc.local.d ] && {
1580 for i in $CTDB_BASE/rc.local.d/* ; do
1581 [ -x "$i" ] && . "$i"
1585 script_name="${0##*/}" # basename
1586 service_fail_limit=1