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"
224 ######################################################
225 # wrapper around getting file contents from /proc/ to allow
226 # this to be hooked for testing
227 # 1st arg is relative path under /proc/
233 ######################################################
234 # Print up to $_max kernel stack traces for processes named $_program
235 program_stack_traces ()
241 for _pid in $(pidof "$_prog") ; do
242 [ $_count -le $_max ] || break
244 # Do this first to avoid racing with process exit
245 _stack=$(get_proc "${_pid}/stack" 2>/dev/null)
246 if [ -n "$_stack" ] ; then
247 echo "Stack trace for ${_prog}[${_pid}]:"
249 _count=$(($_count + 1))
254 ######################################################
255 # Check that an RPC service is healthy -
256 # this includes allowing a certain number of failures
257 # before marking the NFS service unhealthy.
259 # usage: nfs_check_rpc_service SERVICE_NAME [ triple ...]
261 # each triple is a set of 3 arguments: an operator, a
262 # fail count limit and an action string.
266 # nfs_check_rpc_service "lockd" \
267 # -ge 15 "verbose restart unhealthy" \
268 # -eq 10 "restart:bs"
270 # says that if lockd is down for 15 iterations then do
271 # a verbose restart of lockd and mark the node unhealthy.
272 # Before this, after 10 iterations of failure, the
273 # service is restarted silently in the background.
274 # Order is important: the number of failures need to be
275 # specified in reverse order because processing stops
276 # after the first condition that is true.
277 ######################################################
278 nfs_check_rpc_service ()
280 _prog_name="$1" ; shift
282 if _nfs_check_rpc_common "$_prog_name" ; then
286 while [ -n "$3" ] ; do
287 if _nfs_check_rpc_action "$1" "$2" "$3" ; then
294 # The new way of doing things...
295 nfs_check_rpc_services ()
297 # Files must end with .check - avoids editor backups, RPM fu, ...
298 for _f in "${CTDB_BASE}/nfs-rpc-checks.d/"[0-9][0-9].*.check ; do
300 _prog_name="${_t##*/[0-9][0-9].}"
302 # If $_prog_name contains '@' then the bit after it is the
304 _family="${_prog_name#*@}"
305 if [ "$_family" = "$_prog_name" ] ; then
308 _prog_name="${_prog_name%@*}"
311 if _nfs_check_rpc_common "$_prog_name" "$_family" ; then
312 # This RPC service is up, check next service...
316 # Check each line in the file in turn until one of the limit
318 while read _cmp _lim _rest ; do
324 if _nfs_check_rpc_action "$_cmp" "$_lim" "$_rest" ; then
325 # Limit was hit on this line, no further checking...
332 _nfs_check_rpc_common ()
337 # Some platforms don't have separate programs for all services.
338 case "$_prog_name" in
340 type "rpc.${_prog_name}" >/dev/null 2>&1 || return 0
343 case "$_prog_name" in
365 echo "Internal error: unknown RPC program \"$_prog_name\"."
369 _service_name="nfs_${_prog_name}${_family:+_}${_family}"
371 if ctdb_check_rpc "$_rpc_prog" "$_version" "$_family" >/dev/null ; then
372 ctdb_counter_init "$_service_name"
376 ctdb_counter_incr "$_service_name"
381 _nfs_check_rpc_action ()
387 if ctdb_check_counter "quiet" "$_cmp" "$_limit" "$_service_name" ; then
391 for _action in $_actions ; do
394 echo "$ctdb_check_rpc_out"
397 _nfs_restart_rpc_service "$_prog_name"
400 _nfs_restart_rpc_service "$_prog_name" true
406 echo "Internal error: unknown action \"$_action\"."
414 _nfs_restart_rpc_service ()
417 _background="${2:-false}"
419 if $_background ; then
420 _maybe_background="background_with_logging"
425 _p="rpc.${_prog_name}"
427 case "$_prog_name" in
429 echo "Trying to restart NFS service"
430 $_maybe_background startstop_nfs restart
433 echo "Trying to restart $_prog_name [${_p}]"
435 nfs_dump_some_threads "$_p"
436 $_maybe_background $_p $RPCMOUNTDOPTS \
437 ${MOUNTD_PORT:+-p} $MOUNTD_PORT
440 echo "Trying to restart $_prog_name [${_p}]"
442 nfs_dump_some_threads "$_p"
443 $_maybe_background $_p ${RQUOTAD_PORT:+-p} $RQUOTAD_PORT
446 echo "Trying to restart lock manager service"
447 $_maybe_background startstop_nfslock restart
450 echo "Trying to restart $_prog_name [${_p}]"
452 nfs_dump_some_threads "$_p"
453 $_maybe_background $_p \
454 ${STATD_HOSTNAME:+-n} $STATD_HOSTNAME \
455 ${STATD_PORT:+-p} $STATD_PORT \
456 ${STATD_OUTGOING_PORT:+-o} $STATD_OUTGOING_PORT
459 echo "Internal error: unknown RPC program \"$_prog_name\"."
464 ######################################################
465 # check that a rpc server is registered with portmap
466 # and responding to requests
467 # usage: ctdb_check_rpc SERVICE_NAME VERSION
468 ######################################################
475 _localhost="${CTDB_RPCINFO_LOCALHOST:-127.0.0.1}"
477 if ! ctdb_check_rpc_out=$(rpcinfo -T $_family $_localhost $progname $version 2>&1) ; then
478 ctdb_check_rpc_out="ERROR: $progname failed RPC check:
480 echo "$ctdb_check_rpc_out"
485 ######################################################
486 # Ensure $service_name is set
487 assert_service_name ()
489 [ -n "$service_name" ] || die "INTERNAL ERROR: \$service_name not set"
492 ######################################################
493 # check a set of directories is available
494 # return 1 on a missing directory
495 # directories are read from stdin
496 ######################################################
497 ctdb_check_directories_probe()
499 while IFS="" read d ; do
505 [ -d "${d}/." ] || return 1
510 ######################################################
511 # check a set of directories is available
512 # directories are read from stdin
513 ######################################################
514 ctdb_check_directories()
516 ctdb_check_directories_probe || {
517 echo "ERROR: $service_name directory \"$d\" not available"
522 ######################################################
523 # check a set of tcp ports
524 # usage: ctdb_check_tcp_ports <ports...>
525 ######################################################
527 # This flag file is created when a service is initially started. It
528 # is deleted the first time TCP port checks for that service succeed.
529 # Until then ctdb_check_tcp_ports() prints a more subtle "error"
530 # message if a port check fails.
531 _ctdb_check_tcp_common ()
534 _ctdb_service_started_file="$ctdb_fail_dir/$service_name.started"
537 ctdb_check_tcp_init ()
539 _ctdb_check_tcp_common
540 mkdir -p "${_ctdb_service_started_file%/*}" # dirname
541 touch "$_ctdb_service_started_file"
544 # Check whether something is listening on all of the given TCP ports
545 # using the "ctdb checktcpport" command.
546 ctdb_check_tcp_ports()
548 if [ -z "$1" ] ; then
549 echo "INTERNAL ERROR: ctdb_check_tcp_ports - no ports specified"
553 for _p ; do # process each function argument (port)
554 _cmd="ctdb checktcpport $_p"
559 _ctdb_check_tcp_common
560 if [ ! -f "$_ctdb_service_started_file" ] ; then
561 echo "ERROR: $service_name tcp port $_p is not responding"
562 debug "\"ctdb checktcpport $_p\" was able to bind to port"
564 echo "INFO: $service_name tcp port $_p is not responding"
570 # Couldn't bind, something already listening, next port...
574 echo "ERROR: unexpected error running \"ctdb checktcpport\""
576 ctdb checktcpport (exited with $_ret) with output:
583 # All ports listening
584 _ctdb_check_tcp_common
585 rm -f "$_ctdb_service_started_file"
589 ######################################################
590 # check a unix socket
591 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
592 ######################################################
593 ctdb_check_unix_socket() {
595 [ -z "$socket_path" ] && return
597 if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
598 echo "ERROR: $service_name socket $socket_path not found"
603 ######################################################
604 # check a command returns zero status
605 # usage: ctdb_check_command <command>
606 ######################################################
607 ctdb_check_command ()
609 _out=$("$@" 2>&1) || {
610 echo "ERROR: $* returned error"
616 ################################################
617 # kill off any TCP connections with the given IP
618 ################################################
619 kill_tcp_connections ()
624 if [ "$2" = "oneway" ] ; then
628 get_tcp_connections_for_ip "$_ip" | {
633 while read _dst _src; do
634 _destport="${_dst##*:}"
637 # we only do one-way killtcp for CIFS
638 139|445) __oneway=true ;;
641 echo "Killing TCP connection $_src $_dst"
642 _connections="${_connections}${_nl}${_src} ${_dst}"
643 if ! $__oneway ; then
644 _connections="${_connections}${_nl}${_dst} ${_src}"
647 _killcount=$(($_killcount + 1))
650 if [ $_killcount -eq 0 ] ; then
654 echo "$_connections" | ctdb killtcp || {
655 echo "Failed to send killtcp control"
661 _remaining=$(get_tcp_connections_for_ip $_ip | wc -l)
663 if [ $_remaining -eq 0 ] ; then
664 echo "Killed $_killcount TCP connections to released IP $_ip"
668 _count=$(($_count + 1))
669 if [ $_count -gt 3 ] ; then
670 echo "Timed out killing tcp connections for IP $_ip ($_remaining remaining)"
674 echo "Waiting for $_remaining connections to be killed for IP $_ip"
680 ##################################################################
681 # kill off the local end for any TCP connections with the given IP
682 ##################################################################
683 kill_tcp_connections_local_only ()
685 kill_tcp_connections "$1" "oneway"
688 ##################################################################
689 # tickle any TCP connections with the given IP
690 ##################################################################
691 tickle_tcp_connections ()
695 get_tcp_connections_for_ip "$_ip" |
699 while read dest src; do
700 echo "Tickle TCP connection $src $dest"
701 ctdb tickle $src $dest >/dev/null 2>&1 || _failed=true
702 echo "Tickle TCP connection $dest $src"
703 ctdb tickle $dest $src >/dev/null 2>&1 || _failed=true
707 echo "Failed to send tickle control"
712 get_tcp_connections_for_ip ()
716 netstat -tn | awk -v ip=$_ip \
717 'index($1, "tcp") == 1 && \
718 (index($4, ip ":") == 1 || index($4, "::ffff:" ip ":") == 1) \
719 && $6 == "ESTABLISHED" \
723 ##################################################################
724 # use statd-callout to update NFS lock info
725 ##################################################################
726 nfs_update_lock_info ()
728 if [ -x "$CTDB_BASE/statd-callout" ] ; then
729 "$CTDB_BASE/statd-callout" update
733 ########################################################
734 # start/stop the Ganesha nfs service
735 ########################################################
738 _service_name="nfs-ganesha-$CTDB_CLUSTER_FILESYSTEM_TYPE"
741 service "$_service_name" start
744 service "$_service_name" stop
747 service "$_service_name" stop
748 nfs_dump_some_threads "rpc.statd"
749 service "$_service_name" start
754 ########################################################
755 # start/stop the nfs service on different platforms
756 ########################################################
759 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
762 [ -x $CTDB_ETCDIR/init.d/nfslock -o \
763 -r /usr/lib/systemd/system/nfs-lock.service ] && {
771 service nfsserver start
774 service nfsserver stop > /dev/null 2>&1
777 set_proc "fs/nfsd/threads" 0
778 service nfsserver stop > /dev/null 2>&1
780 nfs_dump_some_threads
781 service nfsserver start
788 service nfslock start
796 set_proc "fs/nfsd/threads" 0
797 service nfs stop > /dev/null 2>&1
798 service nfslock stop > /dev/null 2>&1
800 nfs_dump_some_threads
801 service nfslock start
807 echo "Unknown platform. NFS is not supported with ctdb"
813 # Dump up to the configured number of nfsd thread backtraces.
814 nfs_dump_some_threads ()
818 _num="${CTDB_NFS_DUMP_STUCK_THREADS:-5}"
819 [ $_num -gt 0 ] || return 0
821 program_stack_traces "$_prog" $_num
824 ########################################################
825 # start/stop the nfs lockmanager service on different platforms
826 ########################################################
827 startstop_nfslock() {
829 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
832 [ -x $CTDB_ETCDIR/init.d/nfslock -o \
833 -r /usr/lib/systemd/system/nfs-lock.service ] && {
839 # for sles there is no service for lockmanager
840 # so we instead just shutdown/restart nfs
843 service nfsserver start
846 service nfsserver stop > /dev/null 2>&1
849 service nfsserver stop > /dev/null 2>&1
850 service nfsserver start
857 service nfslock start
860 service nfslock stop > /dev/null 2>&1
863 service nfslock stop > /dev/null 2>&1
864 service nfslock start
869 echo "Unknown platform. NFS locking is not supported with ctdb"
875 ########################################################
883 # Ensure interface is up
884 ip link set "$_iface" up || \
885 die "Failed to bringup interface $_iface"
887 # Only need to define broadcast for IPv4
893 ip addr add "$_ip/$_maskbits" $_bcast dev "$_iface" || {
894 echo "Failed to add $_ip/$_maskbits on dev $_iface"
898 # Wait 5 seconds for IPv6 addresses to stop being tentative...
899 if [ -z "$_bcast" ] ; then
900 for _x in $(seq 1 10) ; do
901 ip addr show to "${_ip}/128" | grep -q "tentative" || break
905 # If the address was a duplicate then it won't be on the
906 # interface so flag an error.
907 _t=$(ip addr show to "${_ip}/128")
910 echo "Failed to add $_ip/$_maskbits on dev $_iface"
913 *tentative*|*dadfailed*)
914 echo "Failed to add $_ip/$_maskbits on dev $_iface"
915 ip addr del "$_ip/$_maskbits" dev "$_iface"
922 delete_ip_from_iface()
928 # This could be set globally for all interfaces but it is probably
929 # better to avoid surprises, so limit it the interfaces where CTDB
930 # has public IP addresses. There isn't anywhere else convenient
931 # to do this so just set it each time. This is much cheaper than
932 # remembering and re-adding secondaries.
933 set_proc "sys/net/ipv4/conf/${_iface}/promote_secondaries" 1
935 ip addr del "$_ip/$_maskbits" dev "$_iface" || {
936 echo "Failed to del $_ip on dev $_iface"
941 # If the given IP is hosted then print 2 items: maskbits and iface
947 *:*) _family="inet6" ; _bits=128 ;;
948 *) _family="inet" ; _bits=32 ;;
951 ip addr show to "${_addr}/${_bits}" 2>/dev/null | \
952 awk -v family="${_family}" \
953 'NR == 1 { iface = $2; sub(":$", "", iface) } \
954 $1 ~ /inet/ { mask = $2; sub(".*/", "", mask); \
955 print mask, iface, family }'
960 _addr="${1%/*}" # Remove optional maskbits
962 set -- $(ip_maskbits_iface $_addr)
963 if [ -n "$1" ] ; then
966 echo "Removing public address $_addr/$_maskbits from device $_iface"
967 delete_ip_from_iface $_iface $_addr $_maskbits >/dev/null 2>&1
971 drop_all_public_ips ()
973 while read _ip _x ; do
975 done <"${CTDB_PUBLIC_ADDRESSES:-/dev/null}"
980 set_proc sys/net/ipv4/route/flush 1
981 set_proc sys/net/ipv6/route/flush 1
984 ########################################################
986 _ctdb_counter_common () {
987 _service_name="${1:-${service_name:-${script_name}}}"
988 _counter_file="$ctdb_fail_dir/$_service_name"
989 mkdir -p "${_counter_file%/*}" # dirname
991 ctdb_counter_init () {
992 _ctdb_counter_common "$1"
996 ctdb_counter_incr () {
997 _ctdb_counter_common "$1"
1000 echo -n 1 >> "$_counter_file"
1002 ctdb_check_counter () {
1003 _msg="${1:-error}" # "error" - anything else is silent on fail
1004 _op="${2:--ge}" # an integer operator supported by test
1005 _limit="${3:-${service_fail_limit}}"
1007 _ctdb_counter_common "$1"
1010 _size=$(stat -c "%s" "$_counter_file" 2>/dev/null || echo 0)
1012 if [ "$_op" != "%" ] ; then
1013 if [ $_size $_op $_limit ] ; then
1017 if [ $(($_size $_op $_limit)) -eq 0 ] ; then
1022 if [ "$_msg" = "error" ] ; then
1023 echo "ERROR: $_size consecutive failures for $_service_name, marking node unhealthy"
1031 ########################################################
1033 ctdb_status_dir="$CTDB_VARDIR/state/service_status"
1034 ctdb_fail_dir="$CTDB_VARDIR/state/failcount"
1036 ctdb_setup_service_state_dir ()
1038 service_state_dir="$CTDB_VARDIR/state/service_state/${1:-${service_name}}"
1039 mkdir -p "$service_state_dir" || {
1040 echo "Error creating state dir \"$service_state_dir\""
1045 ########################################################
1046 # Managed status history, for auto-start/stop
1048 ctdb_managed_dir="$CTDB_VARDIR/state/managed_history"
1050 _ctdb_managed_common ()
1052 _ctdb_managed_file="$ctdb_managed_dir/$service_name"
1055 ctdb_service_managed ()
1057 _ctdb_managed_common
1058 mkdir -p "$ctdb_managed_dir"
1059 touch "$_ctdb_managed_file"
1062 ctdb_service_unmanaged ()
1064 _ctdb_managed_common
1065 rm -f "$_ctdb_managed_file"
1068 is_ctdb_previously_managed_service ()
1070 _ctdb_managed_common
1071 [ -f "$_ctdb_managed_file" ]
1074 ########################################################
1075 # Check and set status
1079 echo "node is \"$1\", \"${script_name}\" reports problem: $(cat $2)"
1084 if [ -r "$ctdb_status_dir/$script_name/unhealthy" ] ; then
1085 log_status_cat "unhealthy" "$ctdb_status_dir/$script_name/unhealthy"
1087 elif [ -r "$ctdb_status_dir/$script_name/banned" ] ; then
1088 log_status_cat "banned" "$ctdb_status_dir/$script_name/banned"
1097 d="$ctdb_status_dir/$script_name"
1104 for i in "banned" "unhealthy" ; do
1111 ##################################################################
1112 # Reconfigure a service on demand
1114 _ctdb_service_reconfigure_common ()
1116 _d="$ctdb_status_dir/${service_name}"
1118 _ctdb_service_reconfigure_flag="$_d/reconfigure"
1121 ctdb_service_needs_reconfigure ()
1123 _ctdb_service_reconfigure_common
1124 [ -e "$_ctdb_service_reconfigure_flag" ]
1127 ctdb_service_set_reconfigure ()
1129 _ctdb_service_reconfigure_common
1130 >"$_ctdb_service_reconfigure_flag"
1133 ctdb_service_unset_reconfigure ()
1135 _ctdb_service_reconfigure_common
1136 rm -f "$_ctdb_service_reconfigure_flag"
1139 ctdb_service_reconfigure ()
1141 echo "Reconfiguring service \"${service_name}\"..."
1142 ctdb_service_unset_reconfigure
1143 service_reconfigure || return $?
1147 # Default service_reconfigure() function does nothing.
1148 service_reconfigure ()
1153 ctdb_reconfigure_take_lock ()
1155 _ctdb_service_reconfigure_common
1156 _lock="${_d}/reconfigure_lock"
1157 mkdir -p "${_lock%/*}" # dirname
1162 # This is overkill but will work if we need to extend this to
1163 # allow certain events to run multiple times in parallel
1164 # (e.g. takeip) and write multiple PIDs to the file.
1166 if [ -n "$_locker_event" ] ; then
1167 while read _pid ; do
1168 if [ -n "$_pid" -a "$_pid" != $$ ] && \
1169 kill -0 "$_pid" 2>/dev/null ; then
1175 printf "%s\n%s\n" "$event_name" $$ >"$_lock"
1180 ctdb_reconfigure_release_lock ()
1182 _ctdb_service_reconfigure_common
1183 _lock="${_d}/reconfigure_lock"
1188 ctdb_replay_monitor_status ()
1190 echo "Replaying previous status for this script due to reconfigure..."
1191 # Leading separator ('|') is missing in some versions...
1192 _out=$(ctdb scriptstatus -X | grep -E "^\|?monitor\|${script_name}\|")
1193 # Output looks like this:
1194 # |monitor|60.nfs|1|ERROR|1314764004.030861|1314764004.035514|foo bar|
1195 # This is the cheapest way of getting fields in the middle.
1196 set -- $(IFS="|" ; echo $_out)
1199 # The error output field can include colons so we'll try to
1200 # preserve them. The weak checking at the beginning tries to make
1201 # this work for both broken (no leading '|') and fixed output.
1203 _err_out="${_out#*monitor|${script_name}|*|*|*|*|}"
1205 OK) : ;; # Do nothing special.
1207 # Recast this as an error, since we can't exit with the
1208 # correct negative number.
1210 _err_out="[Replay of TIMEDOUT scriptstatus - note incorrect return code.] ${_err_out}"
1213 # Recast this as an OK, since we can't exit with the
1214 # correct negative number.
1216 _err_out="[Replay of DISABLED scriptstatus - note incorrect return code.] ${_err_out}"
1218 *) : ;; # Must be ERROR, do nothing special.
1220 if [ -n "$_err_out" ] ; then
1226 ctdb_service_check_reconfigure ()
1230 # We only care about some events in this function. For others we
1232 case "$event_name" in
1233 monitor|ipreallocated|reconfigure) : ;;
1237 if ctdb_reconfigure_take_lock ; then
1238 # No events covered by this function are running, so proceed
1240 case "$event_name" in
1242 (ctdb_service_reconfigure)
1246 if ctdb_service_needs_reconfigure ; then
1247 ctdb_service_reconfigure
1252 ctdb_reconfigure_release_lock
1254 # Somebody else is running an event we don't want to collide
1255 # with. We proceed with caution.
1256 case "$event_name" in
1258 # Tell whoever called us to retry.
1262 # Defer any scheduled reconfigure and just run the
1263 # rest of the ipreallocated event, as per the
1264 # eventscript. There's an assumption here that the
1265 # event doesn't depend on any scheduled reconfigure.
1266 # This is true in the current code.
1270 # There is most likely a reconfigure in progress so
1271 # the service is possibly unstable. As above, we
1272 # defer any scheduled reconfigured. We also replay
1273 # the previous monitor status since that's the best
1274 # information we have.
1275 ctdb_replay_monitor_status
1281 ##################################################################
1282 # Does CTDB manage this service? - and associated auto-start/stop
1284 ctdb_compat_managed_service ()
1286 if [ "$1" = "yes" -a "$2" = "$service_name" ] ; then
1287 CTDB_MANAGED_SERVICES="$CTDB_MANAGED_SERVICES $2"
1291 is_ctdb_managed_service ()
1295 # $t is used just for readability and to allow better accurate
1296 # matching via leading/trailing spaces
1297 t=" $CTDB_MANAGED_SERVICES "
1299 # Return 0 if "<space>$service_name<space>" appears in $t
1300 if [ "${t#* ${service_name} }" != "${t}" ] ; then
1304 # If above didn't match then update $CTDB_MANAGED_SERVICES for
1305 # backward compatibility and try again.
1306 ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD" "vsftpd"
1307 ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA" "samba"
1308 ctdb_compat_managed_service "$CTDB_MANAGES_WINBIND" "winbind"
1309 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "apache2"
1310 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "httpd"
1311 ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI" "iscsi"
1312 ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD" "clamd"
1313 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs"
1314 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs-ganesha-gpfs"
1316 t=" $CTDB_MANAGED_SERVICES "
1318 # Return 0 if "<space>$service_name<space>" appears in $t
1319 [ "${t#* ${service_name} }" != "${t}" ]
1322 ctdb_start_stop_service ()
1326 # Allow service-start/service-stop pseudo-events to start/stop
1327 # services when we're not auto-starting/stopping and we're not
1329 case "$event_name" in
1331 if is_ctdb_managed_service ; then
1332 die 'service-start event not permitted when service is managed'
1334 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1335 die 'service-start event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1341 if is_ctdb_managed_service ; then
1342 die 'service-stop event not permitted when service is managed'
1344 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1345 die 'service-stop event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1352 # Do nothing unless configured to...
1353 [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] || return 0
1355 [ "$event_name" = "monitor" ] || return 0
1357 if is_ctdb_managed_service ; then
1358 if ! is_ctdb_previously_managed_service ; then
1359 echo "Starting service \"$service_name\" - now managed"
1360 background_with_logging ctdb_service_start
1364 if is_ctdb_previously_managed_service ; then
1365 echo "Stopping service \"$service_name\" - no longer managed"
1366 background_with_logging ctdb_service_stop
1372 ctdb_service_start ()
1374 # The service is marked managed if we've ever tried to start it.
1375 ctdb_service_managed
1377 service_start || return $?
1383 ctdb_service_stop ()
1385 ctdb_service_unmanaged
1389 # Default service_start() and service_stop() functions.
1391 # These may be overridden in an eventscript.
1394 service "$service_name" start
1399 service "$service_name" stop
1402 ##################################################################
1404 ctdb_standard_event_handler ()
1421 _family="$1" ; shift
1422 if [ "$_family" = "inet6" ] ; then
1423 _iptables_cmd="ip6tables"
1425 _iptables_cmd="iptables"
1428 # iptables doesn't like being re-entered, so flock-wrap it.
1429 flock -w 30 "${CTDB_VARDIR}/iptables-ctdb.flock" "$_iptables_cmd" "$@"
1432 # AIX (and perhaps others?) doesn't have mktemp
1433 if ! type mktemp >/dev/null 2>&1 ; then
1437 if [ "$1" = "-d" ] ; then
1441 _d="${TMPDIR:-/tmp}"
1442 _hex10=$(dd if=/dev/urandom count=20 2>/dev/null | \
1444 sed -e 's@\(..........\).*@\1@')
1445 _t="${_d}/tmp.${_hex10}"
1458 ########################################################
1460 ########################################################
1466 tickledir="$CTDB_VARDIR/state/tickles"
1467 mkdir -p "$tickledir"
1471 # What public IPs do I hold?
1472 _ips=$(ctdb -X ip | awk -F'|' -v pnn=$pnn '$3 == pnn {print $2}')
1474 # IPs as a regexp choice
1475 _ipschoice="($(echo $_ips | sed -e 's/ /|/g' -e 's/\./\\\\./g'))"
1477 # Record connections to our public IPs in a temporary file
1478 _my_connections="${tickledir}/${_port}.connections"
1479 rm -f "$_my_connections"
1481 awk -v destpat="^${_ipschoice}:${_port}\$" \
1482 '$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ destpat {print $5, $4}' |
1483 sort >"$_my_connections"
1485 # Record our current tickles in a temporary file
1486 _my_tickles="${tickledir}/${_port}.tickles"
1487 rm -f "$_my_tickles"
1488 for _i in $_ips ; do
1489 ctdb -X gettickles $_i $_port |
1490 awk -F'|' 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
1492 sort >"$_my_tickles"
1494 # Add tickles for connections that we haven't already got tickles for
1495 comm -23 "$_my_connections" "$_my_tickles" |
1496 while read _src _dst ; do
1497 ctdb addtickle $_src $_dst
1500 # Remove tickles for connections that are no longer there
1501 comm -13 "$_my_connections" "$_my_tickles" |
1502 while read _src _dst ; do
1503 ctdb deltickle $_src $_dst
1506 rm -f "$_my_connections" "$_my_tickles"
1509 ########################################################
1510 # load a site local config file
1511 ########################################################
1513 [ -n "$CTDB_RC_LOCAL" -a -x "$CTDB_RC_LOCAL" ] && {
1517 [ -x $CTDB_BASE/rc.local ] && {
1518 . $CTDB_BASE/rc.local
1521 [ -d $CTDB_BASE/rc.local.d ] && {
1522 for i in $CTDB_BASE/rc.local.d/* ; do
1523 [ -x "$i" ] && . "$i"
1527 script_name="${0##*/}" # basename
1528 service_fail_limit=1