1 # utility functions for ctdb event scripts
3 PATH=/bin:/usr/bin:/usr/sbin:/sbin:$PATH
5 #######################################
6 # pull in a system config file, if any
10 foo="${service_config:-${service_name}}"
11 if [ -n "$foo" ] ; then
14 elif [ "$1" != "ctdb" ] ; then
19 if [ -f /etc/sysconfig/$1 ]; then
21 elif [ -f /etc/default/$1 ]; then
23 elif [ -f $CTDB_BASE/sysconfig/$1 ]; then
24 . $CTDB_BASE/sysconfig/$1
32 ##############################################################
33 # determine on what type of system (init style) we are running
35 # only do detection if not already set:
36 test "x$CTDB_INIT_STYLE" != "x" && return
38 if [ -x /sbin/startproc ]; then
39 CTDB_INIT_STYLE="suse"
40 elif [ -x /sbin/start-stop-daemon ]; then
41 CTDB_INIT_STYLE="debian"
43 CTDB_INIT_STYLE="redhat"
47 ######################################################
48 # simulate /sbin/service on platforms that don't have it
53 # do nothing, when no service was specified
54 [ -z "$_service_name" ] && return
56 if [ -x /sbin/service ]; then
57 /sbin/service "$_service_name" "$_op"
58 elif [ -x /etc/init.d/$_service_name ]; then
59 /etc/init.d/$_service_name "$_op"
60 elif [ -x /etc/rc.d/init.d/$_service_name ]; then
61 /etc/rc.d/init.d/$_service_name "$_op"
65 ######################################################
66 # simulate /sbin/service (niced) on platforms that don't have it
71 # do nothing, when no service was specified
72 [ -z "$_service_name" ] && return
74 if [ -x /sbin/service ]; then
75 nice /sbin/service "$_service_name" "$_op"
76 elif [ -x /etc/init.d/$_service_name ]; then
77 nice /etc/init.d/$_service_name "$_op"
78 elif [ -x /etc/rc.d/init.d/$_service_name ]; then
79 nice /etc/rc.d/init.d/$_service_name "$_op"
83 ######################################################
84 # wait for a command to return a zero exit status
85 # usage: ctdb_wait_command SERVICE_NAME <command>
86 ######################################################
90 [ -z "$wait_cmd" ] && return;
92 echo "Waiting for service $service_name to start"
93 while [ $all_ok -eq 0 ]; do
94 $wait_cmd > /dev/null 2>&1 && all_ok=1
95 ctdb status > /dev/null 2>&1 || {
96 echo "ctdb daemon has died. Exiting wait for $service_name"
99 [ $all_ok -eq 1 ] || sleep 1
101 echo "Local service $service_name is up"
105 ######################################################
106 # wait for a set of tcp ports
107 # usage: ctdb_wait_tcp_ports SERVICE_NAME <ports...>
108 ######################################################
109 ctdb_wait_tcp_ports() {
113 [ -z "$wait_ports" ] && return;
115 echo "Waiting for tcp service $service_name to start"
116 while [ $all_ok -eq 0 ]; do
118 for p in $wait_ports; do
119 if [ -x /usr/bin/netcat ]; then
120 /usr/bin/netcat -z 127.0.0.1 $p > /dev/null || all_ok=0
121 elif [ -x /usr/bin/nc ]; then
122 /usr/bin/nc -z 127.0.0.1 $p > /dev/null || all_ok=0
123 elif [ -x /usr/bin/netstat ]; then
124 (netstat -a -n | egrep "0.0.0.0:$p[[:space:]]*LISTEN" > /dev/null) || all_ok=0
125 elif [ -x /bin/netstat ]; then
126 (netstat -a -n | egrep "0.0.0.0:$p[[:space:]]*LISTEN" > /dev/null) || all_ok=0
128 echo "No tool to check tcp ports availabe. can not check in ctdb_wait_tcp_ports"
132 [ $all_ok -eq 1 ] || sleep 1
133 ctdb status > /dev/null 2>&1 || {
134 echo "ctdb daemon has died. Exiting tcp wait $service_name"
138 echo "Local tcp services for $service_name are up"
142 ######################################################
143 # check that a rpc server is registered with portmap
144 # and responding to requests
145 # usage: ctdb_check_rpc SERVICE_NAME PROGNUM VERSION
146 ######################################################
152 ctdb_check_rpc_out=$(rpcinfo -u localhost $prognum $version 2>&1)
153 if [ $? -ne 0 ] ; then
154 ctdb_check_rpc_out="ERROR: $progname failed RPC check:
156 echo "$ctdb_check_rpc_out"
161 ######################################################
162 # check a set of directories is available
163 # return 1 on a missing directory
164 # usage: ctdb_check_directories_probe SERVICE_NAME <directories...>
165 ######################################################
166 ctdb_check_directories_probe() {
167 while IFS="" read d ; do
173 [ -d "${d}/." ] || return 1
178 ######################################################
179 # check a set of directories is available
180 # usage: ctdb_check_directories SERVICE_NAME <directories...>
181 ######################################################
182 ctdb_check_directories() {
183 n="${1:-${service_name}}"
184 ctdb_check_directories_probe || {
185 echo "ERROR: $n directory \"$d\" not available"
190 ######################################################
191 # check a set of tcp ports
192 # usage: ctdb_check_tcp_ports <ports...>
193 ######################################################
194 ctdb_check_tcp_ports() {
197 if ! netstat -a -t -n | grep -q "0\.0\.0\.0:$p .*LISTEN" ; then
198 if ! netstat -a -t -n | grep -q ":::$p .*LISTEN" ; then
199 echo "ERROR: $service_name tcp port $p is not responding"
206 ######################################################
207 # check a unix socket
208 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
209 ######################################################
210 ctdb_check_unix_socket() {
212 [ -z "$socket_path" ] && return
214 if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
215 echo "ERROR: $service_name socket $socket_path not found"
220 ######################################################
221 # check a command returns zero status
222 # usage: ctdb_check_command SERVICE_NAME <command>
223 ######################################################
224 ctdb_check_command() {
227 [ -z "$wait_cmd" ] && return;
228 $wait_cmd > /dev/null 2>&1 || {
229 echo "ERROR: $service_name - $wait_cmd returned error"
234 ################################################
235 # kill off any TCP connections with the given IP
236 ################################################
237 kill_tcp_connections() {
242 connfile="$CTDB_VARDIR/state/connections.$_IP"
243 netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
244 netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
246 while read dest src; do
247 srcip=`echo $src | sed -e "s/:[^:]*$//"`
248 srcport=`echo $src | sed -e "s/^.*://"`
249 destip=`echo $dest | sed -e "s/:[^:]*$//"`
250 destport=`echo $dest | sed -e "s/^.*://"`
251 echo "Killing TCP connection $srcip:$srcport $destip:$destport"
252 ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
254 # we only do one-way killtcp for CIFS
256 # for all others we do 2-way
258 ctdb killtcp $destip:$destport $srcip:$srcport >/dev/null 2>&1 || _failed=1
261 _killcount=`expr $_killcount + 1`
265 [ $_failed = 0 ] || {
266 echo "Failed to send killtcp control"
269 [ $_killcount -gt 0 ] || {
273 while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
275 _count=`expr $_count + 1`
276 [ $_count -gt 3 ] && {
277 echo "Timed out killing tcp connections for IP $_IP"
281 echo "killed $_killcount TCP connections to released IP $_IP"
284 ##################################################################
285 # kill off the local end for any TCP connections with the given IP
286 ##################################################################
287 kill_tcp_connections_local_only() {
292 connfile="$CTDB_VARDIR/state/connections.$_IP"
293 netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
294 netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
296 while read dest src; do
297 srcip=`echo $src | sed -e "s/:[^:]*$//"`
298 srcport=`echo $src | sed -e "s/^.*://"`
299 destip=`echo $dest | sed -e "s/:[^:]*$//"`
300 destport=`echo $dest | sed -e "s/^.*://"`
301 echo "Killing TCP connection $srcip:$srcport $destip:$destport"
302 ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
303 _killcount=`expr $_killcount + 1`
307 [ $_failed = 0 ] || {
308 echo "Failed to send killtcp control"
311 [ $_killcount -gt 0 ] || {
315 while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
317 _count=`expr $_count + 1`
318 [ $_count -gt 3 ] && {
319 echo "Timed out killing tcp connections for IP $_IP"
323 echo "killed $_killcount TCP connections to released IP $_IP"
326 ##################################################################
327 # tickle any TCP connections with the given IP
328 ##################################################################
329 tickle_tcp_connections() {
334 connfile="$CTDB_VARDIR/state/connections.$_IP"
335 netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
336 netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
338 while read dest src; do
339 srcip=`echo $src | sed -e "s/:[^:]*$//"`
340 srcport=`echo $src | sed -e "s/^.*://"`
341 destip=`echo $dest | sed -e "s/:[^:]*$//"`
342 destport=`echo $dest | sed -e "s/^.*://"`
343 echo "Tickle TCP connection $srcip:$srcport $destip:$destport"
344 ctdb tickle $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
345 echo "Tickle TCP connection $destip:$destport $srcip:$srcport"
346 ctdb tickle $destip:$destport $srcip:$srcport >/dev/null 2>&1 || _failed=1
350 [ $_failed = 0 ] || {
351 echo "Failed to send tickle control"
356 ########################################################
357 # start/stop the nfs service on different platforms
358 ########################################################
361 [ -x /etc/init.d/nfsserver ] && {
364 [ -x /etc/init.d/nfslock ] && {
372 service nfsserver start
375 service nfsserver stop > /dev/null 2>&1
378 service nfsserver restart
385 service nfslock start
389 service nfs stop > /dev/null 2>&1
390 service nfslock stop > /dev/null 2>&1
393 service nfslock restart
399 echo "Unknown platform. NFS is not supported with ctdb"
405 ########################################################
406 # start/stop the nfs lockmanager service on different platforms
407 ########################################################
408 startstop_nfslock() {
410 [ -x /etc/init.d/nfsserver ] && {
413 [ -x /etc/init.d/nfslock ] && {
419 # for sles there is no service for lockmanager
420 # so we instead just shutdown/restart nfs
423 service nfsserver start
426 service nfsserver stop > /dev/null 2>&1
429 service nfsserver stop
430 service nfsserver start
437 service nfslock start
440 service nfslock stop > /dev/null 2>&1
444 service nfslock start
449 echo "Unknown platform. NFS locking is not supported with ctdb"
455 # better use delete_ip_from_iface() together with add_ip_to_iface
456 # remove_ip should be removed in future
458 local _ip_maskbits=$1
460 local _ip=`echo "$_ip_maskbits" | cut -d '/' -f1`
461 local _maskbits=`echo "$_ip_maskbits" | cut -d '/' -f2`
463 delete_ip_from_iface "$_iface" "$_ip" "$_maskbits"
472 local _state_dir="$CTDB_VARDIR/state/interface_modify"
473 local _lockfile="$_state_dir/$_iface.flock"
474 local _readd_base="$_state_dir/$_iface.readd.d"
476 mkdir -p $_state_dir || {
478 echo "Failed to mkdir -p $_state_dir - $ret"
482 test -f $_lockfile || {
486 flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh add "$_iface" "$_ip" "$_maskbits" "$_readd_base"
490 delete_ip_from_iface()
495 local _state_dir="$CTDB_VARDIR/state/interface_modify"
496 local _lockfile="$_state_dir/$_iface.flock"
497 local _readd_base="$_state_dir/$_iface.readd.d"
499 mkdir -p $_state_dir || {
501 echo "Failed to mkdir -p $_state_dir - $ret"
505 test -f $_lockfile || {
509 flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh delete "$_iface" "$_ip" "$_maskbits" "$_readd_base"
513 setup_iface_ip_readd_script()
518 local _readd_script=$4
519 local _state_dir="$CTDB_VARDIR/state/interface_modify"
520 local _lockfile="$_state_dir/$_iface.flock"
521 local _readd_base="$_state_dir/$_iface.readd.d"
523 mkdir -p $_state_dir || {
525 echo "Failed to mkdir -p $_state_dir - $ret"
529 test -f $_lockfile || {
533 flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh readd_script "$_iface" "$_ip" "$_maskbits" "$_readd_base" "$_readd_script"
537 ########################################################
538 # some simple logic for counting events - per eventscript
539 # usage: ctdb_counter_init
541 # ctdb_check_counter_limit <limit>
542 # ctdb_check_counter_limit succeeds when count >= <limit>
543 ########################################################
544 _ctdb_counter_common () {
545 _counter_file="$ctdb_fail_dir/$service_name"
546 mkdir -p "${_counter_file%/*}" # dirname
548 ctdb_counter_init () {
553 ctdb_counter_incr () {
557 echo -n 1 >> "$_counter_file"
559 ctdb_check_counter_limit () {
562 _limit="${1:-${service_fail_limit}}"
566 _size=$(stat -c "%s" "$_counter_file" 2>/dev/null || echo 0)
567 if [ $_size -ge $_limit ] ; then
568 echo "ERROR: more than $_limit consecutive failures for $service_name, marking cluster unhealthy"
570 elif [ $_size -gt 0 -a -z "$_quiet" ] ; then
571 echo "WARNING: less than $_limit consecutive failures ($_size) for $service_name, not unhealthy yet"
574 ########################################################
576 ctdb_spool_dir="/var/spool/ctdb"
577 ctdb_status_dir="$ctdb_spool_dir/status"
578 ctdb_fail_dir="$ctdb_spool_dir/failcount"
579 ctdb_active_dir="$ctdb_spool_dir/active"
583 echo "node is \"$1\", \"${script_name}\" reports problem: $(cat $2)"
588 if [ -r "$ctdb_status_dir/$script_name/unhealthy" ] ; then
589 log_status_cat "unhealthy" "$ctdb_status_dir/$script_name/unhealthy"
591 elif [ -r "$ctdb_status_dir/$script_name/banned" ] ; then
592 log_status_cat "banned" "$ctdb_status_dir/$script_name/banned"
601 d="$ctdb_status_dir/$script_name"
608 for i in "banned" "unhealthy" ; do
615 ctdb_service_needs_reconfigure ()
617 [ -e "$ctdb_status_dir/$service_name/reconfigure" ]
620 ctdb_service_set_reconfigure ()
622 d="$ctdb_status_dir/$service_name"
627 ctdb_service_unset_reconfigure ()
629 rm -f "$ctdb_status_dir/$service_name/reconfigure"
632 ctdb_service_reconfigure ()
634 if [ -n "$service_reconfigure" ] ; then
635 eval $service_reconfigure
637 service "$service_name" restart
639 ctdb_service_unset_reconfigure
643 ctdb_compat_managed_service ()
645 if [ "$1" = "yes" ] ; then
650 is_ctdb_managed_service ()
652 t=" $CTDB_MANAGED_SERVICES "
654 ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD" "vsftpd"
655 ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA" "samba"
656 ctdb_compat_managed_service "$CTDB_MANAGES_SCP" "scp"
657 ctdb_compat_managed_service "$CTDB_MANAGES_WINDBIND" "windbind"
658 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "httpd"
659 ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI" "iscsi"
660 ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD" "clamd"
661 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs"
663 # Returns 0 if "<space>$service_name<space>" appears in $t
664 [ "${t#* ${service_name} }" != "${t}" ]
667 ctdb_start_stop_service ()
669 _active="$ctdb_active_dir/$service_name"
671 if is_ctdb_managed_service ; then
672 if ! [ -e "$_active" ] ; then
673 echo "Starting service $service_name"
674 ctdb_service_start || exit $?
675 mkdir -p "$ctdb_active_dir"
679 elif ! is_ctdb_managed_service ; then
680 if [ -e "$_active" ] ; then
681 echo "Stopping service $service_name"
682 ctdb_service_stop || exit $?
689 ctdb_service_start ()
691 if [ -n "$service_start" ] ; then
694 service "$service_name" start
701 if [ -n "$service_stop" ] ; then
704 service "$service_name" stop
708 ctdb_standard_event_handler ()
723 ipv4_host_addr_to_net_addr()
728 local HOST0=$(echo $HOST | awk -F . '{print $4}')
729 local HOST1=$(echo $HOST | awk -F . '{print $3}')
730 local HOST2=$(echo $HOST | awk -F . '{print $2}')
731 local HOST3=$(echo $HOST | awk -F . '{print $1}')
733 local HOST_NUM=$(( $HOST0 + $HOST1 * 256 + $HOST2 * (256 ** 2) + $HOST3 * (256 ** 3) ))
735 local MASK_NUM=$(( ( (2**32 - 1) * (2**(32 - $MASKBITS)) ) & (2**32 - 1) ))
737 local NET_NUM=$(( $HOST_NUM & $MASK_NUM))
739 local NET0=$(( $NET_NUM & 255 ))
740 local NET1=$(( ($NET_NUM & (255 * 256)) / 256 ))
741 local NET2=$(( ($NET_NUM & (255 * 256**2)) / 256**2 ))
742 local NET3=$(( ($NET_NUM & (255 * 256**3)) / 256**3 ))
744 echo "$NET3.$NET2.$NET1.$NET0"
747 ipv4_maskbits_to_net_mask()
751 local MASK_NUM=$(( ( (2**32 - 1) * (2**(32 - $MASKBITS)) ) & (2**32 - 1) ))
753 local MASK0=$(( $MASK_NUM & 255 ))
754 local MASK1=$(( ($MASK_NUM & (255 * 256)) / 256 ))
755 local MASK2=$(( ($MASK_NUM & (255 * 256**2)) / 256**2 ))
756 local MASK3=$(( ($MASK_NUM & (255 * 256**3)) / 256**3 ))
758 echo "$MASK3.$MASK2.$MASK1.$MASK0"
766 local N=`echo $ADDR | sed -e 's/[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*//'`
767 test -n "$N" && fail=1
769 local ADDR0=$(echo $ADDR | awk -F . '{print $4}')
770 local ADDR1=$(echo $ADDR | awk -F . '{print $3}')
771 local ADDR2=$(echo $ADDR | awk -F . '{print $2}')
772 local ADDR3=$(echo $ADDR | awk -F . '{print $1}')
774 test "$ADDR0" -gt 255 && fail=1
775 test "$ADDR1" -gt 255 && fail=1
776 test "$ADDR2" -gt 255 && fail=1
777 test "$ADDR3" -gt 255 && fail=1
779 test x"$fail" != x"0" && {
780 #echo "IPv4: '$ADDR' is not a valid address"
787 # iptables doesn't like being re-entered, so flock-wrap it.
790 flock -w 30 /var/ctdb/iptables-ctdb.flock /sbin/iptables "$@"
793 ########################################################
795 ########################################################
797 # Temporary directory for tickles.
798 tickledir="$CTDB_VARDIR/state/tickles"
799 mkdir -p "$tickledir"
805 mkdir -p "$tickledir" # Just in case
808 _pnn=$(ctdb pnn) ; _pnn=${_pnn#PNN:}
810 # What public IPs do I hold?
811 _ips=$(ctdb -Y ip | awk -F: -v pnn=$_pnn '$3 == pnn {print $2}')
813 # IPs as a regexp choice
814 _ipschoice="($(echo $_ips | sed -e 's/ /|/g' -e 's/\./\\\\./g'))"
816 # Record connections to our public IPs in a temporary file
817 _my_connections="${tickledir}/${_port}.connections"
818 rm -f "$_my_connections"
820 awk -v destpat="^${_ipschoice}:${_port}\$" \
821 '$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ destpat {print $5, $4}' |
822 sort >"$_my_connections"
824 # Record our current tickles in a temporary file
825 _my_tickles="${tickledir}/${_port}.tickles"
828 ctdb -Y gettickles $_i $_port |
829 awk -F: 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
833 # Add tickles for connections that we haven't already got tickles for
834 comm -23 "$_my_connections" "$_my_tickles" |
835 while read _src _dst ; do
836 ctdb addtickle $_src $_dst
839 # Remove tickles for connections that are no longer there
840 comm -13 "$_my_connections" "$_my_tickles" |
841 while read _src _dst ; do
842 ctdb deltickle $_src $_dst
845 rm -f "$_my_connections" "$_my_tickles"
848 ########################################################
849 # load a site local config file
850 ########################################################
852 [ -x $CTDB_BASE/rc.local ] && {
853 . $CTDB_BASE/rc.local
856 [ -d $CTDB_BASE/rc.local.d ] && {
857 for i in $CTDB_BASE/rc.local.d/* ; do
858 [ -x "$i" ] && . "$i"
862 script_name="${0##*/}" # basename
863 service_name="$script_name" # default is just the script name