1 # utility functions for ctdb event scripts
3 #######################################
4 # pull in a system config file, if any
7 if [ -f /etc/sysconfig/$name ]; then
9 elif [ -f /etc/default/$name ]; then
11 elif [ -f $CTDB_BASE/sysconfig/$name ]; then
12 . $CTDB_BASE/sysconfig/$name
16 ##############################################################
17 # determine on what type of system (init style) we are running
19 # only do detection if not already set:
20 test "x$CTDB_INIT_STYLE" != "x" && return
22 if [ -x /sbin/startproc ]; then
23 CTDB_INIT_STYLE="suse"
24 elif [ -x /sbin/start-stop-daemon ]; then
25 CTDB_INIT_STYLE="debian"
27 CTDB_INIT_STYLE="redhat"
31 ######################################################
32 # simulate /sbin/service on platforms that don't have it
37 # do nothing, when no service was specified
38 test "x$service_name" = "x" && return
40 if [ -x /sbin/service ]; then
41 /sbin/service "$service_name" "$op"
42 elif [ -x /etc/init.d/$service_name ]; then
43 /etc/init.d/$service_name "$op"
44 elif [ -x /etc/rc.d/init.d/$service_name ]; then
45 /etc/rc.d/init.d/$service_name "$op"
49 ######################################################
50 # simulate /sbin/service (niced) on platforms that don't have it
55 # do nothing, when no service was specified
56 test "x$service_name" = "x" && return
58 if [ -x /sbin/service ]; then
59 nice /sbin/service "$service_name" "$op"
60 elif [ -x /etc/init.d/$service_name ]; then
61 nice /etc/init.d/$service_name "$op"
62 elif [ -x /etc/rc.d/init.d/$service_name ]; then
63 nice /etc/rc.d/init.d/$service_name "$op"
67 ######################################################
68 # wait for a command to return a zero exit status
69 # usage: ctdb_wait_command SERVICE_NAME <command>
70 ######################################################
74 [ -z "$wait_cmd" ] && return;
76 echo "Waiting for service $service_name to start"
77 while [ $all_ok -eq 0 ]; do
78 $wait_cmd > /dev/null 2>&1 && all_ok=1
79 ctdb status > /dev/null 2>&1 || {
80 echo "ctdb daemon has died. Exiting wait for $service_name"
83 [ $all_ok -eq 1 ] || sleep 1
85 echo "Local service $service_name is up"
89 ######################################################
90 # wait for a set of tcp ports
91 # usage: ctdb_wait_tcp_ports SERVICE_NAME <ports...>
92 ######################################################
93 ctdb_wait_tcp_ports() {
97 [ -z "$wait_ports" ] && return;
99 echo "Waiting for tcp service $service_name to start"
100 while [ $all_ok -eq 0 ]; do
102 for p in $wait_ports; do
103 if [ -x /usr/bin/netcat ]; then
104 /usr/bin/netcat -z 127.0.0.1 $p > /dev/null || all_ok=0
105 elif [ -x /usr/bin/nc ]; then
106 /usr/bin/nc -z 127.0.0.1 $p > /dev/null || all_ok=0
107 elif [ -x /usr/bin/netstat ]; then
108 (netstat -a -n | egrep "0.0.0.0:$p[[:space:]]*LISTEN" > /dev/null) || all_ok=0
109 elif [ -x /bin/netstat ]; then
110 (netstat -a -n | egrep "0.0.0.0:$p[[:space:]]*LISTEN" > /dev/null) || all_ok=0
112 echo "No tool to check tcp ports availabe. can not check in ctdb_wait_tcp_ports"
116 [ $all_ok -eq 1 ] || sleep 1
117 ctdb status > /dev/null 2>&1 || {
118 echo "ctdb daemon has died. Exiting tcp wait $service_name"
122 echo "Local tcp services for $service_name are up"
127 ######################################################
128 # wait for a set of directories
129 # usage: ctdb_wait_directories SERVICE_NAME <directories...>
130 ######################################################
131 ctdb_wait_directories() {
135 [ -z "$wait_dirs" ] && return;
137 echo "Waiting for local directories for $service_name"
138 while [ $all_ok -eq 0 ]; do
140 for d in $wait_dirs; do
141 [ -d $d ] || all_ok=0
143 [ $all_ok -eq 1 ] || sleep 1
144 ctdb status > /dev/null 2>&1 || {
145 echo "ctdb daemon has died. Exiting directory wait for $service_name"
149 echo "Local directories for $service_name are available"
153 ######################################################
154 # check that a rpc server is registered with portmap
155 # and responding to requests
156 # usage: ctdb_check_rpc SERVICE_NAME PROGNUM VERSION
157 ######################################################
162 rpcinfo -u localhost $prognum $version > /dev/null || {
163 echo "ERROR: $service_name not responding to rpc requests"
168 ######################################################
169 # check a set of directories is available
170 # return 0 on a missing directory
171 # usage: ctdb_check_directories_probe SERVICE_NAME <directories...>
172 ######################################################
173 ctdb_check_directories_probe() {
177 [ -z "$wait_dirs" ] && return;
178 for d in $wait_dirs; do
179 ( echo $d | grep -q '%' ) && continue
180 [ -d $d ] || return 1
185 ######################################################
186 # check a set of directories is available
187 # usage: ctdb_check_directories SERVICE_NAME <directories...>
188 ######################################################
189 ctdb_check_directories() {
193 ctdb_check_directories_probe "$service_name" $wait_dirs || {
194 echo "ERROR: $service_name directory $d not available"
199 ######################################################
200 # check a set of tcp ports
201 # usage: ctdb_check_tcp_ports SERVICE_NAME <ports...>
202 ######################################################
203 ctdb_check_tcp_ports() {
207 [ -z "$wait_ports" ] && return;
209 # check availability of netcat or netstat first
212 if [ -x /usr/bin/netstat ]; then
213 NETSTAT=/usr/bin/netstat
214 elif [ -x /bin/netstat ]; then
216 elif [ -x /usr/bin/netcat ]; then
217 NETCAT=/usr/bin/netcat
218 elif [ -x /bin/netcat ]; then
220 elif [ -x /usr/bin/nc ]; then
222 elif [ -x /bin/nc ]; then
226 for p in $wait_ports; do
229 if [ "x${NETCAT}" != "x" ]; then
230 ${NETCAT} -z 127.0.0.1 $p > /dev/null || all_ok=0
231 elif [ "x${NETSTAT}" != "x" ]; then
232 if ! ${NETSTAT} -a -n | egrep "0.0.0.0:$p .*LISTEN" > /dev/null ; then
233 if ! ${NETSTAT} -a -n | egrep ":::$p .*LISTEN" > /dev/null ; then
238 echo "ERROR: neither netcat (or nc) nor netstat found!"
239 echo "ERROR: can't monitor ${service_name} tcp port ${p}"
243 [ $all_ok -eq 1 ] || {
244 echo "ERROR: $service_name tcp port $p is not responding"
250 ######################################################
251 # check a unix socket
252 # usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
253 ######################################################
254 ctdb_check_unix_socket() {
257 [ -z "$socket_path" ] && return;
259 # check availability of netstat first
261 if [ -x $(type -p netstat) ]; then
262 NETSTAT=$(type -p netstat)
263 elif [ -x /usr/bin/netstat ]; then
264 NETSTAT=/usr/bin/netstat
265 elif [ -x /bin/netstat ]; then
270 if [ "x$NETSTAT" != "x" ]; then
271 if $NETSTAT -l -a -n | grep -qE "^unix.*LISTEN.*${socket_path}$"; then
277 [ -S ${socket_path} ] && all_ok=1 || all_ok=0
280 [ $all_ok -eq 1 ] || {
281 echo "ERROR: $service_name socket $socket_path not found"
286 ######################################################
287 # check a command returns zero status
288 # usage: ctdb_check_command SERVICE_NAME <command>
289 ######################################################
290 ctdb_check_command() {
293 [ -z "$wait_cmd" ] && return;
294 $wait_cmd > /dev/null 2>&1 || {
295 echo "ERROR: $service_name - $wait_cmd returned error"
300 ################################################
301 # kill off any TCP connections with the given IP
302 ################################################
303 kill_tcp_connections() {
308 connfile="$CTDB_BASE/state/connections.$_IP"
309 netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
310 netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
312 while read dest src; do
313 srcip=`echo $src | sed -e "s/:[^:]*$//"`
314 srcport=`echo $src | sed -e "s/^.*://"`
315 destip=`echo $dest | sed -e "s/:[^:]*$//"`
316 destport=`echo $dest | sed -e "s/^.*://"`
317 echo "Killing TCP connection $srcip:$srcport $destip:$destport"
318 ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
320 # we only do one-way killtcp for NFS and CIFS
322 # for all others we do 2-way
324 ctdb killtcp $destip:$destport $srcip:$srcport >/dev/null 2>&1 || _failed=1
327 _killcount=`expr $_killcount + 1`
331 [ $_failed = 0 ] || {
332 echo "Failed to send killtcp control"
335 [ $_killcount -gt 0 ] || {
339 while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
341 _count=`expr $_count + 1`
342 [ $_count -gt 3 ] && {
343 echo "Timed out killing tcp connections for IP $_IP"
347 echo "killed $_killcount TCP connections to released IP $_IP"
350 ##################################################################
351 # kill off the local end for any TCP connections with the given IP
352 ##################################################################
353 kill_tcp_connections_local_only() {
358 connfile="$CTDB_BASE/state/connections.$_IP"
359 netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
360 netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
362 while read dest src; do
363 srcip=`echo $src | sed -e "s/:[^:]*$//"`
364 srcport=`echo $src | sed -e "s/^.*://"`
365 destip=`echo $dest | sed -e "s/:[^:]*$//"`
366 destport=`echo $dest | sed -e "s/^.*://"`
367 echo "Killing TCP connection $srcip:$srcport $destip:$destport"
368 ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
369 _killcount=`expr $_killcount + 1`
373 [ $_failed = 0 ] || {
374 echo "Failed to send killtcp control"
377 [ $_killcount -gt 0 ] || {
381 while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
383 _count=`expr $_count + 1`
384 [ $_count -gt 3 ] && {
385 echo "Timed out killing tcp connections for IP $_IP"
389 echo "killed $_killcount TCP connections to released IP $_IP"
392 ########################################################
393 # start/stop the nfs service on different platforms
394 ########################################################
397 [ -x /etc/init.d/nfsserver ] && {
400 [ -x /etc/init.d/nfslock ] && {
408 service nfsserver start
411 service nfsserver stop > /dev/null 2>&1
418 service nfslock start
422 service nfs stop > /dev/null 2>&1
423 service nfslock stop > /dev/null 2>&1
428 echo "Unknown platform. NFS is not supported with ctdb"
434 ########################################################
435 # start/stop the nfs lockmanager service on different platforms
436 ########################################################
437 startstop_nfslock() {
439 [ -x /etc/init.d/nfsserver ] && {
442 [ -x /etc/init.d/nfslock ] && {
448 # for sles there is no service for lockmanager
449 # so we instead just shutdown/restart nfs
452 service nfsserver start
455 service nfsserver stop > /dev/null 2>&1
462 service nfslock start
465 service nfslock stop > /dev/null 2>&1
470 echo "Unknown platform. NFS locking is not supported with ctdb"
476 ########################################################
477 # remove an ip address from an interface
478 ########################################################
480 # the ip tool will delete all secondary IPs if this is the primary.
481 # To work around this _very_ annoying behaviour we have to keep a
482 # record of the secondaries and re-add them afterwards. yuck
484 if ip addr list dev $2 primary | grep -q "inet $1 " ; then
485 secondaries=`ip addr list dev $2 secondary | grep " inet " | awk '{print $2}'`
487 ip addr del $1 dev $2 >/dev/null 2>/dev/null || failed=1
488 [ -z "$secondaries" ] || {
489 for i in $secondaries; do
490 if ip addr list dev $2 | grep -q "inet $i" ; then
491 echo "kept secondary $i on dev $2"
493 echo "re-adding secondary address $i to dev $2"
494 ip addr add $i dev $2 || failed=1
500 ########################################################
501 # load a site local config file
502 ########################################################
504 [ -x $CTDB_BASE/rc.local ] && {
505 . $CTDB_BASE/rc.local