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
87 # Log given message or stdin to either syslog or a CTDB log file
88 # $1 is the tag passed to logger if syslog is in use.
93 case "$CTDB_LOGGING" in
95 if [ -n "$CTDB_LOGGING" ] ; then
96 _file="${CTDB_LOGGING#file:}"
98 _file="/var/log/log.ctdb"
101 if [ -n "$*" ] ; then
109 # Handle all syslog:* variants here too. There's no tool to do
110 # the lossy things, so just use logger.
111 logger -t "ctdbd: ${_tag}" $*
116 # When things are run in the background in an eventscript then logging
117 # output might get lost. This is the "solution". :-)
118 background_with_logging ()
121 "$@" 2>&1 </dev/null |
122 script_log "${script_name}&"
128 ##############################################################
129 # check number of args for different events
135 echo "ERROR: must supply interface, IP and maskbits"
141 echo "ERROR: must supply old interface, new interface, IP and maskbits"
148 ##############################################################
149 # determine on what type of system (init style) we are running
152 # only do detection if not already set:
153 [ -z "$CTDB_INIT_STYLE" ] || return
155 if [ -x /sbin/startproc ]; then
156 CTDB_INIT_STYLE="suse"
157 elif [ -x /sbin/start-stop-daemon ]; then
158 CTDB_INIT_STYLE="debian"
160 CTDB_INIT_STYLE="redhat"
164 ######################################################
165 # simulate /sbin/service on platforms that don't have it
166 # _service() makes it easier to hook the service() function for
173 # do nothing, when no service was specified
174 [ -z "$_service_name" ] && return
176 if [ -x /sbin/service ]; then
177 $_nice /sbin/service "$_service_name" "$_op"
178 elif [ -x /usr/sbin/service ]; then
179 $_nice /usr/sbin/service "$_service_name" "$_op"
180 elif [ -x $CTDB_ETCDIR/init.d/$_service_name ]; then
181 $_nice $CTDB_ETCDIR/init.d/$_service_name "$_op"
182 elif [ -x $CTDB_ETCDIR/rc.d/init.d/$_service_name ]; then
183 $_nice $CTDB_ETCDIR/rc.d/init.d/$_service_name "$_op"
193 ######################################################
194 # simulate /sbin/service (niced) on platforms that don't have it
201 ######################################################
202 # Cached retrieval of PNN from local node. This never changes so why
203 # open a client connection to the server each time this is needed?
204 # This sets $pnn - this avoid an unnecessary subprocess.
207 _pnn_file="$CTDB_VARDIR/state/my-pnn"
208 if [ ! -f "$_pnn_file" ] ; then
209 ctdb pnn | sed -e 's@.*:@@' >"$_pnn_file"
212 read pnn <"$_pnn_file"
215 ######################################################
216 # wrapper around /proc/ settings to allow them to be hooked
218 # 1st arg is relative path under /proc/, 2nd arg is value to set
221 echo "$2" >"/proc/$1"
226 if [ -w "/proc/$1" ] ; then
231 ######################################################
232 # wrapper around getting file contents from /proc/ to allow
233 # this to be hooked for testing
234 # 1st arg is relative path under /proc/
240 ######################################################
241 # Print up to $_max kernel stack traces for processes named $_program
242 program_stack_traces ()
248 for _pid in $(pidof "$_prog") ; do
249 [ $_count -le $_max ] || break
251 # Do this first to avoid racing with process exit
252 _stack=$(get_proc "${_pid}/stack" 2>/dev/null)
253 if [ -n "$_stack" ] ; then
254 echo "Stack trace for ${_prog}[${_pid}]:"
256 _count=$(($_count + 1))
261 ######################################################
262 # Check that an RPC service is healthy -
263 # this includes allowing a certain number of failures
264 # before marking the NFS service unhealthy.
266 # usage: nfs_check_rpc_service SERVICE_NAME [ triple ...]
268 # each triple is a set of 3 arguments: an operator, a
269 # fail count limit and an action string.
273 # nfs_check_rpc_service "lockd" \
274 # -ge 15 "verbose restart unhealthy" \
275 # -eq 10 "restart:bs"
277 # says that if lockd is down for 15 iterations then do
278 # a verbose restart of lockd and mark the node unhealthy.
279 # Before this, after 10 iterations of failure, the
280 # service is restarted silently in the background.
281 # Order is important: the number of failures need to be
282 # specified in reverse order because processing stops
283 # after the first condition that is true.
284 ######################################################
285 nfs_check_rpc_service ()
287 _prog_name="$1" ; shift
289 if _nfs_check_rpc_common "$_prog_name" ; then
293 while [ -n "$3" ] ; do
294 if _nfs_check_rpc_action "$1" "$2" "$3" ; then
301 # The new way of doing things...
302 nfs_check_rpc_services ()
304 # Files must end with .check - avoids editor backups, RPM fu, ...
305 for _f in "${CTDB_BASE}/nfs-rpc-checks.d/"[0-9][0-9].*.check ; do
307 _prog_name="${_t##*/[0-9][0-9].}"
309 # If $_prog_name contains '@' then the bit after it is the
311 _family="${_prog_name#*@}"
312 if [ "$_family" = "$_prog_name" ] ; then
315 _prog_name="${_prog_name%@*}"
318 if _nfs_check_rpc_common "$_prog_name" "$_family" ; then
319 # This RPC service is up, check next service...
323 # Check each line in the file in turn until one of the limit
325 while read _cmp _lim _rest ; do
331 if _nfs_check_rpc_action "$_cmp" "$_lim" "$_rest" ; then
332 # Limit was hit on this line, no further checking...
339 _nfs_check_rpc_common ()
344 # Some platforms don't have separate programs for all services.
345 case "$_prog_name" in
347 type "rpc.${_prog_name}" >/dev/null 2>&1 || return 0
350 case "$_prog_name" in
372 echo "Internal error: unknown RPC program \"$_prog_name\"."
376 _service_name="nfs_${_prog_name}${_family:+_}${_family}"
378 if ctdb_check_rpc "$_rpc_prog" "$_version" "$_family" >/dev/null ; then
379 ctdb_counter_init "$_service_name"
383 ctdb_counter_incr "$_service_name"
388 _nfs_check_rpc_action ()
394 if ctdb_check_counter "quiet" "$_cmp" "$_limit" "$_service_name" ; then
398 for _action in $_actions ; do
401 echo "ERROR: $ctdb_check_rpc_out"
404 _nfs_restart_rpc_service "$_prog_name"
407 _nfs_restart_rpc_service "$_prog_name" true
413 echo "Internal error: unknown action \"$_action\"."
421 _nfs_restart_rpc_service ()
424 _background="${2:-false}"
426 if $_background ; then
427 _maybe_background="background_with_logging"
432 _p="rpc.${_prog_name}"
434 case "$_prog_name" in
436 echo "Trying to restart NFS service"
437 $_maybe_background startstop_nfs restart
440 echo "Trying to restart $_prog_name [${_p}]"
442 nfs_dump_some_threads "$_p"
443 $_maybe_background $_p $RPCMOUNTDOPTS \
444 ${MOUNTD_PORT:+-p} $MOUNTD_PORT
447 echo "Trying to restart $_prog_name [${_p}]"
449 nfs_dump_some_threads "$_p"
450 $_maybe_background $_p ${RQUOTAD_PORT:+-p} $RQUOTAD_PORT
453 echo "Trying to restart lock manager service"
454 $_maybe_background startstop_nfslock restart
457 echo "Trying to restart $_prog_name [${_p}]"
459 nfs_dump_some_threads "$_p"
460 $_maybe_background $_p \
461 ${STATD_HOSTNAME:+-n} $STATD_HOSTNAME \
462 ${STATD_PORT:+-p} $STATD_PORT \
463 ${STATD_OUTGOING_PORT:+-o} $STATD_OUTGOING_PORT
466 echo "Internal error: unknown RPC program \"$_prog_name\"."
471 ######################################################
472 # Check the health of NFS services
474 # Use .check files in given directory.
475 # Default is "${CTDB_BASE}/nfs-checks.d/"
476 ######################################################
477 nfs_check_services ()
479 _dir="${1:-${CTDB_NFS_CHECKS_DIR:-${CTDB_BASE}/nfs-checks.d}}"
481 # Files must end with .check - avoids editor backups, RPM fu, ...
482 for _f in "$_dir"/[0-9][0-9].*.check ; do
484 _progname="${_t##*/[0-9][0-9].}"
486 nfs_check_service "$_progname" <"$_f"
490 ######################################################
491 # Check the health of an NFS service
493 # $1 - progname, passed to rpcinfo (looked up in /etc/rpc)
495 # Reads variables from stdin
499 # * family - "tcp" or "udp" or space separated list
501 # * version - optional, RPC service version number
502 # default is to omit to check for any version
503 # * unhealthy_after - number of check fails before unhealthy
505 # * restart_every - number of check fails before restart
506 # default: 0, meaning no restart
507 # * service_stop_cmd - command to stop service
508 # default: no default, must be provided if
510 # * service_start_cmd - command to start service
511 # default: no default, must be provided if
513 # * service_debug_cmd - command to debug a service after trying to stop it;
514 # for example, it can be useful to print stack
515 # traces of threads that have not exited, since
516 # they may be stuck doing I/O;
517 # no default, see also function program_stack_traces()
519 # Quoting in values is not preserved
521 ######################################################
527 # Subshell to restrict scope variables...
538 # Eval line-by-line. Expands variable references in values.
539 # Also allows variable name checking, which seems useful.
540 while read _line ; do
542 \#*|"") : ;; # Ignore comments, blank lines
545 unhealthy_after=*|restart_every=*|\
546 service_stop_cmd=*|service_start_cmd=*|\
552 echo "ERROR: Unknown variable for ${_progname}: ${_line}"
557 _service_name="nfs_${_progname}"
559 if nfs_check_rpcinfo \
560 "$_progname" "$version" "$family" >/dev/null ; then
561 if [ $unhealthy_after -ne 1 -o $restart_every -ne 0 ] ; then
562 ctdb_counter_init "$_service_name"
567 ctdb_counter_incr "$_service_name"
568 _failcount=$(ctdb_counter_get "$_service_name")
571 if [ $unhealthy_after -gt 0 ] ; then
572 if [ $_failcount -ge $unhealthy_after ] ; then
574 echo "ERROR: $ctdb_check_rpc_out"
578 if [ $restart_every -gt 0 ] ; then
579 if [ $(($_failcount % $restart_every)) -eq 0 ] ; then
580 if ! $_unhealthy ; then
581 echo "WARNING: $ctdb_check_rpc_out"
587 if $_unhealthy ; then
595 # Uses: stop_service, start_service, debug_stuck_threads
596 nfs_restart_service ()
598 if [ -z "$service_stop_cmd" -o -z "$service_start_cmd" ] ; then
599 die "ERROR: Can not restart service \"${_progname}\" without corresponding service_start_cmd/service_stop_cmd settings"
602 echo "Trying to restart service \"${_progname}\"..."
603 # Using eval means variables can contain semicolon separated commands
604 eval "$service_stop_cmd"
605 if [ -n "$service_debug_cmd" ] ; then
606 eval "$service_debug_cmd"
608 background_with_logging eval "$service_start_cmd"
611 ######################################################
612 # Check an RPC service with rpcinfo
613 ######################################################
616 _progname="$1" # passed to rpcinfo (looked up in /etc/rpc)
617 _version="$2" # optional, not passed if empty/unset
618 _family="${3:-tcp}" # optional, default is "tcp"
620 _localhost="${CTDB_RPCINFO_LOCALHOST:-127.0.0.1}"
622 if ! ctdb_check_rpc_out=$(rpcinfo -T $_family $_localhost \
623 $_progname $_version 2>&1) ; then
624 ctdb_check_rpc_out="$_progname failed RPC check:
626 echo "$ctdb_check_rpc_out"
633 _progname="$1" # passed to rpcinfo (looked up in /etc/rpc)
634 _versions="$2" # optional, space separated, not passed if empty/unset
635 _families="${3:-tcp}" # optional, space separated, default is "tcp"
637 for _family in $_families ; do
638 if [ -n "$_versions" ] ; then
639 for _version in $_versions ; do
640 ctdb_check_rpc $_progname $_version $_family || return $?
643 ctdb_check_rpc $_progname "" $_family || return $?
648 ######################################################
649 # Ensure $service_name is set
650 assert_service_name ()
652 [ -n "$service_name" ] || die "INTERNAL ERROR: \$service_name not set"
655 ######################################################
656 # check a set of directories is available
657 # return 1 on a missing directory
658 # directories are read from stdin
659 ######################################################
660 ctdb_check_directories_probe()
662 while IFS="" read d ; do
668 [ -d "${d}/." ] || return 1
673 ######################################################
674 # check a set of directories is available
675 # directories are read from stdin
676 ######################################################
677 ctdb_check_directories()
679 ctdb_check_directories_probe || {
680 echo "ERROR: $service_name directory \"$d\" not available"
685 ######################################################
686 # check a set of tcp ports
687 # usage: ctdb_check_tcp_ports <ports...>
688 ######################################################
690 # This flag file is created when a service is initially started. It
691 # is deleted the first time TCP port checks for that service succeed.
692 # Until then ctdb_check_tcp_ports() prints a more subtle "error"
693 # message if a port check fails.
694 _ctdb_check_tcp_common ()
697 _ctdb_service_started_file="$ctdb_fail_dir/$service_name.started"
700 ctdb_check_tcp_init ()
702 _ctdb_check_tcp_common
703 mkdir -p "${_ctdb_service_started_file%/*}" # dirname
704 touch "$_ctdb_service_started_file"
707 # Check whether something is listening on all of the given TCP ports
708 # using the "ctdb checktcpport" command.
709 ctdb_check_tcp_ports()
711 if [ -z "$1" ] ; then
712 echo "INTERNAL ERROR: ctdb_check_tcp_ports - no ports specified"
716 for _p ; do # process each function argument (port)
717 _cmd="ctdb checktcpport $_p"
722 _ctdb_check_tcp_common
723 if [ ! -f "$_ctdb_service_started_file" ] ; then
724 echo "ERROR: $service_name tcp port $_p is not responding"
725 debug "\"ctdb checktcpport $_p\" was able to bind to port"
727 echo "INFO: $service_name tcp port $_p is not responding"
733 # Couldn't bind, something already listening, next port...
737 echo "ERROR: unexpected error running \"ctdb checktcpport\""
739 ctdb checktcpport (exited with $_ret) with output:
746 # All ports listening
747 _ctdb_check_tcp_common
748 rm -f "$_ctdb_service_started_file"
752 ######################################################
753 # check a unix socket
754 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
755 ######################################################
756 ctdb_check_unix_socket() {
758 [ -z "$socket_path" ] && return
760 if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
761 echo "ERROR: $service_name socket $socket_path not found"
766 ######################################################
767 # check a command returns zero status
768 # usage: ctdb_check_command <command>
769 ######################################################
770 ctdb_check_command ()
772 _out=$("$@" 2>&1) || {
773 echo "ERROR: $* returned error"
779 ################################################
780 # kill off any TCP connections with the given IP
781 ################################################
782 kill_tcp_connections ()
787 if [ "$2" = "oneway" ] ; then
791 get_tcp_connections_for_ip "$_ip" | {
796 while read _dst _src; do
797 _destport="${_dst##*:}"
800 # we only do one-way killtcp for CIFS
801 139|445) __oneway=true ;;
804 echo "Killing TCP connection $_src $_dst"
805 _connections="${_connections}${_nl}${_src} ${_dst}"
806 if ! $__oneway ; then
807 _connections="${_connections}${_nl}${_dst} ${_src}"
810 _killcount=$(($_killcount + 1))
813 if [ $_killcount -eq 0 ] ; then
817 echo "$_connections" | ctdb killtcp || {
818 echo "Failed to send killtcp control"
824 _remaining=$(get_tcp_connections_for_ip $_ip | wc -l)
826 if [ $_remaining -eq 0 ] ; then
827 echo "Killed $_killcount TCP connections to released IP $_ip"
831 _count=$(($_count + 1))
832 if [ $_count -gt 3 ] ; then
833 echo "Timed out killing tcp connections for IP $_ip ($_remaining remaining)"
837 echo "Waiting for $_remaining connections to be killed for IP $_ip"
843 ##################################################################
844 # kill off the local end for any TCP connections with the given IP
845 ##################################################################
846 kill_tcp_connections_local_only ()
848 kill_tcp_connections "$1" "oneway"
851 ##################################################################
852 # tickle any TCP connections with the given IP
853 ##################################################################
854 tickle_tcp_connections ()
858 get_tcp_connections_for_ip "$_ip" |
862 while read dest src; do
863 echo "Tickle TCP connection $src $dest"
864 ctdb tickle $src $dest >/dev/null 2>&1 || _failed=true
865 echo "Tickle TCP connection $dest $src"
866 ctdb tickle $dest $src >/dev/null 2>&1 || _failed=true
870 echo "Failed to send tickle control"
875 get_tcp_connections_for_ip ()
879 netstat -tn | awk -v ip=$_ip \
880 'index($1, "tcp") == 1 && \
881 (index($4, ip ":") == 1 || index($4, "::ffff:" ip ":") == 1) \
882 && $6 == "ESTABLISHED" \
886 ##################################################################
887 # use statd-callout to update NFS lock info
888 ##################################################################
889 nfs_update_lock_info ()
891 if [ -x "$CTDB_BASE/statd-callout" ] ; then
892 "$CTDB_BASE/statd-callout" update
896 ########################################################
897 # start/stop the Ganesha nfs service
898 ########################################################
901 _service_name="nfs-ganesha-$CTDB_CLUSTER_FILESYSTEM_TYPE"
904 service "$_service_name" start
907 service "$_service_name" stop
910 service "$_service_name" stop
911 nfs_dump_some_threads "rpc.statd"
912 service "$_service_name" start
917 ########################################################
918 # start/stop the nfs service on different platforms
919 ########################################################
922 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
925 [ -x $CTDB_ETCDIR/init.d/nfslock -o \
926 -r /usr/lib/systemd/system/nfs-lock.service ] && {
934 service nfsserver start
937 service nfsserver stop > /dev/null 2>&1
940 set_proc "fs/nfsd/threads" 0
941 service nfsserver stop > /dev/null 2>&1
943 nfs_dump_some_threads
944 service nfsserver start
951 service nfslock start
959 set_proc "fs/nfsd/threads" 0
960 service nfs stop > /dev/null 2>&1
961 service nfslock stop > /dev/null 2>&1
963 nfs_dump_some_threads
964 service nfslock start
970 echo "Unknown platform. NFS is not supported with ctdb"
976 # Dump up to the configured number of nfsd thread backtraces.
977 nfs_dump_some_threads ()
981 _num="${CTDB_NFS_DUMP_STUCK_THREADS:-5}"
982 [ $_num -gt 0 ] || return 0
984 program_stack_traces "$_prog" $_num
987 ########################################################
988 # start/stop the nfs lockmanager service on different platforms
989 ########################################################
990 startstop_nfslock() {
992 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
995 [ -x $CTDB_ETCDIR/init.d/nfslock -o \
996 -r /usr/lib/systemd/system/nfs-lock.service ] && {
1002 # for sles there is no service for lockmanager
1003 # so we instead just shutdown/restart nfs
1006 service nfsserver start
1009 service nfsserver stop > /dev/null 2>&1
1012 service nfsserver stop > /dev/null 2>&1
1013 service nfsserver start
1020 service nfslock start
1023 service nfslock stop > /dev/null 2>&1
1026 service nfslock stop > /dev/null 2>&1
1027 service nfslock start
1032 echo "Unknown platform. NFS locking is not supported with ctdb"
1038 ########################################################
1046 # Ensure interface is up
1047 ip link set "$_iface" up || \
1048 die "Failed to bringup interface $_iface"
1050 # Only need to define broadcast for IPv4
1053 *) _bcast="brd +" ;;
1056 ip addr add "$_ip/$_maskbits" $_bcast dev "$_iface" || {
1057 echo "Failed to add $_ip/$_maskbits on dev $_iface"
1061 # Wait 5 seconds for IPv6 addresses to stop being tentative...
1062 if [ -z "$_bcast" ] ; then
1063 for _x in $(seq 1 10) ; do
1064 ip addr show to "${_ip}/128" | grep -q "tentative" || break
1068 # If the address was a duplicate then it won't be on the
1069 # interface so flag an error.
1070 _t=$(ip addr show to "${_ip}/128")
1073 echo "Failed to add $_ip/$_maskbits on dev $_iface"
1076 *tentative*|*dadfailed*)
1077 echo "Failed to add $_ip/$_maskbits on dev $_iface"
1078 ip addr del "$_ip/$_maskbits" dev "$_iface"
1085 delete_ip_from_iface()
1091 # This could be set globally for all interfaces but it is probably
1092 # better to avoid surprises, so limit it the interfaces where CTDB
1093 # has public IP addresses. There isn't anywhere else convenient
1094 # to do this so just set it each time. This is much cheaper than
1095 # remembering and re-adding secondaries.
1096 set_proc "sys/net/ipv4/conf/${_iface}/promote_secondaries" 1
1098 ip addr del "$_ip/$_maskbits" dev "$_iface" || {
1099 echo "Failed to del $_ip on dev $_iface"
1104 # If the given IP is hosted then print 2 items: maskbits and iface
1105 ip_maskbits_iface ()
1110 *:*) _family="inet6" ; _bits=128 ;;
1111 *) _family="inet" ; _bits=32 ;;
1114 ip addr show to "${_addr}/${_bits}" 2>/dev/null | \
1115 awk -v family="${_family}" \
1116 'NR == 1 { iface = $2; sub(":$", "", iface) ; \
1117 sub("@.*", "", iface) } \
1118 $1 ~ /inet/ { mask = $2; sub(".*/", "", mask); \
1119 print mask, iface, family }'
1124 _addr="${1%/*}" # Remove optional maskbits
1126 set -- $(ip_maskbits_iface $_addr)
1127 if [ -n "$1" ] ; then
1130 echo "Removing public address $_addr/$_maskbits from device $_iface"
1131 delete_ip_from_iface $_iface $_addr $_maskbits >/dev/null 2>&1
1135 drop_all_public_ips ()
1137 while read _ip _x ; do
1139 done <"${CTDB_PUBLIC_ADDRESSES:-/dev/null}"
1142 flush_route_cache ()
1144 set_proc_maybe sys/net/ipv4/route/flush 1
1145 set_proc_maybe sys/net/ipv6/route/flush 1
1148 ########################################################
1150 _ctdb_counter_common () {
1151 _service_name="${1:-${service_name:-${script_name}}}"
1152 _counter_file="$ctdb_fail_dir/$_service_name"
1153 mkdir -p "${_counter_file%/*}" # dirname
1155 ctdb_counter_init () {
1156 _ctdb_counter_common "$1"
1160 ctdb_counter_incr () {
1161 _ctdb_counter_common "$1"
1164 echo -n 1 >> "$_counter_file"
1166 ctdb_counter_get () {
1167 _ctdb_counter_common "$1"
1169 stat -c "%s" "$_counter_file" 2>/dev/null || echo 0
1171 ctdb_check_counter () {
1172 _msg="${1:-error}" # "error" - anything else is silent on fail
1173 _op="${2:--ge}" # an integer operator supported by test
1174 _limit="${3:-${service_fail_limit}}"
1177 _size=$(ctdb_counter_get "$1")
1180 if [ "$_op" != "%" ] ; then
1181 if [ $_size $_op $_limit ] ; then
1185 if [ $(($_size $_op $_limit)) -eq 0 ] ; then
1190 if [ "$_msg" = "error" ] ; then
1191 echo "ERROR: $_size consecutive failures for $_service_name, marking node unhealthy"
1199 ########################################################
1201 ctdb_status_dir="$CTDB_VARDIR/state/service_status"
1202 ctdb_fail_dir="$CTDB_VARDIR/state/failcount"
1204 ctdb_setup_service_state_dir ()
1206 service_state_dir="$CTDB_VARDIR/state/service_state/${1:-${service_name}}"
1207 mkdir -p "$service_state_dir" || {
1208 echo "Error creating state dir \"$service_state_dir\""
1213 ########################################################
1214 # Managed status history, for auto-start/stop
1216 ctdb_managed_dir="$CTDB_VARDIR/state/managed_history"
1218 _ctdb_managed_common ()
1220 _ctdb_managed_file="$ctdb_managed_dir/$service_name"
1223 ctdb_service_managed ()
1225 _ctdb_managed_common
1226 mkdir -p "$ctdb_managed_dir"
1227 touch "$_ctdb_managed_file"
1230 ctdb_service_unmanaged ()
1232 _ctdb_managed_common
1233 rm -f "$_ctdb_managed_file"
1236 is_ctdb_previously_managed_service ()
1238 _ctdb_managed_common
1239 [ -f "$_ctdb_managed_file" ]
1242 ########################################################
1243 # Check and set status
1247 echo "node is \"$1\", \"${script_name}\" reports problem: $(cat $2)"
1252 if [ -r "$ctdb_status_dir/$script_name/unhealthy" ] ; then
1253 log_status_cat "unhealthy" "$ctdb_status_dir/$script_name/unhealthy"
1255 elif [ -r "$ctdb_status_dir/$script_name/banned" ] ; then
1256 log_status_cat "banned" "$ctdb_status_dir/$script_name/banned"
1265 d="$ctdb_status_dir/$script_name"
1272 for i in "banned" "unhealthy" ; do
1279 ##################################################################
1280 # Reconfigure a service on demand
1282 _ctdb_service_reconfigure_common ()
1284 _d="$ctdb_status_dir/${service_name}"
1286 _ctdb_service_reconfigure_flag="$_d/reconfigure"
1289 ctdb_service_needs_reconfigure ()
1291 _ctdb_service_reconfigure_common
1292 [ -e "$_ctdb_service_reconfigure_flag" ]
1295 ctdb_service_set_reconfigure ()
1297 _ctdb_service_reconfigure_common
1298 >"$_ctdb_service_reconfigure_flag"
1301 ctdb_service_unset_reconfigure ()
1303 _ctdb_service_reconfigure_common
1304 rm -f "$_ctdb_service_reconfigure_flag"
1307 ctdb_service_reconfigure ()
1309 echo "Reconfiguring service \"${service_name}\"..."
1310 ctdb_service_unset_reconfigure
1311 service_reconfigure || return $?
1315 # Default service_reconfigure() function does nothing.
1316 service_reconfigure ()
1321 ctdb_reconfigure_take_lock ()
1323 _ctdb_service_reconfigure_common
1324 _lock="${_d}/reconfigure_lock"
1325 mkdir -p "${_lock%/*}" # dirname
1330 # This is overkill but will work if we need to extend this to
1331 # allow certain events to run multiple times in parallel
1332 # (e.g. takeip) and write multiple PIDs to the file.
1334 if [ -n "$_locker_event" ] ; then
1335 while read _pid ; do
1336 if [ -n "$_pid" -a "$_pid" != $$ ] && \
1337 kill -0 "$_pid" 2>/dev/null ; then
1343 printf "%s\n%s\n" "$event_name" $$ >"$_lock"
1348 ctdb_reconfigure_release_lock ()
1350 _ctdb_service_reconfigure_common
1351 _lock="${_d}/reconfigure_lock"
1356 ctdb_replay_monitor_status ()
1358 echo "Replaying previous status for this script due to reconfigure..."
1359 # Leading separator ('|') is missing in some versions...
1360 _out=$(ctdb scriptstatus -X | grep -E "^\|?monitor\|${script_name}\|")
1361 # Output looks like this:
1362 # |monitor|60.nfs|1|ERROR|1314764004.030861|1314764004.035514|foo bar|
1363 # This is the cheapest way of getting fields in the middle.
1364 set -- $(IFS="|" ; echo $_out)
1367 # The error output field can include colons so we'll try to
1368 # preserve them. The weak checking at the beginning tries to make
1369 # this work for both broken (no leading '|') and fixed output.
1371 _err_out="${_out#*monitor|${script_name}|*|*|*|*|}"
1373 OK) : ;; # Do nothing special.
1375 # Recast this as an error, since we can't exit with the
1376 # correct negative number.
1378 _err_out="[Replay of TIMEDOUT scriptstatus - note incorrect return code.] ${_err_out}"
1381 # Recast this as an OK, since we can't exit with the
1382 # correct negative number.
1384 _err_out="[Replay of DISABLED scriptstatus - note incorrect return code.] ${_err_out}"
1386 *) : ;; # Must be ERROR, do nothing special.
1388 if [ -n "$_err_out" ] ; then
1394 ctdb_service_check_reconfigure ()
1398 # We only care about some events in this function. For others we
1400 case "$event_name" in
1401 monitor|ipreallocated|reconfigure) : ;;
1405 if ctdb_reconfigure_take_lock ; then
1406 # No events covered by this function are running, so proceed
1408 case "$event_name" in
1410 (ctdb_service_reconfigure)
1414 if ctdb_service_needs_reconfigure ; then
1415 ctdb_service_reconfigure
1420 ctdb_reconfigure_release_lock
1422 # Somebody else is running an event we don't want to collide
1423 # with. We proceed with caution.
1424 case "$event_name" in
1426 # Tell whoever called us to retry.
1430 # Defer any scheduled reconfigure and just run the
1431 # rest of the ipreallocated event, as per the
1432 # eventscript. There's an assumption here that the
1433 # event doesn't depend on any scheduled reconfigure.
1434 # This is true in the current code.
1438 # There is most likely a reconfigure in progress so
1439 # the service is possibly unstable. As above, we
1440 # defer any scheduled reconfigured. We also replay
1441 # the previous monitor status since that's the best
1442 # information we have.
1443 ctdb_replay_monitor_status
1449 ##################################################################
1450 # Does CTDB manage this service? - and associated auto-start/stop
1452 ctdb_compat_managed_service ()
1454 if [ "$1" = "yes" -a "$2" = "$service_name" ] ; then
1455 CTDB_MANAGED_SERVICES="$CTDB_MANAGED_SERVICES $2"
1459 is_ctdb_managed_service ()
1463 # $t is used just for readability and to allow better accurate
1464 # matching via leading/trailing spaces
1465 t=" $CTDB_MANAGED_SERVICES "
1467 # Return 0 if "<space>$service_name<space>" appears in $t
1468 if [ "${t#* ${service_name} }" != "${t}" ] ; then
1472 # If above didn't match then update $CTDB_MANAGED_SERVICES for
1473 # backward compatibility and try again.
1474 ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD" "vsftpd"
1475 ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA" "samba"
1476 ctdb_compat_managed_service "$CTDB_MANAGES_WINBIND" "winbind"
1477 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "apache2"
1478 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "httpd"
1479 ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI" "iscsi"
1480 ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD" "clamd"
1481 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs"
1482 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs-ganesha-gpfs"
1484 t=" $CTDB_MANAGED_SERVICES "
1486 # Return 0 if "<space>$service_name<space>" appears in $t
1487 [ "${t#* ${service_name} }" != "${t}" ]
1490 ctdb_start_stop_service ()
1494 # Allow service-start/service-stop pseudo-events to start/stop
1495 # services when we're not auto-starting/stopping and we're not
1497 case "$event_name" in
1499 if is_ctdb_managed_service ; then
1500 die 'service-start event not permitted when service is managed'
1502 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1503 die 'service-start event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1509 if is_ctdb_managed_service ; then
1510 die 'service-stop event not permitted when service is managed'
1512 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1513 die 'service-stop event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1520 # Do nothing unless configured to...
1521 [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] || return 0
1523 [ "$event_name" = "monitor" ] || return 0
1525 if is_ctdb_managed_service ; then
1526 if ! is_ctdb_previously_managed_service ; then
1527 echo "Starting service \"$service_name\" - now managed"
1528 background_with_logging ctdb_service_start
1532 if is_ctdb_previously_managed_service ; then
1533 echo "Stopping service \"$service_name\" - no longer managed"
1534 background_with_logging ctdb_service_stop
1540 ctdb_service_start ()
1542 # The service is marked managed if we've ever tried to start it.
1543 ctdb_service_managed
1545 service_start || return $?
1551 ctdb_service_stop ()
1553 ctdb_service_unmanaged
1557 # Default service_start() and service_stop() functions.
1559 # These may be overridden in an eventscript.
1562 service "$service_name" start
1567 service "$service_name" stop
1570 ##################################################################
1572 ctdb_standard_event_handler ()
1589 _family="$1" ; shift
1590 if [ "$_family" = "inet6" ] ; then
1591 _iptables_cmd="ip6tables"
1593 _iptables_cmd="iptables"
1596 # iptables doesn't like being re-entered, so flock-wrap it.
1597 flock -w 30 "${CTDB_VARDIR}/iptables-ctdb.flock" "$_iptables_cmd" "$@"
1600 # AIX (and perhaps others?) doesn't have mktemp
1601 if ! type mktemp >/dev/null 2>&1 ; then
1605 if [ "$1" = "-d" ] ; then
1609 _d="${TMPDIR:-/tmp}"
1610 _hex10=$(dd if=/dev/urandom count=20 2>/dev/null | \
1612 sed -e 's@\(..........\).*@\1@')
1613 _t="${_d}/tmp.${_hex10}"
1626 ########################################################
1628 ########################################################
1634 tickledir="$CTDB_VARDIR/state/tickles"
1635 mkdir -p "$tickledir"
1639 # What public IPs do I hold?
1640 _ips=$(ctdb -X ip | awk -F'|' -v pnn=$pnn '$3 == pnn {print $2}')
1642 # IPs as a regexp choice
1643 _ipschoice="($(echo $_ips | sed -e 's/ /|/g' -e 's/\./\\\\./g'))"
1645 # Record connections to our public IPs in a temporary file
1646 _my_connections="${tickledir}/${_port}.connections"
1647 rm -f "$_my_connections"
1649 awk -v destpat="^${_ipschoice}:${_port}\$" \
1650 '$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ destpat {print $5, $4}' |
1651 sort >"$_my_connections"
1653 # Record our current tickles in a temporary file
1654 _my_tickles="${tickledir}/${_port}.tickles"
1655 rm -f "$_my_tickles"
1656 for _i in $_ips ; do
1657 ctdb -X gettickles $_i $_port |
1658 awk -F'|' 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
1660 sort >"$_my_tickles"
1662 # Add tickles for connections that we haven't already got tickles for
1663 comm -23 "$_my_connections" "$_my_tickles" |
1664 while read _src _dst ; do
1665 ctdb addtickle $_src $_dst
1668 # Remove tickles for connections that are no longer there
1669 comm -13 "$_my_connections" "$_my_tickles" |
1670 while read _src _dst ; do
1671 ctdb deltickle $_src $_dst
1674 rm -f "$_my_connections" "$_my_tickles"
1677 ########################################################
1678 # load a site local config file
1679 ########################################################
1681 [ -n "$CTDB_RC_LOCAL" -a -x "$CTDB_RC_LOCAL" ] && {
1685 [ -x $CTDB_BASE/rc.local ] && {
1686 . $CTDB_BASE/rc.local
1689 [ -d $CTDB_BASE/rc.local.d ] && {
1690 for i in $CTDB_BASE/rc.local.d/* ; do
1691 [ -x "$i" ] && . "$i"
1695 script_name="${0##*/}" # basename
1696 service_fail_limit=1