1 # Hey Emacs, this is a -*- shell-script -*- !!!
3 # utility functions for ctdb event scripts
5 [ -z "$CTDB_VARDIR" ] && {
6 if [ -d "/var/lib/ctdb" ] ; then
7 export CTDB_VARDIR="/var/lib/ctdb"
9 export CTDB_VARDIR="/var/ctdb"
12 [ -z "$CTDB_ETCDIR" ] && {
13 export CTDB_ETCDIR="/etc"
16 #######################################
17 # pull in a system config file, if any
21 foo="${service_config:-${service_name}}"
22 if [ -n "$foo" ] ; then
28 if [ "$1" != "ctdb" ] ; then
36 if [ -f $CTDB_ETCDIR/sysconfig/$1 ]; then
37 . $CTDB_ETCDIR/sysconfig/$1
38 elif [ -f $CTDB_ETCDIR/default/$1 ]; then
39 . $CTDB_ETCDIR/default/$1
40 elif [ -f $CTDB_BASE/sysconfig/$1 ]; then
41 . $CTDB_BASE/sysconfig/$1
44 if [ "$1" = "ctdb" ] ; then
45 _config="${CTDB_BASE}/ctdbd.conf"
46 if [ -r "$_config" ] ; then
56 ##############################################################
58 # CTDB_SCRIPT_DEBUGLEVEL can be overwritten by setting it in a
62 if [ ${CTDB_SCRIPT_DEBUGLEVEL:-2} -ge 4 ] ; then
63 # If there are arguments then echo them. Otherwise expect to
64 # use stdin, which allows us to pass lots of debug using a
83 # Log given message or stdin to either syslog or a CTDB log file
84 # $1 is the tag passed to logger if syslog is in use.
89 if [ "$CTDB_SYSLOG" = "yes" ] ; then
90 logger -t "ctdbd: ${_tag}" $*
98 } >>"${CTDB_LOGFILE:-/var/log/log.ctdb}"
102 # When things are run in the background in an eventscript then logging
103 # output might get lost. This is the "solution". :-)
104 background_with_logging ()
107 "$@" 2>&1 </dev/null |
108 script_log "${script_name}&"
114 ##############################################################
115 # check number of args for different events
121 echo "ERROR: must supply interface, IP and maskbits"
127 echo "ERROR: must supply old interface, new interface, IP and maskbits"
134 ##############################################################
135 # determine on what type of system (init style) we are running
138 # only do detection if not already set:
139 [ -z "$CTDB_INIT_STYLE" ] || return
141 if [ -x /sbin/startproc ]; then
142 CTDB_INIT_STYLE="suse"
143 elif [ -x /sbin/start-stop-daemon ]; then
144 CTDB_INIT_STYLE="debian"
146 CTDB_INIT_STYLE="redhat"
150 ######################################################
151 # simulate /sbin/service on platforms that don't have it
152 # _service() makes it easier to hook the service() function for
159 # do nothing, when no service was specified
160 [ -z "$_service_name" ] && return
162 if [ -x /sbin/service ]; then
163 $_nice /sbin/service "$_service_name" "$_op"
164 elif [ -x $CTDB_ETCDIR/init.d/$_service_name ]; then
165 $_nice $CTDB_ETCDIR/init.d/$_service_name "$_op"
166 elif [ -x $CTDB_ETCDIR/rc.d/init.d/$_service_name ]; then
167 $_nice $CTDB_ETCDIR/rc.d/init.d/$_service_name "$_op"
177 ######################################################
178 # simulate /sbin/service (niced) on platforms that don't have it
185 ######################################################
186 # wrapper around /proc/ settings to allow them to be hooked
188 # 1st arg is relative path under /proc/, 2nd arg is value to set
191 echo "$2" >"/proc/$1"
194 ######################################################
195 # wrapper around getting file contents from /proc/ to allow
196 # this to be hooked for testing
197 # 1st arg is relative path under /proc/
203 ######################################################
204 # Check that an RPC service is healthy -
205 # this includes allowing a certain number of failures
206 # before marking the NFS service unhealthy.
208 # usage: nfs_check_rpc_service SERVICE_NAME [ triple ...]
210 # each triple is a set of 3 arguments: an operator, a
211 # fail count limit and an action string.
215 # nfs_check_rpc_service "lockd" \
216 # -ge 15 "verbose restart unhealthy" \
217 # -eq 10 "restart:bs"
219 # says that if lockd is down for 15 iterations then do
220 # a verbose restart of lockd and mark the node unhealthy.
221 # Before this, after 10 iterations of failure, the
222 # service is restarted silently in the background.
223 # Order is important: the number of failures need to be
224 # specified in reverse order because processing stops
225 # after the first condition that is true.
226 ######################################################
227 nfs_check_rpc_service ()
229 _prog_name="$1" ; shift
231 if _nfs_check_rpc_common "$_prog_name" ; then
235 while [ -n "$3" ] ; do
236 if _nfs_check_rpc_action "$1" "$2" "$3" ; then
243 # The new way of doing things...
244 nfs_check_rpc_services ()
246 # Files must end with .check - avoids editor backups, RPM fu, ...
247 for _f in "${CTDB_BASE}/nfs-rpc-checks.d/"[0-9][0-9].*.check ; do
249 _prog_name="${_t##*/[0-9][0-9].}"
251 if _nfs_check_rpc_common "$_prog_name" ; then
252 # This RPC service is up, check next service...
256 # Check each line in the file in turn until one of the limit
258 while read _cmp _lim _rest ; do
264 if _nfs_check_rpc_action "$_cmp" "$_lim" "$_rest" ; then
265 # Limit was hit on this line, no further checking...
272 _nfs_check_rpc_common ()
276 # Some platforms don't have separate programs for all services.
277 case "$_prog_name" in
279 which "rpc.${_prog_name}" >/dev/null 2>&1 || return 0
282 case "$_prog_name" in
304 echo "Internal error: unknown RPC program \"$_prog_name\"."
308 _service_name="nfs_${_prog_name}"
310 if ctdb_check_rpc "$_rpc_prog" $_version >/dev/null ; then
311 ctdb_counter_init "$_service_name"
315 ctdb_counter_incr "$_service_name"
320 _nfs_check_rpc_action ()
326 if ctdb_check_counter "quiet" "$_cmp" "$_limit" "$_service_name" ; then
330 for _action in $_actions ; do
333 echo "$ctdb_check_rpc_out"
336 _nfs_restart_rpc_service "$_prog_name"
339 _nfs_restart_rpc_service "$_prog_name" true
345 echo "Internal error: unknown action \"$_action\"."
353 _nfs_restart_rpc_service ()
356 _background="${2:-false}"
358 if $_background ; then
359 _maybe_background="background_with_logging"
364 _p="rpc.${_prog_name}"
366 case "$_prog_name" in
368 echo "Trying to restart NFS service"
369 $_maybe_background startstop_nfs restart
372 echo "Trying to restart $_prog_name [${_p}]"
374 $_maybe_background $_p ${MOUNTD_PORT:+-p} $MOUNTD_PORT
377 echo "Trying to restart $_prog_name [${_p}]"
379 $_maybe_background $_p ${RQUOTAD_PORT:+-p} $RQUOTAD_PORT
382 echo "Trying to restart lock manager service"
383 $_maybe_background startstop_nfslock restart
386 echo "Trying to restart $_prog_name [${_p}]"
388 $_maybe_background $_p \
389 ${STATD_HOSTNAME:+-n} $STATD_HOSTNAME \
390 ${STATD_PORT:+-p} $STATD_PORT \
391 ${STATD_OUTGOING_PORT:+-o} $STATD_OUTGOING_PORT
394 echo "Internal error: unknown RPC program \"$_prog_name\"."
399 ######################################################
400 # check that a rpc server is registered with portmap
401 # and responding to requests
402 # usage: ctdb_check_rpc SERVICE_NAME VERSION
403 ######################################################
409 _localhost="${CTDB_RPCINFO_LOCALHOST:-127.0.0.1}"
411 if ! ctdb_check_rpc_out=$(rpcinfo -u $_localhost $progname $version 2>&1) ; then
412 ctdb_check_rpc_out="ERROR: $progname failed RPC check:
414 echo "$ctdb_check_rpc_out"
419 ######################################################
420 # Ensure $service_name is set
421 assert_service_name ()
423 [ -n "$service_name" ] || die "INTERNAL ERROR: \$service_name not set"
426 ######################################################
427 # check a set of directories is available
428 # return 1 on a missing directory
429 # directories are read from stdin
430 ######################################################
431 ctdb_check_directories_probe()
433 while IFS="" read d ; do
439 [ -d "${d}/." ] || return 1
444 ######################################################
445 # check a set of directories is available
446 # directories are read from stdin
447 ######################################################
448 ctdb_check_directories()
450 ctdb_check_directories_probe || {
451 echo "ERROR: $service_name directory \"$d\" not available"
456 ######################################################
457 # check a set of tcp ports
458 # usage: ctdb_check_tcp_ports <ports...>
459 ######################################################
461 # This flag file is created when a service is initially started. It
462 # is deleted the first time TCP port checks for that service succeed.
463 # Until then ctdb_check_tcp_ports() prints a more subtle "error"
464 # message if a port check fails.
465 _ctdb_check_tcp_common ()
468 _ctdb_service_started_file="$ctdb_fail_dir/$service_name.started"
471 ctdb_check_tcp_init ()
473 _ctdb_check_tcp_common
474 mkdir -p "${_ctdb_service_started_file%/*}" # dirname
475 touch "$_ctdb_service_started_file"
478 # Check whether something is listening on all of the given TCP ports
479 # using the "ctdb checktcpport" command.
480 ctdb_check_tcp_ports()
482 if [ -z "$1" ] ; then
483 echo "INTERNAL ERROR: ctdb_check_tcp_ports - no ports specified"
487 for _p ; do # process each function argument (port)
488 _cmd="ctdb checktcpport $_p"
493 _ctdb_check_tcp_common
494 if [ ! -f "$_ctdb_service_started_file" ] ; then
495 echo "ERROR: $service_name tcp port $_p is not responding"
496 debug "\"ctdb checktcpport $_p\" was able to bind to port"
498 echo "INFO: $service_name tcp port $_p is not responding"
504 # Couldn't bind, something already listening, next port...
508 echo "ERROR: unexpected error running \"ctdb checktcpport\""
510 ctdb checktcpport (exited with $_ret) with output:
517 # All ports listening
518 _ctdb_check_tcp_common
519 rm -f "$_ctdb_service_started_file"
523 ######################################################
524 # check a unix socket
525 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
526 ######################################################
527 ctdb_check_unix_socket() {
529 [ -z "$socket_path" ] && return
531 if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
532 echo "ERROR: $service_name socket $socket_path not found"
537 ######################################################
538 # check a command returns zero status
539 # usage: ctdb_check_command <command>
540 ######################################################
541 ctdb_check_command ()
543 _out=$("$@" 2>&1) || {
544 echo "ERROR: $* returned error"
550 ################################################
551 # kill off any TCP connections with the given IP
552 ################################################
553 kill_tcp_connections ()
558 if [ "$2" = "oneway" ] ; then
562 get_tcp_connections_for_ip "$_ip" | {
567 while read _dst _src; do
568 _destport="${_dst##*:}"
571 # we only do one-way killtcp for CIFS
572 139|445) __oneway=true ;;
575 echo "Killing TCP connection $_src $_dst"
576 _connections="${_connections}${_nl}${_src} ${_dst}"
577 if ! $__oneway ; then
578 _connections="${_connections}${_nl}${_dst} ${_src}"
581 _killcount=$(($_killcount + 1))
584 if [ $_killcount -eq 0 ] ; then
588 echo "$_connections" | ctdb killtcp || {
589 echo "Failed to send killtcp control"
595 _remaining=$(get_tcp_connections_for_ip $_ip | wc -l)
597 if [ $_remaining -eq 0 ] ; then
598 echo "Killed $_killcount TCP connections to released IP $_ip"
602 _count=$(($_count + 1))
603 if [ $_count -gt 3 ] ; then
604 echo "Timed out killing tcp connections for IP $_ip ($_remaining remaining)"
608 echo "Waiting for $_remaining connections to be killed for IP $_ip"
614 ##################################################################
615 # kill off the local end for any TCP connections with the given IP
616 ##################################################################
617 kill_tcp_connections_local_only ()
619 kill_tcp_connections "$1" "oneway"
622 ##################################################################
623 # tickle any TCP connections with the given IP
624 ##################################################################
625 tickle_tcp_connections ()
629 get_tcp_connections_for_ip "$_ip" |
633 while read dest src; do
634 echo "Tickle TCP connection $src $dest"
635 ctdb tickle $src $dest >/dev/null 2>&1 || _failed=true
636 echo "Tickle TCP connection $dest $src"
637 ctdb tickle $dest $src >/dev/null 2>&1 || _failed=true
641 echo "Failed to send tickle control"
646 get_tcp_connections_for_ip ()
650 netstat -tn | awk -v ip=$_ip \
651 'index($1, "tcp") == 1 && \
652 (index($4, ip ":") == 1 || index($4, "::ffff:" ip ":") == 1) \
653 && $6 == "ESTABLISHED" \
657 ########################################################
658 # start/stop the Ganesha nfs service
659 ########################################################
662 _service_name="nfs-ganesha-$CTDB_CLUSTER_FILESYSTEM_TYPE"
665 service "$_service_name" start
668 service "$_service_name" stop
671 service "$_service_name" restart
676 ########################################################
677 # start/stop the nfs service on different platforms
678 ########################################################
681 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
684 [ -x $CTDB_ETCDIR/init.d/nfslock ] && {
692 service nfsserver start
695 service nfsserver stop > /dev/null 2>&1
698 set_proc "fs/nfsd/threads" 0
699 service nfsserver stop > /dev/null 2>&1
701 nfs_dump_some_threads
702 service nfsserver start
709 service nfslock start
717 set_proc "fs/nfsd/threads" 0
718 service nfs stop > /dev/null 2>&1
719 service nfslock stop > /dev/null 2>&1
721 nfs_dump_some_threads
722 service nfslock start
728 echo "Unknown platform. NFS is not supported with ctdb"
734 # Dump up to the configured number of nfsd thread backtraces.
735 nfs_dump_some_threads ()
737 [ -n "$CTDB_NFS_DUMP_STUCK_THREADS" ] || CTDB_NFS_DUMP_STUCK_THREADS=5
739 # Optimisation to avoid running an unnecessary pidof
740 [ $CTDB_NFS_DUMP_STUCK_THREADS -gt 0 ] || return 0
743 for _pid in $(pidof nfsd) ; do
744 [ $_count -le $CTDB_NFS_DUMP_STUCK_THREADS ] || break
746 # Do this first to avoid racing with thread exit
747 _stack=$(get_proc "${_pid}/stack" 2>/dev/null)
748 if [ -n "$_stack" ] ; then
749 echo "Stack trace for stuck nfsd thread [${_pid}]:"
751 _count=$(($_count + 1))
756 ########################################################
757 # start/stop the nfs lockmanager service on different platforms
758 ########################################################
759 startstop_nfslock() {
761 [ -x $CTDB_ETCDIR/init.d/nfsserver ] && {
764 [ -x $CTDB_ETCDIR/init.d/nfslock ] && {
770 # for sles there is no service for lockmanager
771 # so we instead just shutdown/restart nfs
774 service nfsserver start
777 service nfsserver stop > /dev/null 2>&1
780 service nfsserver stop > /dev/null 2>&1
781 service nfsserver start
788 service nfslock start
791 service nfslock stop > /dev/null 2>&1
794 service nfslock stop > /dev/null 2>&1
795 service nfslock start
800 echo "Unknown platform. NFS locking is not supported with ctdb"
806 # Periodically update the statd database
811 _statd_update_trigger="$service_state_dir/update-trigger"
812 [ -f "$_statd_update_trigger" ] || touch "$_statd_update_trigger"
814 _last_update=$(stat --printf="%Y" "$_statd_update_trigger")
815 _current_time=$(date +"%s")
816 if [ $(( $_current_time - $_last_update)) -ge $_update_period ] ; then
817 touch "$_statd_update_trigger"
818 $CTDB_BASE/statd-callout updatelocal &
819 $CTDB_BASE/statd-callout updateremote &
823 ########################################################
831 # Ensure interface is up
832 ip link set "$_iface" up || \
833 die "Failed to bringup interface $_iface"
835 ip addr add "$_ip/$_maskbits" brd + dev "$_iface" || {
836 echo "Failed to add $_ip/$_maskbits on dev $_iface"
841 delete_ip_from_iface()
847 # This could be set globally for all interfaces but it is probably
848 # better to avoid surprises, so limit it the interfaces where CTDB
849 # has public IP addresses. There isn't anywhere else convenient
850 # to do this so just set it each time. This is much cheaper than
851 # remembering and re-adding secondaries.
852 set_proc "sys/net/ipv4/conf/${_iface}/promote_secondaries" 1
854 ip addr del "$_ip/$_maskbits" dev "$_iface" || {
855 echo "Failed to del $_ip on dev $_iface"
860 # If the given IP is hosted then print 2 items: maskbits and iface
865 ip addr show to "${_addr}/32" 2>/dev/null | \
866 awk '$1 == "inet" { print gensub(".*/", "", 1, $2), $NF }'
871 _addr="${1%/*}" # Remove optional maskbits
873 set -- $(ip_maskbits_iface $_addr)
874 if [ -n "$1" ] ; then
877 echo "Removing public address $_addr/$_maskbits from device $_iface"
878 delete_ip_from_iface $_iface $_addr $_maskbits >/dev/null 2>&1
882 drop_all_public_ips ()
884 while read _ip _x ; do
886 done <"${CTDB_PUBLIC_ADDRESSES:-/dev/null}"
889 ########################################################
891 _ctdb_counter_common () {
892 _service_name="${1:-${service_name:-${script_name}}}"
893 _counter_file="$ctdb_fail_dir/$_service_name"
894 mkdir -p "${_counter_file%/*}" # dirname
896 ctdb_counter_init () {
897 _ctdb_counter_common "$1"
901 ctdb_counter_incr () {
902 _ctdb_counter_common "$1"
905 echo -n 1 >> "$_counter_file"
907 ctdb_check_counter () {
908 _msg="${1:-error}" # "error" - anything else is silent on fail
909 _op="${2:--ge}" # an integer operator supported by test
910 _limit="${3:-${service_fail_limit}}"
912 _ctdb_counter_common "$1"
915 _size=$(stat -c "%s" "$_counter_file" 2>/dev/null || echo 0)
917 if [ "$_op" != "%" ] ; then
918 if [ $_size $_op $_limit ] ; then
922 if [ $(($_size $_op $_limit)) -eq 0 ] ; then
927 if [ "$_msg" = "error" ] ; then
928 echo "ERROR: $_size consecutive failures for $_service_name, marking node unhealthy"
936 ########################################################
938 ctdb_status_dir="$CTDB_VARDIR/state/service_status"
939 ctdb_fail_dir="$CTDB_VARDIR/state/failcount"
941 ctdb_setup_service_state_dir ()
943 service_state_dir="$CTDB_VARDIR/state/service_state/${1:-${service_name}}"
944 mkdir -p "$service_state_dir" || {
945 echo "Error creating state dir \"$service_state_dir\""
950 ########################################################
951 # Managed status history, for auto-start/stop
953 ctdb_managed_dir="$CTDB_VARDIR/state/managed_history"
955 _ctdb_managed_common ()
957 _ctdb_managed_file="$ctdb_managed_dir/$service_name"
960 ctdb_service_managed ()
963 mkdir -p "$ctdb_managed_dir"
964 touch "$_ctdb_managed_file"
967 ctdb_service_unmanaged ()
970 rm -f "$_ctdb_managed_file"
973 is_ctdb_previously_managed_service ()
976 [ -f "$_ctdb_managed_file" ]
979 ########################################################
980 # Check and set status
984 echo "node is \"$1\", \"${script_name}\" reports problem: $(cat $2)"
989 if [ -r "$ctdb_status_dir/$script_name/unhealthy" ] ; then
990 log_status_cat "unhealthy" "$ctdb_status_dir/$script_name/unhealthy"
992 elif [ -r "$ctdb_status_dir/$script_name/banned" ] ; then
993 log_status_cat "banned" "$ctdb_status_dir/$script_name/banned"
1002 d="$ctdb_status_dir/$script_name"
1009 for i in "banned" "unhealthy" ; do
1016 ##################################################################
1017 # Reconfigure a service on demand
1019 _ctdb_service_reconfigure_common ()
1021 _d="$ctdb_status_dir/${service_name}"
1023 _ctdb_service_reconfigure_flag="$_d/reconfigure"
1026 ctdb_service_needs_reconfigure ()
1028 _ctdb_service_reconfigure_common
1029 [ -e "$_ctdb_service_reconfigure_flag" ]
1032 ctdb_service_set_reconfigure ()
1034 _ctdb_service_reconfigure_common
1035 >"$_ctdb_service_reconfigure_flag"
1038 ctdb_service_unset_reconfigure ()
1040 _ctdb_service_reconfigure_common
1041 rm -f "$_ctdb_service_reconfigure_flag"
1044 ctdb_service_reconfigure ()
1046 echo "Reconfiguring service \"${service_name}\"..."
1047 ctdb_service_unset_reconfigure
1048 service_reconfigure || return $?
1052 # Default service_reconfigure() function does nothing.
1053 service_reconfigure ()
1058 ctdb_reconfigure_take_lock ()
1060 _ctdb_service_reconfigure_common
1061 _lock="${_d}/reconfigure_lock"
1062 mkdir -p "${_lock%/*}" # dirname
1067 # This is overkill but will work if we need to extend this to
1068 # allow certain events to run multiple times in parallel
1069 # (e.g. takeip) and write multiple PIDs to the file.
1071 if [ -n "$_locker_event" ] ; then
1072 while read _pid ; do
1073 if [ -n "$_pid" -a "$_pid" != $$ ] && \
1074 kill -0 "$_pid" 2>/dev/null ; then
1080 printf "%s\n%s\n" "$event_name" $$ >"$_lock"
1085 ctdb_reconfigure_release_lock ()
1087 _ctdb_service_reconfigure_common
1088 _lock="${_d}/reconfigure_lock"
1093 ctdb_replay_monitor_status ()
1095 echo "Replaying previous status for this script due to reconfigure..."
1096 # Leading colon (':') is missing in some versions...
1097 _out=$(ctdb scriptstatus -Y | grep -E "^:?monitor:${script_name}:")
1098 # Output looks like this:
1099 # :monitor:60.nfs:1:ERROR:1314764004.030861:1314764004.035514:foo bar:
1100 # This is the cheapest way of getting fields in the middle.
1101 set -- $(IFS=":" ; echo $_out)
1104 # The error output field can include colons so we'll try to
1105 # preserve them. The weak checking at the beginning tries to make
1106 # this work for both broken (no leading ':') and fixed output.
1108 _err_out="${_out#*monitor:${script_name}:*:*:*:*:}"
1110 OK) : ;; # Do nothing special.
1112 # Recast this as an error, since we can't exit with the
1113 # correct negative number.
1115 _err_out="[Replay of TIMEDOUT scriptstatus - note incorrect return code.] ${_err_out}"
1118 # Recast this as an OK, since we can't exit with the
1119 # correct negative number.
1121 _err_out="[Replay of DISABLED scriptstatus - note incorrect return code.] ${_err_out}"
1123 *) : ;; # Must be ERROR, do nothing special.
1125 if [ -n "$_err_out" ] ; then
1131 ctdb_service_check_reconfigure ()
1135 # We only care about some events in this function. For others we
1137 case "$event_name" in
1138 monitor|ipreallocated|reconfigure) : ;;
1142 if ctdb_reconfigure_take_lock ; then
1143 # No events covered by this function are running, so proceed
1145 case "$event_name" in
1147 (ctdb_service_reconfigure)
1151 if ctdb_service_needs_reconfigure ; then
1152 ctdb_service_reconfigure
1157 ctdb_reconfigure_release_lock
1159 # Somebody else is running an event we don't want to collide
1160 # with. We proceed with caution.
1161 case "$event_name" in
1163 # Tell whoever called us to retry.
1167 # Defer any scheduled reconfigure and just run the
1168 # rest of the ipreallocated event, as per the
1169 # eventscript. There's an assumption here that the
1170 # event doesn't depend on any scheduled reconfigure.
1171 # This is true in the current code.
1175 # There is most likely a reconfigure in progress so
1176 # the service is possibly unstable. As above, we
1177 # defer any scheduled reconfigured. We also replay
1178 # the previous monitor status since that's the best
1179 # information we have.
1180 ctdb_replay_monitor_status
1186 ##################################################################
1187 # Does CTDB manage this service? - and associated auto-start/stop
1189 ctdb_compat_managed_service ()
1191 if [ "$1" = "yes" -a "$2" = "$service_name" ] ; then
1192 CTDB_MANAGED_SERVICES="$CTDB_MANAGED_SERVICES $2"
1196 is_ctdb_managed_service ()
1200 # $t is used just for readability and to allow better accurate
1201 # matching via leading/trailing spaces
1202 t=" $CTDB_MANAGED_SERVICES "
1204 # Return 0 if "<space>$service_name<space>" appears in $t
1205 if [ "${t#* ${service_name} }" != "${t}" ] ; then
1209 # If above didn't match then update $CTDB_MANAGED_SERVICES for
1210 # backward compatibility and try again.
1211 ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD" "vsftpd"
1212 ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA" "samba"
1213 ctdb_compat_managed_service "$CTDB_MANAGES_WINBIND" "winbind"
1214 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "apache2"
1215 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "httpd"
1216 ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI" "iscsi"
1217 ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD" "clamd"
1218 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs"
1219 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs-ganesha-gpfs"
1221 t=" $CTDB_MANAGED_SERVICES "
1223 # Return 0 if "<space>$service_name<space>" appears in $t
1224 [ "${t#* ${service_name} }" != "${t}" ]
1227 ctdb_start_stop_service ()
1231 # Allow service-start/service-stop pseudo-events to start/stop
1232 # services when we're not auto-starting/stopping and we're not
1234 case "$event_name" in
1236 if is_ctdb_managed_service ; then
1237 die 'service-start event not permitted when service is managed'
1239 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1240 die 'service-start event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1246 if is_ctdb_managed_service ; then
1247 die 'service-stop event not permitted when service is managed'
1249 if [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] ; then
1250 die 'service-stop event not permitted with $CTDB_SERVICE_AUTOSTARTSTOP = yes'
1257 # Do nothing unless configured to...
1258 [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] || return 0
1260 [ "$event_name" = "monitor" ] || return 0
1262 if is_ctdb_managed_service ; then
1263 if ! is_ctdb_previously_managed_service ; then
1264 echo "Starting service \"$service_name\" - now managed"
1265 background_with_logging ctdb_service_start
1269 if is_ctdb_previously_managed_service ; then
1270 echo "Stopping service \"$service_name\" - no longer managed"
1271 background_with_logging ctdb_service_stop
1277 ctdb_service_start ()
1279 # The service is marked managed if we've ever tried to start it.
1280 ctdb_service_managed
1282 service_start || return $?
1288 ctdb_service_stop ()
1290 ctdb_service_unmanaged
1294 # Default service_start() and service_stop() functions.
1296 # These may be overridden in an eventscript.
1299 service "$service_name" start
1304 service "$service_name" stop
1307 ##################################################################
1309 ctdb_standard_event_handler ()
1324 # iptables doesn't like being re-entered, so flock-wrap it.
1327 flock -w 30 $CTDB_VARDIR/iptables-ctdb.flock /sbin/iptables "$@"
1330 # AIX (and perhaps others?) doesn't have mktemp
1331 if ! which mktemp >/dev/null 2>&1 ; then
1335 if [ "$1" = "-d" ] ; then
1339 _d="${TMPDIR:-/tmp}"
1340 _hex10=$(dd if=/dev/urandom count=20 2>/dev/null | \
1342 sed -e 's@\(..........\).*@\1@')
1343 _t="${_d}/tmp.${_hex10}"
1356 ########################################################
1358 ########################################################
1364 tickledir="$CTDB_VARDIR/state/tickles"
1365 mkdir -p "$tickledir"
1368 _pnn=$(ctdb pnn) ; _pnn=${_pnn#PNN:}
1370 # What public IPs do I hold?
1371 _ips=$(ctdb -Y ip | awk -F: -v pnn=$_pnn '$3 == pnn {print $2}')
1373 # IPs as a regexp choice
1374 _ipschoice="($(echo $_ips | sed -e 's/ /|/g' -e 's/\./\\\\./g'))"
1376 # Record connections to our public IPs in a temporary file
1377 _my_connections="${tickledir}/${_port}.connections"
1378 rm -f "$_my_connections"
1380 awk -v destpat="^${_ipschoice}:${_port}\$" \
1381 '$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ destpat {print $5, $4}' |
1382 sort >"$_my_connections"
1384 # Record our current tickles in a temporary file
1385 _my_tickles="${tickledir}/${_port}.tickles"
1386 rm -f "$_my_tickles"
1387 for _i in $_ips ; do
1388 ctdb -Y gettickles $_i $_port |
1389 awk -F: 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
1391 sort >"$_my_tickles"
1393 # Add tickles for connections that we haven't already got tickles for
1394 comm -23 "$_my_connections" "$_my_tickles" |
1395 while read _src _dst ; do
1396 ctdb addtickle $_src $_dst
1399 # Remove tickles for connections that are no longer there
1400 comm -13 "$_my_connections" "$_my_tickles" |
1401 while read _src _dst ; do
1402 ctdb deltickle $_src $_dst
1405 rm -f "$_my_connections" "$_my_tickles"
1408 ########################################################
1409 # load a site local config file
1410 ########################################################
1412 [ -n "$CTDB_RC_LOCAL" -a -x "$CTDB_RC_LOCAL" ] && {
1416 [ -x $CTDB_BASE/rc.local ] && {
1417 . $CTDB_BASE/rc.local
1420 [ -d $CTDB_BASE/rc.local.d ] && {
1421 for i in $CTDB_BASE/rc.local.d/* ; do
1422 [ -x "$i" ] && . "$i"
1426 script_name="${0##*/}" # basename
1427 service_fail_limit=1