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
17 ######################################################
18 # simulate /sbin/service on platforms that don't have it
22 if [ -x /sbin/service ]; then
23 /sbin/service "$service_name" "$op"
24 elif [ -x /etc/init.d/$service_name ]; then
25 /etc/init.d/$service_name "$op"
26 elif [ -x /etc/rc.d/init.d/$service_name ]; then
27 /etc/rc.d/init.d/$service_name "$op"
32 ######################################################
33 # wait for a command to return a zero exit status
34 # usage: ctdb_wait_command SERVICE_NAME <command>
35 ######################################################
39 [ -z "$wait_cmd" ] && return;
41 echo "`/bin/date` Waiting for service $service_name to start"
42 while [ $all_ok -eq 0 ]; do
43 $wait_cmd > /dev/null 2>&1 && all_ok=1
44 ctdb status > /dev/null 2>&1 || {
45 echo "ctdb daemon has died. Exiting wait for $service_name"
48 [ $all_ok -eq 1 ] || sleep 1
50 echo "`/bin/date` Local service $service_name is up"
54 ######################################################
55 # wait for a set of tcp ports
56 # usage: ctdb_wait_tcp_ports SERVICE_NAME <ports...>
57 ######################################################
58 ctdb_wait_tcp_ports() {
62 [ -z "$wait_ports" ] && return;
64 echo "`/bin/date` Waiting for tcp service $service_name to start"
65 while [ $all_ok -eq 0 ]; do
67 for p in $wait_ports; do
68 if [ -x /usr/bin/netcat ]; then
69 /usr/bin/netcat -z 127.0.0.1 $p > /dev/null || all_ok=0
70 elif [ -x /usr/bin/nc ]; then
71 /usr/bin/nc -z 127.0.0.1 $p > /dev/null || all_ok=0
72 elif [ -x /usr/bin/netstat ]; then
73 (/usr/bin/netstat -a -n | egrep "0.0.0.0:$p\s*LISTEN" > /dev/null) || all_ok=0
75 echo "`date` - No tool to check tcp ports availabe. can not check in ctdb_wait_tcp_ports"
79 [ $all_ok -eq 1 ] || sleep 1
80 ctdb status > /dev/null 2>&1 || {
81 echo "ctdb daemon has died. Exiting tcp wait $service_name"
85 echo "`/bin/date` Local tcp services for $service_name are up"
90 ######################################################
91 # wait for a set of directories
92 # usage: ctdb_wait_directories SERVICE_NAME <directories...>
93 ######################################################
94 ctdb_wait_directories() {
98 [ -z "$wait_dirs" ] && return;
100 echo "`/bin/date` Waiting for local directories for $service_name"
101 while [ $all_ok -eq 0 ]; do
103 for d in $wait_dirs; do
104 [ -d $d ] || all_ok=0
106 [ $all_ok -eq 1 ] || sleep 1
107 ctdb status > /dev/null 2>&1 || {
108 echo "ctdb daemon has died. Exiting directory wait for $service_name"
112 echo "`/bin/date` Local directories for $service_name are available"
116 ######################################################
117 # check that a rpc server is registered with portmap
118 # and responding to requests
119 # usage: ctdb_check_rpc SERVICE_NAME PROGNUM VERSION
120 ######################################################
125 rpcinfo -u localhost $prognum $version > /dev/null || {
126 echo "`date` ERROR: $service_name not responding to rpc requests"
131 ######################################################
132 # check a set of directories is available
133 # usage: ctdb_check_directories SERVICE_NAME <directories...>
134 ######################################################
135 ctdb_check_directories() {
139 [ -z "$wait_dirs" ] && return;
140 for d in $wait_dirs; do
142 echo "`date` ERROR: $service_name directory $d not available"
148 ######################################################
149 # check a set of tcp ports
150 # usage: ctdb_check_tcp_ports SERVICE_NAME <ports...>
151 ######################################################
152 ctdb_check_tcp_ports() {
156 [ -z "$wait_ports" ] && return;
157 for p in $wait_ports; do
159 if [ -x /usr/bin/netcat ]; then
160 /usr/bin/netcat -z 127.0.0.1 $p > /dev/null || all_ok=0
161 elif [ -x /usr/bin/nc ]; then
162 /usr/bin/nc -z 127.0.0.1 $p > /dev/null || all_ok=0
163 elif [ -x /usr/bin/netstat ]; then
164 (/usr/bin/netstat -a -n | egrep "0.0.0.0:$p .*LISTEN" > /dev/null ) || all_ok=0
166 [ $all_ok -eq 1 ] || {
167 echo "`date` ERROR: $service_name tcp port $p is not responding"
173 ######################################################
174 # check a command returns zero status
175 # usage: ctdb_check_command SERVICE_NAME <command>
176 ######################################################
177 ctdb_check_command() {
180 [ -z "$wait_cmd" ] && return;
181 $wait_cmd > /dev/null 2>&1 || {
182 echo "`date` ERROR: $service_name - $wait_cmd returned error"
187 ################################################
188 # kill off any TCP connections with the given IP
189 ################################################
190 kill_tcp_connections() {
195 connfile="$CTDB_BASE/state/connections.$_IP"
196 netstat -tn |egrep "^tcp.*\s+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
197 while read dest src; do
198 srcip=`echo $src | cut -d: -f1`
199 srcport=`echo $src | cut -d: -f2`
200 destip=`echo $dest | cut -d: -f1`
201 destport=`echo $dest | cut -d: -f2`
202 ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
203 echo "`date` Killing TCP connection $srcip:$srcport $destip:$destport"
204 _killcount=`expr $_killcount + 1`
207 [ $_failed = 0 ] || {
208 echo "`date` Failed to send killtcp control"
211 [ $_killcount -gt 0 ] || {
215 while netstat -tn |egrep "^tcp.*\s+$_IP:.*ESTABLISHED" > /dev/null; do
217 _count=`expr $_count + 1`
218 [ $_count -gt 3 ] && {
219 echo "`date` Timed out killing tcp connections for IP $_IP"
223 echo "`date` killed $_killcount TCP connections to released IP $_IP"