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 case "$CTDB_LOGGING" in
91 if [ -n "$CTDB_LOGGING" ] ; then
92 _file="${CTDB_LOGGING#file:}"
94 _file="/var/log/log.ctdb"
105 logger -t "ctdbd: ${_tag}" $*
110 # When things are run in the background in an eventscript then logging
111 # output might get lost. This is the "solution". :-)
112 background_with_logging ()
115 "$@" 2>&1 </dev/null |
116 script_log "${script_name}&"
122 ##############################################################
123 # check number of args for different events
129 echo "ERROR: must supply interface, IP and maskbits"
135 echo "ERROR: must supply old interface, new interface, IP and maskbits"
142 ##############################################################
143 # determine on what type of system (init style) we are running
146 # only do detection if not already set:
147 [ -z "$CTDB_INIT_STYLE" ] || return
149 if [ -x /sbin/startproc ]; then
150 CTDB_INIT_STYLE="suse"
151 elif [ -x /sbin/start-stop-daemon ]; then
152 CTDB_INIT_STYLE="debian"
154 CTDB_INIT_STYLE="redhat"
158 ######################################################
159 # simulate /sbin/service on platforms that don't have it
160 # _service() makes it easier to hook the service() function for
167 # do nothing, when no service was specified
168 [ -z "$_service_name" ] && return
170 if [ -x /sbin/service ]; then
171 $_nice /sbin/service "$_service_name" "$_op"
172 elif [ -x $CTDB_ETCDIR/init.d/$_service_name ]; then
173 $_nice $CTDB_ETCDIR/init.d/$_service_name "$_op"
174 elif [ -x $CTDB_ETCDIR/rc.d/init.d/$_service_name ]; then
175 $_nice $CTDB_ETCDIR/rc.d/init.d/$_service_name "$_op"
185 ######################################################
186 # simulate /sbin/service (niced) on platforms that don't have it
193 ######################################################
194 # wrapper around /proc/ settings to allow them to be hooked
196 # 1st arg is relative path under /proc/, 2nd arg is value to set
199 echo "$2" >"/proc/$1"
202 ######################################################
203 # wrapper around getting file contents from /proc/ to allow
204 # this to be hooked for testing
205 # 1st arg is relative path under /proc/
211 ######################################################
212 # Check that an RPC service is healthy -
213 # this includes allowing a certain number of failures
214 # before marking the NFS service unhealthy.
216 # usage: nfs_check_rpc_service SERVICE_NAME [ triple ...]
218 # each triple is a set of 3 arguments: an operator, a
219 # fail count limit and an action string.
223 # nfs_check_rpc_service "lockd" \
224 # -ge 15 "verbose restart unhealthy" \
225 # -eq 10 "restart:bs"
227 # says that if lockd is down for 15 iterations then do
228 # a verbose restart of lockd and mark the node unhealthy.
229 # Before this, after 10 iterations of failure, the
230 # service is restarted silently in the background.
231 # Order is important: the number of failures need to be
232 # specified in reverse order because processing stops
233 # after the first condition that is true.
234 ######################################################
235 nfs_check_rpc_service ()
237 _prog_name="$1" ; shift
239 if _nfs_check_rpc_common "$_prog_name" ; then
243 while [ -n "$3" ] ; do
244 if _nfs_check_rpc_action "$1" "$2" "$3" ; then
251 # The new way of doing things...
252 nfs_check_rpc_services ()
254 # Files must end with .check - avoids editor backups, RPM fu, ...
255 for _f in "${CTDB_BASE}/nfs-rpc-checks.d/"[0-9][0-9].*.check ; do
257 _prog_name="${_t##*/[0-9][0-9].}"
259 if _nfs_check_rpc_common "$_prog_name" ; then
260 # This RPC service is up, check next service...
264 # Check each line in the file in turn until one of the limit
266 while read _cmp _lim _rest ; do
272 if _nfs_check_rpc_action "$_cmp" "$_lim" "$_rest" ; then
273 # Limit was hit on this line, no further checking...
280 _nfs_check_rpc_common ()
284 # Some platforms don't have separate programs for all services.
285 case "$_prog_name" in
287 which "rpc.${_prog_name}" >/dev/null 2>&1 || return 0
290 case "$_prog_name" in
312 echo "Internal error: unknown RPC program \"$_prog_name\"."
316 _service_name="nfs_${_prog_name}"
318 if ctdb_check_rpc "$_rpc_prog" $_version >/dev/null ; then
319 ctdb_counter_init "$_service_name"
323 ctdb_counter_incr "$_service_name"
328 _nfs_check_rpc_action ()
334 if ctdb_check_counter "quiet" "$_cmp" "$_limit" "$_service_name" ; then
338 for _action in $_actions ; do
341 echo "$ctdb_check_rpc_out"
344 _nfs_restart_rpc_service "$_prog_name"
347 _nfs_restart_rpc_service "$_prog_name" true
353 echo "Internal error: unknown action \"$_action\"."
361 _nfs_restart_rpc_service ()
364 _background="${2:-false}"
366 if $_background ; then
367 _maybe_background="background_with_logging"
372 _p="rpc.${_prog_name}"
374 case "$_prog_name" in
376 echo "Trying to restart NFS service"
377 $_maybe_background startstop_nfs restart
380 echo "Trying to restart $_prog_name [${_p}]"
382 $_maybe_background $_p ${MOUNTD_PORT:+-p} $MOUNTD_PORT
385 echo "Trying to restart $_prog_name [${_p}]"
387 $_maybe_background $_p ${RQUOTAD_PORT:+-p} $RQUOTAD_PORT
390 echo "Trying to restart lock manager service"
391 $_maybe_background startstop_nfslock restart
394 echo "Trying to restart $_prog_name [${_p}]"
396 $_maybe_background $_p \
397 ${STATD_HOSTNAME:+-n} $STATD_HOSTNAME \
398 ${STATD_PORT:+-p} $STATD_PORT \
399 ${STATD_OUTGOING_PORT:+-o} $STATD_OUTGOING_PORT
402 echo "Internal error: unknown RPC program \"$_prog_name\"."
407 ######################################################
408 # check that a rpc server is registered with portmap
409 # and responding to requests
410 # usage: ctdb_check_rpc SERVICE_NAME VERSION
411 ######################################################
417 _localhost="${CTDB_RPCINFO_LOCALHOST:-127.0.0.1}"
419 if ! ctdb_check_rpc_out=$(rpcinfo -u $_localhost $progname $version 2>&1) ; then
420 ctdb_check_rpc_out="ERROR: $progname failed RPC check:
422 echo "$ctdb_check_rpc_out"
427 ######################################################
428 # Ensure $service_name is set
429 assert_service_name ()
431 [ -n "$service_name" ] || die "INTERNAL ERROR: \$service_name not set"
434 ######################################################
435 # check a set of directories is available
436 # return 1 on a missing directory
437 # directories are read from stdin
438 ######################################################
439 ctdb_check_directories_probe()
441 while IFS="" read d ; do
447 [ -d "${d}/." ] || return 1
452 ######################################################
453 # check a set of directories is available
454 # directories are read from stdin
455 ######################################################
456 ctdb_check_directories()
458 ctdb_check_directories_probe || {
459 echo "ERROR: $service_name directory \"$d\" not available"
464 ######################################################
465 # check a set of tcp ports
466 # usage: ctdb_check_tcp_ports <ports...>
467 ######################################################
469 # This flag file is created when a service is initially started. It
470 # is deleted the first time TCP port checks for that service succeed.
471 # Until then ctdb_check_tcp_ports() prints a more subtle "error"
472 # message if a port check fails.
473 _ctdb_check_tcp_common ()
476 _ctdb_service_started_file="$ctdb_fail_dir/$service_name.started"
479 ctdb_check_tcp_init ()
481 _ctdb_check_tcp_common
482 mkdir -p "${_ctdb_service_started_file%/*}" # dirname
483 touch "$_ctdb_service_started_file"
486 # Check whether something is listening on all of the given TCP ports
487 # using the "ctdb checktcpport" command.
488 ctdb_check_tcp_ports()
490 if [ -z "$1" ] ; then
491 echo "INTERNAL ERROR: ctdb_check_tcp_ports - no ports specified"
495 for _p ; do # process each function argument (port)
496 _cmd="ctdb checktcpport $_p"
501 _ctdb_check_tcp_common
502 if [ ! -f "$_ctdb_service_started_file" ] ; then
503 echo "ERROR: $service_name tcp port $_p is not responding"
504 debug "\"ctdb checktcpport $_p\" was able to bind to port"
506 echo "INFO: $service_name tcp port $_p is not responding"
512 # Couldn't bind, something already listening, next port...
516 echo "ERROR: unexpected error running \"ctdb checktcpport\""
518 ctdb checktcpport (exited with $_ret) with output:
525 # All ports listening
526 _ctdb_check_tcp_common
527 rm -f "$_ctdb_service_started_file"
531 ######################################################
532 # check a unix socket
533 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
534 ######################################################
535 ctdb_check_unix_socket() {
537 [ -z "$socket_path" ] && return
539 if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
540 echo "ERROR: $service_name socket $socket_path not found"
545 ######################################################
546 # check a command returns zero status
547 # usage: ctdb_check_command <command>
548 ######################################################
549 ctdb_check_command ()
551 _out=$("$@" 2>&1) || {
552 echo "ERROR: $* returned error"
558 ################################################
559 # kill off any TCP connections with the given IP
560 ################################################
561 kill_tcp_connections ()
566 if [ "$2" = "oneway" ] ; then
570 get_tcp_connections_for_ip "$_ip" | {
575 while read _dst _src; do
576 _destport="${_dst##*:}"
579 # we only do one-way killtcp for CIFS
580 139|445) __oneway=true ;;
583 echo "Killing TCP connection $_src $_dst"
584 _connections="${_connections}${_nl}${_src} ${_dst}"
585 if ! $__oneway ; then
586 _connections="${_connections}${_nl}${_dst} ${_src}"
589 _killcount=$(($_killcount + 1))
592 if [ $_killcount -eq 0 ] ; then
596 echo "$_connections" | ctdb killtcp || {
597 echo "Failed to send killtcp control"
603 _remaining=$(get_tcp_connections_for_ip $_ip | wc -l)
605 if [ $_remaining -eq 0 ] ; then
606 echo "Killed $_killcount TCP connections to released IP $_ip"
610 _count=$(($_count + 1))
611 if [ $_count -gt 3 ] ; then
612 echo "Timed out killing tcp connections for IP $_ip ($_remaining remaining)"
616 echo "Waiting for $_remaining connections to be killed for IP $_ip"
622 ##################################################################
623 # kill off the local end for any TCP connections with the given IP
624 ##################################################################
625 kill_tcp_connections_local_only ()
627 kill_tcp_connections "$1" "oneway"
630 ##################################################################
631 # tickle any TCP connections with the given IP
632 ##################################################################
633 tickle_tcp_connections ()
637 get_tcp_connections_for_ip "$_ip" |
641 while read dest src; do
642 echo "Tickle TCP connection $src $dest"
643 ctdb tickle $src $dest >/dev/null 2>&1 || _failed=true
644 echo "Tickle TCP connection $dest $src"
645 ctdb tickle $dest $src >/dev/null 2>&1 || _failed=true
649 echo "Failed to send tickle control"
654 get_tcp_connections_for_ip ()
658 netstat -tn | awk -v ip=$_ip \
659 'index($1, "tcp") == 1 && \
660 (index($4, ip ":") == 1 || index($4, "::ffff:" ip ":") == 1) \
661 && $6 == "ESTABLISHED" \
665 ########################################################
666 # start/stop the Ganesha nfs service
667 ########################################################
670 _service_name="nfs-ganesha-$CTDB_CLUSTER_FILESYSTEM_TYPE"
673 service "$_service_name" start
676 service "$_service_name" stop
679 service "$_service_name" restart
684 ########################################################
685 # start/stop the nfs service on different platforms
686 ########################################################
689 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
692 [ -x $CTDB_ETCDIR/init.d/nfslock -o \
693 -r /usr/lib/systemd/system/nfs-lock.service ] && {
701 service nfsserver start
704 service nfsserver stop > /dev/null 2>&1
707 set_proc "fs/nfsd/threads" 0
708 service nfsserver stop > /dev/null 2>&1
710 nfs_dump_some_threads
711 service nfsserver start
718 service nfslock start
726 set_proc "fs/nfsd/threads" 0
727 service nfs stop > /dev/null 2>&1
728 service nfslock stop > /dev/null 2>&1
730 nfs_dump_some_threads
731 service nfslock start
737 echo "Unknown platform. NFS is not supported with ctdb"
743 # Dump up to the configured number of nfsd thread backtraces.
744 nfs_dump_some_threads ()
746 [ -n "$CTDB_NFS_DUMP_STUCK_THREADS" ] || CTDB_NFS_DUMP_STUCK_THREADS=5
748 # Optimisation to avoid running an unnecessary pidof
749 [ $CTDB_NFS_DUMP_STUCK_THREADS -gt 0 ] || return 0
752 for _pid in $(pidof nfsd) ; do
753 [ $_count -le $CTDB_NFS_DUMP_STUCK_THREADS ] || break
755 # Do this first to avoid racing with thread exit
756 _stack=$(get_proc "${_pid}/stack" 2>/dev/null)
757 if [ -n "$_stack" ] ; then
758 echo "Stack trace for stuck nfsd thread [${_pid}]:"
760 _count=$(($_count + 1))
765 ########################################################
766 # start/stop the nfs lockmanager service on different platforms
767 ########################################################
768 startstop_nfslock() {
770 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
773 [ -x $CTDB_ETCDIR/init.d/nfslock -o \
774 -r /usr/lib/systemd/system/nfs-lock.service ] && {
780 # for sles there is no service for lockmanager
781 # so we instead just shutdown/restart nfs
784 service nfsserver start
787 service nfsserver stop > /dev/null 2>&1
790 service nfsserver stop > /dev/null 2>&1
791 service nfsserver start
798 service nfslock start
801 service nfslock stop > /dev/null 2>&1
804 service nfslock stop > /dev/null 2>&1
805 service nfslock start
810 echo "Unknown platform. NFS locking is not supported with ctdb"
816 # Periodically update the statd database
821 _statd_update_trigger="$service_state_dir/update-trigger"
822 [ -f "$_statd_update_trigger" ] || touch "$_statd_update_trigger"
824 _last_update=$(stat --printf="%Y" "$_statd_update_trigger")
825 _current_time=$(date +"%s")
826 if [ $(( $_current_time - $_last_update)) -ge $_update_period ] ; then
827 touch "$_statd_update_trigger"
828 $CTDB_BASE/statd-callout updatelocal &
829 $CTDB_BASE/statd-callout updateremote &
833 ########################################################
841 # Ensure interface is up
842 ip link set "$_iface" up || \
843 die "Failed to bringup interface $_iface"
845 ip addr add "$_ip/$_maskbits" brd + dev "$_iface" || {
846 echo "Failed to add $_ip/$_maskbits on dev $_iface"
851 delete_ip_from_iface()
857 # This could be set globally for all interfaces but it is probably
858 # better to avoid surprises, so limit it the interfaces where CTDB
859 # has public IP addresses. There isn't anywhere else convenient
860 # to do this so just set it each time. This is much cheaper than
861 # remembering and re-adding secondaries.
862 set_proc "sys/net/ipv4/conf/${_iface}/promote_secondaries" 1
864 ip addr del "$_ip/$_maskbits" dev "$_iface" || {
865 echo "Failed to del $_ip on dev $_iface"
870 # If the given IP is hosted then print 2 items: maskbits and iface
875 ip addr show to "${_addr}/32" 2>/dev/null | \
876 awk '$1 == "inet" { print gensub(".*/", "", 1, $2), $NF }'
881 _addr="${1%/*}" # Remove optional maskbits
883 set -- $(ip_maskbits_iface $_addr)
884 if [ -n "$1" ] ; then
887 echo "Removing public address $_addr/$_maskbits from device $_iface"
888 delete_ip_from_iface $_iface $_addr $_maskbits >/dev/null 2>&1
892 drop_all_public_ips ()
894 while read _ip _x ; do
896 done <"${CTDB_PUBLIC_ADDRESSES:-/dev/null}"
899 ########################################################
901 _ctdb_counter_common () {
902 _service_name="${1:-${service_name:-${script_name}}}"
903 _counter_file="$ctdb_fail_dir/$_service_name"
904 mkdir -p "${_counter_file%/*}" # dirname
906 ctdb_counter_init () {
907 _ctdb_counter_common "$1"
911 ctdb_counter_incr () {
912 _ctdb_counter_common "$1"
915 echo -n 1 >> "$_counter_file"
917 ctdb_check_counter () {
918 _msg="${1:-error}" # "error" - anything else is silent on fail
919 _op="${2:--ge}" # an integer operator supported by test
920 _limit="${3:-${service_fail_limit}}"
922 _ctdb_counter_common "$1"
925 _size=$(stat -c "%s" "$_counter_file" 2>/dev/null || echo 0)
927 if [ "$_op" != "%" ] ; then
928 if [ $_size $_op $_limit ] ; then
932 if [ $(($_size $_op $_limit)) -eq 0 ] ; then
937 if [ "$_msg" = "error" ] ; then
938 echo "ERROR: $_size consecutive failures for $_service_name, marking node unhealthy"
946 ########################################################
948 ctdb_status_dir="$CTDB_VARDIR/state/service_status"
949 ctdb_fail_dir="$CTDB_VARDIR/state/failcount"
951 ctdb_setup_service_state_dir ()
953 service_state_dir="$CTDB_VARDIR/state/service_state/${1:-${service_name}}"
954 mkdir -p "$service_state_dir" || {
955 echo "Error creating state dir \"$service_state_dir\""
960 ########################################################
961 # Managed status history, for auto-start/stop
963 ctdb_managed_dir="$CTDB_VARDIR/state/managed_history"
965 _ctdb_managed_common ()
967 _ctdb_managed_file="$ctdb_managed_dir/$service_name"
970 ctdb_service_managed ()
973 mkdir -p "$ctdb_managed_dir"
974 touch "$_ctdb_managed_file"
977 ctdb_service_unmanaged ()
980 rm -f "$_ctdb_managed_file"
983 is_ctdb_previously_managed_service ()
986 [ -f "$_ctdb_managed_file" ]
989 ########################################################
990 # Check and set status
994 echo "node is \"$1\", \"${script_name}\" reports problem: $(cat $2)"
999 if [ -r "$ctdb_status_dir/$script_name/unhealthy" ] ; then
1000 log_status_cat "unhealthy" "$ctdb_status_dir/$script_name/unhealthy"
1002 elif [ -r "$ctdb_status_dir/$script_name/banned" ] ; then
1003 log_status_cat "banned" "$ctdb_status_dir/$script_name/banned"
1012 d="$ctdb_status_dir/$script_name"
1019 for i in "banned" "unhealthy" ; do
1026 ##################################################################
1027 # Reconfigure a service on demand
1029 _ctdb_service_reconfigure_common ()
1031 _d="$ctdb_status_dir/${service_name}"
1033 _ctdb_service_reconfigure_flag="$_d/reconfigure"
1036 ctdb_service_needs_reconfigure ()
1038 _ctdb_service_reconfigure_common
1039 [ -e "$_ctdb_service_reconfigure_flag" ]
1042 ctdb_service_set_reconfigure ()
1044 _ctdb_service_reconfigure_common
1045 >"$_ctdb_service_reconfigure_flag"
1048 ctdb_service_unset_reconfigure ()
1050 _ctdb_service_reconfigure_common
1051 rm -f "$_ctdb_service_reconfigure_flag"
1054 ctdb_service_reconfigure ()
1056 echo "Reconfiguring service \"${service_name}\"..."
1057 ctdb_service_unset_reconfigure
1058 service_reconfigure || return $?
1062 # Default service_reconfigure() function does nothing.
1063 service_reconfigure ()
1068 ctdb_reconfigure_take_lock ()
1070 _ctdb_service_reconfigure_common
1071 _lock="${_d}/reconfigure_lock"
1072 mkdir -p "${_lock%/*}" # dirname
1077 # This is overkill but will work if we need to extend this to
1078 # allow certain events to run multiple times in parallel
1079 # (e.g. takeip) and write multiple PIDs to the file.
1081 if [ -n "$_locker_event" ] ; then
1082 while read _pid ; do
1083 if [ -n "$_pid" -a "$_pid" != $$ ] && \
1084 kill -0 "$_pid" 2>/dev/null ; then
1090 printf "%s\n%s\n" "$event_name" $$ >"$_lock"
1095 ctdb_reconfigure_release_lock ()
1097 _ctdb_service_reconfigure_common
1098 _lock="${_d}/reconfigure_lock"
1103 ctdb_replay_monitor_status ()
1105 echo "Replaying previous status for this script due to reconfigure..."
1106 # Leading colon (':') is missing in some versions...
1107 _out=$(ctdb scriptstatus -Y | grep -E "^:?monitor:${script_name}:")
1108 # Output looks like this:
1109 # :monitor:60.nfs:1:ERROR:1314764004.030861:1314764004.035514:foo bar:
1110 # This is the cheapest way of getting fields in the middle.
1111 set -- $(IFS=":" ; echo $_out)
1114 # The error output field can include colons so we'll try to
1115 # preserve them. The weak checking at the beginning tries to make
1116 # this work for both broken (no leading ':') and fixed output.
1118 _err_out="${_out#*monitor:${script_name}:*:*:*:*:}"
1120 OK) : ;; # Do nothing special.
1122 # Recast this as an error, since we can't exit with the
1123 # correct negative number.
1125 _err_out="[Replay of TIMEDOUT scriptstatus - note incorrect return code.] ${_err_out}"
1128 # Recast this as an OK, since we can't exit with the
1129 # correct negative number.
1131 _err_out="[Replay of DISABLED scriptstatus - note incorrect return code.] ${_err_out}"
1133 *) : ;; # Must be ERROR, do nothing special.
1135 if [ -n "$_err_out" ] ; then
1141 ctdb_service_check_reconfigure ()
1145 # We only care about some events in this function. For others we
1147 case "$event_name" in
1148 monitor|ipreallocated|reconfigure) : ;;
1152 if ctdb_reconfigure_take_lock ; then
1153 # No events covered by this function are running, so proceed
1155 case "$event_name" in
1157 (ctdb_service_reconfigure)
1161 if ctdb_service_needs_reconfigure ; then
1162 ctdb_service_reconfigure
1167 ctdb_reconfigure_release_lock
1169 # Somebody else is running an event we don't want to collide
1170 # with. We proceed with caution.
1171 case "$event_name" in
1173 # Tell whoever called us to retry.
1177 # Defer any scheduled reconfigure and just run the
1178 # rest of the ipreallocated event, as per the
1179 # eventscript. There's an assumption here that the
1180 # event doesn't depend on any scheduled reconfigure.
1181 # This is true in the current code.
1185 # There is most likely a reconfigure in progress so
1186 # the service is possibly unstable. As above, we
1187 # defer any scheduled reconfigured. We also replay
1188 # the previous monitor status since that's the best
1189 # information we have.
1190 ctdb_replay_monitor_status
1196 ##################################################################
1197 # Does CTDB manage this service? - and associated auto-start/stop
1199 ctdb_compat_managed_service ()
1201 if [ "$1" = "yes" -a "$2" = "$service_name" ] ; then
1202 CTDB_MANAGED_SERVICES="$CTDB_MANAGED_SERVICES $2"
1206 is_ctdb_managed_service ()
1210 # $t is used just for readability and to allow better accurate
1211 # matching via leading/trailing spaces
1212 t=" $CTDB_MANAGED_SERVICES "
1214 # Return 0 if "<space>$service_name<space>" appears in $t
1215 if [ "${t#* ${service_name} }" != "${t}" ] ; then
1219 # If above didn't match then update $CTDB_MANAGED_SERVICES for
1220 # backward compatibility and try again.
1221 ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD" "vsftpd"
1222 ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA" "samba"
1223 ctdb_compat_managed_service "$CTDB_MANAGES_WINBIND" "winbind"
1224 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "apache2"
1225 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "httpd"
1226 ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI" "iscsi"
1227 ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD" "clamd"
1228 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs"
1229 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs-ganesha-gpfs"
1231 t=" $CTDB_MANAGED_SERVICES "
1233 # Return 0 if "<space>$service_name<space>" appears in $t
1234 [ "${t#* ${service_name} }" != "${t}" ]
1237 ctdb_start_stop_service ()
1241 # Allow service-start/service-stop pseudo-events to start/stop
1242 # services when we're not auto-starting/stopping and we're not
1244 case "$event_name" in
1246 if is_ctdb_managed_service ; then
1247 die 'service-start event not permitted when service is managed'
1249 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1250 die 'service-start event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1256 if is_ctdb_managed_service ; then
1257 die 'service-stop event not permitted when service is managed'
1259 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1260 die 'service-stop event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1267 # Do nothing unless configured to...
1268 [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] || return 0
1270 [ "$event_name" = "monitor" ] || return 0
1272 if is_ctdb_managed_service ; then
1273 if ! is_ctdb_previously_managed_service ; then
1274 echo "Starting service \"$service_name\" - now managed"
1275 background_with_logging ctdb_service_start
1279 if is_ctdb_previously_managed_service ; then
1280 echo "Stopping service \"$service_name\" - no longer managed"
1281 background_with_logging ctdb_service_stop
1287 ctdb_service_start ()
1289 # The service is marked managed if we've ever tried to start it.
1290 ctdb_service_managed
1292 service_start || return $?
1298 ctdb_service_stop ()
1300 ctdb_service_unmanaged
1304 # Default service_start() and service_stop() functions.
1306 # These may be overridden in an eventscript.
1309 service "$service_name" start
1314 service "$service_name" stop
1317 ##################################################################
1319 ctdb_standard_event_handler ()
1334 # iptables doesn't like being re-entered, so flock-wrap it.
1337 flock -w 30 $CTDB_VARDIR/iptables-ctdb.flock /sbin/iptables "$@"
1340 # AIX (and perhaps others?) doesn't have mktemp
1341 if ! which mktemp >/dev/null 2>&1 ; then
1345 if [ "$1" = "-d" ] ; then
1349 _d="${TMPDIR:-/tmp}"
1350 _hex10=$(dd if=/dev/urandom count=20 2>/dev/null | \
1352 sed -e 's@\(..........\).*@\1@')
1353 _t="${_d}/tmp.${_hex10}"
1366 ########################################################
1368 ########################################################
1374 tickledir="$CTDB_VARDIR/state/tickles"
1375 mkdir -p "$tickledir"
1378 _pnn=$(ctdb pnn) ; _pnn=${_pnn#PNN:}
1380 # What public IPs do I hold?
1381 _ips=$(ctdb -Y ip | awk -F: -v pnn=$_pnn '$3 == pnn {print $2}')
1383 # IPs as a regexp choice
1384 _ipschoice="($(echo $_ips | sed -e 's/ /|/g' -e 's/\./\\\\./g'))"
1386 # Record connections to our public IPs in a temporary file
1387 _my_connections="${tickledir}/${_port}.connections"
1388 rm -f "$_my_connections"
1390 awk -v destpat="^${_ipschoice}:${_port}\$" \
1391 '$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ destpat {print $5, $4}' |
1392 sort >"$_my_connections"
1394 # Record our current tickles in a temporary file
1395 _my_tickles="${tickledir}/${_port}.tickles"
1396 rm -f "$_my_tickles"
1397 for _i in $_ips ; do
1398 ctdb -Y gettickles $_i $_port |
1399 awk -F: 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
1401 sort >"$_my_tickles"
1403 # Add tickles for connections that we haven't already got tickles for
1404 comm -23 "$_my_connections" "$_my_tickles" |
1405 while read _src _dst ; do
1406 ctdb addtickle $_src $_dst
1409 # Remove tickles for connections that are no longer there
1410 comm -13 "$_my_connections" "$_my_tickles" |
1411 while read _src _dst ; do
1412 ctdb deltickle $_src $_dst
1415 rm -f "$_my_connections" "$_my_tickles"
1418 ########################################################
1419 # load a site local config file
1420 ########################################################
1422 [ -n "$CTDB_RC_LOCAL" -a -x "$CTDB_RC_LOCAL" ] && {
1426 [ -x $CTDB_BASE/rc.local ] && {
1427 . $CTDB_BASE/rc.local
1430 [ -d $CTDB_BASE/rc.local.d ] && {
1431 for i in $CTDB_BASE/rc.local.d/* ; do
1432 [ -x "$i" ] && . "$i"
1436 script_name="${0##*/}" # basename
1437 service_fail_limit=1