Eventscripts: make loadconfig() function hookable by the test suite.
[ctdb.git] / config / functions
1 # utility functions for ctdb event scripts
2
3 PATH=/bin:/usr/bin:/usr/sbin:/sbin:$PATH
4
5 #######################################
6 # pull in a system config file, if any
7 _loadconfig() {
8
9     if [ -z "$1" ] ; then
10         foo="${service_config:-${service_name}}"
11         if [ -n "$foo" ] ; then
12             loadconfig "$foo"
13         fi
14     elif [ "$1" != "ctdb" ] ; then
15         loadconfig "ctdb"
16     fi
17
18
19     if [ -f /etc/sysconfig/$1 ]; then
20         . /etc/sysconfig/$1
21     elif [ -f /etc/default/$1 ]; then
22         . /etc/default/$1
23     elif [ -f $CTDB_BASE/sysconfig/$1 ]; then
24         . $CTDB_BASE/sysconfig/$1
25     fi
26 }
27
28 loadconfig () {
29     _loadconfig "$@"
30 }
31
32 ##############################################################
33 # determine on what type of system (init style) we are running
34 detect_init_style() {
35     # only do detection if not already set:
36     test "x$CTDB_INIT_STYLE" != "x" && return
37
38     if [ -x /sbin/startproc ]; then
39         CTDB_INIT_STYLE="suse"
40     elif [ -x /sbin/start-stop-daemon ]; then
41         CTDB_INIT_STYLE="debian"
42     else
43         CTDB_INIT_STYLE="redhat"
44     fi
45 }
46
47 ######################################################
48 # simulate /sbin/service on platforms that don't have it
49 service() { 
50   _service_name="$1"
51   _op="$2"
52
53   # do nothing, when no service was specified
54   [ -z "$_service_name" ] && return
55
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"
62   fi
63 }
64
65 ######################################################
66 # simulate /sbin/service (niced) on platforms that don't have it
67 nice_service() { 
68   _service_name="$1"
69   _op="$2"
70
71   # do nothing, when no service was specified
72   [ -z "$_service_name" ] && return
73
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"
80   fi
81 }
82
83 ######################################################
84 # wait for a command to return a zero exit status
85 # usage: ctdb_wait_command SERVICE_NAME <command>
86 ######################################################
87 ctdb_wait_command() {
88   service_name="$1"
89   wait_cmd="$2"
90   [ -z "$wait_cmd" ] && return;
91   all_ok=0
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"
97                 exit 1
98           }
99           [ $all_ok -eq 1 ] || sleep 1
100   done
101   echo "Local service $service_name is up"
102 }
103
104
105 ######################################################
106 # wait for a set of tcp ports
107 # usage: ctdb_wait_tcp_ports SERVICE_NAME <ports...>
108 ######################################################
109 ctdb_wait_tcp_ports() {
110   service_name="$1"
111   shift
112   wait_ports="$*"
113   [ -z "$wait_ports" ] && return;
114   all_ok=0
115   echo "Waiting for tcp service $service_name to start"
116   while [ $all_ok -eq 0 ]; do
117           all_ok=1
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
127               else 
128                   echo "No tool to check tcp ports availabe. can not check in ctdb_wait_tcp_ports"
129                   return 127
130               fi
131           done
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"
135                 return 1
136           }
137   done
138   echo "Local tcp services for $service_name are up"
139 }
140
141
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 ######################################################
147 ctdb_check_rpc() {
148     progname="$1"
149     prognum="$2"
150     version="$3"
151
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:
155 $ctdb_check_rpc_out"
156         echo "$ctdb_check_rpc_out"
157         return 1
158     fi
159 }
160
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
168         case "$d" in
169             *%*)
170                 continue
171                 ;;
172             *)
173                 [ -d "${d}/." ] || return 1
174         esac
175     done
176 }
177
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"
186         exit 1
187     }
188 }
189
190 ######################################################
191 # check a set of tcp ports
192 # usage: ctdb_check_tcp_ports <ports...>
193 ######################################################
194 ctdb_check_tcp_ports() {
195
196     for p ; do
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"
200                 return 1
201             fi
202         fi
203     done
204 }
205
206 ######################################################
207 # check a unix socket
208 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
209 ######################################################
210 ctdb_check_unix_socket() {
211     socket_path="$1"
212     [ -z "$socket_path" ] && return
213
214     if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
215         echo "ERROR: $service_name socket $socket_path not found"
216         return 1
217     fi
218 }
219
220 ######################################################
221 # check a command returns zero status
222 # usage: ctdb_check_command SERVICE_NAME <command>
223 ######################################################
224 ctdb_check_command() {
225   service_name="$1"
226   wait_cmd="$2"
227   [ -z "$wait_cmd" ] && return;
228   $wait_cmd > /dev/null 2>&1 || {
229       echo "ERROR: $service_name - $wait_cmd returned error"
230       exit 1
231   }
232 }
233
234 ################################################
235 # kill off any TCP connections with the given IP
236 ################################################
237 kill_tcp_connections() {
238     _IP="$1"    
239     _failed=0
240
241     _killcount=0
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
245
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
253         case $destport in
254           # we only do one-way killtcp for CIFS
255           139|445) : ;;
256           # for all others we do 2-way
257           *) 
258                 ctdb killtcp $destip:$destport $srcip:$srcport >/dev/null 2>&1 || _failed=1
259                 ;;
260         esac
261         _killcount=`expr $_killcount + 1`
262      done < $connfile
263     /bin/rm -f $connfile
264
265     [ $_failed = 0 ] || {
266         echo "Failed to send killtcp control"
267         return;
268     }
269     [ $_killcount -gt 0 ] || {
270         return;
271     }
272     _count=0
273     while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
274         sleep 1
275         _count=`expr $_count + 1`
276         [ $_count -gt 3 ] && {
277             echo "Timed out killing tcp connections for IP $_IP"
278             return;
279         }
280     done
281     echo "killed $_killcount TCP connections to released IP $_IP"
282 }
283
284 ##################################################################
285 # kill off the local end for any TCP connections with the given IP
286 ##################################################################
287 kill_tcp_connections_local_only() {
288     _IP="$1"    
289     _failed=0
290
291     _killcount=0
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
295
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`
304      done < $connfile
305     /bin/rm -f $connfile
306
307     [ $_failed = 0 ] || {
308         echo "Failed to send killtcp control"
309         return;
310     }
311     [ $_killcount -gt 0 ] || {
312         return;
313     }
314     _count=0
315     while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
316         sleep 1
317         _count=`expr $_count + 1`
318         [ $_count -gt 3 ] && {
319             echo "Timed out killing tcp connections for IP $_IP"
320             return;
321         }
322     done
323     echo "killed $_killcount TCP connections to released IP $_IP"
324 }
325
326 ##################################################################
327 # tickle any TCP connections with the given IP
328 ##################################################################
329 tickle_tcp_connections() {
330     _IP="$1"
331     _failed=0
332
333     _killcount=0
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
337
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
347      done < $connfile
348     /bin/rm -f $connfile
349
350     [ $_failed = 0 ] || {
351         echo "Failed to send tickle control"
352         return;
353     }
354 }
355
356 ########################################################
357 # start/stop the nfs service on different platforms
358 ########################################################
359 startstop_nfs() {
360         PLATFORM="unknown"
361         [ -x /etc/init.d/nfsserver ] && {
362                 PLATFORM="sles"
363         }
364         [ -x /etc/init.d/nfslock ] && {
365                 PLATFORM="rhel"
366         }
367
368         case $PLATFORM in
369         sles)
370                 case $1 in
371                 start)
372                         service nfsserver start
373                         ;;
374                 stop)
375                         service nfsserver stop > /dev/null 2>&1
376                         ;;
377                 restart)
378                         service nfsserver restart
379                         ;;
380                 esac
381                 ;;
382         rhel)
383                 case $1 in
384                 start)
385                         service nfslock start
386                         service nfs start
387                         ;;
388                 stop)
389                         service nfs stop > /dev/null 2>&1
390                         service nfslock stop > /dev/null 2>&1
391                         ;;
392                 restart)
393                         service nfslock restart
394                         service nfs restart
395                         ;;
396                 esac
397                 ;;
398         *)
399                 echo "Unknown platform. NFS is not supported with ctdb"
400                 exit 1
401                 ;;
402         esac
403 }
404
405 ########################################################
406 # start/stop the nfs lockmanager service on different platforms
407 ########################################################
408 startstop_nfslock() {
409         PLATFORM="unknown"
410         [ -x /etc/init.d/nfsserver ] && {
411                 PLATFORM="sles"
412         }
413         [ -x /etc/init.d/nfslock ] && {
414                 PLATFORM="rhel"
415         }
416
417         case $PLATFORM in
418         sles)
419                 # for sles there is no service for lockmanager
420                 # so we instead just shutdown/restart nfs
421                 case $1 in
422                 start)
423                         service nfsserver start
424                         ;;
425                 stop)
426                         service nfsserver stop > /dev/null 2>&1
427                         ;;
428                 restart)
429                         service nfsserver stop
430                         service nfsserver start
431                         ;;
432                 esac
433                 ;;
434         rhel)
435                 case $1 in
436                 start)
437                         service nfslock start
438                         ;;
439                 stop)
440                         service nfslock stop > /dev/null 2>&1
441                         ;;
442                 restart)
443                         service nfslock stop
444                         service nfslock start
445                         ;;
446                 esac
447                 ;;
448         *)
449                 echo "Unknown platform. NFS locking is not supported with ctdb"
450                 exit 1
451                 ;;
452         esac
453 }
454
455 # better use delete_ip_from_iface() together with add_ip_to_iface
456 # remove_ip should be removed in future
457 remove_ip() {
458         local _ip_maskbits=$1
459         local _iface=$2
460         local _ip=`echo "$_ip_maskbits" | cut -d '/' -f1`
461         local _maskbits=`echo "$_ip_maskbits" | cut -d '/' -f2`
462
463         delete_ip_from_iface "$_iface" "$_ip" "$_maskbits"
464         return $?
465 }
466
467 add_ip_to_iface()
468 {
469         local _iface=$1
470         local _ip=$2
471         local _maskbits=$3
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"
475
476         mkdir -p $_state_dir || {
477                 ret=$?
478                 echo "Failed to mkdir -p $_state_dir - $ret"
479                 return $ret
480         }
481
482         test -f $_lockfile || {
483                 touch $_lockfile
484         }
485
486         flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh add "$_iface" "$_ip" "$_maskbits" "$_readd_base"
487         return $?
488 }
489
490 delete_ip_from_iface()
491 {
492         local _iface=$1
493         local _ip=$2
494         local _maskbits=$3
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"
498
499         mkdir -p $_state_dir || {
500                 ret=$?
501                 echo "Failed to mkdir -p $_state_dir - $ret"
502                 return $ret
503         }
504
505         test -f $_lockfile || {
506                 touch $_lockfile
507         }
508
509         flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh delete "$_iface" "$_ip" "$_maskbits" "$_readd_base"
510         return $?
511 }
512
513 setup_iface_ip_readd_script()
514 {
515         local _iface=$1
516         local _ip=$2
517         local _maskbits=$3
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"
522
523         mkdir -p $_state_dir || {
524                 ret=$?
525                 echo "Failed to mkdir -p $_state_dir - $ret"
526                 return $ret
527         }
528
529         test -f $_lockfile || {
530                 touch $_lockfile
531         }
532
533         flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh readd_script "$_iface" "$_ip" "$_maskbits" "$_readd_base" "$_readd_script"
534         return $?
535 }
536
537 ########################################################
538 # some simple logic for counting events - per eventscript
539 # usage: ctdb_counter_init
540 #        ctdb_counter_incr
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
547 }
548 ctdb_counter_init () {
549     _ctdb_counter_common
550
551     >"$_counter_file"
552 }
553 ctdb_counter_incr () {
554     _ctdb_counter_common
555
556     # unary counting!
557     echo -n 1 >> "$_counter_file"
558 }
559 ctdb_check_counter_limit () {
560     _ctdb_counter_common
561
562     _limit="${1:-${service_fail_limit}}"
563     _quiet="$2"
564
565     # unary counting!
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"
569         exit 1
570     elif [ $_size -gt 0 -a -z "$_quiet" ] ; then
571         echo "WARNING: less than $_limit consecutive failures ($_size) for $service_name, not unhealthy yet"
572     fi
573 }
574 ########################################################
575
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"
580
581 log_status_cat ()
582 {
583     echo "node is \"$1\", \"${script_name}\" reports problem: $(cat $2)"
584 }
585
586 ctdb_checkstatus ()
587 {
588     if [ -r "$ctdb_status_dir/$script_name/unhealthy" ] ; then
589         log_status_cat "unhealthy" "$ctdb_status_dir/$script_name/unhealthy"
590         return 1
591     elif [ -r "$ctdb_status_dir/$script_name/banned" ] ; then
592         log_status_cat "banned" "$ctdb_status_dir/$script_name/banned"
593         return 2
594     else
595         return 0
596     fi
597 }
598
599 ctdb_setstatus ()
600 {
601     d="$ctdb_status_dir/$script_name"
602     case "$1" in
603         unhealthy|banned)
604             mkdir -p "$d"
605             cat "$2" >"$d/$1"
606             ;;
607         *)
608             for i in "banned" "unhealthy" ; do
609                 rm -f "$d/$i"
610             done
611             ;;
612     esac
613 }
614
615 ctdb_service_needs_reconfigure ()
616 {
617     [ -e "$ctdb_status_dir/$service_name/reconfigure" ]
618 }
619
620 ctdb_service_set_reconfigure ()
621 {
622     d="$ctdb_status_dir/$service_name"
623     mkdir -p "$d"
624     >"$d/reconfigure"
625 }
626
627 ctdb_service_unset_reconfigure ()
628 {
629     rm -f "$ctdb_status_dir/$service_name/reconfigure"
630 }
631
632 ctdb_service_reconfigure ()
633 {
634     if [ -n "$service_reconfigure" ] ; then
635         eval $service_reconfigure
636     else
637         service "$service_name" restart
638     fi
639     ctdb_service_unset_reconfigure
640     ctdb_counter_init
641 }
642
643 ctdb_compat_managed_service ()
644 {
645     if [ "$1" = "yes" ] ; then
646         t="$t $2 "
647     fi
648 }
649
650 is_ctdb_managed_service ()
651 {
652     t=" $CTDB_MANAGED_SERVICES "
653
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"
662
663     # Returns 0 if "<space>$service_name<space>" appears in $t
664     [ "${t#* ${service_name} }" != "${t}" ]
665 }
666
667 ctdb_start_stop_service ()
668 {
669     _active="$ctdb_active_dir/$service_name"
670
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"
676             touch "$_active"
677             exit 0
678         fi
679     elif ! is_ctdb_managed_service ; then
680         if [ -e "$_active" ] ; then
681             echo "Stopping service $service_name"
682             ctdb_service_stop || exit $?
683             rm -f "$_active"
684         fi
685         exit 0
686     fi
687 }
688
689 ctdb_service_start ()
690 {
691     if [ -n "$service_start" ] ; then
692         eval $service_start
693     else
694         service "$service_name" start
695     fi
696     ctdb_counter_init
697 }
698
699 ctdb_service_stop ()
700 {
701     if [ -n "$service_stop" ] ; then
702         eval $service_stop
703     else
704         service "$service_name" stop
705     fi
706 }
707
708 ctdb_standard_event_handler ()
709 {
710     case "$1" in
711         status)
712             ctdb_checkstatus
713             exit
714             ;;
715         setstatus)
716             shift
717             ctdb_setstatus "$@"
718             exit
719             ;;
720     esac
721 }
722
723 ipv4_host_addr_to_net_addr()
724 {
725         local HOST=$1
726         local MASKBITS=$2
727
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}')
732
733         local HOST_NUM=$(( $HOST0 + $HOST1 * 256 + $HOST2 * (256 ** 2) + $HOST3 * (256 ** 3) ))
734
735         local MASK_NUM=$(( ( (2**32 - 1) * (2**(32 - $MASKBITS)) ) & (2**32 - 1) ))
736
737         local NET_NUM=$(( $HOST_NUM & $MASK_NUM))
738
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 ))
743
744         echo "$NET3.$NET2.$NET1.$NET0"
745 }
746
747 ipv4_maskbits_to_net_mask()
748 {
749         local MASKBITS=$1
750
751         local MASK_NUM=$(( ( (2**32 - 1) * (2**(32 - $MASKBITS)) ) & (2**32 - 1) ))
752
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 ))
757
758         echo "$MASK3.$MASK2.$MASK1.$MASK0"
759 }
760
761 ipv4_is_valid_addr()
762 {
763         local ADDR=$1
764         local fail=0
765
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
768
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}')
773
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
778
779         test x"$fail" != x"0" && {
780                 #echo "IPv4: '$ADDR' is not a valid address"
781                 return 1;
782         }
783
784         return 0;
785 }
786
787 # iptables doesn't like being re-entered, so flock-wrap it.
788 iptables()
789 {
790         flock -w 30 /var/ctdb/iptables-ctdb.flock /sbin/iptables "$@"
791 }
792
793 ########################################################
794 # tickle handling
795 ########################################################
796
797 # Temporary directory for tickles.
798 tickledir="$CTDB_VARDIR/state/tickles"
799 mkdir -p "$tickledir"
800
801 update_tickles ()
802 {
803         _port="$1"
804
805         mkdir -p "$tickledir" # Just in case
806
807         # Who am I?
808         _pnn=$(ctdb pnn) ; _pnn=${_pnn#PNN:}
809
810         # What public IPs do I hold?
811         _ips=$(ctdb -Y ip | awk -F: -v pnn=$_pnn '$3 == pnn {print $2}')
812
813         # IPs as a regexp choice
814         _ipschoice="($(echo $_ips | sed -e 's/ /|/g' -e 's/\./\\\\./g'))"
815
816         # Record connections to our public IPs in a temporary file
817         _my_connections="${tickledir}/${_port}.connections"
818         rm -f "$_my_connections"
819         netstat -tn |
820         awk -v destpat="^${_ipschoice}:${_port}\$" \
821           '$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ destpat {print $5, $4}' |
822         sort >"$_my_connections"
823
824         # Record our current tickles in a temporary file
825         _my_tickles="${tickledir}/${_port}.tickles"
826         rm -f "$_my_tickles"
827         for _i in $_ips ; do
828                 ctdb -Y gettickles $_i $_port | 
829                 awk -F: 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
830         done |
831         sort >"$_my_tickles"
832
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
837         done
838
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
843         done
844
845         rm -f "$_my_connections" "$_my_tickles" 
846 }
847
848 ########################################################
849 # load a site local config file
850 ########################################################
851
852 [ -x $CTDB_BASE/rc.local ] && {
853         . $CTDB_BASE/rc.local
854 }
855
856 [ -d $CTDB_BASE/rc.local.d ] && {
857         for i in $CTDB_BASE/rc.local.d/* ; do
858                 [ -x "$i" ] && . "$i"
859         done
860 }
861
862 script_name="${0##*/}"       # basename
863 service_name="$script_name"  # default is just the script name
864 service_fail_limit=1