merge from ronnie
[vlendec/samba-autobuild/.git] / ctdb / config / functions
1 # utility functions for ctdb event scripts
2
3 #######################################
4 # pull in a system config file, if any
5 loadconfig() {
6     name="$1"
7     if [ -f /etc/sysconfig/$name ]; then
8         . /etc/sysconfig/$name
9     elif [ -f /etc/default/$name ]; then
10         . /etc/default/$name
11     elif [ -f $CTDB_BASE/sysconfig/$name ]; then
12         . $CTDB_BASE/sysconfig/$name
13     fi
14 }
15
16
17 ######################################################
18 # simulate /sbin/service on platforms that don't have it
19 service() { 
20   service_name="$1"
21   op="$2"
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"
28   fi
29 }
30
31
32 ######################################################
33 # wait for a command to return a zero exit status
34 # usage: ctdb_wait_command SERVICE_NAME <command>
35 ######################################################
36 ctdb_wait_command() {
37   service_name="$1"
38   wait_cmd="$2"
39   [ -z "$wait_cmd" ] && return;
40   all_ok=0
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"
46                 exit 1
47           }
48           [ $all_ok -eq 1 ] || sleep 1
49   done
50   echo "`/bin/date` Local service $service_name is up"
51 }
52
53
54 ######################################################
55 # wait for a set of tcp ports
56 # usage: ctdb_wait_tcp_ports SERVICE_NAME <ports...>
57 ######################################################
58 ctdb_wait_tcp_ports() {
59   service_name="$1"
60   shift
61   wait_ports="$*"
62   [ -z "$wait_ports" ] && return;
63   all_ok=0
64   echo "`/bin/date` Waiting for tcp service $service_name to start"
65   while [ $all_ok -eq 0 ]; do
66           all_ok=1
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
74               else 
75                   echo "`date` - No tool to check tcp ports availabe. can not check in ctdb_wait_tcp_ports"
76                   return
77               fi
78           done
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"
82                 exit 1
83           }
84   done
85   echo "`/bin/date` Local tcp services for $service_name are up"
86 }
87
88
89
90 ######################################################
91 # wait for a set of directories
92 # usage: ctdb_wait_directories SERVICE_NAME <directories...>
93 ######################################################
94 ctdb_wait_directories() {
95   service_name="$1"
96   shift
97   wait_dirs="$*"
98   [ -z "$wait_dirs" ] && return;
99   all_ok=0
100   echo "`/bin/date` Waiting for local directories for $service_name"
101   while [ $all_ok -eq 0 ]; do
102           all_ok=1
103           for d in $wait_dirs; do
104               [ -d $d ] || all_ok=0
105           done
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"
109                 exit 1
110           }
111   done
112   echo "`/bin/date` Local directories for $service_name are available"
113 }
114
115
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 ######################################################
121 ctdb_check_rpc() {
122     service_name="$1"
123     prognum="$2"
124     version="$3"
125     rpcinfo -u localhost $prognum $version > /dev/null || {
126             echo "`date` ERROR: $service_name not responding to rpc requests"
127             exit 1
128     }
129 }
130
131 ######################################################
132 # check a set of directories is available
133 # usage: ctdb_check_directories SERVICE_NAME <directories...>
134 ######################################################
135 ctdb_check_directories() {
136   service_name="$1"
137   shift
138   wait_dirs="$*"
139   [ -z "$wait_dirs" ] && return;
140   for d in $wait_dirs; do
141       [ -d $d ] || {
142           echo "`date` ERROR: $service_name directory $d not available"
143           exit 1
144       }
145   done
146 }
147
148 ######################################################
149 # check a set of tcp ports
150 # usage: ctdb_check_tcp_ports SERVICE_NAME <ports...>
151 ######################################################
152 ctdb_check_tcp_ports() {
153   service_name="$1"
154   shift
155   wait_ports="$*"
156   [ -z "$wait_ports" ] && return;
157   for p in $wait_ports; do
158       all_ok=1
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
165       fi
166       [ $all_ok -eq 1 ] || {
167           echo "`date` ERROR: $service_name tcp port $p is not responding"
168           exit 1
169       }
170   done
171 }
172
173 ######################################################
174 # check a command returns zero status
175 # usage: ctdb_check_command SERVICE_NAME <command>
176 ######################################################
177 ctdb_check_command() {
178   service_name="$1"
179   wait_cmd="$2"
180   [ -z "$wait_cmd" ] && return;
181   $wait_cmd > /dev/null 2>&1 || {
182       echo "`date` ERROR: $service_name - $wait_cmd returned error"
183       exit 1
184   }
185 }
186
187 ################################################
188 # kill off any TCP connections with the given IP
189 ################################################
190 kill_tcp_connections() {
191     _IP="$1"    
192     _failed=0
193
194     _killcount=0
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`
205     done < $connfile
206     /bin/rm -f $connfile
207     [ $_failed = 0 ] || {
208         echo "`date` Failed to send killtcp control"
209         return;
210     }
211     [ $_killcount -gt 0 ] || {
212         return;
213     }
214     _count=0
215     while netstat -tn |egrep "^tcp.*\s+$_IP:.*ESTABLISHED" > /dev/null; do
216         sleep 1
217         _count=`expr $_count + 1`
218         [ $_count -gt 3 ] && {
219             echo "`date` Timed out killing tcp connections for IP $_IP"
220             return;
221         }
222     done
223     echo "`date` killed $_killcount TCP connections to released IP $_IP"
224 }
225