1 # utility functions for ctdb event scripts
3 PATH=/bin:/usr/bin:/usr/sbin:/sbin:$PATH
5 #######################################
6 # pull in a system config file, if any
10 foo="${service_config:-${service_name}}"
11 if [ -n "$foo" ] ; then
14 elif [ "$1" != "ctdb" ] ; then
19 if [ -f /etc/sysconfig/$1 ]; then
21 elif [ -f /etc/default/$1 ]; then
23 elif [ -f $CTDB_BASE/sysconfig/$1 ]; then
24 . $CTDB_BASE/sysconfig/$1
28 ##############################################################
29 # determine on what type of system (init style) we are running
31 # only do detection if not already set:
32 test "x$CTDB_INIT_STYLE" != "x" && return
34 if [ -x /sbin/startproc ]; then
35 CTDB_INIT_STYLE="suse"
36 elif [ -x /sbin/start-stop-daemon ]; then
37 CTDB_INIT_STYLE="debian"
39 CTDB_INIT_STYLE="redhat"
43 ######################################################
44 # simulate /sbin/service on platforms that don't have it
49 # do nothing, when no service was specified
50 [ -z "$_service_name" ] && return
52 if [ -x /sbin/service ]; then
53 /sbin/service "$_service_name" "$_op"
54 elif [ -x /etc/init.d/$_service_name ]; then
55 /etc/init.d/$_service_name "$_op"
56 elif [ -x /etc/rc.d/init.d/$_service_name ]; then
57 /etc/rc.d/init.d/$_service_name "$_op"
61 ######################################################
62 # simulate /sbin/service (niced) on platforms that don't have it
67 # do nothing, when no service was specified
68 [ -z "$_service_name" ] && return
70 if [ -x /sbin/service ]; then
71 nice /sbin/service "$_service_name" "$_op"
72 elif [ -x /etc/init.d/$_service_name ]; then
73 nice /etc/init.d/$_service_name "$_op"
74 elif [ -x /etc/rc.d/init.d/$_service_name ]; then
75 nice /etc/rc.d/init.d/$_service_name "$_op"
79 ######################################################
80 # wait for a command to return a zero exit status
81 # usage: ctdb_wait_command SERVICE_NAME <command>
82 ######################################################
86 [ -z "$wait_cmd" ] && return;
88 echo "Waiting for service $service_name to start"
89 while [ $all_ok -eq 0 ]; do
90 $wait_cmd > /dev/null 2>&1 && all_ok=1
91 ctdb status > /dev/null 2>&1 || {
92 echo "ctdb daemon has died. Exiting wait for $service_name"
95 [ $all_ok -eq 1 ] || sleep 1
97 echo "Local service $service_name is up"
101 ######################################################
102 # wait for a set of tcp ports
103 # usage: ctdb_wait_tcp_ports SERVICE_NAME <ports...>
104 ######################################################
105 ctdb_wait_tcp_ports() {
109 [ -z "$wait_ports" ] && return;
111 echo "Waiting for tcp service $service_name to start"
112 while [ $all_ok -eq 0 ]; do
114 for p in $wait_ports; do
115 if [ -x /usr/bin/netcat ]; then
116 /usr/bin/netcat -z 127.0.0.1 $p > /dev/null || all_ok=0
117 elif [ -x /usr/bin/nc ]; then
118 /usr/bin/nc -z 127.0.0.1 $p > /dev/null || all_ok=0
119 elif [ -x /usr/bin/netstat ]; then
120 (netstat -a -n | egrep "0.0.0.0:$p[[:space:]]*LISTEN" > /dev/null) || all_ok=0
121 elif [ -x /bin/netstat ]; then
122 (netstat -a -n | egrep "0.0.0.0:$p[[:space:]]*LISTEN" > /dev/null) || all_ok=0
124 echo "No tool to check tcp ports availabe. can not check in ctdb_wait_tcp_ports"
128 [ $all_ok -eq 1 ] || sleep 1
129 ctdb status > /dev/null 2>&1 || {
130 echo "ctdb daemon has died. Exiting tcp wait $service_name"
134 echo "Local tcp services for $service_name are up"
138 ######################################################
139 # check that a rpc server is registered with portmap
140 # and responding to requests
141 # usage: ctdb_check_rpc SERVICE_NAME PROGNUM VERSION
142 ######################################################
147 rpcinfo -u localhost $prognum $version > /dev/null || {
148 echo "ERROR: $progname not responding to rpc requests"
153 ######################################################
154 # check a set of directories is available
155 # return 1 on a missing directory
156 # usage: ctdb_check_directories_probe SERVICE_NAME <directories...>
157 ######################################################
158 ctdb_check_directories_probe() {
159 while IFS="" read d ; do
165 [ -d "${d}/." ] || return 1
170 ######################################################
171 # check a set of directories is available
172 # usage: ctdb_check_directories SERVICE_NAME <directories...>
173 ######################################################
174 ctdb_check_directories() {
175 n="${1:-${service_name}}"
176 ctdb_check_directories_probe || {
177 echo "ERROR: $n directory \"$d\" not available"
182 ######################################################
183 # check a set of tcp ports
184 # usage: ctdb_check_tcp_ports <ports...>
185 ######################################################
186 ctdb_check_tcp_ports() {
189 if ! netstat -a -t -n | grep -q "0\.0\.0\.0:$p .*LISTEN" ; then
190 if ! netstat -a -t -n | grep -q ":::$p .*LISTEN" ; then
191 echo "ERROR: $service_name tcp port $p is not responding"
198 ######################################################
199 # check a unix socket
200 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
201 ######################################################
202 ctdb_check_unix_socket() {
204 [ -z "$socket_path" ] && return
206 if ! netstat --unix -a -n | grep -q "^unix.*LISTEN.*${socket_path}$"; then
207 echo "ERROR: $service_name socket $socket_path not found"
212 ######################################################
213 # check a command returns zero status
214 # usage: ctdb_check_command SERVICE_NAME <command>
215 ######################################################
216 ctdb_check_command() {
219 [ -z "$wait_cmd" ] && return;
220 $wait_cmd > /dev/null 2>&1 || {
221 echo "ERROR: $service_name - $wait_cmd returned error"
226 ################################################
227 # kill off any TCP connections with the given IP
228 ################################################
229 kill_tcp_connections() {
234 connfile="$CTDB_BASE/state/connections.$_IP"
235 netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
236 netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
238 while read dest src; do
239 srcip=`echo $src | sed -e "s/:[^:]*$//"`
240 srcport=`echo $src | sed -e "s/^.*://"`
241 destip=`echo $dest | sed -e "s/:[^:]*$//"`
242 destport=`echo $dest | sed -e "s/^.*://"`
243 echo "Killing TCP connection $srcip:$srcport $destip:$destport"
244 ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
246 # we only do one-way killtcp for CIFS
248 # for all others we do 2-way
250 ctdb killtcp $destip:$destport $srcip:$srcport >/dev/null 2>&1 || _failed=1
253 _killcount=`expr $_killcount + 1`
257 [ $_failed = 0 ] || {
258 echo "Failed to send killtcp control"
261 [ $_killcount -gt 0 ] || {
265 while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
267 _count=`expr $_count + 1`
268 [ $_count -gt 3 ] && {
269 echo "Timed out killing tcp connections for IP $_IP"
273 echo "killed $_killcount TCP connections to released IP $_IP"
276 ##################################################################
277 # kill off the local end for any TCP connections with the given IP
278 ##################################################################
279 kill_tcp_connections_local_only() {
284 connfile="$CTDB_BASE/state/connections.$_IP"
285 netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
286 netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
288 while read dest src; do
289 srcip=`echo $src | sed -e "s/:[^:]*$//"`
290 srcport=`echo $src | sed -e "s/^.*://"`
291 destip=`echo $dest | sed -e "s/:[^:]*$//"`
292 destport=`echo $dest | sed -e "s/^.*://"`
293 echo "Killing TCP connection $srcip:$srcport $destip:$destport"
294 ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
295 _killcount=`expr $_killcount + 1`
299 [ $_failed = 0 ] || {
300 echo "Failed to send killtcp control"
303 [ $_killcount -gt 0 ] || {
307 while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
309 _count=`expr $_count + 1`
310 [ $_count -gt 3 ] && {
311 echo "Timed out killing tcp connections for IP $_IP"
315 echo "killed $_killcount TCP connections to released IP $_IP"
318 ##################################################################
319 # tickle any TCP connections with the given IP
320 ##################################################################
321 tickle_tcp_connections() {
326 connfile="$CTDB_BASE/state/connections.$_IP"
327 netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
328 netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
330 while read dest src; do
331 srcip=`echo $src | sed -e "s/:[^:]*$//"`
332 srcport=`echo $src | sed -e "s/^.*://"`
333 destip=`echo $dest | sed -e "s/:[^:]*$//"`
334 destport=`echo $dest | sed -e "s/^.*://"`
335 echo "Tickle TCP connection $srcip:$srcport $destip:$destport"
336 ctdb tickle $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
337 echo "Tickle TCP connection $destip:$destport $srcip:$srcport"
338 ctdb tickle $destip:$destport $srcip:$srcport >/dev/null 2>&1 || _failed=1
342 [ $_failed = 0 ] || {
343 echo "Failed to send tickle control"
348 ########################################################
349 # start/stop the nfs service on different platforms
350 ########################################################
353 [ -x /etc/init.d/nfsserver ] && {
356 [ -x /etc/init.d/nfslock ] && {
364 service nfsserver start
367 service nfsserver stop > /dev/null 2>&1
370 service nfsserver restart
377 service nfslock start
381 service nfs stop > /dev/null 2>&1
382 service nfslock stop > /dev/null 2>&1
385 service nfslock restart
391 echo "Unknown platform. NFS is not supported with ctdb"
397 ########################################################
398 # start/stop the nfs lockmanager service on different platforms
399 ########################################################
400 startstop_nfslock() {
402 [ -x /etc/init.d/nfsserver ] && {
405 [ -x /etc/init.d/nfslock ] && {
411 # for sles there is no service for lockmanager
412 # so we instead just shutdown/restart nfs
415 service nfsserver start
418 service nfsserver stop > /dev/null 2>&1
425 service nfslock start
428 service nfslock stop > /dev/null 2>&1
433 echo "Unknown platform. NFS locking is not supported with ctdb"
439 # better use delete_ip_from_iface() together with add_ip_to_iface
440 # remove_ip should be removed in future
442 local _ip_maskbits=$1
444 local _ip=`echo "$_ip_maskbits" | cut -d '/' -f1`
445 local _maskbits=`echo "$_ip_maskbits" | cut -d '/' -f2`
447 delete_ip_from_iface "$_iface" "$_ip" "$_maskbits"
456 local _state_dir="$CTDB_BASE/state/interface_modify"
457 local _lockfile="$_state_dir/$_iface.flock"
458 local _readd_base="$_state_dir/$_iface.readd.d"
460 mkdir -p $_state_dir || {
462 echo "Failed to mkdir -p $_state_dir - $ret"
466 test -f $_lockfile || {
470 flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh add "$_iface" "$_ip" "$_maskbits" "$_readd_base"
474 delete_ip_from_iface()
479 local _state_dir="$CTDB_BASE/state/interface_modify"
480 local _lockfile="$_state_dir/$_iface.flock"
481 local _readd_base="$_state_dir/$_iface.readd.d"
483 mkdir -p $_state_dir || {
485 echo "Failed to mkdir -p $_state_dir - $ret"
489 test -f $_lockfile || {
493 flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh delete "$_iface" "$_ip" "$_maskbits" "$_readd_base"
497 setup_iface_ip_readd_script()
502 local _readd_script=$4
503 local _state_dir="$CTDB_BASE/state/interface_modify"
504 local _lockfile="$_state_dir/$_iface.flock"
505 local _readd_base="$_state_dir/$_iface.readd.d"
507 mkdir -p $_state_dir || {
509 echo "Failed to mkdir -p $_state_dir - $ret"
513 test -f $_lockfile || {
517 flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh readd_script "$_iface" "$_ip" "$_maskbits" "$_readd_base" "$_readd_script"
521 ########################################################
522 # some simple logic for counting events - per eventscript
523 # usage: ctdb_counter_init
525 # ctdb_check_counter_limit <limit>
526 # ctdb_check_counter_limit succeeds when count >= <limit>
527 ########################################################
528 _ctdb_counter_common () {
529 _counter_file="$ctdb_fail_dir/$service_name"
530 mkdir -p "${_counter_file%/*}" # dirname
532 ctdb_counter_init () {
537 ctdb_counter_incr () {
541 echo -n 1 >> "$_counter_file"
543 ctdb_check_counter_limit () {
546 _limit="${1:-${service_fail_limit}}"
550 _size=$(stat -c "%s" "$_counter_file" 2>/dev/null || echo 0)
551 if [ $_size -ge $_limit ] ; then
552 echo "ERROR: more than $_limit consecutive failures for $service_name, marking cluster unhealthy"
554 elif [ $_size -gt 0 -a -z "$_quiet" ] ; then
555 echo "WARNING: less than $_limit consecutive failures ($_size) for $service_name, not unhealthy yet"
558 ########################################################
560 ctdb_spool_dir="/var/spool/ctdb"
561 ctdb_status_dir="$ctdb_spool_dir/status"
562 ctdb_fail_dir="$ctdb_spool_dir/failcount"
563 ctdb_active_dir="$ctdb_spool_dir/active"
567 echo "node is \"$1\", \"${script_name}\" reports problem: $(cat $2)"
572 if [ -r "$ctdb_status_dir/$script_name/unhealthy" ] ; then
573 log_status_cat "unhealthy" "$ctdb_status_dir/$script_name/unhealthy"
575 elif [ -r "$ctdb_status_dir/$script_name/banned" ] ; then
576 log_status_cat "banned" "$ctdb_status_dir/$script_name/banned"
585 d="$ctdb_status_dir/$script_name"
592 for i in "banned" "unhealthy" ; do
599 ctdb_service_needs_reconfigure ()
601 [ -e "$ctdb_status_dir/$service_name/reconfigure" ]
604 ctdb_service_set_reconfigure ()
606 d="$ctdb_status_dir/$service_name"
611 ctdb_service_unset_reconfigure ()
613 rm -f "$ctdb_status_dir/$service_name/reconfigure"
616 ctdb_service_reconfigure ()
618 if [ -n "$service_reconfigure" ] ; then
619 eval $service_reconfigure
621 service "$service_name" restart
623 ctdb_service_unset_reconfigure
627 ctdb_compat_managed_service ()
629 if [ "$1" = "yes" ] ; then
634 is_ctdb_managed_service ()
636 t=" $CTDB_MANAGED_SERVICES "
638 ctdb_compat_managed_service "$CTDB_MANAGES_VSFTPD" "vsftpd"
639 ctdb_compat_managed_service "$CTDB_MANAGES_SAMBA" "samba"
640 ctdb_compat_managed_service "$CTDB_MANAGES_SCP" "scp"
641 ctdb_compat_managed_service "$CTDB_MANAGES_WINDBIND" "windbind"
642 ctdb_compat_managed_service "$CTDB_MANAGES_HTTPD" "httpd"
643 ctdb_compat_managed_service "$CTDB_MANAGES_ISCSI" "iscsi"
644 ctdb_compat_managed_service "$CTDB_MANAGES_CLAMD" "clamd"
645 ctdb_compat_managed_service "$CTDB_MANAGES_NFS" "nfs"
647 # Returns 0 if "<space>$service_name<space>" appears in $t
648 [ "${t#* ${service_name} }" != "${t}" ]
651 ctdb_start_stop_service ()
653 _active="$ctdb_active_dir/$service_name"
655 if is_ctdb_managed_service ; then
656 if ! [ -e "$_active" ] ; then
657 echo "Starting service $service_name"
658 ctdb_service_start || exit $?
659 mkdir -p "$ctdb_active_dir"
663 elif ! is_ctdb_managed_service ; then
664 if [ -e "$_active" ] ; then
665 echo "Stopping service $service_name"
666 ctdb_service_stop || exit $?
673 ctdb_service_start ()
675 if [ -n "$service_start" ] ; then
678 service "$service_name" start
685 if [ -n "$service_stop" ] ; then
688 service "$service_name" stop
692 ctdb_standard_event_handler ()
707 ipv4_host_addr_to_net_addr()
712 local HOST0=$(echo $HOST | awk -F . '{print $4}')
713 local HOST1=$(echo $HOST | awk -F . '{print $3}')
714 local HOST2=$(echo $HOST | awk -F . '{print $2}')
715 local HOST3=$(echo $HOST | awk -F . '{print $1}')
717 local HOST_NUM=$(( $HOST0 + $HOST1 * 256 + $HOST2 * (256 ** 2) + $HOST3 * (256 ** 3) ))
719 local MASK_NUM=$(( ( (2**32 - 1) * (2**(32 - $MASKBITS)) ) & (2**32 - 1) ))
721 local NET_NUM=$(( $HOST_NUM & $MASK_NUM))
723 local NET0=$(( $NET_NUM & 255 ))
724 local NET1=$(( ($NET_NUM & (255 * 256)) / 256 ))
725 local NET2=$(( ($NET_NUM & (255 * 256**2)) / 256**2 ))
726 local NET3=$(( ($NET_NUM & (255 * 256**3)) / 256**3 ))
728 echo "$NET3.$NET2.$NET1.$NET0"
731 ipv4_maskbits_to_net_mask()
735 local MASK_NUM=$(( ( (2**32 - 1) * (2**(32 - $MASKBITS)) ) & (2**32 - 1) ))
737 local MASK0=$(( $MASK_NUM & 255 ))
738 local MASK1=$(( ($MASK_NUM & (255 * 256)) / 256 ))
739 local MASK2=$(( ($MASK_NUM & (255 * 256**2)) / 256**2 ))
740 local MASK3=$(( ($MASK_NUM & (255 * 256**3)) / 256**3 ))
742 echo "$MASK3.$MASK2.$MASK1.$MASK0"
750 local N=`echo $ADDR | sed -e 's/[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*//'`
751 test -n "$N" && fail=1
753 local ADDR0=$(echo $ADDR | awk -F . '{print $4}')
754 local ADDR1=$(echo $ADDR | awk -F . '{print $3}')
755 local ADDR2=$(echo $ADDR | awk -F . '{print $2}')
756 local ADDR3=$(echo $ADDR | awk -F . '{print $1}')
758 test "$ADDR0" -gt 255 && fail=1
759 test "$ADDR1" -gt 255 && fail=1
760 test "$ADDR2" -gt 255 && fail=1
761 test "$ADDR3" -gt 255 && fail=1
763 test x"$fail" != x"0" && {
764 #echo "IPv4: '$ADDR' is not a valid address"
771 # iptables doesn't like being re-entered, so flock-wrap it.
774 flock -w 30 /var/ctdb/iptables-ctdb.flock /sbin/iptables "$@"
777 ########################################################
778 # load a site local config file
779 ########################################################
781 [ -x $CTDB_BASE/rc.local ] && {
782 . $CTDB_BASE/rc.local
785 [ -d $CTDB_BASE/rc.local.d ] && {
786 for i in $CTDB_BASE/rc.local.d/* ; do
787 [ -x "$i" ] && . "$i"
791 script_name="${0##*/}" # basename
792 service_name="$script_name" # default is just the script name