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 "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 "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 "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 (netstat -a -n | egrep "0.0.0.0:$p\s*LISTEN" > /dev/null) || all_ok=0
74 elif [ -x /bin/netstat ]; then
75 (netstat -a -n | egrep "0.0.0.0:$p\s*LISTEN" > /dev/null) || all_ok=0
77 echo "No tool to check tcp ports availabe. can not check in ctdb_wait_tcp_ports"
81 [ $all_ok -eq 1 ] || sleep 1
82 ctdb status > /dev/null 2>&1 || {
83 echo "ctdb daemon has died. Exiting tcp wait $service_name"
87 echo "Local tcp services for $service_name are up"
92 ######################################################
93 # wait for a set of directories
94 # usage: ctdb_wait_directories SERVICE_NAME <directories...>
95 ######################################################
96 ctdb_wait_directories() {
100 [ -z "$wait_dirs" ] && return;
102 echo "Waiting for local directories for $service_name"
103 while [ $all_ok -eq 0 ]; do
105 for d in $wait_dirs; do
106 [ -d $d ] || all_ok=0
108 [ $all_ok -eq 1 ] || sleep 1
109 ctdb status > /dev/null 2>&1 || {
110 echo "ctdb daemon has died. Exiting directory wait for $service_name"
114 echo "Local directories for $service_name are available"
118 ######################################################
119 # check that a rpc server is registered with portmap
120 # and responding to requests
121 # usage: ctdb_check_rpc SERVICE_NAME PROGNUM VERSION
122 ######################################################
127 rpcinfo -u localhost $prognum $version > /dev/null || {
128 echo "ERROR: $service_name not responding to rpc requests"
133 ######################################################
134 # check a set of directories is available
135 # usage: ctdb_check_directories SERVICE_NAME <directories...>
136 ######################################################
137 ctdb_check_directories() {
141 [ -z "$wait_dirs" ] && return;
142 for d in $wait_dirs; do
144 echo "ERROR: $service_name directory $d not available"
150 ######################################################
151 # check a set of tcp ports
152 # usage: ctdb_check_tcp_ports SERVICE_NAME <ports...>
153 ######################################################
154 ctdb_check_tcp_ports() {
158 [ -z "$wait_ports" ] && return;
159 for p in $wait_ports; do
161 if [ -x /usr/bin/netcat ]; then
162 /usr/bin/netcat -z 127.0.0.1 $p > /dev/null || all_ok=0
163 elif [ -x /usr/bin/nc ]; then
164 /usr/bin/nc -z 127.0.0.1 $p > /dev/null || all_ok=0
165 elif [ -x /usr/bin/netstat ]; then
166 (netstat -a -n | egrep "0.0.0.0:$p .*LISTEN" > /dev/null ) || all_ok=0
167 elif [ -x /bin/netstat ]; then
168 (netstat -a -n | egrep "0.0.0.0:$p .*LISTEN" > /dev/null ) || all_ok=0
170 [ $all_ok -eq 1 ] || {
171 echo "ERROR: $service_name tcp port $p is not responding"
177 ######################################################
178 # check a command returns zero status
179 # usage: ctdb_check_command SERVICE_NAME <command>
180 ######################################################
181 ctdb_check_command() {
184 [ -z "$wait_cmd" ] && return;
185 $wait_cmd > /dev/null 2>&1 || {
186 echo "ERROR: $service_name - $wait_cmd returned error"
191 ################################################
192 # kill off any TCP connections with the given IP
193 ################################################
194 kill_tcp_connections() {
199 connfile="$CTDB_BASE/state/connections.$_IP"
200 netstat -tn |egrep "^tcp.*\s+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
201 while read dest src; do
202 srcip=`echo $src | cut -d: -f1`
203 srcport=`echo $src | cut -d: -f2`
204 destip=`echo $dest | cut -d: -f1`
205 destport=`echo $dest | cut -d: -f2`
206 ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
207 echo "Killing TCP connection $srcip:$srcport $destip:$destport"
208 _killcount=`expr $_killcount + 1`
211 [ $_failed = 0 ] || {
212 echo "Failed to send killtcp control"
215 [ $_killcount -gt 0 ] || {
219 while netstat -tn |egrep "^tcp.*\s+$_IP:.*ESTABLISHED" > /dev/null; do
221 _count=`expr $_count + 1`
222 [ $_count -gt 3 ] && {
223 echo "Timed out killing tcp connections for IP $_IP"
227 echo "killed $_killcount TCP connections to released IP $_IP"
230 ########################################################
231 # start/stop the nfs service on different platforms
232 ########################################################
235 [ -x /etc/init.d/nfsserver ] && {
238 [ -x /etc/init.d/nfslock ] && {
246 service nfsserver start
249 service nfsserver stop > /dev/null 2>&1
256 service nfslock start
260 service nfs stop > /dev/null 2>&1
261 service nfslock stop > /dev/null 2>&1
266 echo "Unknown platform. NFS is not supported with ctdb"
272 ########################################################
273 # start/stop the nfs lockmanager service on different platforms
274 ########################################################
275 startstop_nfslock() {
277 [ -x /etc/init.d/nfsserver ] && {
280 [ -x /etc/init.d/nfslock ] && {
286 # for sles there is no service for lockmanager
287 # so we instead just shutdown/restart nfs
290 service nfsserver start
293 service nfsserver stop > /dev/null 2>&1
300 service nfslock start
303 service nfslock stop > /dev/null 2>&1
308 echo "Unknown platform. NFS locking is not supported with ctdb"