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
28 ##############################################################
29 # determine on what type of system (init style) we are running
31 # only do detection if not already set:
32 test "x$CTDB_INIT_STYLE" != "x" && return
34 if [ -x /sbin/startproc ]; then
35 CTDB_INIT_STYLE="suse"
36 elif [ -x /sbin/start-stop-daemon ]; then
37 CTDB_INIT_STYLE="debian"
39 CTDB_INIT_STYLE="redhat"
43 ######################################################
44 # simulate /sbin/service on platforms that don't have it
49 # do nothing, when no service was specified
50 [ -z "$_service_name" ] && return
52 if [ -x /sbin/service ]; then
53 /sbin/service "$_service_name" "$_op"
54 elif [ -x /etc/init.d/$_service_name ]; then
55 /etc/init.d/$_service_name "$_op"
56 elif [ -x /etc/rc.d/init.d/$_service_name ]; then
57 /etc/rc.d/init.d/$_service_name "$_op"
61 ######################################################
62 # simulate /sbin/service (niced) on platforms that don't have it
67 # do nothing, when no service was specified
68 [ -z "$_service_name" ] && return
70 if [ -x /sbin/service ]; then
71 nice /sbin/service "$_service_name" "$_op"
72 elif [ -x /etc/init.d/$_service_name ]; then
73 nice /etc/init.d/$_service_name "$_op"
74 elif [ -x /etc/rc.d/init.d/$_service_name ]; then
75 nice /etc/rc.d/init.d/$_service_name "$_op"
79 ######################################################
80 # wait for a command to return a zero exit status
81 # usage: ctdb_wait_command SERVICE_NAME <command>
82 ######################################################
86 [ -z "$wait_cmd" ] && return;
88 echo "Waiting for service $service_name to start"
89 while [ $all_ok -eq 0 ]; do
90 $wait_cmd > /dev/null 2>&1 && all_ok=1
91 ctdb status > /dev/null 2>&1 || {
92 echo "ctdb daemon has died. Exiting wait for $service_name"
95 [ $all_ok -eq 1 ] || sleep 1
97 echo "Local service $service_name is up"
101 ######################################################
102 # wait for a set of tcp ports
103 # usage: ctdb_wait_tcp_ports SERVICE_NAME <ports...>
104 ######################################################
105 ctdb_wait_tcp_ports() {
109 [ -z "$wait_ports" ] && return;
111 echo "Waiting for tcp service $service_name to start"
112 while [ $all_ok -eq 0 ]; do
114 for p in $wait_ports; do
115 if [ -x /usr/bin/netcat ]; then
116 /usr/bin/netcat -z 127.0.0.1 $p > /dev/null || all_ok=0
117 elif [ -x /usr/bin/nc ]; then
118 /usr/bin/nc -z 127.0.0.1 $p > /dev/null || all_ok=0
119 elif [ -x /usr/bin/netstat ]; then
120 (netstat -a -n | egrep "0.0.0.0:$p[[:space:]]*LISTEN" > /dev/null) || all_ok=0
121 elif [ -x /bin/netstat ]; then
122 (netstat -a -n | egrep "0.0.0.0:$p[[:space:]]*LISTEN" > /dev/null) || all_ok=0
124 echo "No tool to check tcp ports availabe. can not check in ctdb_wait_tcp_ports"
128 [ $all_ok -eq 1 ] || sleep 1
129 ctdb status > /dev/null 2>&1 || {
130 echo "ctdb daemon has died. Exiting tcp wait $service_name"
134 echo "Local tcp services for $service_name are up"
138 ######################################################
139 # check that a rpc server is registered with portmap
140 # and responding to requests
141 # usage: ctdb_check_rpc SERVICE_NAME PROGNUM VERSION
142 ######################################################
148 ctdb_check_rpc_out=$(rpcinfo -u localhost $prognum $version 2>&1)
149 if [ $? -ne 0 ] ; then
150 ctdb_check_rpc_out="ERROR: $progname failed RPC check:
152 echo "$ctdb_check_rpc_out"
157 ######################################################
158 # check a set of directories is available
159 # return 1 on a missing directory
160 # usage: ctdb_check_directories_probe SERVICE_NAME <directories...>
161 ######################################################
162 ctdb_check_directories_probe() {
163 while IFS="" read d ; do
169 [ -d "${d}/." ] || return 1
174 ######################################################
175 # check a set of directories is available
176 # usage: ctdb_check_directories SERVICE_NAME <directories...>
177 ######################################################
178 ctdb_check_directories() {
179 n="${1:-${service_name}}"
180 ctdb_check_directories_probe || {
181 echo "ERROR: $n directory \"$d\" not available"
186 ######################################################
187 # check a set of tcp ports
188 # usage: ctdb_check_tcp_ports <ports...>
189 ######################################################
190 ctdb_check_tcp_ports() {
193 if ! netstat -a -t -n | grep -q "0\.0\.0\.0:$p .*LISTEN" ; then
194 if ! netstat -a -t -n | grep -q ":::$p .*LISTEN" ; then
195 echo "ERROR: $service_name tcp port $p is not responding"
202 ######################################################
203 # check a unix socket
204 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
205 ######################################################
206 ctdb_check_unix_socket() {
208 [ -z "$socket_path" ] && return
210 if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
211 echo "ERROR: $service_name socket $socket_path not found"
216 ######################################################
217 # check a command returns zero status
218 # usage: ctdb_check_command SERVICE_NAME <command>
219 ######################################################
220 ctdb_check_command() {
223 [ -z "$wait_cmd" ] && return;
224 $wait_cmd > /dev/null 2>&1 || {
225 echo "ERROR: $service_name - $wait_cmd returned error"
230 ################################################
231 # kill off any TCP connections with the given IP
232 ################################################
233 kill_tcp_connections() {
238 connfile="$CTDB_VARDIR/state/connections.$_IP"
239 netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
240 netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
242 while read dest src; do
243 srcip=`echo $src | sed -e "s/:[^:]*$//"`
244 srcport=`echo $src | sed -e "s/^.*://"`
245 destip=`echo $dest | sed -e "s/:[^:]*$//"`
246 destport=`echo $dest | sed -e "s/^.*://"`
247 echo "Killing TCP connection $srcip:$srcport $destip:$destport"
248 ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
250 # we only do one-way killtcp for CIFS
252 # for all others we do 2-way
254 ctdb killtcp $destip:$destport $srcip:$srcport >/dev/null 2>&1 || _failed=1
257 _killcount=`expr $_killcount + 1`
261 [ $_failed = 0 ] || {
262 echo "Failed to send killtcp control"
265 [ $_killcount -gt 0 ] || {
269 while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
271 _count=`expr $_count + 1`
272 [ $_count -gt 3 ] && {
273 echo "Timed out killing tcp connections for IP $_IP"
277 echo "killed $_killcount TCP connections to released IP $_IP"
280 ##################################################################
281 # kill off the local end for any TCP connections with the given IP
282 ##################################################################
283 kill_tcp_connections_local_only() {
288 connfile="$CTDB_VARDIR/state/connections.$_IP"
289 netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
290 netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
292 while read dest src; do
293 srcip=`echo $src | sed -e "s/:[^:]*$//"`
294 srcport=`echo $src | sed -e "s/^.*://"`
295 destip=`echo $dest | sed -e "s/:[^:]*$//"`
296 destport=`echo $dest | sed -e "s/^.*://"`
297 echo "Killing TCP connection $srcip:$srcport $destip:$destport"
298 ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
299 _killcount=`expr $_killcount + 1`
303 [ $_failed = 0 ] || {
304 echo "Failed to send killtcp control"
307 [ $_killcount -gt 0 ] || {
311 while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
313 _count=`expr $_count + 1`
314 [ $_count -gt 3 ] && {
315 echo "Timed out killing tcp connections for IP $_IP"
319 echo "killed $_killcount TCP connections to released IP $_IP"
322 ##################################################################
323 # tickle any TCP connections with the given IP
324 ##################################################################
325 tickle_tcp_connections() {
330 connfile="$CTDB_VARDIR/state/connections.$_IP"
331 netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
332 netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
334 while read dest src; do
335 srcip=`echo $src | sed -e "s/:[^:]*$//"`
336 srcport=`echo $src | sed -e "s/^.*://"`
337 destip=`echo $dest | sed -e "s/:[^:]*$//"`
338 destport=`echo $dest | sed -e "s/^.*://"`
339 echo "Tickle TCP connection $srcip:$srcport $destip:$destport"
340 ctdb tickle $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
341 echo "Tickle TCP connection $destip:$destport $srcip:$srcport"
342 ctdb tickle $destip:$destport $srcip:$srcport >/dev/null 2>&1 || _failed=1
346 [ $_failed = 0 ] || {
347 echo "Failed to send tickle control"
352 ########################################################
353 # start/stop the nfs service on different platforms
354 ########################################################
357 [ -x /etc/init.d/nfsserver ] && {
360 [ -x /etc/init.d/nfslock ] && {
368 service nfsserver start
371 service nfsserver stop > /dev/null 2>&1
374 service nfsserver restart
381 service nfslock start
385 service nfs stop > /dev/null 2>&1
386 service nfslock stop > /dev/null 2>&1
389 service nfslock restart
395 echo "Unknown platform. NFS is not supported with ctdb"
401 ########################################################
402 # start/stop the nfs lockmanager service on different platforms
403 ########################################################
404 startstop_nfslock() {
406 [ -x /etc/init.d/nfsserver ] && {
409 [ -x /etc/init.d/nfslock ] && {
415 # for sles there is no service for lockmanager
416 # so we instead just shutdown/restart nfs
419 service nfsserver start
422 service nfsserver stop > /dev/null 2>&1
425 service nfsserver stop
426 service nfsserver start
433 service nfslock start
436 service nfslock stop > /dev/null 2>&1
440 service nfslock start
445 echo "Unknown platform. NFS locking is not supported with ctdb"
451 # better use delete_ip_from_iface() together with add_ip_to_iface
452 # remove_ip should be removed in future
454 local _ip_maskbits=$1
456 local _ip=`echo "$_ip_maskbits" | cut -d '/' -f1`
457 local _maskbits=`echo "$_ip_maskbits" | cut -d '/' -f2`
459 delete_ip_from_iface "$_iface" "$_ip" "$_maskbits"
468 local _state_dir="$CTDB_VARDIR/state/interface_modify"
469 local _lockfile="$_state_dir/$_iface.flock"
470 local _readd_base="$_state_dir/$_iface.readd.d"
472 mkdir -p $_state_dir || {
474 echo "Failed to mkdir -p $_state_dir - $ret"
478 test -f $_lockfile || {
482 flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh add "$_iface" "$_ip" "$_maskbits" "$_readd_base"
486 delete_ip_from_iface()
491 local _state_dir="$CTDB_VARDIR/state/interface_modify"
492 local _lockfile="$_state_dir/$_iface.flock"
493 local _readd_base="$_state_dir/$_iface.readd.d"
495 mkdir -p $_state_dir || {
497 echo "Failed to mkdir -p $_state_dir - $ret"
501 test -f $_lockfile || {
505 flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh delete "$_iface" "$_ip" "$_maskbits" "$_readd_base"
509 setup_iface_ip_readd_script()
514 local _readd_script=$4
515 local _state_dir="$CTDB_VARDIR/state/interface_modify"
516 local _lockfile="$_state_dir/$_iface.flock"
517 local _readd_base="$_state_dir/$_iface.readd.d"
519 mkdir -p $_state_dir || {
521 echo "Failed to mkdir -p $_state_dir - $ret"
525 test -f $_lockfile || {
529 flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh readd_script "$_iface" "$_ip" "$_maskbits" "$_readd_base" "$_readd_script"
533 ########################################################
534 # some simple logic for counting events - per eventscript
535 # usage: ctdb_counter_init
537 # ctdb_check_counter_limit <limit>
538 # ctdb_check_counter_limit succeeds when count >= <limit>
539 ########################################################
540 _ctdb_counter_common () {
541 _counter_file="$ctdb_fail_dir/$service_name"
542 mkdir -p "${_counter_file%/*}" # dirname
544 ctdb_counter_init () {
549 ctdb_counter_incr () {
553 echo -n 1 >> "$_counter_file"
555 ctdb_check_counter_limit () {
558 _limit="${1:-${service_fail_limit}}"
562 _size=$(stat -c "%s" "$_counter_file" 2>/dev/null || echo 0)
563 if [ $_size -ge $_limit ] ; then
564 echo "ERROR: more than $_limit consecutive failures for $service_name, marking cluster unhealthy"
566 elif [ $_size -gt 0 -a -z "$_quiet" ] ; then
567 echo "WARNING: less than $_limit consecutive failures ($_size) for $service_name, not unhealthy yet"
570 ########################################################
572 ctdb_spool_dir="/var/spool/ctdb"
573 ctdb_status_dir="$ctdb_spool_dir/status"
574 ctdb_fail_dir="$ctdb_spool_dir/failcount"
575 ctdb_active_dir="$ctdb_spool_dir/active"
579 echo "node is \"$1\", \"${script_name}\" reports problem: $(cat $2)"
584 if [ -r "$ctdb_status_dir/$script_name/unhealthy" ] ; then
585 log_status_cat "unhealthy" "$ctdb_status_dir/$script_name/unhealthy"
587 elif [ -r "$ctdb_status_dir/$script_name/banned" ] ; then
588 log_status_cat "banned" "$ctdb_status_dir/$script_name/banned"
597 d="$ctdb_status_dir/$script_name"
604 for i in "banned" "unhealthy" ; do
611 ctdb_service_needs_reconfigure ()
613 [ -e "$ctdb_status_dir/$service_name/reconfigure" ]
616 ctdb_service_set_reconfigure ()
618 d="$ctdb_status_dir/$service_name"
623 ctdb_service_unset_reconfigure ()
625 rm -f "$ctdb_status_dir/$service_name/reconfigure"
628 ctdb_service_reconfigure ()
630 if [ -n "$service_reconfigure" ] ; then
631 eval $service_reconfigure
633 service "$service_name" restart
635 ctdb_service_unset_reconfigure
639 ctdb_compat_managed_service ()
641 if [ "$1" = "yes" ] ; then
646 is_ctdb_managed_service ()
648 t=" $CTDB_MANAGED_SERVICES "
650 ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD" "vsftpd"
651 ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA" "samba"
652 ctdb_compat_managed_service "$CTDB_MANAGES_SCP" "scp"
653 ctdb_compat_managed_service "$CTDB_MANAGES_WINDBIND" "windbind"
654 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "httpd"
655 ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI" "iscsi"
656 ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD" "clamd"
657 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs"
659 # Returns 0 if "<space>$service_name<space>" appears in $t
660 [ "${t#* ${service_name} }" != "${t}" ]
663 ctdb_start_stop_service ()
665 _active="$ctdb_active_dir/$service_name"
667 if is_ctdb_managed_service ; then
668 if ! [ -e "$_active" ] ; then
669 echo "Starting service $service_name"
670 ctdb_service_start || exit $?
671 mkdir -p "$ctdb_active_dir"
675 elif ! is_ctdb_managed_service ; then
676 if [ -e "$_active" ] ; then
677 echo "Stopping service $service_name"
678 ctdb_service_stop || exit $?
685 ctdb_service_start ()
687 if [ -n "$service_start" ] ; then
690 service "$service_name" start
697 if [ -n "$service_stop" ] ; then
700 service "$service_name" stop
704 ctdb_standard_event_handler ()
719 ipv4_host_addr_to_net_addr()
724 local HOST0=$(echo $HOST | awk -F . '{print $4}')
725 local HOST1=$(echo $HOST | awk -F . '{print $3}')
726 local HOST2=$(echo $HOST | awk -F . '{print $2}')
727 local HOST3=$(echo $HOST | awk -F . '{print $1}')
729 local HOST_NUM=$(( $HOST0 + $HOST1 * 256 + $HOST2 * (256 ** 2) + $HOST3 * (256 ** 3) ))
731 local MASK_NUM=$(( ( (2**32 - 1) * (2**(32 - $MASKBITS)) ) & (2**32 - 1) ))
733 local NET_NUM=$(( $HOST_NUM & $MASK_NUM))
735 local NET0=$(( $NET_NUM & 255 ))
736 local NET1=$(( ($NET_NUM & (255 * 256)) / 256 ))
737 local NET2=$(( ($NET_NUM & (255 * 256**2)) / 256**2 ))
738 local NET3=$(( ($NET_NUM & (255 * 256**3)) / 256**3 ))
740 echo "$NET3.$NET2.$NET1.$NET0"
743 ipv4_maskbits_to_net_mask()
747 local MASK_NUM=$(( ( (2**32 - 1) * (2**(32 - $MASKBITS)) ) & (2**32 - 1) ))
749 local MASK0=$(( $MASK_NUM & 255 ))
750 local MASK1=$(( ($MASK_NUM & (255 * 256)) / 256 ))
751 local MASK2=$(( ($MASK_NUM & (255 * 256**2)) / 256**2 ))
752 local MASK3=$(( ($MASK_NUM & (255 * 256**3)) / 256**3 ))
754 echo "$MASK3.$MASK2.$MASK1.$MASK0"
762 local N=`echo $ADDR | sed -e 's/[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*//'`
763 test -n "$N" && fail=1
765 local ADDR0=$(echo $ADDR | awk -F . '{print $4}')
766 local ADDR1=$(echo $ADDR | awk -F . '{print $3}')
767 local ADDR2=$(echo $ADDR | awk -F . '{print $2}')
768 local ADDR3=$(echo $ADDR | awk -F . '{print $1}')
770 test "$ADDR0" -gt 255 && fail=1
771 test "$ADDR1" -gt 255 && fail=1
772 test "$ADDR2" -gt 255 && fail=1
773 test "$ADDR3" -gt 255 && fail=1
775 test x"$fail" != x"0" && {
776 #echo "IPv4: '$ADDR' is not a valid address"
783 # iptables doesn't like being re-entered, so flock-wrap it.
786 flock -w 30 /var/ctdb/iptables-ctdb.flock /sbin/iptables "$@"
789 ########################################################
791 ########################################################
793 # Temporary directory for tickles.
794 tickledir="$CTDB_VARDIR/state/tickles"
795 mkdir -p "$tickledir"
801 mkdir -p "$tickledir" # Just in case
804 _pnn=$(ctdb pnn) ; _pnn=${_pnn#PNN:}
806 # What public IPs do I hold?
807 _ips=$(ctdb -Y ip | awk -F: -v pnn=$_pnn '$3 == pnn {print $2}')
809 # IPs as a regexp choice
810 _ipschoice="($(echo $_ips | sed -e 's/ /|/g' -e 's/\./\\\\./g'))"
812 # Record connections to our public IPs in a temporary file
813 _my_connections="${tickledir}/${_port}.connections"
814 rm -f "$_my_connections"
816 awk -v destpat="^${_ipschoice}:${_port}\$" \
817 '$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ destpat {print $5, $4}' |
818 sort >"$_my_connections"
820 # Record our current tickles in a temporary file
821 _my_tickles="${tickledir}/${_port}.tickles"
824 ctdb -Y gettickles $_i $_port |
825 awk -F: 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
829 # Add tickles for connections that we haven't already got tickles for
830 comm -23 "$_my_connections" "$_my_tickles" |
831 while read _src _dst ; do
832 ctdb addtickle $_src $_dst
835 # Remove tickles for connections that are no longer there
836 comm -13 "$_my_connections" "$_my_tickles" |
837 while read _src _dst ; do
838 ctdb deltickle $_src $_dst
841 rm -f "$_my_connections" "$_my_tickles"
844 ########################################################
845 # load a site local config file
846 ########################################################
848 [ -x $CTDB_BASE/rc.local ] && {
849 . $CTDB_BASE/rc.local
852 [ -d $CTDB_BASE/rc.local.d ] && {
853 for i in $CTDB_BASE/rc.local.d/* ; do
854 [ -x "$i" ] && . "$i"
858 script_name="${0##*/}" # basename
859 service_name="$script_name" # default is just the script name