2e79c3330a549ab796b9287d9a680d4163d53865
[sahlberg/ctdb.git] / 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 "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 "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 "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                   (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
76               else 
77                   echo "No tool to check tcp ports availabe. can not check in ctdb_wait_tcp_ports"
78                   return
79               fi
80           done
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"
84                 exit 1
85           }
86   done
87   echo "Local tcp services for $service_name are up"
88 }
89
90
91
92 ######################################################
93 # wait for a set of directories
94 # usage: ctdb_wait_directories SERVICE_NAME <directories...>
95 ######################################################
96 ctdb_wait_directories() {
97   service_name="$1"
98   shift
99   wait_dirs="$*"
100   [ -z "$wait_dirs" ] && return;
101   all_ok=0
102   echo "Waiting for local directories for $service_name"
103   while [ $all_ok -eq 0 ]; do
104           all_ok=1
105           for d in $wait_dirs; do
106               [ -d $d ] || all_ok=0
107           done
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"
111                 exit 1
112           }
113   done
114   echo "Local directories for $service_name are available"
115 }
116
117
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 ######################################################
123 ctdb_check_rpc() {
124     service_name="$1"
125     prognum="$2"
126     version="$3"
127     rpcinfo -u localhost $prognum $version > /dev/null || {
128             echo "ERROR: $service_name not responding to rpc requests"
129             exit 1
130     }
131 }
132
133 ######################################################
134 # check a set of directories is available
135 # usage: ctdb_check_directories SERVICE_NAME <directories...>
136 ######################################################
137 ctdb_check_directories() {
138   service_name="$1"
139   shift
140   wait_dirs="$*"
141   [ -z "$wait_dirs" ] && return;
142   for d in $wait_dirs; do
143       [ -d $d ] || {
144           echo "ERROR: $service_name directory $d not available"
145           exit 1
146       }
147   done
148 }
149
150 ######################################################
151 # check a set of tcp ports
152 # usage: ctdb_check_tcp_ports SERVICE_NAME <ports...>
153 ######################################################
154 ctdb_check_tcp_ports() {
155   service_name="$1"
156   shift
157   wait_ports="$*"
158   [ -z "$wait_ports" ] && return;
159   for p in $wait_ports; do
160       all_ok=1
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
169       fi
170       [ $all_ok -eq 1 ] || {
171           echo "ERROR: $service_name tcp port $p is not responding"
172           exit 1
173       }
174   done
175 }
176
177 ######################################################
178 # check a command returns zero status
179 # usage: ctdb_check_command SERVICE_NAME <command>
180 ######################################################
181 ctdb_check_command() {
182   service_name="$1"
183   wait_cmd="$2"
184   [ -z "$wait_cmd" ] && return;
185   $wait_cmd > /dev/null 2>&1 || {
186       echo "ERROR: $service_name - $wait_cmd returned error"
187       exit 1
188   }
189 }
190
191 ################################################
192 # kill off any TCP connections with the given IP
193 ################################################
194 kill_tcp_connections() {
195     _IP="$1"    
196     _failed=0
197
198     _killcount=0
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`
209     done < $connfile
210     /bin/rm -f $connfile
211     [ $_failed = 0 ] || {
212         echo "Failed to send killtcp control"
213         return;
214     }
215     [ $_killcount -gt 0 ] || {
216         return;
217     }
218     _count=0
219     while netstat -tn |egrep "^tcp.*\s+$_IP:.*ESTABLISHED" > /dev/null; do
220         sleep 1
221         _count=`expr $_count + 1`
222         [ $_count -gt 3 ] && {
223             echo "Timed out killing tcp connections for IP $_IP"
224             return;
225         }
226     done
227     echo "killed $_killcount TCP connections to released IP $_IP"
228 }
229
230 ########################################################
231 # start/stop the nfs service on different platforms
232 ########################################################
233 startstop_nfs() {
234         PLATFORM="unknown"
235         [ -x /etc/init.d/nfsserver ] && {
236                 PLATFORM="sles"
237         }
238         [ -x /etc/init.d/nfslock ] && {
239                 PLATFORM="rhel"
240         }
241
242         case $PLATFORM in
243         sles)
244                 case $1 in
245                 start)
246                         service nfsserver start
247                         ;;
248                 stop)
249                         service nfsserver stop > /dev/null 2>&1
250                         ;;
251                 esac
252                 ;;
253         rhel)
254                 case $1 in
255                 start)
256                         service nfslock start
257                         service nfs start
258                         ;;
259                 stop)
260                         service nfs stop > /dev/null 2>&1
261                         service nfslock stop > /dev/null 2>&1
262                         ;;
263                 esac
264                 ;;
265         *)
266                 echo "Unknown platform. NFS is not supported with ctdb"
267                 exit 1
268                 ;;
269         esac
270 }
271
272 ########################################################
273 # start/stop the nfs lockmanager service on different platforms
274 ########################################################
275 startstop_nfslock() {
276         PLATFORM="unknown"
277         [ -x /etc/init.d/nfsserver ] && {
278                 PLATFORM="sles"
279         }
280         [ -x /etc/init.d/nfslock ] && {
281                 PLATFORM="rhel"
282         }
283
284         case $PLATFORM in
285         sles)
286                 # for sles there is no service for lockmanager
287                 # so we instead just shutdown/restart nfs
288                 case $1 in
289                 start)
290                         service nfsserver start
291                         ;;
292                 stop)
293                         service nfsserver stop > /dev/null 2>&1
294                         ;;
295                 esac
296                 ;;
297         rhel)
298                 case $1 in
299                 start)
300                         service nfslock start
301                         ;;
302                 stop)
303                         service nfslock stop > /dev/null 2>&1
304                         ;;
305                 esac
306                 ;;
307         *)
308                 echo "Unknown platform. NFS locking is not supported with ctdb"
309                 exit 1
310                 ;;
311         esac
312 }