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"
31 ######################################################
32 # simulate /sbin/service (niced) on platforms that don't have it
36 if [ -x /sbin/service ]; then
37 nice /sbin/service "$service_name" "$op"
38 elif [ -x /etc/init.d/$service_name ]; then
39 nice /etc/init.d/$service_name "$op"
40 elif [ -x /etc/rc.d/init.d/$service_name ]; then
41 nice /etc/rc.d/init.d/$service_name "$op"
45 ######################################################
46 # wait for a command to return a zero exit status
47 # usage: ctdb_wait_command SERVICE_NAME <command>
48 ######################################################
52 [ -z "$wait_cmd" ] && return;
54 echo "Waiting for service $service_name to start"
55 while [ $all_ok -eq 0 ]; do
56 $wait_cmd > /dev/null 2>&1 && all_ok=1
57 ctdb status > /dev/null 2>&1 || {
58 echo "ctdb daemon has died. Exiting wait for $service_name"
61 [ $all_ok -eq 1 ] || sleep 1
63 echo "Local service $service_name is up"
67 ######################################################
68 # wait for a set of tcp ports
69 # usage: ctdb_wait_tcp_ports SERVICE_NAME <ports...>
70 ######################################################
71 ctdb_wait_tcp_ports() {
75 [ -z "$wait_ports" ] && return;
77 echo "Waiting for tcp service $service_name to start"
78 while [ $all_ok -eq 0 ]; do
80 for p in $wait_ports; do
81 if [ -x /usr/bin/netcat ]; then
82 /usr/bin/netcat -z 127.0.0.1 $p > /dev/null || all_ok=0
83 elif [ -x /usr/bin/nc ]; then
84 /usr/bin/nc -z 127.0.0.1 $p > /dev/null || all_ok=0
85 elif [ -x /usr/bin/netstat ]; then
86 (netstat -a -n | egrep "0.0.0.0:$p[[:space:]]*LISTEN" > /dev/null) || all_ok=0
87 elif [ -x /bin/netstat ]; then
88 (netstat -a -n | egrep "0.0.0.0:$p[[:space:]]*LISTEN" > /dev/null) || all_ok=0
90 echo "No tool to check tcp ports availabe. can not check in ctdb_wait_tcp_ports"
94 [ $all_ok -eq 1 ] || sleep 1
95 ctdb status > /dev/null 2>&1 || {
96 echo "ctdb daemon has died. Exiting tcp wait $service_name"
100 echo "Local tcp services for $service_name are up"
105 ######################################################
106 # wait for a set of directories
107 # usage: ctdb_wait_directories SERVICE_NAME <directories...>
108 ######################################################
109 ctdb_wait_directories() {
113 [ -z "$wait_dirs" ] && return;
115 echo "Waiting for local directories for $service_name"
116 while [ $all_ok -eq 0 ]; do
118 for d in $wait_dirs; do
119 [ -d $d ] || all_ok=0
121 [ $all_ok -eq 1 ] || sleep 1
122 ctdb status > /dev/null 2>&1 || {
123 echo "ctdb daemon has died. Exiting directory wait for $service_name"
127 echo "Local directories for $service_name are available"
131 ######################################################
132 # check that a rpc server is registered with portmap
133 # and responding to requests
134 # usage: ctdb_check_rpc SERVICE_NAME PROGNUM VERSION
135 ######################################################
140 rpcinfo -u localhost $prognum $version > /dev/null || {
141 echo "ERROR: $service_name not responding to rpc requests"
146 ######################################################
147 # check a set of directories is available
148 # return 0 on a missing directory
149 # usage: ctdb_check_directories_probe SERVICE_NAME <directories...>
150 ######################################################
151 ctdb_check_directories_probe() {
155 [ -z "$wait_dirs" ] && return;
156 for d in $wait_dirs; do
157 ( echo $d | grep -q '%' ) && continue
158 [ -d $d ] || return 1
163 ######################################################
164 # check a set of directories is available
165 # usage: ctdb_check_directories SERVICE_NAME <directories...>
166 ######################################################
167 ctdb_check_directories() {
171 ctdb_check_directories_probe "$service_name" $wait_dirs || {
172 echo "ERROR: $service_name directory $d not available"
177 ######################################################
178 # check a set of tcp ports
179 # usage: ctdb_check_tcp_ports SERVICE_NAME <ports...>
180 ######################################################
181 ctdb_check_tcp_ports() {
185 [ -z "$wait_ports" ] && return;
186 for p in $wait_ports; do
188 if [ -x /usr/bin/netcat ]; then
189 /usr/bin/netcat -z 127.0.0.1 $p > /dev/null || all_ok=0
190 elif [ -x /usr/bin/nc ]; then
191 /usr/bin/nc -z 127.0.0.1 $p > /dev/null || all_ok=0
192 elif [ -x /usr/bin/netstat ]; then
193 (netstat -a -n | egrep "0.0.0.0:$p .*LISTEN" > /dev/null ) || all_ok=0
194 elif [ -x /bin/netstat ]; then
195 (netstat -a -n | egrep "0.0.0.0:$p .*LISTEN" > /dev/null ) || all_ok=0
197 [ $all_ok -eq 1 ] || {
198 echo "ERROR: $service_name tcp port $p is not responding"
204 ######################################################
205 # check a command returns zero status
206 # usage: ctdb_check_command SERVICE_NAME <command>
207 ######################################################
208 ctdb_check_command() {
211 [ -z "$wait_cmd" ] && return;
212 $wait_cmd > /dev/null 2>&1 || {
213 echo "ERROR: $service_name - $wait_cmd returned error"
218 ################################################
219 # kill off any TCP connections with the given IP
220 ################################################
221 kill_tcp_connections() {
226 connfile="$CTDB_BASE/state/connections.$_IP"
227 netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
228 netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
230 while read dest src; do
231 srcip=`echo $src | sed -e "s/:[^:]*$//"`
232 srcport=`echo $src | sed -e "s/^.*://"`
233 destip=`echo $dest | sed -e "s/:[^:]*$//"`
234 destport=`echo $dest | sed -e "s/^.*://"`
235 echo "Killing TCP connection $srcip:$srcport $destip:$destport"
236 ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
238 # we only do one-way killtcp for NFS and CIFS
240 # for all others we do 2-way
242 ctdb killtcp $destip:$destport $srcip:$srcport >/dev/null 2>&1 || _failed=1
245 _killcount=`expr $_killcount + 1`
249 [ $_failed = 0 ] || {
250 echo "Failed to send killtcp control"
253 [ $_killcount -gt 0 ] || {
257 while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
259 _count=`expr $_count + 1`
260 [ $_count -gt 3 ] && {
261 echo "Timed out killing tcp connections for IP $_IP"
265 echo "killed $_killcount TCP connections to released IP $_IP"
268 ########################################################
269 # start/stop the nfs service on different platforms
270 ########################################################
273 [ -x /etc/init.d/nfsserver ] && {
276 [ -x /etc/init.d/nfslock ] && {
284 service nfsserver start
287 service nfsserver stop > /dev/null 2>&1
294 service nfslock start
298 service nfs stop > /dev/null 2>&1
299 service nfslock stop > /dev/null 2>&1
304 echo "Unknown platform. NFS is not supported with ctdb"
310 ########################################################
311 # start/stop the nfs lockmanager service on different platforms
312 ########################################################
313 startstop_nfslock() {
315 [ -x /etc/init.d/nfsserver ] && {
318 [ -x /etc/init.d/nfslock ] && {
324 # for sles there is no service for lockmanager
325 # so we instead just shutdown/restart nfs
328 service nfsserver start
331 service nfsserver stop > /dev/null 2>&1
338 service nfslock start
341 service nfslock stop > /dev/null 2>&1
346 echo "Unknown platform. NFS locking is not supported with ctdb"
352 ########################################################
353 # load a site local config file
354 ########################################################
356 [ -x $CTDB_BASE/rc.local ] && {
357 . $CTDB_BASE/rc.local