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
285 _rpc_prog="$_prog_name"
288 case "$_prog_name" in
292 _restart="echo 'Trying to restart NFS service'"
293 _restart="${_restart}; startstop_nfs restart"
296 _opts="${MOUNTD_PORT:+ -p }${MOUNTD_PORT}"
299 _opts="${RQUOTAD_PORT:+ -p }${RQUOTAD_PORT}"
304 _restart="echo 'Trying to restart lock manager service'"
305 _restart="${_restart}; startstop_nfslock restart"
309 _opts="${STATD_HOSTNAME:+ -n }${STATD_HOSTNAME}"
310 _opts="${_opts}${STATD_PORT:+ -p }${STATD_PORT}"
311 _opts="${_opts}${STATD_OUTGOING_PORT:+ -o }${STATD_OUTGOING_PORT}"
314 echo "Internal error: unknown RPC program \"$_prog_name\"."
318 _service_name="nfs_${_prog_name}"
320 if ctdb_check_rpc "$_rpc_prog" $_version >/dev/null ; then
321 ctdb_counter_init "$_service_name"
325 ctdb_counter_incr "$_service_name"
330 _nfs_check_rpc_action ()
336 if ctdb_check_counter "quiet" "$_cmp" "$_limit" "$_service_name" ; then
340 for _action in $_actions ; do
343 echo "$ctdb_check_rpc_out"
346 # No explicit command specified, construct rpc command.
347 if [ -z "$_restart" ] ; then
348 _p="rpc.${_prog_name}"
349 _restart="echo 'Trying to restart $_prog_name [${_p}${_opts}]'"
350 _restart="${_restart}; killall -q -9 $_p"
351 _restart="${_restart}; $_p $_opts"
354 # Process restart flags...
355 _flags="${_action#restart:}"
356 # There may not have been a colon...
357 [ "$_flags" != "$_action" ] || _flags=""
358 # q=quiet - everything to /dev/null
359 if [ "${_flags#*q}" != "$_flags" ] ; then
360 _restart="{ ${_restart} ; } >/dev/null 2>&1"
362 # s=stealthy - last command to /dev/null
363 if [ "${_flags#*s}" != "$_flags" ] ; then
364 _restart="${_restart} >/dev/null 2>&1"
366 # b=background - the whole thing, easy and reliable
367 if [ "${_flags#*b}" != "$_flags" ] ; then
368 _restart="{ ${_restart} ; } &"
378 echo "Internal error: unknown action \"$_action\"."
386 ######################################################
387 # check that a rpc server is registered with portmap
388 # and responding to requests
389 # usage: ctdb_check_rpc SERVICE_NAME VERSION
390 ######################################################
396 if ! ctdb_check_rpc_out=$(rpcinfo -u localhost $progname $version 2>&1) ; then
397 ctdb_check_rpc_out="ERROR: $progname failed RPC check:
399 echo "$ctdb_check_rpc_out"
404 ######################################################
405 # Ensure $service_name is set
406 assert_service_name ()
408 [ -n "$service_name" ] || die "INTERNAL ERROR: \$service_name not set"
411 ######################################################
412 # check a set of directories is available
413 # return 1 on a missing directory
414 # directories are read from stdin
415 ######################################################
416 ctdb_check_directories_probe()
418 while IFS="" read d ; do
424 [ -d "${d}/." ] || return 1
429 ######################################################
430 # check a set of directories is available
431 # directories are read from stdin
432 ######################################################
433 ctdb_check_directories()
435 ctdb_check_directories_probe || {
436 echo "ERROR: $service_name directory \"$d\" not available"
441 ######################################################
442 # check a set of tcp ports
443 # usage: ctdb_check_tcp_ports <ports...>
444 ######################################################
446 # This flag file is created when a service is initially started. It
447 # is deleted the first time TCP port checks for that service succeed.
448 # Until then ctdb_check_tcp_ports() prints a more subtle "error"
449 # message if a port check fails.
450 _ctdb_check_tcp_common ()
453 _ctdb_service_started_file="$ctdb_fail_dir/$service_name.started"
456 ctdb_check_tcp_init ()
458 _ctdb_check_tcp_common
459 mkdir -p "${_ctdb_service_started_file%/*}" # dirname
460 touch "$_ctdb_service_started_file"
463 ctdb_check_tcp_ports()
465 if [ -z "$1" ] ; then
466 echo "INTERNAL ERROR: ctdb_check_tcp_ports - no ports specified"
470 # Set default value for CTDB_TCP_PORT_CHECKS if unset.
471 # If any of these defaults are unsupported then this variable can
472 # be overridden in /etc/sysconfig/ctdb or via a file in
473 # /etc/ctdb/rc.local.d/.
474 : ${CTDB_TCP_PORT_CHECKERS:=ctdb nmap netstat}
476 for _c in $CTDB_TCP_PORT_CHECKERS ; do
477 ctdb_check_tcp_ports_$_c "$@"
480 _ctdb_check_tcp_common
481 rm -f "$_ctdb_service_started_file"
485 _ctdb_check_tcp_common
486 if [ ! -f "$_ctdb_service_started_file" ] ; then
487 echo "ERROR: $service_name tcp port $_p is not responding"
489 $ctdb_check_tcp_ports_debug
492 echo "INFO: $service_name tcp port $_p is not responding"
499 ctdb_check_ports - checker $_c not implemented
500 output from checker was:
501 $ctdb_check_tcp_ports_debug
509 echo "INTERNAL ERROR: ctdb_check_ports - no working checkers in CTDB_TCP_PORT_CHECKERS=\"$CTDB_TCP_PORT_CHECKERS\""
514 ctdb_check_tcp_ports_netstat ()
516 _cmd='netstat -l -t -n'
518 if [ $? -eq 127 ] ; then
519 # netstat probably not installed - unlikely?
520 ctdb_check_tcp_ports_debug="$_ns"
524 for _p ; do # process each function argument (port)
525 for _a in '0\.0\.0\.0' '::' ; do
526 _pat="[[:space:]]${_a}:${_p}[[:space:]]+[^[:space:]]+[[:space:]]+LISTEN"
527 if echo "$_ns" | grep -E -q "$_pat" ; then
528 # We matched the port, so process next port
533 # We didn't match the port, so flag an error.
534 ctdb_check_tcp_ports_debug="$_cmd shows this output:
542 ctdb_check_tcp_ports_nmap ()
544 # nmap wants a comma-separated list of ports
547 _ports="${_ports}${_ports:+,}${_p}"
550 _cmd="nmap -n -oG - -PS 127.0.0.1 -p $_ports"
552 _nmap_out=$($_cmd 2>&1)
553 if [ $? -eq 127 ] ; then
554 # nmap probably not installed
555 ctdb_check_tcp_ports_debug="$_nmap_out"
559 # get the port-related output
560 _port_info=$(echo "$_nmap_out" | sed -n -r -e 's@^.*Ports:[[:space:]]@@p')
563 # looking for something like this:
564 # 445/open/tcp//microsoft-ds///
565 # possibly followed by a comma
567 case "$_port_info" in
568 # The info we're after must be either at the beginning of
569 # the string or it must follow a space.
572 # Nope, flag an error...
573 ctdb_check_tcp_ports_debug="$_cmd shows this output:
582 # Use the new "ctdb checktcpport" command to check the port.
583 # This is very cheap.
584 ctdb_check_tcp_ports_ctdb ()
586 for _p ; do # process each function argument (port)
587 _cmd="ctdb checktcpport $_p"
592 ctdb_check_tcp_ports_debug="\"$_cmd\" was able to bind to port"
596 # Couldn't bind, something already listening, next port...
600 ctdb_check_tcp_ports_debug="$_cmd (exited with $_ret) with output:
602 # assume not implemented
610 ######################################################
611 # check a unix socket
612 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
613 ######################################################
614 ctdb_check_unix_socket() {
616 [ -z "$socket_path" ] && return
618 if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
619 echo "ERROR: $service_name socket $socket_path not found"
624 ######################################################
625 # check a command returns zero status
626 # usage: ctdb_check_command <command>
627 ######################################################
628 ctdb_check_command ()
630 _out=$("$@" 2>&1) || {
631 echo "ERROR: $* returned error"
637 ################################################
638 # kill off any TCP connections with the given IP
639 ################################################
640 kill_tcp_connections ()
645 if [ "$2" = "oneway" ] ; then
649 get_tcp_connections_for_ip "$_ip" | {
653 while read dest src; do
654 echo "Killing TCP connection $src $dest"
655 ctdb killtcp $src $dest >/dev/null 2>&1 || _failed=true
656 _destport="${dest##*:}"
659 # we only do one-way killtcp for CIFS
660 139|445) __oneway=true ;;
662 if ! $__oneway ; then
663 ctdb killtcp $dest $src >/dev/null 2>&1 || _failed=true
666 _killcount=$(($_killcount + 1))
670 echo "Failed to send killtcp control"
673 if [ $_killcount -eq 0 ] ; then
679 if [ -z "$(get_tcp_connections_for_ip $_ip)" ] ; then
680 echo "Killed $_killcount TCP connections to released IP $_ip"
684 _count=$(($_count + 1))
685 if [ $_count -gt 3 ] ; then
686 echo "Timed out killing tcp connections for IP $_ip"
695 ##################################################################
696 # kill off the local end for any TCP connections with the given IP
697 ##################################################################
698 kill_tcp_connections_local_only ()
700 kill_tcp_connections "$1" "oneway"
703 ##################################################################
704 # tickle any TCP connections with the given IP
705 ##################################################################
706 tickle_tcp_connections ()
710 get_tcp_connections_for_ip "$_ip" |
714 while read dest src; do
715 echo "Tickle TCP connection $src $dest"
716 ctdb tickle $src $dest >/dev/null 2>&1 || _failed=true
717 echo "Tickle TCP connection $dest $src"
718 ctdb tickle $dest $src >/dev/null 2>&1 || _failed=true
722 echo "Failed to send tickle control"
727 get_tcp_connections_for_ip ()
731 netstat -tn | awk -v ip=$_ip \
732 'index($1, "tcp") == 1 && \
733 (index($4, ip ":") == 1 || index($4, "::ffff:" ip ":") == 1) \
734 && $6 == "ESTABLISHED" \
738 ########################################################
739 # start/stop the Ganesha nfs service
740 ########################################################
743 _service_name="nfs-ganesha-$CTDB_CLUSTER_FILESYSTEM_TYPE"
746 service "$_service_name" start
749 service "$_service_name" stop
752 service "$_service_name" restart
757 ########################################################
758 # start/stop the nfs service on different platforms
759 ########################################################
762 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
765 [ -x $CTDB_ETCDIR/init.d/nfslock ] && {
773 service nfsserver start
776 service nfsserver stop > /dev/null 2>&1
779 set_proc "fs/nfsd/threads" 0
780 service nfsserver stop > /dev/null 2>&1
782 nfs_dump_some_threads
783 service nfsserver start
790 service nfslock start
798 set_proc "fs/nfsd/threads" 0
799 service nfs stop > /dev/null 2>&1
800 service nfslock stop > /dev/null 2>&1
802 nfs_dump_some_threads
803 service nfslock start
809 echo "Unknown platform. NFS is not supported with ctdb"
815 # Dump up to the configured number of nfsd thread backtraces.
816 nfs_dump_some_threads ()
818 [ -n "$CTDB_NFS_DUMP_STUCK_THREADS" ] || return 0
820 # Optimisation to avoid running an unnecessary pidof
821 [ $CTDB_NFS_DUMP_STUCK_THREADS -gt 0 ] || return 0
824 for _pid in $(pidof nfsd) ; do
825 [ $_count -le $CTDB_NFS_DUMP_STUCK_THREADS ] || break
827 # Do this first to avoid racing with thread exit
828 _stack=$(get_proc "${_pid}/stack" 2>/dev/null)
829 if [ -n "$_stack" ] ; then
830 echo "Stack trace for stuck nfsd thread [${_pid}]:"
832 _count=$(($_count + 1))
837 ########################################################
838 # start/stop the nfs lockmanager service on different platforms
839 ########################################################
840 startstop_nfslock() {
842 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
845 [ -x $CTDB_ETCDIR/init.d/nfslock ] && {
851 # for sles there is no service for lockmanager
852 # so we instead just shutdown/restart nfs
855 service nfsserver start
858 service nfsserver stop > /dev/null 2>&1
861 service nfsserver stop
862 service nfsserver start
869 service nfslock start
872 service nfslock stop > /dev/null 2>&1
876 service nfslock start
881 echo "Unknown platform. NFS locking is not supported with ctdb"
887 # Periodically update the statd database
892 _statd_update_trigger="$service_state_dir/update-trigger"
893 [ -f "$_statd_update_trigger" ] || touch "$_statd_update_trigger"
895 _last_update=$(stat --printf="%Y" "$_statd_update_trigger")
896 _current_time=$(date +"%s")
897 if [ $(( $_current_time - $_last_update)) -ge $_update_period ] ; then
898 touch "$_statd_update_trigger"
899 $CTDB_BASE/statd-callout updatelocal &
900 $CTDB_BASE/statd-callout updateremote &
910 _lockfile="${CTDB_VARDIR}/state/interface_modify_${_iface}.flock"
911 mkdir -p "${_lockfile%/*}" # dirname
912 [ -f "$_lockfile" ] || touch "$_lockfile"
915 # Note: use of return/exit/die() below only gets us out of the
916 # sub-shell, which is actually what we want. That is, the
917 # function should just return non-zero.
919 flock --timeout 30 0 || \
920 die "add_ip_to_iface: unable to get lock for ${_iface}"
922 # Ensure interface is up
923 ip link set "$_iface" up || \
924 die "Failed to bringup interface $_iface"
926 ip addr add "$_ip/$_maskbits" brd + dev "$_iface" || \
927 die "Failed to add $_ip/$_maskbits on dev $_iface"
930 # Do nothing here - return above only gets us out of the subshell
931 # and doing anything here will affect the return code.
934 delete_ip_from_iface()
940 _lockfile="${CTDB_VARDIR}/state/interface_modify_${_iface}.flock"
941 mkdir -p "${_lockfile%/*}" # dirname
942 [ -f "$_lockfile" ] || touch "$_lockfile"
945 # Note: use of return/exit/die() below only gets us out of the
946 # sub-shell, which is actually what we want. That is, the
947 # function should just return non-zero.
949 flock --timeout 30 0 || \
950 die "delete_ip_from_iface: unable to get lock for ${_iface}"
952 _im="$_ip/$_maskbits" # shorthand for readability
954 # "ip addr del" will delete all secondary IPs if this is the
955 # primary. To work around this _very_ annoying behaviour we
956 # have to keep a record of the secondaries and re-add them
960 if ip addr list dev "$_iface" primary | grep -Fq "inet $_im " ; then
961 _secondaries=$(ip addr list dev "$_iface" secondary | \
962 awk '$1 == "inet" { print $2 }')
966 ip addr del "$_im" dev "$_iface" || {
967 echo "Failed to del $_ip on dev $_iface"
971 if [ -n "$_secondaries" ] ; then
972 for _i in $_secondaries; do
973 if ip addr list dev "$_iface" | grep -Fq "inet $_i" ; then
974 echo "Kept secondary $_i on dev $_iface"
976 echo "Re-adding secondary address $_i to dev $_iface"
977 ip addr add $_i brd + dev $_iface || {
978 echo "Failed to re-add address $_i to dev $_iface"
988 # Do nothing here - return above only gets us out of the subshell
989 # and doing anything here will affect the return code.
992 # If the given IP is hosted then print 2 items: maskbits and iface
997 ip addr show to "${_addr}/32" 2>/dev/null | \
998 awk '$1 == "inet" { print gensub(".*/", "", 1, $2), $NF }'
1003 _addr="${1%/*}" # Remove optional maskbits
1005 set -- $(ip_maskbits_iface $_addr)
1006 if [ -n "$1" ] ; then
1009 echo "Removing public address $_addr/$_maskbits from device $_iface"
1010 delete_ip_from_iface $_iface $_addr $_maskbits >/dev/null 2>&1
1014 drop_all_public_ips ()
1016 while read _ip _x ; do
1018 done <"${CTDB_PUBLIC_ADDRESSES:-/dev/null}"
1021 ########################################################
1022 # some simple logic for counting events - per eventscript
1023 # usage: ctdb_counter_init
1025 # ctdb_check_counter_limit <limit>
1026 # ctdb_check_counter_limit fails when count >= <limit>
1027 ########################################################
1028 _ctdb_counter_common () {
1029 _service_name="${1:-${service_name:-${script_name}}}"
1030 _counter_file="$ctdb_fail_dir/$_service_name"
1031 mkdir -p "${_counter_file%/*}" # dirname
1033 ctdb_counter_init () {
1034 _ctdb_counter_common "$1"
1038 ctdb_counter_incr () {
1039 _ctdb_counter_common "$1"
1042 echo -n 1 >> "$_counter_file"
1044 ctdb_check_counter () {
1045 _msg="${1:-error}" # "error" - anything else is silent on fail
1046 _op="${2:--ge}" # an integer operator supported by test
1047 _limit="${3:-${service_fail_limit}}"
1049 _ctdb_counter_common "$1"
1052 _size=$(stat -c "%s" "$_counter_file" 2>/dev/null || echo 0)
1053 if [ $_size $_op $_limit ] ; then
1054 if [ "$_msg" = "error" ] ; then
1055 echo "ERROR: $_limit consecutive failures for $_service_name, marking node unhealthy"
1063 ########################################################
1065 ctdb_status_dir="$CTDB_VARDIR/status"
1066 ctdb_fail_dir="$CTDB_VARDIR/failcount"
1068 ctdb_setup_service_state_dir ()
1070 service_state_dir="$CTDB_VARDIR/state/${1:-${service_name}}"
1071 mkdir -p "$service_state_dir" || {
1072 echo "Error creating state dir \"$service_state_dir\""
1077 ########################################################
1078 # Managed status history, for auto-start/stop
1080 ctdb_managed_dir="$CTDB_VARDIR/managed_history"
1082 _ctdb_managed_common ()
1084 _ctdb_managed_file="$ctdb_managed_dir/$service_name"
1087 ctdb_service_managed ()
1089 _ctdb_managed_common
1090 mkdir -p "$ctdb_managed_dir"
1091 touch "$_ctdb_managed_file"
1094 ctdb_service_unmanaged ()
1096 _ctdb_managed_common
1097 rm -f "$_ctdb_managed_file"
1100 is_ctdb_previously_managed_service ()
1102 _ctdb_managed_common
1103 [ -f "$_ctdb_managed_file" ]
1106 ########################################################
1107 # Check and set status
1111 echo "node is \"$1\", \"${script_name}\" reports problem: $(cat $2)"
1116 if [ -r "$ctdb_status_dir/$script_name/unhealthy" ] ; then
1117 log_status_cat "unhealthy" "$ctdb_status_dir/$script_name/unhealthy"
1119 elif [ -r "$ctdb_status_dir/$script_name/banned" ] ; then
1120 log_status_cat "banned" "$ctdb_status_dir/$script_name/banned"
1129 d="$ctdb_status_dir/$script_name"
1136 for i in "banned" "unhealthy" ; do
1143 ##################################################################
1144 # Reconfigure a service on demand
1146 _ctdb_service_reconfigure_common ()
1148 _d="$ctdb_status_dir/${service_name}"
1150 _ctdb_service_reconfigure_flag="$_d/reconfigure"
1153 ctdb_service_needs_reconfigure ()
1155 _ctdb_service_reconfigure_common
1156 [ -e "$_ctdb_service_reconfigure_flag" ]
1159 ctdb_service_set_reconfigure ()
1161 _ctdb_service_reconfigure_common
1162 >"$_ctdb_service_reconfigure_flag"
1165 ctdb_service_unset_reconfigure ()
1167 _ctdb_service_reconfigure_common
1168 rm -f "$_ctdb_service_reconfigure_flag"
1171 ctdb_service_reconfigure ()
1173 echo "Reconfiguring service \"${service_name}\"..."
1174 ctdb_service_unset_reconfigure
1175 service_reconfigure || return $?
1179 # Default service_reconfigure() function does nothing.
1180 service_reconfigure ()
1185 ctdb_reconfigure_try_lock ()
1187 _ctdb_service_reconfigure_common
1188 _lock="${_d}/reconfigure_lock"
1189 mkdir -p "${_lock%/*}" # dirname
1194 # This is overkill but will work if we need to extend this to
1195 # allow certain events to run multiple times in parallel
1196 # (e.g. takeip) and write multiple PIDs to the file.
1198 if [ -n "$_locker_event" ] ; then
1199 while read _pid ; do
1200 if [ -n "$_pid" -a "$_pid" != $$ ] && \
1201 kill -0 "$_pid" 2>/dev/null ; then
1207 printf "%s\n%s\n" "$event_name" $$ >"$_lock"
1212 ctdb_replay_monitor_status ()
1214 echo "Replaying previous status for this script due to reconfigure..."
1215 # Leading colon (':') is missing in some versions...
1216 _out=$(ctdb scriptstatus -Y | grep -E "^:?monitor:${script_name}:")
1217 # Output looks like this:
1218 # :monitor:60.nfs:1:ERROR:1314764004.030861:1314764004.035514:foo bar:
1219 # This is the cheapest way of getting fields in the middle.
1220 set -- $(IFS=":" ; echo $_out)
1223 # The error output field can include colons so we'll try to
1224 # preserve them. The weak checking at the beginning tries to make
1225 # this work for both broken (no leading ':') and fixed output.
1227 _err_out="${_out#*monitor:${script_name}:*:*:*:*:}"
1229 OK) : ;; # Do nothing special.
1231 # Recast this as an error, since we can't exit with the
1232 # correct negative number.
1234 _err_out="[Replay of TIMEDOUT scriptstatus - note incorrect return code.] ${_err_out}"
1237 # Recast this as an OK, since we can't exit with the
1238 # correct negative number.
1240 _err_out="[Replay of DISABLED scriptstatus - note incorrect return code.] ${_err_out}"
1242 *) : ;; # Must be ERROR, do nothing special.
1248 ctdb_service_check_reconfigure ()
1252 # We only care about some events in this function. For others we
1254 case "$event_name" in
1255 monitor|ipreallocated|reconfigure) : ;;
1259 if ctdb_reconfigure_try_lock ; then
1260 # No events covered by this function are running, so proceed
1262 case "$event_name" in
1264 (ctdb_service_reconfigure)
1268 if ctdb_service_needs_reconfigure ; then
1269 ctdb_service_reconfigure
1273 if ctdb_service_needs_reconfigure ; then
1274 ctdb_service_reconfigure
1275 # Given that the reconfigure might not have
1276 # resulted in the service being stable yet, we
1277 # replay the previous status since that's the best
1278 # information we have.
1279 ctdb_replay_monitor_status
1284 # Somebody else is running an event we don't want to collide
1285 # with. We proceed with caution.
1286 case "$event_name" in
1288 # Tell whoever called us to retry.
1292 # Defer any scheduled reconfigure and just run the
1293 # rest of the ipreallocated event, as per the
1294 # eventscript. There's an assumption here that the
1295 # event doesn't depend on any scheduled reconfigure.
1296 # This is true in the current code.
1300 # There is most likely a reconfigure in progress so
1301 # the service is possibly unstable. As above, we
1302 # defer any scheduled reconfigured. We also replay
1303 # the previous monitor status since that's the best
1304 # information we have.
1305 ctdb_replay_monitor_status
1311 ##################################################################
1312 # Does CTDB manage this service? - and associated auto-start/stop
1314 ctdb_compat_managed_service ()
1316 if [ "$1" = "yes" -a "$2" = "$service_name" ] ; then
1317 CTDB_MANAGED_SERVICES="$CTDB_MANAGED_SERVICES $2"
1321 is_ctdb_managed_service ()
1325 # $t is used just for readability and to allow better accurate
1326 # matching via leading/trailing spaces
1327 t=" $CTDB_MANAGED_SERVICES "
1329 # Return 0 if "<space>$service_name<space>" appears in $t
1330 if [ "${t#* ${service_name} }" != "${t}" ] ; then
1334 # If above didn't match then update $CTDB_MANAGED_SERVICES for
1335 # backward compatibility and try again.
1336 ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD" "vsftpd"
1337 ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA" "samba"
1338 ctdb_compat_managed_service "$CTDB_MANAGES_SCP" "scp"
1339 ctdb_compat_managed_service "$CTDB_MANAGES_WINBIND" "winbind"
1340 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "apache2"
1341 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "httpd"
1342 ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI" "iscsi"
1343 ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD" "clamd"
1344 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs"
1345 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs-ganesha-gpfs"
1347 t=" $CTDB_MANAGED_SERVICES "
1349 # Return 0 if "<space>$service_name<space>" appears in $t
1350 [ "${t#* ${service_name} }" != "${t}" ]
1353 ctdb_start_stop_service ()
1357 # Allow service-start/service-stop pseudo-events to start/stop
1358 # services when we're not auto-starting/stopping and we're not
1360 case "$event_name" in
1362 if is_ctdb_managed_service ; then
1363 die 'service-start event not permitted when service is managed'
1365 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1366 die 'service-start event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1372 if is_ctdb_managed_service ; then
1373 die 'service-stop event not permitted when service is managed'
1375 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1376 die 'service-stop event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1383 # Do nothing unless configured to...
1384 [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] || return 0
1386 [ "$event_name" = "monitor" ] || return 0
1388 if is_ctdb_managed_service ; then
1389 if ! is_ctdb_previously_managed_service ; then
1390 echo "Starting service \"$service_name\" - now managed"
1391 background_with_logging ctdb_service_start
1395 if is_ctdb_previously_managed_service ; then
1396 echo "Stopping service \"$service_name\" - no longer managed"
1397 background_with_logging ctdb_service_stop
1403 ctdb_service_start ()
1405 # The service is marked managed if we've ever tried to start it.
1406 ctdb_service_managed
1408 service_start || return $?
1414 ctdb_service_stop ()
1416 ctdb_service_unmanaged
1420 # Default service_start() and service_stop() functions.
1422 # These may be overridden in an eventscript. When overriding, the
1423 # following convention must be followed. If these functions are
1424 # called with no arguments then they may use internal logic to
1425 # determine whether the service is managed and, therefore, whether
1426 # they should take any action. However, if the service name is
1427 # specified as an argument then an attempt must be made to start or
1428 # stop the service. This is because the auto-start/stop code calls
1429 # them with the service name as an argument.
1432 service "$service_name" start
1437 service "$service_name" stop
1440 ##################################################################
1442 ctdb_standard_event_handler ()
1457 # iptables doesn't like being re-entered, so flock-wrap it.
1460 flock -w 30 $CTDB_VARDIR/iptables-ctdb.flock /sbin/iptables "$@"
1463 # AIX (and perhaps others?) doesn't have mktemp
1464 if ! which mktemp >/dev/null 2>&1 ; then
1468 if [ "$1" = "-d" ] ; then
1472 _d="${TMPDIR:-/tmp}"
1473 _hex10=$(dd if=/dev/urandom count=20 2>/dev/null | \
1475 sed -e 's@\(..........\).*@\1@')
1476 _t="${_d}/tmp.${_hex10}"
1489 ########################################################
1491 ########################################################
1497 tickledir="$CTDB_VARDIR/state/tickles"
1498 mkdir -p "$tickledir"
1501 _pnn=$(ctdb pnn) ; _pnn=${_pnn#PNN:}
1503 # What public IPs do I hold?
1504 _ips=$(ctdb -Y ip | awk -F: -v pnn=$_pnn '$3 == pnn {print $2}')
1506 # IPs as a regexp choice
1507 _ipschoice="($(echo $_ips | sed -e 's/ /|/g' -e 's/\./\\\\./g'))"
1509 # Record connections to our public IPs in a temporary file
1510 _my_connections="${tickledir}/${_port}.connections"
1511 rm -f "$_my_connections"
1513 awk -v destpat="^${_ipschoice}:${_port}\$" \
1514 '$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ destpat {print $5, $4}' |
1515 sort >"$_my_connections"
1517 # Record our current tickles in a temporary file
1518 _my_tickles="${tickledir}/${_port}.tickles"
1519 rm -f "$_my_tickles"
1520 for _i in $_ips ; do
1521 ctdb -Y gettickles $_i $_port |
1522 awk -F: 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
1524 sort >"$_my_tickles"
1526 # Add tickles for connections that we haven't already got tickles for
1527 comm -23 "$_my_connections" "$_my_tickles" |
1528 while read _src _dst ; do
1529 ctdb addtickle $_src $_dst
1532 # Remove tickles for connections that are no longer there
1533 comm -13 "$_my_connections" "$_my_tickles" |
1534 while read _src _dst ; do
1535 ctdb deltickle $_src $_dst
1538 rm -f "$_my_connections" "$_my_tickles"
1541 ########################################################
1542 # load a site local config file
1543 ########################################################
1545 [ -n "$CTDB_RC_LOCAL" -a -x "$CTDB_RC_LOCAL" ] && {
1549 [ -x $CTDB_BASE/rc.local ] && {
1550 . $CTDB_BASE/rc.local
1553 [ -d $CTDB_BASE/rc.local.d ] && {
1554 for i in $CTDB_BASE/rc.local.d/* ; do
1555 [ -x "$i" ] && . "$i"
1559 script_name="${0##*/}" # basename
1560 service_fail_limit=1