Eventscripts: work around NFS restart failure under load.
[sahlberg/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                         echo 0 >/proc/fs/nfsd/threads
379                         service nfsserver stop > /dev/null 2>&1
380                         pkill -9 nfsd
381                         service nfsserver start
382                         ;;
383                 esac
384                 ;;
385         rhel)
386                 case $1 in
387                 start)
388                         service nfslock start
389                         service nfs start
390                         ;;
391                 stop)
392                         service nfs stop > /dev/null 2>&1
393                         service nfslock stop > /dev/null 2>&1
394                         ;;
395                 restart)
396                         echo 0 >/proc/fs/nfsd/threads
397                         service nfs stop > /dev/null 2>&1
398                         service nfslock stop > /dev/null 2>&1
399                         pkill -9 nfsd
400                         service nfslock start
401                         service nfs start
402                         ;;
403                 esac
404                 ;;
405         *)
406                 echo "Unknown platform. NFS is not supported with ctdb"
407                 exit 1
408                 ;;
409         esac
410 }
411
412 ########################################################
413 # start/stop the nfs lockmanager service on different platforms
414 ########################################################
415 startstop_nfslock() {
416         PLATFORM="unknown"
417         [ -x /etc/init.d/nfsserver ] && {
418                 PLATFORM="sles"
419         }
420         [ -x /etc/init.d/nfslock ] && {
421                 PLATFORM="rhel"
422         }
423
424         case $PLATFORM in
425         sles)
426                 # for sles there is no service for lockmanager
427                 # so we instead just shutdown/restart nfs
428                 case $1 in
429                 start)
430                         service nfsserver start
431                         ;;
432                 stop)
433                         service nfsserver stop > /dev/null 2>&1
434                         ;;
435                 restart)
436                         service nfsserver stop
437                         service nfsserver start
438                         ;;
439                 esac
440                 ;;
441         rhel)
442                 case $1 in
443                 start)
444                         service nfslock start
445                         ;;
446                 stop)
447                         service nfslock stop > /dev/null 2>&1
448                         ;;
449                 restart)
450                         service nfslock stop
451                         service nfslock start
452                         ;;
453                 esac
454                 ;;
455         *)
456                 echo "Unknown platform. NFS locking is not supported with ctdb"
457                 exit 1
458                 ;;
459         esac
460 }
461
462 # better use delete_ip_from_iface() together with add_ip_to_iface
463 # remove_ip should be removed in future
464 remove_ip() {
465         local _ip_maskbits=$1
466         local _iface=$2
467         local _ip=`echo "$_ip_maskbits" | cut -d '/' -f1`
468         local _maskbits=`echo "$_ip_maskbits" | cut -d '/' -f2`
469
470         delete_ip_from_iface "$_iface" "$_ip" "$_maskbits"
471         return $?
472 }
473
474 add_ip_to_iface()
475 {
476         local _iface=$1
477         local _ip=$2
478         local _maskbits=$3
479         local _state_dir="$CTDB_VARDIR/state/interface_modify"
480         local _lockfile="$_state_dir/$_iface.flock"
481         local _readd_base="$_state_dir/$_iface.readd.d"
482
483         mkdir -p $_state_dir || {
484                 ret=$?
485                 echo "Failed to mkdir -p $_state_dir - $ret"
486                 return $ret
487         }
488
489         test -f $_lockfile || {
490                 touch $_lockfile
491         }
492
493         flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh add "$_iface" "$_ip" "$_maskbits" "$_readd_base"
494         return $?
495 }
496
497 delete_ip_from_iface()
498 {
499         local _iface=$1
500         local _ip=$2
501         local _maskbits=$3
502         local _state_dir="$CTDB_VARDIR/state/interface_modify"
503         local _lockfile="$_state_dir/$_iface.flock"
504         local _readd_base="$_state_dir/$_iface.readd.d"
505
506         mkdir -p $_state_dir || {
507                 ret=$?
508                 echo "Failed to mkdir -p $_state_dir - $ret"
509                 return $ret
510         }
511
512         test -f $_lockfile || {
513                 touch $_lockfile
514         }
515
516         flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh delete "$_iface" "$_ip" "$_maskbits" "$_readd_base"
517         return $?
518 }
519
520 setup_iface_ip_readd_script()
521 {
522         local _iface=$1
523         local _ip=$2
524         local _maskbits=$3
525         local _readd_script=$4
526         local _state_dir="$CTDB_VARDIR/state/interface_modify"
527         local _lockfile="$_state_dir/$_iface.flock"
528         local _readd_base="$_state_dir/$_iface.readd.d"
529
530         mkdir -p $_state_dir || {
531                 ret=$?
532                 echo "Failed to mkdir -p $_state_dir - $ret"
533                 return $ret
534         }
535
536         test -f $_lockfile || {
537                 touch $_lockfile
538         }
539
540         flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh readd_script "$_iface" "$_ip" "$_maskbits" "$_readd_base" "$_readd_script"
541         return $?
542 }
543
544 ########################################################
545 # some simple logic for counting events - per eventscript
546 # usage: ctdb_counter_init
547 #        ctdb_counter_incr
548 #        ctdb_check_counter_limit <limit>
549 # ctdb_check_counter_limit succeeds when count >= <limit>
550 ########################################################
551 _ctdb_counter_common () {
552     _counter_file="$ctdb_fail_dir/$service_name"
553     mkdir -p "${_counter_file%/*}" # dirname
554 }
555 ctdb_counter_init () {
556     _ctdb_counter_common
557
558     >"$_counter_file"
559 }
560 ctdb_counter_incr () {
561     _ctdb_counter_common
562
563     # unary counting!
564     echo -n 1 >> "$_counter_file"
565 }
566 ctdb_check_counter_limit () {
567     _ctdb_counter_common
568
569     _limit="${1:-${service_fail_limit}}"
570     _quiet="$2"
571
572     # unary counting!
573     _size=$(stat -c "%s" "$_counter_file" 2>/dev/null || echo 0)
574     if [ $_size -ge $_limit ] ; then
575         echo "ERROR: more than $_limit consecutive failures for $service_name, marking cluster unhealthy"
576         exit 1
577     elif [ $_size -gt 0 -a -z "$_quiet" ] ; then
578         echo "WARNING: less than $_limit consecutive failures ($_size) for $service_name, not unhealthy yet"
579     fi
580 }
581 ctdb_check_counter_equal () {
582     _ctdb_counter_common
583
584     _limit=$1
585
586     # unary counting!
587     _size=$(stat -c "%s" "$_counter_file" 2>/dev/null || echo 0)
588     if [ $_size -eq $_limit ] ; then
589         return 1
590     fi
591     return 0
592 }
593
594 ########################################################
595
596 ctdb_spool_dir="/var/spool/ctdb"
597 ctdb_status_dir="$ctdb_spool_dir/status"
598 ctdb_fail_dir="$ctdb_spool_dir/failcount"
599 ctdb_active_dir="$ctdb_spool_dir/active"
600
601 log_status_cat ()
602 {
603     echo "node is \"$1\", \"${script_name}\" reports problem: $(cat $2)"
604 }
605
606 ctdb_checkstatus ()
607 {
608     if [ -r "$ctdb_status_dir/$script_name/unhealthy" ] ; then
609         log_status_cat "unhealthy" "$ctdb_status_dir/$script_name/unhealthy"
610         return 1
611     elif [ -r "$ctdb_status_dir/$script_name/banned" ] ; then
612         log_status_cat "banned" "$ctdb_status_dir/$script_name/banned"
613         return 2
614     else
615         return 0
616     fi
617 }
618
619 ctdb_setstatus ()
620 {
621     d="$ctdb_status_dir/$script_name"
622     case "$1" in
623         unhealthy|banned)
624             mkdir -p "$d"
625             cat "$2" >"$d/$1"
626             ;;
627         *)
628             for i in "banned" "unhealthy" ; do
629                 rm -f "$d/$i"
630             done
631             ;;
632     esac
633 }
634
635 ctdb_service_needs_reconfigure ()
636 {
637     [ -e "$ctdb_status_dir/$service_name/reconfigure" ]
638 }
639
640 ctdb_service_set_reconfigure ()
641 {
642     d="$ctdb_status_dir/$service_name"
643     mkdir -p "$d"
644     >"$d/reconfigure"
645 }
646
647 ctdb_service_unset_reconfigure ()
648 {
649     rm -f "$ctdb_status_dir/$service_name/reconfigure"
650 }
651
652 ctdb_service_reconfigure ()
653 {
654     if [ -n "$service_reconfigure" ] ; then
655         eval $service_reconfigure
656     else
657         service "$service_name" restart
658     fi
659     ctdb_service_unset_reconfigure
660     ctdb_counter_init
661 }
662
663 ctdb_compat_managed_service ()
664 {
665     if [ "$1" = "yes" ] ; then
666         t="$t $2 "
667     fi
668 }
669
670 is_ctdb_managed_service ()
671 {
672     _service_name="${1:-${service_name}}"
673
674     t=" $CTDB_MANAGED_SERVICES "
675
676     ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD"   "vsftpd"
677     ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA"    "samba"
678     ctdb_compat_managed_service "$CTDB_MANAGES_SCP"      "scp"
679     ctdb_compat_managed_service "$CTDB_MANAGES_WINBIND"  "winbind"
680     ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD"    "httpd"
681     ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI"    "iscsi"
682     ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD"    "clamd"
683     ctdb_compat_managed_service "$CTDB_MANAGES_NFS"      "nfs"
684     ctdb_compat_managed_service "$CTDB_MANAGES_NFS"      "nfs-ganesha-gpfs"
685
686     # Returns 0 if "<space>$_service_name<space>" appears in $t
687     [ "${t#* ${_service_name} }" != "${t}" ]
688 }
689
690 ctdb_start_stop_service ()
691 {
692     _service_name="${1:-${service_name}}"
693
694     _active="$ctdb_active_dir/$_service_name"
695     if is_ctdb_managed_service "$_service_name"; then
696         if ! [ -e "$_active" ] ; then
697             echo "Starting service $_service_name"
698             ctdb_service_start || exit $?
699             mkdir -p "$ctdb_active_dir"
700             touch "$_active"
701             exit 0
702         fi
703     else
704         if [ -e "$_active" ] ; then
705             echo "Stopping service $_service_name"
706             ctdb_service_stop || exit $?
707             rm -f "$_active"
708             exit 0
709         fi
710     fi
711 }
712
713 ctdb_service_start ()
714 {
715     if [ -n "$service_start" ] ; then
716         eval $service_start || return $?
717     else
718         service "$service_name" start || return $?
719     fi
720     ctdb_counter_init
721 }
722
723 ctdb_service_stop ()
724 {
725     if [ -n "$service_stop" ] ; then
726         eval $service_stop
727     else
728         service "$service_name" stop
729     fi
730 }
731
732 ctdb_standard_event_handler ()
733 {
734     case "$1" in
735         status)
736             ctdb_checkstatus
737             exit
738             ;;
739         setstatus)
740             shift
741             ctdb_setstatus "$@"
742             exit
743             ;;
744     esac
745 }
746
747 ipv4_host_addr_to_net_addr()
748 {
749         local HOST=$1
750         local MASKBITS=$2
751
752         local HOST0=$(echo $HOST | awk -F . '{print $4}')
753         local HOST1=$(echo $HOST | awk -F . '{print $3}')
754         local HOST2=$(echo $HOST | awk -F . '{print $2}')
755         local HOST3=$(echo $HOST | awk -F . '{print $1}')
756
757         local HOST_NUM=$(( $HOST0 + $HOST1 * 256 + $HOST2 * (256 ** 2) + $HOST3 * (256 ** 3) ))
758
759         local MASK_NUM=$(( ( (2**32 - 1) * (2**(32 - $MASKBITS)) ) & (2**32 - 1) ))
760
761         local NET_NUM=$(( $HOST_NUM & $MASK_NUM))
762
763         local NET0=$(( $NET_NUM & 255 ))
764         local NET1=$(( ($NET_NUM & (255 * 256)) / 256 ))
765         local NET2=$(( ($NET_NUM & (255 * 256**2)) / 256**2 ))
766         local NET3=$(( ($NET_NUM & (255 * 256**3)) / 256**3 ))
767
768         echo "$NET3.$NET2.$NET1.$NET0"
769 }
770
771 ipv4_maskbits_to_net_mask()
772 {
773         local MASKBITS=$1
774
775         local MASK_NUM=$(( ( (2**32 - 1) * (2**(32 - $MASKBITS)) ) & (2**32 - 1) ))
776
777         local MASK0=$(( $MASK_NUM & 255 ))
778         local MASK1=$(( ($MASK_NUM & (255 * 256)) / 256 ))
779         local MASK2=$(( ($MASK_NUM & (255 * 256**2)) / 256**2 ))
780         local MASK3=$(( ($MASK_NUM & (255 * 256**3)) / 256**3 ))
781
782         echo "$MASK3.$MASK2.$MASK1.$MASK0"
783 }
784
785 ipv4_is_valid_addr()
786 {
787         local ADDR=$1
788         local fail=0
789
790         local N=`echo $ADDR | sed -e 's/[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*//'`
791         test -n "$N" && fail=1
792
793         local ADDR0=$(echo $ADDR | awk -F . '{print $4}')
794         local ADDR1=$(echo $ADDR | awk -F . '{print $3}')
795         local ADDR2=$(echo $ADDR | awk -F . '{print $2}')
796         local ADDR3=$(echo $ADDR | awk -F . '{print $1}')
797
798         test "$ADDR0" -gt 255 && fail=1
799         test "$ADDR1" -gt 255 && fail=1
800         test "$ADDR2" -gt 255 && fail=1
801         test "$ADDR3" -gt 255 && fail=1
802
803         test x"$fail" != x"0" && {
804                 #echo "IPv4: '$ADDR' is not a valid address"
805                 return 1;
806         }
807
808         return 0;
809 }
810
811 # iptables doesn't like being re-entered, so flock-wrap it.
812 iptables()
813 {
814         flock -w 30 /var/ctdb/iptables-ctdb.flock /sbin/iptables "$@"
815 }
816
817 ########################################################
818 # tickle handling
819 ########################################################
820
821 # Temporary directory for tickles.
822 tickledir="$CTDB_VARDIR/state/tickles"
823 mkdir -p "$tickledir"
824
825 update_tickles ()
826 {
827         _port="$1"
828
829         mkdir -p "$tickledir" # Just in case
830
831         # Who am I?
832         _pnn=$(ctdb pnn) ; _pnn=${_pnn#PNN:}
833
834         # What public IPs do I hold?
835         _ips=$(ctdb -Y ip | awk -F: -v pnn=$_pnn '$3 == pnn {print $2}')
836
837         # IPs as a regexp choice
838         _ipschoice="($(echo $_ips | sed -e 's/ /|/g' -e 's/\./\\\\./g'))"
839
840         # Record connections to our public IPs in a temporary file
841         _my_connections="${tickledir}/${_port}.connections"
842         rm -f "$_my_connections"
843         netstat -tn |
844         awk -v destpat="^${_ipschoice}:${_port}\$" \
845           '$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ destpat {print $5, $4}' |
846         sort >"$_my_connections"
847
848         # Record our current tickles in a temporary file
849         _my_tickles="${tickledir}/${_port}.tickles"
850         rm -f "$_my_tickles"
851         for _i in $_ips ; do
852                 ctdb -Y gettickles $_i $_port | 
853                 awk -F: 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
854         done |
855         sort >"$_my_tickles"
856
857         # Add tickles for connections that we haven't already got tickles for
858         comm -23 "$_my_connections" "$_my_tickles" |
859         while read _src _dst ; do
860                 ctdb addtickle $_src $_dst
861         done
862
863         # Remove tickles for connections that are no longer there
864         comm -13 "$_my_connections" "$_my_tickles" |
865         while read _src _dst ; do
866                 ctdb deltickle $_src $_dst
867         done
868
869         rm -f "$_my_connections" "$_my_tickles" 
870 }
871
872 ########################################################
873 # load a site local config file
874 ########################################################
875
876 [ -x $CTDB_BASE/rc.local ] && {
877         . $CTDB_BASE/rc.local
878 }
879
880 [ -d $CTDB_BASE/rc.local.d ] && {
881         for i in $CTDB_BASE/rc.local.d/* ; do
882                 [ -x "$i" ] && . "$i"
883         done
884 }
885
886 script_name="${0##*/}"       # basename
887 service_name="$script_name"  # default is just the script name
888 service_fail_limit=1