2d5d2792c2f234e9f96da4287b7cfd3bcb9083ac
[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
195 # This flag file is created when a service is initially started.  It
196 # is deleted the first time TCP port checks for that service succeed.
197 # Until then ctdb_check_tcp_ports() prints a more subtle "error"
198 # message if a port check fails.
199 _ctdb_check_tcp_common ()
200 {
201     _ctdb_service_started_file="$ctdb_fail_dir/$service_name.started"
202 }
203
204 ctdb_check_tcp_init ()
205 {
206     _ctdb_check_tcp_common
207     mkdir -p "${_ctdb_service_started_file%/*}" # dirname
208     touch "$_ctdb_service_started_file"
209 }
210
211 ctdb_check_tcp_ports()
212 {
213     if [ -z "$1" ] ; then
214         echo "INTERNAL ERROR: ctdb_check_tcp_ports - no ports specified"
215         exit 1
216     fi
217
218     # Set default value for CTDB_TCP_PORT_CHECKS if unset.
219     # If any of these defaults are unsupported then this variable can
220     # be overridden in /etc/sysconfig/ctdb or via a file in
221     # /etc/ctdb/rc.local.d/.
222     : ${CTDB_TCP_PORT_CHECKERS:=ctdb nmap netstat}
223
224     for _c in $CTDB_TCP_PORT_CHECKERS ; do
225         ctdb_check_tcp_ports_$_c "$@"
226         case "$?" in
227             0)
228                 rm -f "$_ctdb_service_started_file"
229                 return 0
230                 ;;
231             1)
232                 _ctdb_check_tcp_common
233                 if [ ! -f "$_ctdb_service_started_file" ] ; then
234                     echo "ERROR: $service_name tcp port $_p is not responding"
235                     cat <<EOF
236 $ctdb_check_tcp_ports_debug
237 EOF
238                 else
239                     echo "INFO: $service_name tcp port $_p is not responding"
240                 fi
241
242                 return 1
243                 ;;
244             127)
245                 : # Not implemented
246                 ;;
247             *)
248                 
249         esac
250     done
251
252     echo "INTERNAL ERROR: ctdb_check_ports - no working checkers in CTDB_TCP_PORT_CHECKERS=\"$CTDB_TCP_PORT_CHECKERS\""
253
254     return 127
255 }
256
257 ctdb_check_tcp_ports_netstat ()
258 {
259     _cmd='netstat -l -t -n'
260     _ns=$($_cmd 2>&1)
261     if [ $? -eq 127 ] ; then
262         # netstat probably not installed - unlikely?
263         ctdb_check_tcp_ports_debug="$_ns"
264         return 127
265     fi
266
267     for _p ; do  # process each function argument (port)
268         for _a in '0\.0\.0\.0' '::' ; do
269             _pat="[[:space:]]${_a}:${_p}[[:space:]]+[^[:space:]]+[[:space:]]+LISTEN"
270             if echo "$_ns" | grep -E -q "$_pat" ; then
271                 # We matched the port, so process next port
272                 continue 2
273             fi
274         done
275
276         # We didn't match the port, so flag an error.
277         ctdb_check_tcp_ports_debug="$_cmd shows this output:
278 $_ns"
279         return 1
280     done
281
282     return 0
283 }
284
285 ctdb_check_tcp_ports_nmap ()
286 {
287     # nmap wants a comma-separated list of ports
288     _ports=""
289     for _p ; do
290         _ports="${_ports}${_ports:+,}${_p}"
291     done
292
293     _cmd="nmap -n -oG - -PS 127.0.0.1 -p $_ports"
294
295     _nmap_out=$($_cmd 2>&1)
296     if [ $? -eq 127 ] ; then
297         # nmap probably not installed
298         ctdb_check_tcp_ports_debug="$_nmap_out"
299         return 127
300     fi
301
302     # get the port-related output
303     _port_info=$(echo "$_nmap_out" | sed -n -r -e 's@^.*Ports:[[:space:]]@@p')
304
305     for _p ; do
306         # looking for something like this:
307         #  445/open/tcp//microsoft-ds///
308         # possibly followed by a comma
309         _t="$_p/open/tcp//"
310         case "$_port_info" in
311             # The info we're after must be either at the beginning of
312             # the string or it must follow a space.
313             $_t*|*\ $_t*) : ;;
314             *)
315                 # Nope, flag an error...
316                 ctdb_check_tcp_ports_debug="$_cmd shows this output:
317 $_nmap_out"
318                 return 1
319         esac
320     done
321
322     return 0
323 }
324
325 # Use the new "ctdb checktcpport" command to check the port.
326 # This is very cheap.
327 ctdb_check_tcp_ports_ctdb ()
328 {
329     for _p ; do  # process each function argument (port)
330         _cmd="ctdb checktcpport $_p"
331         _out=$($_cmd 2>&1)
332         _ret=$?
333         case "$_ret" in
334             0)
335                 ctdb_check_tcp_ports_debug="\"$_cmd\" was able to bind to port"
336                 return 1
337                 ;;
338             98)
339                 # Couldn't bind, something already listening, next port...
340                 continue
341                 ;;
342             *)
343                 ctdb_check_tcp_ports_debug="$_cmd (exited with $_ret) with output:
344 $_out"
345                 # assume not implemented
346                 return 127
347         esac
348     done
349
350     return 0
351 }
352
353 ######################################################
354 # check a unix socket
355 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
356 ######################################################
357 ctdb_check_unix_socket() {
358     socket_path="$1"
359     [ -z "$socket_path" ] && return
360
361     if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
362         echo "ERROR: $service_name socket $socket_path not found"
363         return 1
364     fi
365 }
366
367 ######################################################
368 # check a command returns zero status
369 # usage: ctdb_check_command SERVICE_NAME <command>
370 ######################################################
371 ctdb_check_command() {
372   service_name="$1"
373   wait_cmd="$2"
374   [ -z "$wait_cmd" ] && return;
375   $wait_cmd > /dev/null 2>&1 || {
376       echo "ERROR: $service_name - $wait_cmd returned error"
377       exit 1
378   }
379 }
380
381 ################################################
382 # kill off any TCP connections with the given IP
383 ################################################
384 kill_tcp_connections() {
385     _IP="$1"    
386     _failed=0
387
388     _killcount=0
389     connfile="$CTDB_VARDIR/state/connections.$_IP"
390     netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
391     netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
392
393     while read dest src; do
394         srcip=`echo $src | sed -e "s/:[^:]*$//"`
395         srcport=`echo $src | sed -e "s/^.*://"`
396         destip=`echo $dest | sed -e "s/:[^:]*$//"`
397         destport=`echo $dest | sed -e "s/^.*://"`
398         echo "Killing TCP connection $srcip:$srcport $destip:$destport"
399         ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
400         case $destport in
401           # we only do one-way killtcp for CIFS
402           139|445) : ;;
403           # for all others we do 2-way
404           *) 
405                 ctdb killtcp $destip:$destport $srcip:$srcport >/dev/null 2>&1 || _failed=1
406                 ;;
407         esac
408         _killcount=`expr $_killcount + 1`
409      done < $connfile
410     /bin/rm -f $connfile
411
412     [ $_failed = 0 ] || {
413         echo "Failed to send killtcp control"
414         return;
415     }
416     [ $_killcount -gt 0 ] || {
417         return;
418     }
419     _count=0
420     while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
421         sleep 1
422         _count=`expr $_count + 1`
423         [ $_count -gt 3 ] && {
424             echo "Timed out killing tcp connections for IP $_IP"
425             return;
426         }
427     done
428     echo "killed $_killcount TCP connections to released IP $_IP"
429 }
430
431 ##################################################################
432 # kill off the local end for any TCP connections with the given IP
433 ##################################################################
434 kill_tcp_connections_local_only() {
435     _IP="$1"    
436     _failed=0
437
438     _killcount=0
439     connfile="$CTDB_VARDIR/state/connections.$_IP"
440     netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
441     netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
442
443     while read dest src; do
444         srcip=`echo $src | sed -e "s/:[^:]*$//"`
445         srcport=`echo $src | sed -e "s/^.*://"`
446         destip=`echo $dest | sed -e "s/:[^:]*$//"`
447         destport=`echo $dest | sed -e "s/^.*://"`
448         echo "Killing TCP connection $srcip:$srcport $destip:$destport"
449         ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
450         _killcount=`expr $_killcount + 1`
451      done < $connfile
452     /bin/rm -f $connfile
453
454     [ $_failed = 0 ] || {
455         echo "Failed to send killtcp control"
456         return;
457     }
458     [ $_killcount -gt 0 ] || {
459         return;
460     }
461     _count=0
462     while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
463         sleep 1
464         _count=`expr $_count + 1`
465         [ $_count -gt 3 ] && {
466             echo "Timed out killing tcp connections for IP $_IP"
467             return;
468         }
469     done
470     echo "killed $_killcount TCP connections to released IP $_IP"
471 }
472
473 ##################################################################
474 # tickle any TCP connections with the given IP
475 ##################################################################
476 tickle_tcp_connections() {
477     _IP="$1"
478     _failed=0
479
480     _killcount=0
481     connfile="$CTDB_VARDIR/state/connections.$_IP"
482     netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
483     netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
484
485     while read dest src; do
486         srcip=`echo $src | sed -e "s/:[^:]*$//"`
487         srcport=`echo $src | sed -e "s/^.*://"`
488         destip=`echo $dest | sed -e "s/:[^:]*$//"`
489         destport=`echo $dest | sed -e "s/^.*://"`
490         echo "Tickle TCP connection $srcip:$srcport $destip:$destport"
491         ctdb tickle $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
492         echo "Tickle TCP connection $destip:$destport $srcip:$srcport"
493         ctdb tickle $destip:$destport $srcip:$srcport >/dev/null 2>&1 || _failed=1
494      done < $connfile
495     /bin/rm -f $connfile
496
497     [ $_failed = 0 ] || {
498         echo "Failed to send tickle control"
499         return;
500     }
501 }
502
503 ########################################################
504 # start/stop the nfs service on different platforms
505 ########################################################
506 startstop_nfs() {
507         PLATFORM="unknown"
508         [ -x /etc/init.d/nfsserver ] && {
509                 PLATFORM="sles"
510         }
511         [ -x /etc/init.d/nfslock ] && {
512                 PLATFORM="rhel"
513         }
514
515         case $PLATFORM in
516         sles)
517                 case $1 in
518                 start)
519                         service nfsserver start
520                         ;;
521                 stop)
522                         service nfsserver stop > /dev/null 2>&1
523                         ;;
524                 restart)
525                         echo 0 >/proc/fs/nfsd/threads
526                         service nfsserver stop > /dev/null 2>&1
527                         pkill -9 nfsd
528                         service nfsserver start
529                         ;;
530                 esac
531                 ;;
532         rhel)
533                 case $1 in
534                 start)
535                         service nfslock start
536                         service nfs start
537                         ;;
538                 stop)
539                         service nfs stop > /dev/null 2>&1
540                         service nfslock stop > /dev/null 2>&1
541                         ;;
542                 restart)
543                         echo 0 >/proc/fs/nfsd/threads
544                         service nfs stop > /dev/null 2>&1
545                         service nfslock stop > /dev/null 2>&1
546                         pkill -9 nfsd
547                         service nfslock start
548                         service nfs start
549                         ;;
550                 esac
551                 ;;
552         *)
553                 echo "Unknown platform. NFS is not supported with ctdb"
554                 exit 1
555                 ;;
556         esac
557 }
558
559 ########################################################
560 # start/stop the nfs lockmanager service on different platforms
561 ########################################################
562 startstop_nfslock() {
563         PLATFORM="unknown"
564         [ -x /etc/init.d/nfsserver ] && {
565                 PLATFORM="sles"
566         }
567         [ -x /etc/init.d/nfslock ] && {
568                 PLATFORM="rhel"
569         }
570
571         case $PLATFORM in
572         sles)
573                 # for sles there is no service for lockmanager
574                 # so we instead just shutdown/restart nfs
575                 case $1 in
576                 start)
577                         service nfsserver start
578                         ;;
579                 stop)
580                         service nfsserver stop > /dev/null 2>&1
581                         ;;
582                 restart)
583                         service nfsserver stop
584                         service nfsserver start
585                         ;;
586                 esac
587                 ;;
588         rhel)
589                 case $1 in
590                 start)
591                         service nfslock start
592                         ;;
593                 stop)
594                         service nfslock stop > /dev/null 2>&1
595                         ;;
596                 restart)
597                         service nfslock stop
598                         service nfslock start
599                         ;;
600                 esac
601                 ;;
602         *)
603                 echo "Unknown platform. NFS locking is not supported with ctdb"
604                 exit 1
605                 ;;
606         esac
607 }
608
609 # better use delete_ip_from_iface() together with add_ip_to_iface
610 # remove_ip should be removed in future
611 remove_ip() {
612         local _ip_maskbits=$1
613         local _iface=$2
614         local _ip=`echo "$_ip_maskbits" | cut -d '/' -f1`
615         local _maskbits=`echo "$_ip_maskbits" | cut -d '/' -f2`
616
617         delete_ip_from_iface "$_iface" "$_ip" "$_maskbits"
618         return $?
619 }
620
621 add_ip_to_iface()
622 {
623         local _iface=$1
624         local _ip=$2
625         local _maskbits=$3
626         local _state_dir="$CTDB_VARDIR/state/interface_modify"
627         local _lockfile="$_state_dir/$_iface.flock"
628         local _readd_base="$_state_dir/$_iface.readd.d"
629
630         mkdir -p $_state_dir || {
631                 ret=$?
632                 echo "Failed to mkdir -p $_state_dir - $ret"
633                 return $ret
634         }
635
636         test -f $_lockfile || {
637                 touch $_lockfile
638         }
639
640         flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh add "$_iface" "$_ip" "$_maskbits" "$_readd_base"
641         return $?
642 }
643
644 delete_ip_from_iface()
645 {
646         local _iface=$1
647         local _ip=$2
648         local _maskbits=$3
649         local _state_dir="$CTDB_VARDIR/state/interface_modify"
650         local _lockfile="$_state_dir/$_iface.flock"
651         local _readd_base="$_state_dir/$_iface.readd.d"
652
653         mkdir -p $_state_dir || {
654                 ret=$?
655                 echo "Failed to mkdir -p $_state_dir - $ret"
656                 return $ret
657         }
658
659         test -f $_lockfile || {
660                 touch $_lockfile
661         }
662
663         flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh delete "$_iface" "$_ip" "$_maskbits" "$_readd_base"
664         return $?
665 }
666
667 setup_iface_ip_readd_script()
668 {
669         local _iface=$1
670         local _ip=$2
671         local _maskbits=$3
672         local _readd_script=$4
673         local _state_dir="$CTDB_VARDIR/state/interface_modify"
674         local _lockfile="$_state_dir/$_iface.flock"
675         local _readd_base="$_state_dir/$_iface.readd.d"
676
677         mkdir -p $_state_dir || {
678                 ret=$?
679                 echo "Failed to mkdir -p $_state_dir - $ret"
680                 return $ret
681         }
682
683         test -f $_lockfile || {
684                 touch $_lockfile
685         }
686
687         flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh readd_script "$_iface" "$_ip" "$_maskbits" "$_readd_base" "$_readd_script"
688         return $?
689 }
690
691 ########################################################
692 # some simple logic for counting events - per eventscript
693 # usage: ctdb_counter_init
694 #        ctdb_counter_incr
695 #        ctdb_check_counter_limit <limit>
696 # ctdb_check_counter_limit succeeds when count >= <limit>
697 ########################################################
698 _ctdb_counter_common () {
699     _counter_file="$ctdb_fail_dir/$service_name"
700     mkdir -p "${_counter_file%/*}" # dirname
701 }
702 ctdb_counter_init () {
703     _ctdb_counter_common
704
705     >"$_counter_file"
706 }
707 ctdb_counter_incr () {
708     _ctdb_counter_common
709
710     # unary counting!
711     echo -n 1 >> "$_counter_file"
712 }
713 ctdb_check_counter_limit () {
714     _ctdb_counter_common
715
716     _limit="${1:-${service_fail_limit}}"
717     _quiet="$2"
718
719     # unary counting!
720     _size=$(stat -c "%s" "$_counter_file" 2>/dev/null || echo 0)
721     if [ $_size -ge $_limit ] ; then
722         echo "ERROR: more than $_limit consecutive failures for $service_name, marking cluster unhealthy"
723         exit 1
724     elif [ $_size -gt 0 -a -z "$_quiet" ] ; then
725         echo "WARNING: less than $_limit consecutive failures ($_size) for $service_name, not unhealthy yet"
726     fi
727 }
728 ctdb_check_counter_equal () {
729     _ctdb_counter_common
730
731     _limit=$1
732
733     # unary counting!
734     _size=$(stat -c "%s" "$_counter_file" 2>/dev/null || echo 0)
735     if [ $_size -eq $_limit ] ; then
736         return 1
737     fi
738     return 0
739 }
740
741 ########################################################
742
743 ctdb_spool_dir="/var/spool/ctdb"
744 ctdb_status_dir="$ctdb_spool_dir/status"
745 ctdb_fail_dir="$ctdb_spool_dir/failcount"
746 ctdb_active_dir="$ctdb_spool_dir/active"
747
748 log_status_cat ()
749 {
750     echo "node is \"$1\", \"${script_name}\" reports problem: $(cat $2)"
751 }
752
753 ctdb_checkstatus ()
754 {
755     if [ -r "$ctdb_status_dir/$script_name/unhealthy" ] ; then
756         log_status_cat "unhealthy" "$ctdb_status_dir/$script_name/unhealthy"
757         return 1
758     elif [ -r "$ctdb_status_dir/$script_name/banned" ] ; then
759         log_status_cat "banned" "$ctdb_status_dir/$script_name/banned"
760         return 2
761     else
762         return 0
763     fi
764 }
765
766 ctdb_setstatus ()
767 {
768     d="$ctdb_status_dir/$script_name"
769     case "$1" in
770         unhealthy|banned)
771             mkdir -p "$d"
772             cat "$2" >"$d/$1"
773             ;;
774         *)
775             for i in "banned" "unhealthy" ; do
776                 rm -f "$d/$i"
777             done
778             ;;
779     esac
780 }
781
782 ctdb_service_needs_reconfigure ()
783 {
784     [ -e "$ctdb_status_dir/$service_name/reconfigure" ]
785 }
786
787 ctdb_service_set_reconfigure ()
788 {
789     d="$ctdb_status_dir/$service_name"
790     mkdir -p "$d"
791     >"$d/reconfigure"
792 }
793
794 ctdb_service_unset_reconfigure ()
795 {
796     rm -f "$ctdb_status_dir/$service_name/reconfigure"
797 }
798
799 ctdb_service_reconfigure ()
800 {
801     echo "Reconfiguring service \"$service_name\"..."
802     if [ -n "$service_reconfigure" ] ; then
803         eval $service_reconfigure
804     else
805         service "$service_name" restart
806     fi
807     ctdb_service_unset_reconfigure
808     ctdb_counter_init
809 }
810
811 ctdb_compat_managed_service ()
812 {
813     if [ "$1" = "yes" ] ; then
814         t="$t $2 "
815     fi
816 }
817
818 is_ctdb_managed_service ()
819 {
820     _service_name="${1:-${service_name}}"
821
822     t=" $CTDB_MANAGED_SERVICES "
823
824     ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD"   "vsftpd"
825     ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA"    "samba"
826     ctdb_compat_managed_service "$CTDB_MANAGES_SCP"      "scp"
827     ctdb_compat_managed_service "$CTDB_MANAGES_WINBIND"  "winbind"
828     ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD"    "httpd"
829     ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI"    "iscsi"
830     ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD"    "clamd"
831     ctdb_compat_managed_service "$CTDB_MANAGES_NFS"      "nfs"
832     ctdb_compat_managed_service "$CTDB_MANAGES_NFS"      "nfs-ganesha-gpfs"
833
834     # Returns 0 if "<space>$_service_name<space>" appears in $t
835     [ "${t#* ${_service_name} }" != "${t}" ]
836 }
837
838 ctdb_start_stop_service ()
839 {
840     # Do nothing unless configured to...
841     [ "$CTDB_SERVICE_AUTOSTARTSTOP" = "yes" ] || return 0
842
843     _service_name="${1:-${service_name}}"
844
845     [ "$event_name" = "monitor" ] || return 0
846
847     _active="$ctdb_active_dir/$_service_name"
848     if is_ctdb_managed_service "$_service_name"; then
849         if ! [ -e "$_active" ] ; then
850             echo "Starting service $_service_name"
851             ctdb_service_start || exit $?
852             mkdir -p "$ctdb_active_dir"
853             touch "$_active"
854             exit 0
855         fi
856     else
857         if [ -e "$_active" ] ; then
858             echo "Stopping service $_service_name"
859             ctdb_service_stop || exit $?
860             rm -f "$_active"
861             exit 0
862         fi
863     fi
864 }
865
866 ctdb_service_start ()
867 {
868     if [ -n "$service_start" ] ; then
869         eval $service_start || return $?
870     else
871         service "$service_name" start || return $?
872     fi
873     ctdb_counter_init
874     ctdb_check_tcp_init
875 }
876
877 ctdb_service_stop ()
878 {
879     if [ -n "$service_stop" ] ; then
880         eval $service_stop
881     else
882         service "$service_name" stop
883     fi
884 }
885
886 ctdb_standard_event_handler ()
887 {
888     case "$1" in
889         status)
890             ctdb_checkstatus
891             exit
892             ;;
893         setstatus)
894             shift
895             ctdb_setstatus "$@"
896             exit
897             ;;
898     esac
899 }
900
901 ipv4_host_addr_to_net_addr()
902 {
903         local HOST=$1
904         local MASKBITS=$2
905
906         local HOST0=$(echo $HOST | awk -F . '{print $4}')
907         local HOST1=$(echo $HOST | awk -F . '{print $3}')
908         local HOST2=$(echo $HOST | awk -F . '{print $2}')
909         local HOST3=$(echo $HOST | awk -F . '{print $1}')
910
911         local HOST_NUM=$(( $HOST0 + $HOST1 * 256 + $HOST2 * (256 ** 2) + $HOST3 * (256 ** 3) ))
912
913         local MASK_NUM=$(( ( (2**32 - 1) * (2**(32 - $MASKBITS)) ) & (2**32 - 1) ))
914
915         local NET_NUM=$(( $HOST_NUM & $MASK_NUM))
916
917         local NET0=$(( $NET_NUM & 255 ))
918         local NET1=$(( ($NET_NUM & (255 * 256)) / 256 ))
919         local NET2=$(( ($NET_NUM & (255 * 256**2)) / 256**2 ))
920         local NET3=$(( ($NET_NUM & (255 * 256**3)) / 256**3 ))
921
922         echo "$NET3.$NET2.$NET1.$NET0"
923 }
924
925 ipv4_maskbits_to_net_mask()
926 {
927         local MASKBITS=$1
928
929         local MASK_NUM=$(( ( (2**32 - 1) * (2**(32 - $MASKBITS)) ) & (2**32 - 1) ))
930
931         local MASK0=$(( $MASK_NUM & 255 ))
932         local MASK1=$(( ($MASK_NUM & (255 * 256)) / 256 ))
933         local MASK2=$(( ($MASK_NUM & (255 * 256**2)) / 256**2 ))
934         local MASK3=$(( ($MASK_NUM & (255 * 256**3)) / 256**3 ))
935
936         echo "$MASK3.$MASK2.$MASK1.$MASK0"
937 }
938
939 ipv4_is_valid_addr()
940 {
941         local ADDR=$1
942         local fail=0
943
944         local N=`echo $ADDR | sed -e 's/[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*//'`
945         test -n "$N" && fail=1
946
947         local ADDR0=$(echo $ADDR | awk -F . '{print $4}')
948         local ADDR1=$(echo $ADDR | awk -F . '{print $3}')
949         local ADDR2=$(echo $ADDR | awk -F . '{print $2}')
950         local ADDR3=$(echo $ADDR | awk -F . '{print $1}')
951
952         test "$ADDR0" -gt 255 && fail=1
953         test "$ADDR1" -gt 255 && fail=1
954         test "$ADDR2" -gt 255 && fail=1
955         test "$ADDR3" -gt 255 && fail=1
956
957         test x"$fail" != x"0" && {
958                 #echo "IPv4: '$ADDR' is not a valid address"
959                 return 1;
960         }
961
962         return 0;
963 }
964
965 # iptables doesn't like being re-entered, so flock-wrap it.
966 iptables()
967 {
968         flock -w 30 /var/ctdb/iptables-ctdb.flock /sbin/iptables "$@"
969 }
970
971 ########################################################
972 # tickle handling
973 ########################################################
974
975 # Temporary directory for tickles.
976 tickledir="$CTDB_VARDIR/state/tickles"
977 mkdir -p "$tickledir"
978
979 update_tickles ()
980 {
981         _port="$1"
982
983         mkdir -p "$tickledir" # Just in case
984
985         # Who am I?
986         _pnn=$(ctdb pnn) ; _pnn=${_pnn#PNN:}
987
988         # What public IPs do I hold?
989         _ips=$(ctdb -Y ip | awk -F: -v pnn=$_pnn '$3 == pnn {print $2}')
990
991         # IPs as a regexp choice
992         _ipschoice="($(echo $_ips | sed -e 's/ /|/g' -e 's/\./\\\\./g'))"
993
994         # Record connections to our public IPs in a temporary file
995         _my_connections="${tickledir}/${_port}.connections"
996         rm -f "$_my_connections"
997         netstat -tn |
998         awk -v destpat="^${_ipschoice}:${_port}\$" \
999           '$1 == "tcp" && $6 == "ESTABLISHED" && $4 ~ destpat {print $5, $4}' |
1000         sort >"$_my_connections"
1001
1002         # Record our current tickles in a temporary file
1003         _my_tickles="${tickledir}/${_port}.tickles"
1004         rm -f "$_my_tickles"
1005         for _i in $_ips ; do
1006                 ctdb -Y gettickles $_i $_port | 
1007                 awk -F: 'NR > 1 { printf "%s:%s %s:%s\n", $2, $3, $4, $5 }'
1008         done |
1009         sort >"$_my_tickles"
1010
1011         # Add tickles for connections that we haven't already got tickles for
1012         comm -23 "$_my_connections" "$_my_tickles" |
1013         while read _src _dst ; do
1014                 ctdb addtickle $_src $_dst
1015         done
1016
1017         # Remove tickles for connections that are no longer there
1018         comm -13 "$_my_connections" "$_my_tickles" |
1019         while read _src _dst ; do
1020                 ctdb deltickle $_src $_dst
1021         done
1022
1023         rm -f "$_my_connections" "$_my_tickles" 
1024 }
1025
1026 ########################################################
1027 # load a site local config file
1028 ########################################################
1029
1030 [ -x $CTDB_BASE/rc.local ] && {
1031         . $CTDB_BASE/rc.local
1032 }
1033
1034 [ -d $CTDB_BASE/rc.local.d ] && {
1035         for i in $CTDB_BASE/rc.local.d/* ; do
1036                 [ -x "$i" ] && . "$i"
1037         done
1038 }
1039
1040 script_name="${0##*/}"       # basename
1041 service_name="$script_name"  # default is just the script name
1042 service_fail_limit=1
1043 event_name="$1"