1 # Hey Emacs, this is a -*- shell-script -*- !!!
3 # utility functions for ctdb event scripts
5 PATH=/bin:/usr/bin:/usr/sbin:/sbin:$PATH
7 [ -z "$CTDB_VARDIR" ] && {
8 if [ -d "/var/lib/ctdb" ] ; then
9 export CTDB_VARDIR="/var/lib/ctdb"
11 export CTDB_VARDIR="/var/ctdb"
14 [ -z "$CTDB_ETCDIR" ] && {
15 export CTDB_ETCDIR="/etc"
18 #######################################
19 # pull in a system config file, if any
23 foo="${service_config:-${service_name}}"
24 if [ -n "$foo" ] ; then
30 if [ "$1" != "ctdb" ] ; then
38 if [ -f $CTDB_ETCDIR/sysconfig/$1 ]; then
39 . $CTDB_ETCDIR/sysconfig/$1
40 elif [ -f $CTDB_ETCDIR/default/$1 ]; then
41 . $CTDB_ETCDIR/default/$1
42 elif [ -f $CTDB_BASE/sysconfig/$1 ]; then
43 . $CTDB_BASE/sysconfig/$1
51 ##############################################################
53 # CTDB_SCRIPT_DEBUGLEVEL can be overwritten by setting it in a
57 if [ ${CTDB_SCRIPT_DEBUGLEVEL:-2} -ge 4 ] ; then
58 # If there are arguments then echo them. Otherwise expect to
59 # use stdin, which allows us to pass lots of debug using a
78 # Log given message or stdin to either syslog or a CTDB log file
79 # $1 is the tag passed to logger if syslog is in use.
85 if [ "$CTDB_SYSLOG" = "yes" -o -z "$CTDB_LOGFILE" ] ; then
88 case "$CTDB_OPTIONS" in
89 *--syslog*) _using_syslog=true ;;
92 if $_using_syslog ; then
93 logger -t "ctdbd: ${_tag}" $*
101 } >>"${CTDB_LOGFILE:-/var/log/log.ctdb}"
105 # When things are run in the background in an eventscript then logging
106 # output might get lost. This is the "solution". :-)
107 background_with_logging ()
110 "$@" 2>&1 </dev/null |
111 script_log "${script_name}&"
117 ##############################################################
118 # check number of args for different events
124 echo "ERROR: must supply interface, IP and maskbits"
130 echo "ERROR: must supply old interface, new interface, IP and maskbits"
137 ##############################################################
138 # determine on what type of system (init style) we are running
139 detect_init_style() {
140 # only do detection if not already set:
141 test "x$CTDB_INIT_STYLE" != "x" && return
143 if [ -x /sbin/startproc ]; then
144 CTDB_INIT_STYLE="suse"
145 elif [ -x /sbin/start-stop-daemon ]; then
146 CTDB_INIT_STYLE="debian"
148 CTDB_INIT_STYLE="redhat"
152 ######################################################
153 # simulate /sbin/service on platforms that don't have it
154 # _service() makes it easier to hook the service() function for
161 # do nothing, when no service was specified
162 [ -z "$_service_name" ] && return
164 if [ -x /sbin/service ]; then
165 $_nice /sbin/service "$_service_name" "$_op"
166 elif [ -x $CTDB_ETCDIR/init.d/$_service_name ]; then
167 $_nice $CTDB_ETCDIR/init.d/$_service_name "$_op"
168 elif [ -x $CTDB_ETCDIR/rc.d/init.d/$_service_name ]; then
169 $_nice $CTDB_ETCDIR/rc.d/init.d/$_service_name "$_op"
179 ######################################################
180 # simulate /sbin/service (niced) on platforms that don't have it
187 ######################################################
188 # wrapper around /proc/ settings to allow them to be hooked
190 # 1st arg is relative path under /proc/, 2nd arg is value to set
193 echo "$2" >"/proc/$1"
196 ######################################################
197 # wrapper around getting file contents from /proc/ to allow
198 # this to be hooked for testing
199 # 1st arg is relative path under /proc/
205 ######################################################
206 # Check that an RPC service is healthy -
207 # this includes allowing a certain number of failures
208 # before marking the NFS service unhealthy.
210 # usage: nfs_check_rpc_service SERVICE_NAME [ triple ...]
212 # each triple is a set of 3 arguments: an operator, a
213 # fail count limit and an action string.
217 # nfs_check_rpc_service "lockd" \
218 # -ge 15 "verbose restart unhealthy" \
219 # -eq 10 "restart:bs"
221 # says that if lockd is down for 15 iterations then do
222 # a verbose restart of lockd and mark the node unhealthy.
223 # Before this, after 10 iterations of failure, the
224 # service is restarted silently in the background.
225 # Order is important: the number of failures need to be
226 # specified in reverse order because processing stops
227 # after the first condition that is true.
228 ######################################################
229 nfs_check_rpc_service ()
231 _prog_name="$1" ; shift
233 if _nfs_check_rpc_common "$_prog_name" ; then
237 while [ -n "$3" ] ; do
238 if _nfs_check_rpc_action "$1" "$2" "$3" ; then
245 # The new way of doing things...
246 nfs_check_rpc_services ()
248 # Files must end with .check - avoids editor backups, RPM fu, ...
249 for _f in "${CTDB_BASE}/nfs-rpc-checks.d/"[0-9][0-9].*.check ; do
251 _prog_name="${_t##*/[0-9][0-9].}"
253 if _nfs_check_rpc_common "$_prog_name" ; then
254 # This RPC service is up, check next service...
258 # Check each line in the file in turn until one of the limit
260 while read _cmp _lim _rest ; do
266 if _nfs_check_rpc_action "$_cmp" "$_lim" "$_rest" ; then
267 # Limit was hit on this line, no further checking...
274 _nfs_check_rpc_common ()
278 # Some platforms don't have separate programs for all services.
279 case "$_prog_name" in
281 which "rpc.${_prog_name}" >/dev/null 2>&1 || return 0
284 case "$_prog_name" in
306 echo "Internal error: unknown RPC program \"$_prog_name\"."
310 _service_name="nfs_${_prog_name}"
312 if ctdb_check_rpc "$_rpc_prog" $_version >/dev/null ; then
313 ctdb_counter_init "$_service_name"
317 ctdb_counter_incr "$_service_name"
322 _nfs_check_rpc_action ()
328 if ctdb_check_counter "quiet" "$_cmp" "$_limit" "$_service_name" ; then
332 for _action in $_actions ; do
335 echo "$ctdb_check_rpc_out"
338 _nfs_restart_rpc_service "$_prog_name"
341 _nfs_restart_rpc_service "$_prog_name" true
347 echo "Internal error: unknown action \"$_action\"."
355 _nfs_restart_rpc_service ()
358 _background="${2:-false}"
360 if $_background ; then
361 _maybe_background="background_with_logging"
366 _p="rpc.${_prog_name}"
368 case "$_prog_name" in
370 echo "Trying to restart NFS service"
371 $_maybe_background startstop_nfs restart
374 echo "Trying to restart $_prog_name [${_p}]"
376 $_maybe_background $_p ${MOUNTD_PORT:+-p} $MOUNTD_PORT
379 echo "Trying to restart $_prog_name [${_p}]"
381 $_maybe_background $_p ${RQUOTAD_PORT:+-p} $RQUOTAD_PORT
384 echo "Trying to restart lock manager service"
385 $_maybe_background startstop_nfslock restart
388 echo "Trying to restart $_prog_name [${_p}]"
390 $_maybe_background $_p \
391 ${STATD_HOSTNAME:+-n} $STATD_HOSTNAME \
392 ${STATD_PORT:+-p} $STATD_PORT \
393 ${STATD_OUTGOING_PORT:+-o} $STATD_OUTGOING_PORT
396 echo "Internal error: unknown RPC program \"$_prog_name\"."
401 ######################################################
402 # check that a rpc server is registered with portmap
403 # and responding to requests
404 # usage: ctdb_check_rpc SERVICE_NAME VERSION
405 ######################################################
411 _localhost="${CTDB_RPCINFO_LOCALHOST:-127.0.0.1}"
413 if ! ctdb_check_rpc_out=$(rpcinfo -u $_localhost $progname $version 2>&1) ; then
414 ctdb_check_rpc_out="ERROR: $progname failed RPC check:
416 echo "$ctdb_check_rpc_out"
421 ######################################################
422 # Ensure $service_name is set
423 assert_service_name ()
425 [ -n "$service_name" ] || die "INTERNAL ERROR: \$service_name not set"
428 ######################################################
429 # check a set of directories is available
430 # return 1 on a missing directory
431 # directories are read from stdin
432 ######################################################
433 ctdb_check_directories_probe()
435 while IFS="" read d ; do
441 [ -d "${d}/." ] || return 1
446 ######################################################
447 # check a set of directories is available
448 # directories are read from stdin
449 ######################################################
450 ctdb_check_directories()
452 ctdb_check_directories_probe || {
453 echo "ERROR: $service_name directory \"$d\" not available"
458 ######################################################
459 # check a set of tcp ports
460 # usage: ctdb_check_tcp_ports <ports...>
461 ######################################################
463 # This flag file is created when a service is initially started. It
464 # is deleted the first time TCP port checks for that service succeed.
465 # Until then ctdb_check_tcp_ports() prints a more subtle "error"
466 # message if a port check fails.
467 _ctdb_check_tcp_common ()
470 _ctdb_service_started_file="$ctdb_fail_dir/$service_name.started"
473 ctdb_check_tcp_init ()
475 _ctdb_check_tcp_common
476 mkdir -p "${_ctdb_service_started_file%/*}" # dirname
477 touch "$_ctdb_service_started_file"
480 ctdb_check_tcp_ports()
482 if [ -z "$1" ] ; then
483 echo "INTERNAL ERROR: ctdb_check_tcp_ports - no ports specified"
487 # Set default value for CTDB_TCP_PORT_CHECKS if unset.
488 # If any of these defaults are unsupported then this variable can
489 # be overridden in /etc/sysconfig/ctdb or via a file in
490 # /etc/ctdb/rc.local.d/.
491 : ${CTDB_TCP_PORT_CHECKERS:=ctdb nmap netstat}
493 for _c in $CTDB_TCP_PORT_CHECKERS ; do
494 ctdb_check_tcp_ports_$_c "$@"
497 _ctdb_check_tcp_common
498 rm -f "$_ctdb_service_started_file"
502 _ctdb_check_tcp_common
503 if [ ! -f "$_ctdb_service_started_file" ] ; then
504 echo "ERROR: $service_name tcp port $_p is not responding"
506 $ctdb_check_tcp_ports_debug
509 echo "INFO: $service_name tcp port $_p is not responding"
516 ctdb_check_ports - checker $_c not implemented
517 output from checker was:
518 $ctdb_check_tcp_ports_debug
526 echo "INTERNAL ERROR: ctdb_check_ports - no working checkers in CTDB_TCP_PORT_CHECKERS=\"$CTDB_TCP_PORT_CHECKERS\""
531 ctdb_check_tcp_ports_netstat ()
533 _cmd='netstat -l -t -n'
535 if [ $? -eq 127 ] ; then
536 # netstat probably not installed - unlikely?
537 ctdb_check_tcp_ports_debug="$_ns"
541 for _p ; do # process each function argument (port)
542 for _a in '0\.0\.0\.0' '::' ; do
543 _pat="[[:space:]]${_a}:${_p}[[:space:]]+[^[:space:]]+[[:space:]]+LISTEN"
544 if echo "$_ns" | grep -E -q "$_pat" ; then
545 # We matched the port, so process next port
550 # We didn't match the port, so flag an error.
551 ctdb_check_tcp_ports_debug="$_cmd shows this output:
559 ctdb_check_tcp_ports_nmap ()
561 # nmap wants a comma-separated list of ports
564 _ports="${_ports}${_ports:+,}${_p}"
567 _cmd="nmap -n -oG - -PS 127.0.0.1 -p $_ports"
569 _nmap_out=$($_cmd 2>&1)
570 if [ $? -eq 127 ] ; then
571 # nmap probably not installed
572 ctdb_check_tcp_ports_debug="$_nmap_out"
576 # get the port-related output
577 _port_info=$(echo "$_nmap_out" | sed -n -r -e 's@^.*Ports:[[:space:]]@@p')
580 # looking for something like this:
581 # 445/open/tcp//microsoft-ds///
582 # possibly followed by a comma
584 case "$_port_info" in
585 # The info we're after must be either at the beginning of
586 # the string or it must follow a space.
589 # Nope, flag an error...
590 ctdb_check_tcp_ports_debug="$_cmd shows this output:
599 # Use the new "ctdb checktcpport" command to check the port.
600 # This is very cheap.
601 ctdb_check_tcp_ports_ctdb ()
603 for _p ; do # process each function argument (port)
604 _cmd="ctdb checktcpport $_p"
609 ctdb_check_tcp_ports_debug="\"$_cmd\" was able to bind to port"
613 # Couldn't bind, something already listening, next port...
617 ctdb_check_tcp_ports_debug="$_cmd (exited with $_ret) with output:
619 # assume not implemented
627 ######################################################
628 # check a unix socket
629 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
630 ######################################################
631 ctdb_check_unix_socket() {
633 [ -z "$socket_path" ] && return
635 if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
636 echo "ERROR: $service_name socket $socket_path not found"
641 ######################################################
642 # check a command returns zero status
643 # usage: ctdb_check_command <command>
644 ######################################################
645 ctdb_check_command ()
647 _out=$("$@" 2>&1) || {
648 echo "ERROR: $* returned error"
654 ################################################
655 # kill off any TCP connections with the given IP
656 ################################################
657 kill_tcp_connections ()
662 if [ "$2" = "oneway" ] ; then
666 get_tcp_connections_for_ip "$_ip" | {
671 while read _dst _src; do
672 _destport="${_dst##*:}"
675 # we only do one-way killtcp for CIFS
676 139|445) __oneway=true ;;
679 echo "Killing TCP connection $_src $_dst"
680 _connections="${_connections}${_nl}${_src} ${_dst}"
681 if ! $__oneway ; then
682 _connections="${_connections}${_nl}${_dst} ${_src}"
685 _killcount=$(($_killcount + 1))
688 if [ $_killcount -eq 0 ] ; then
692 echo "$_connections" | ctdb killtcp || {
693 echo "Failed to send killtcp control"
699 _remaining=$(get_tcp_connections_for_ip $_ip | wc -l)
701 if [ $_remaining -eq 0 ] ; then
702 echo "Killed $_killcount TCP connections to released IP $_ip"
706 _count=$(($_count + 1))
707 if [ $_count -gt 3 ] ; then
708 echo "Timed out killing tcp connections for IP $_ip"
712 echo "Waiting for $_remaining connections to be killed for IP $_ip"
718 ##################################################################
719 # kill off the local end for any TCP connections with the given IP
720 ##################################################################
721 kill_tcp_connections_local_only ()
723 kill_tcp_connections "$1" "oneway"
726 ##################################################################
727 # tickle any TCP connections with the given IP
728 ##################################################################
729 tickle_tcp_connections ()
733 get_tcp_connections_for_ip "$_ip" |
737 while read dest src; do
738 echo "Tickle TCP connection $src $dest"
739 ctdb tickle $src $dest >/dev/null 2>&1 || _failed=true
740 echo "Tickle TCP connection $dest $src"
741 ctdb tickle $dest $src >/dev/null 2>&1 || _failed=true
745 echo "Failed to send tickle control"
750 get_tcp_connections_for_ip ()
754 netstat -tn | awk -v ip=$_ip \
755 'index($1, "tcp") == 1 && \
756 (index($4, ip ":") == 1 || index($4, "::ffff:" ip ":") == 1) \
757 && $6 == "ESTABLISHED" \
761 ########################################################
762 # start/stop the Ganesha nfs service
763 ########################################################
766 _service_name="nfs-ganesha-$CTDB_CLUSTER_FILESYSTEM_TYPE"
769 service "$_service_name" start
772 service "$_service_name" stop
775 service "$_service_name" restart
780 ########################################################
781 # start/stop the nfs service on different platforms
782 ########################################################
785 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
788 [ -x $CTDB_ETCDIR/init.d/nfslock ] && {
796 service nfsserver start
799 service nfsserver stop > /dev/null 2>&1
802 set_proc "fs/nfsd/threads" 0
803 service nfsserver stop > /dev/null 2>&1
805 nfs_dump_some_threads
806 service nfsserver start
813 service nfslock start
821 set_proc "fs/nfsd/threads" 0
822 service nfs stop > /dev/null 2>&1
823 service nfslock stop > /dev/null 2>&1
825 nfs_dump_some_threads
826 service nfslock start
832 echo "Unknown platform. NFS is not supported with ctdb"
838 # Dump up to the configured number of nfsd thread backtraces.
839 nfs_dump_some_threads ()
841 [ -n "$CTDB_NFS_DUMP_STUCK_THREADS" ] || return 0
843 # Optimisation to avoid running an unnecessary pidof
844 [ $CTDB_NFS_DUMP_STUCK_THREADS -gt 0 ] || return 0
847 for _pid in $(pidof nfsd) ; do
848 [ $_count -le $CTDB_NFS_DUMP_STUCK_THREADS ] || break
850 # Do this first to avoid racing with thread exit
851 _stack=$(get_proc "${_pid}/stack" 2>/dev/null)
852 if [ -n "$_stack" ] ; then
853 echo "Stack trace for stuck nfsd thread [${_pid}]:"
855 _count=$(($_count + 1))
860 ########################################################
861 # start/stop the nfs lockmanager service on different platforms
862 ########################################################
863 startstop_nfslock() {
865 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
868 [ -x $CTDB_ETCDIR/init.d/nfslock ] && {
874 # for sles there is no service for lockmanager
875 # so we instead just shutdown/restart nfs
878 service nfsserver start
881 service nfsserver stop > /dev/null 2>&1
884 service nfsserver stop > /dev/null 2>&1
885 service nfsserver start
892 service nfslock start
895 service nfslock stop > /dev/null 2>&1
898 service nfslock stop > /dev/null 2>&1
899 service nfslock start
904 echo "Unknown platform. NFS locking is not supported with ctdb"
910 # Periodically update the statd database
915 _statd_update_trigger="$service_state_dir/update-trigger"
916 [ -f "$_statd_update_trigger" ] || touch "$_statd_update_trigger"
918 _last_update=$(stat --printf="%Y" "$_statd_update_trigger")
919 _current_time=$(date +"%s")
920 if [ $(( $_current_time - $_last_update)) -ge $_update_period ] ; then
921 touch "$_statd_update_trigger"
922 $CTDB_BASE/statd-callout updatelocal &
923 $CTDB_BASE/statd-callout updateremote &
933 _lockfile="${CTDB_VARDIR}/state/interface_modify_${_iface}.flock"
934 mkdir -p "${_lockfile%/*}" # dirname
935 [ -f "$_lockfile" ] || touch "$_lockfile"
938 # Note: use of return/exit/die() below only gets us out of the
939 # sub-shell, which is actually what we want. That is, the
940 # function should just return non-zero.
942 flock --timeout 30 0 || \
943 die "add_ip_to_iface: unable to get lock for ${_iface}"
945 # Ensure interface is up
946 ip link set "$_iface" up || \
947 die "Failed to bringup interface $_iface"
949 ip addr add "$_ip/$_maskbits" brd + dev "$_iface" || \
950 die "Failed to add $_ip/$_maskbits on dev $_iface"
953 # Do nothing here - return above only gets us out of the subshell
954 # and doing anything here will affect the return code.
957 delete_ip_from_iface()
963 _lockfile="${CTDB_VARDIR}/state/interface_modify_${_iface}.flock"
964 mkdir -p "${_lockfile%/*}" # dirname
965 [ -f "$_lockfile" ] || touch "$_lockfile"
968 # Note: use of return/exit/die() below only gets us out of the
969 # sub-shell, which is actually what we want. That is, the
970 # function should just return non-zero.
972 flock --timeout 30 0 || \
973 die "delete_ip_from_iface: unable to get lock for ${_iface}"
975 _im="$_ip/$_maskbits" # shorthand for readability
977 # "ip addr del" will delete all secondary IPs if this is the
978 # primary. To work around this _very_ annoying behaviour we
979 # have to keep a record of the secondaries and re-add them
983 if ip addr list dev "$_iface" primary | grep -Fq "inet $_im " ; then
984 _secondaries=$(ip addr list dev "$_iface" secondary | \
985 awk '$1 == "inet" { print $2 }')
989 ip addr del "$_im" dev "$_iface" || {
990 echo "Failed to del $_ip on dev $_iface"
994 if [ -n "$_secondaries" ] ; then
995 for _i in $_secondaries; do
996 if ip addr list dev "$_iface" | grep -Fq "inet $_i" ; then
997 echo "Kept secondary $_i on dev $_iface"
999 echo "Re-adding secondary address $_i to dev $_iface"
1000 ip addr add $_i brd + dev $_iface || {
1001 echo "Failed to re-add address $_i to dev $_iface"
1011 # Do nothing here - return above only gets us out of the subshell
1012 # and doing anything here will affect the return code.
1015 # If the given IP is hosted then print 2 items: maskbits and iface
1016 ip_maskbits_iface ()
1020 ip addr show to "${_addr}/32" 2>/dev/null | \
1021 awk '$1 == "inet" { print gensub(".*/", "", 1, $2), $NF }'
1026 _addr="${1%/*}" # Remove optional maskbits
1028 set -- $(ip_maskbits_iface $_addr)
1029 if [ -n "$1" ] ; then
1032 echo "Removing public address $_addr/$_maskbits from device $_iface"
1033 delete_ip_from_iface $_iface $_addr $_maskbits >/dev/null 2>&1
1037 drop_all_public_ips ()
1039 while read _ip _x ; do
1041 done <"${CTDB_PUBLIC_ADDRESSES:-/dev/null}"
1044 ########################################################
1046 _ctdb_counter_common () {
1047 _service_name="${1:-${service_name:-${script_name}}}"
1048 _counter_file="$ctdb_fail_dir/$_service_name"
1049 mkdir -p "${_counter_file%/*}" # dirname
1051 ctdb_counter_init () {
1052 _ctdb_counter_common "$1"
1056 ctdb_counter_incr () {
1057 _ctdb_counter_common "$1"
1060 echo -n 1 >> "$_counter_file"
1062 ctdb_check_counter () {
1063 _msg="${1:-error}" # "error" - anything else is silent on fail
1064 _op="${2:--ge}" # an integer operator supported by test
1065 _limit="${3:-${service_fail_limit}}"
1067 _ctdb_counter_common "$1"
1070 _size=$(stat -c "%s" "$_counter_file" 2>/dev/null || echo 0)
1072 if [ "$_op" != "%" ] ; then
1073 if [ $_size $_op $_limit ] ; then
1077 if [ $(($_size $_op $_limit)) -eq 0 ] ; then
1082 if [ "$_msg" = "error" ] ; then
1083 echo "ERROR: $_limit consecutive failures for $_service_name, marking node unhealthy"
1091 ########################################################
1093 ctdb_status_dir="$CTDB_VARDIR/status"
1094 ctdb_fail_dir="$CTDB_VARDIR/failcount"
1096 ctdb_setup_service_state_dir ()
1098 service_state_dir="$CTDB_VARDIR/state/${1:-${service_name}}"
1099 mkdir -p "$service_state_dir" || {
1100 echo "Error creating state dir \"$service_state_dir\""
1105 ########################################################
1106 # Managed status history, for auto-start/stop
1108 ctdb_managed_dir="$CTDB_VARDIR/managed_history"
1110 _ctdb_managed_common ()
1112 _ctdb_managed_file="$ctdb_managed_dir/$service_name"
1115 ctdb_service_managed ()
1117 _ctdb_managed_common
1118 mkdir -p "$ctdb_managed_dir"
1119 touch "$_ctdb_managed_file"
1122 ctdb_service_unmanaged ()
1124 _ctdb_managed_common
1125 rm -f "$_ctdb_managed_file"
1128 is_ctdb_previously_managed_service ()
1130 _ctdb_managed_common
1131 [ -f "$_ctdb_managed_file" ]
1134 ########################################################
1135 # Check and set status
1139 echo "node is \"$1\", \"${script_name}\" reports problem: $(cat $2)"
1144 if [ -r "$ctdb_status_dir/$script_name/unhealthy" ] ; then
1145 log_status_cat "unhealthy" "$ctdb_status_dir/$script_name/unhealthy"
1147 elif [ -r "$ctdb_status_dir/$script_name/banned" ] ; then
1148 log_status_cat "banned" "$ctdb_status_dir/$script_name/banned"
1157 d="$ctdb_status_dir/$script_name"
1164 for i in "banned" "unhealthy" ; do
1171 ##################################################################
1172 # Reconfigure a service on demand
1174 _ctdb_service_reconfigure_common ()
1176 _d="$ctdb_status_dir/${service_name}"
1178 _ctdb_service_reconfigure_flag="$_d/reconfigure"
1181 ctdb_service_needs_reconfigure ()
1183 _ctdb_service_reconfigure_common
1184 [ -e "$_ctdb_service_reconfigure_flag" ]
1187 ctdb_service_set_reconfigure ()
1189 _ctdb_service_reconfigure_common
1190 >"$_ctdb_service_reconfigure_flag"
1193 ctdb_service_unset_reconfigure ()
1195 _ctdb_service_reconfigure_common
1196 rm -f "$_ctdb_service_reconfigure_flag"
1199 ctdb_service_reconfigure ()
1201 echo "Reconfiguring service \"${service_name}\"..."
1202 ctdb_service_unset_reconfigure
1203 service_reconfigure || return $?
1207 # Default service_reconfigure() function does nothing.
1208 service_reconfigure ()
1213 ctdb_reconfigure_try_lock ()
1215 _ctdb_service_reconfigure_common
1216 _lock="${_d}/reconfigure_lock"
1217 mkdir -p "${_lock%/*}" # dirname
1222 # This is overkill but will work if we need to extend this to
1223 # allow certain events to run multiple times in parallel
1224 # (e.g. takeip) and write multiple PIDs to the file.
1226 if [ -n "$_locker_event" ] ; then
1227 while read _pid ; do
1228 if [ -n "$_pid" -a "$_pid" != $$ ] && \
1229 kill -0 "$_pid" 2>/dev/null ; then
1235 printf "%s\n%s\n" "$event_name" $$ >"$_lock"
1240 ctdb_replay_monitor_status ()
1242 echo "Replaying previous status for this script due to reconfigure..."
1243 # Leading colon (':') is missing in some versions...
1244 _out=$(ctdb scriptstatus -Y | grep -E "^:?monitor:${script_name}:")
1245 # Output looks like this:
1246 # :monitor:60.nfs:1:ERROR:1314764004.030861:1314764004.035514:foo bar:
1247 # This is the cheapest way of getting fields in the middle.
1248 set -- $(IFS=":" ; echo $_out)
1251 # The error output field can include colons so we'll try to
1252 # preserve them. The weak checking at the beginning tries to make
1253 # this work for both broken (no leading ':') and fixed output.
1255 _err_out="${_out#*monitor:${script_name}:*:*:*:*:}"
1257 OK) : ;; # Do nothing special.
1259 # Recast this as an error, since we can't exit with the
1260 # correct negative number.
1262 _err_out="[Replay of TIMEDOUT scriptstatus - note incorrect return code.] ${_err_out}"
1265 # Recast this as an OK, since we can't exit with the
1266 # correct negative number.
1268 _err_out="[Replay of DISABLED scriptstatus - note incorrect return code.] ${_err_out}"
1270 *) : ;; # Must be ERROR, do nothing special.
1272 if [ -n "$_err_out" ] ; then
1278 ctdb_service_check_reconfigure ()
1282 # We only care about some events in this function. For others we
1284 case "$event_name" in
1285 monitor|ipreallocated|reconfigure) : ;;
1289 if ctdb_reconfigure_try_lock ; then
1290 # No events covered by this function are running, so proceed
1292 case "$event_name" in
1294 (ctdb_service_reconfigure)
1298 if ctdb_service_needs_reconfigure ; then
1299 ctdb_service_reconfigure
1303 if ctdb_service_needs_reconfigure ; then
1304 ctdb_service_reconfigure
1305 # Given that the reconfigure might not have
1306 # resulted in the service being stable yet, we
1307 # replay the previous status since that's the best
1308 # information we have.
1309 ctdb_replay_monitor_status
1314 # Somebody else is running an event we don't want to collide
1315 # with. We proceed with caution.
1316 case "$event_name" in
1318 # Tell whoever called us to retry.
1322 # Defer any scheduled reconfigure and just run the
1323 # rest of the ipreallocated event, as per the
1324 # eventscript. There's an assumption here that the
1325 # event doesn't depend on any scheduled reconfigure.
1326 # This is true in the current code.
1330 # There is most likely a reconfigure in progress so
1331 # the service is possibly unstable. As above, we
1332 # defer any scheduled reconfigured. We also replay
1333 # the previous monitor status since that's the best
1334 # information we have.
1335 ctdb_replay_monitor_status
1341 ##################################################################
1342 # Does CTDB manage this service? - and associated auto-start/stop
1344 ctdb_compat_managed_service ()
1346 if [ "$1" = "yes" -a "$2" = "$service_name" ] ; then
1347 CTDB_MANAGED_SERVICES="$CTDB_MANAGED_SERVICES $2"
1351 is_ctdb_managed_service ()
1355 # $t is used just for readability and to allow better accurate
1356 # matching via leading/trailing spaces
1357 t=" $CTDB_MANAGED_SERVICES "
1359 # Return 0 if "<space>$service_name<space>" appears in $t
1360 if [ "${t#* ${service_name} }" != "${t}" ] ; then
1364 # If above didn't match then update $CTDB_MANAGED_SERVICES for
1365 # backward compatibility and try again.
1366 ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD" "vsftpd"
1367 ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA" "samba"
1368 ctdb_compat_managed_service "$CTDB_MANAGES_SCP" "scp"
1369 ctdb_compat_managed_service "$CTDB_MANAGES_WINBIND" "winbind"
1370 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "apache2"
1371 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "httpd"
1372 ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI" "iscsi"
1373 ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD" "clamd"
1374 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs"
1375 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs-ganesha-gpfs"
1377 t=" $CTDB_MANAGED_SERVICES "
1379 # Return 0 if "<space>$service_name<space>" appears in $t
1380 [ "${t#* ${service_name} }" != "${t}" ]
1383 ctdb_start_stop_service ()
1387 # Allow service-start/service-stop pseudo-events to start/stop
1388 # services when we're not auto-starting/stopping and we're not
1390 case "$event_name" in
1392 if is_ctdb_managed_service ; then
1393 die 'service-start event not permitted when service is managed'
1395 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1396 die 'service-start event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1402 if is_ctdb_managed_service ; then
1403 die 'service-stop event not permitted when service is managed'
1405 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1406 die 'service-stop event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1413 # Do nothing unless configured to...
1414 [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] || return 0
1416 [ "$event_name" = "monitor" ] || return 0
1418 if is_ctdb_managed_service ; then
1419 if ! is_ctdb_previously_managed_service ; then
1420 echo "Starting service \"$service_name\" - now managed"
1421 background_with_logging ctdb_service_start
1425 if is_ctdb_previously_managed_service ; then
1426 echo "Stopping service \"$service_name\" - no longer managed"
1427 background_with_logging ctdb_service_stop
1433 ctdb_service_start ()
1435 # The service is marked managed if we've ever tried to start it.
1436 ctdb_service_managed
1438 service_start || return $?
1444 ctdb_service_stop ()
1446 ctdb_service_unmanaged
1450 # Default service_start() and service_stop() functions.
1452 # These may be overridden in an eventscript. When overriding, the
1453 # following convention must be followed. If these functions are
1454 # called with no arguments then they may use internal logic to
1455 # determine whether the service is managed and, therefore, whether
1456 # they should take any action. However, if the service name is
1457 # specified as an argument then an attempt must be made to start or
1458 # stop the service. This is because the auto-start/stop code calls
1459 # them with the service name as an argument.
1462 service "$service_name" start
1467 service "$service_name" stop
1470 ##################################################################
1472 ctdb_standard_event_handler ()
1487 # iptables doesn't like being re-entered, so flock-wrap it.
1490 flock -w 30 $CTDB_VARDIR/iptables-ctdb.flock /sbin/iptables "$@"
1493 # AIX (and perhaps others?) doesn't have mktemp
1494 if ! which mktemp >/dev/null 2>&1 ; then
1498 if [ "$1" = "-d" ] ; then
1502 _d="${TMPDIR:-/tmp}"
1503 _hex10=$(dd if=/dev/urandom count=20 2>/dev/null | \
1505 sed -e 's@\(..........\).*@\1@')
1506 _t="${_d}/tmp.${_hex10}"
1519 ########################################################
1521 ########################################################
1527 tickledir="$CTDB_VARDIR/state/tickles"
1528 mkdir -p "$tickledir"
1531 _pnn=$(ctdb pnn) ; _pnn=${_pnn#PNN:}
1533 # What public IPs do I hold?
1534 _ips=$(ctdb -Y ip | awk -F: -v pnn=$_pnn '$3 == pnn {print $2}')
1536 # IPs as a regexp choice
1537 _ipschoice="($(echo $_ips | sed -e 's/ /|/g' -e 's/\./\\\\./g'))"
1539 # Record connections to our public IPs in a temporary file
1540 _my_connections="${tickledir}/${_port}.connections"
1541 rm -f "$_my_connections"
1543 awk -v destpat="^${_ipschoice}:${_port}\$" \
1544 '$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ destpat {print $5, $4}' |
1545 sort >"$_my_connections"
1547 # Record our current tickles in a temporary file
1548 _my_tickles="${tickledir}/${_port}.tickles"
1549 rm -f "$_my_tickles"
1550 for _i in $_ips ; do
1551 ctdb -Y gettickles $_i $_port |
1552 awk -F: 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
1554 sort >"$_my_tickles"
1556 # Add tickles for connections that we haven't already got tickles for
1557 comm -23 "$_my_connections" "$_my_tickles" |
1558 while read _src _dst ; do
1559 ctdb addtickle $_src $_dst
1562 # Remove tickles for connections that are no longer there
1563 comm -13 "$_my_connections" "$_my_tickles" |
1564 while read _src _dst ; do
1565 ctdb deltickle $_src $_dst
1568 rm -f "$_my_connections" "$_my_tickles"
1571 ########################################################
1572 # load a site local config file
1573 ########################################################
1575 [ -n "$CTDB_RC_LOCAL" -a -x "$CTDB_RC_LOCAL" ] && {
1579 [ -x $CTDB_BASE/rc.local ] && {
1580 . $CTDB_BASE/rc.local
1583 [ -d $CTDB_BASE/rc.local.d ] && {
1584 for i in $CTDB_BASE/rc.local.d/* ; do
1585 [ -x "$i" ] && . "$i"
1589 script_name="${0##*/}" # basename
1590 service_fail_limit=1