funcions: make (nice_)service a noop for empty service name
[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 # determine on what type of system (init style) we are running
18 detect_init_style() {
19     # only do detection if not already set:
20     test "x$CTDB_INIT_STYLE" != "x" && return
21
22     if [ -x /sbin/startproc ]; then
23         CTDB_INIT_STYLE="suse"
24     elif [ -x /sbin/start-stop-daemon ]; then
25         CTDB_INIT_STYLE="ubuntu"
26     else
27         CTDB_INIT_STYLE="redhat"
28     fi
29 }
30
31 ######################################################
32 # simulate /sbin/service on platforms that don't have it
33 service() { 
34   service_name="$1"
35   op="$2"
36
37   # do nothing, when no service was specified
38   test "x$service_name" = "x" && return
39
40   if [ -x /sbin/service ]; then
41       /sbin/service "$service_name" "$op"
42   elif [ -x /etc/init.d/$service_name ]; then
43       /etc/init.d/$service_name "$op"
44   elif [ -x /etc/rc.d/init.d/$service_name ]; then
45       /etc/rc.d/init.d/$service_name "$op"
46   fi
47 }
48
49 ######################################################
50 # simulate /sbin/service (niced) on platforms that don't have it
51 nice_service() { 
52   service_name="$1"
53   op="$2"
54
55   # do nothing, when no service was specified
56   test "x$service_name" = "x" && return
57
58   if [ -x /sbin/service ]; then
59       nice /sbin/service "$service_name" "$op"
60   elif [ -x /etc/init.d/$service_name ]; then
61       nice /etc/init.d/$service_name "$op"
62   elif [ -x /etc/rc.d/init.d/$service_name ]; then
63       nice /etc/rc.d/init.d/$service_name "$op"
64   fi
65 }
66
67 ######################################################
68 # wait for a command to return a zero exit status
69 # usage: ctdb_wait_command SERVICE_NAME <command>
70 ######################################################
71 ctdb_wait_command() {
72   service_name="$1"
73   wait_cmd="$2"
74   [ -z "$wait_cmd" ] && return;
75   all_ok=0
76   echo "Waiting for service $service_name to start"
77   while [ $all_ok -eq 0 ]; do
78           $wait_cmd > /dev/null 2>&1 && all_ok=1
79           ctdb status > /dev/null 2>&1 || {
80                 echo "ctdb daemon has died. Exiting wait for $service_name"
81                 exit 1
82           }
83           [ $all_ok -eq 1 ] || sleep 1
84   done
85   echo "Local service $service_name is up"
86 }
87
88
89 ######################################################
90 # wait for a set of tcp ports
91 # usage: ctdb_wait_tcp_ports SERVICE_NAME <ports...>
92 ######################################################
93 ctdb_wait_tcp_ports() {
94   service_name="$1"
95   shift
96   wait_ports="$*"
97   [ -z "$wait_ports" ] && return;
98   all_ok=0
99   echo "Waiting for tcp service $service_name to start"
100   while [ $all_ok -eq 0 ]; do
101           all_ok=1
102           for p in $wait_ports; do
103               if [ -x /usr/bin/netcat ]; then
104                   /usr/bin/netcat -z 127.0.0.1 $p > /dev/null || all_ok=0
105               elif [ -x /usr/bin/nc ]; then
106                   /usr/bin/nc -z 127.0.0.1 $p > /dev/null || all_ok=0
107               elif [ -x /usr/bin/netstat ]; then
108                   (netstat -a -n | egrep "0.0.0.0:$p[[:space:]]*LISTEN" > /dev/null) || all_ok=0
109               elif [ -x /bin/netstat ]; then
110                   (netstat -a -n | egrep "0.0.0.0:$p[[:space:]]*LISTEN" > /dev/null) || all_ok=0
111               else 
112                   echo "No tool to check tcp ports availabe. can not check in ctdb_wait_tcp_ports"
113                   return
114               fi
115           done
116           [ $all_ok -eq 1 ] || sleep 1
117           ctdb status > /dev/null 2>&1 || {
118                 echo "ctdb daemon has died. Exiting tcp wait $service_name"
119                 exit 1
120           }
121   done
122   echo "Local tcp services for $service_name are up"
123 }
124
125
126
127 ######################################################
128 # wait for a set of directories
129 # usage: ctdb_wait_directories SERVICE_NAME <directories...>
130 ######################################################
131 ctdb_wait_directories() {
132   service_name="$1"
133   shift
134   wait_dirs="$*"
135   [ -z "$wait_dirs" ] && return;
136   all_ok=0
137   echo "Waiting for local directories for $service_name"
138   while [ $all_ok -eq 0 ]; do
139           all_ok=1
140           for d in $wait_dirs; do
141               [ -d $d ] || all_ok=0
142           done
143           [ $all_ok -eq 1 ] || sleep 1
144           ctdb status > /dev/null 2>&1 || {
145                 echo "ctdb daemon has died. Exiting directory wait for $service_name"
146                 exit 1
147           }
148   done
149   echo "Local directories for $service_name are available"
150 }
151
152
153 ######################################################
154 # check that a rpc server is registered with portmap
155 # and responding to requests
156 # usage: ctdb_check_rpc SERVICE_NAME PROGNUM VERSION
157 ######################################################
158 ctdb_check_rpc() {
159     service_name="$1"
160     prognum="$2"
161     version="$3"
162     rpcinfo -u localhost $prognum $version > /dev/null || {
163             echo "ERROR: $service_name not responding to rpc requests"
164             exit 1
165     }
166 }
167
168 ######################################################
169 # check a set of directories is available
170 # return 0 on a missing directory
171 # usage: ctdb_check_directories_probe SERVICE_NAME <directories...>
172 ######################################################
173 ctdb_check_directories_probe() {
174   service_name="$1"
175   shift
176   wait_dirs="$*"
177   [ -z "$wait_dirs" ] && return;
178   for d in $wait_dirs; do
179       ( echo $d | grep -q '%' ) && continue
180       [ -d $d ] || return 1
181   done
182   return 0
183 }
184
185 ######################################################
186 # check a set of directories is available
187 # usage: ctdb_check_directories SERVICE_NAME <directories...>
188 ######################################################
189 ctdb_check_directories() {
190   service_name="$1"
191   shift
192   wait_dirs="$*"
193   ctdb_check_directories_probe "$service_name" $wait_dirs || {
194       echo "ERROR: $service_name directory $d not available"
195       exit 1
196   }
197 }
198
199 ######################################################
200 # check a set of tcp ports
201 # usage: ctdb_check_tcp_ports SERVICE_NAME <ports...>
202 ######################################################
203 ctdb_check_tcp_ports() {
204   service_name="$1"
205   shift
206   wait_ports="$*"
207   [ -z "$wait_ports" ] && return;
208   for p in $wait_ports; do
209       all_ok=1
210       if [ -x /usr/bin/netcat ]; then
211           /usr/bin/netcat -z 127.0.0.1 $p > /dev/null || all_ok=0
212       elif [ -x /usr/bin/nc ]; then
213           /usr/bin/nc -z 127.0.0.1 $p > /dev/null || all_ok=0
214       elif [ -x /usr/bin/netstat ]; then
215           (netstat -a -n | egrep "0.0.0.0:$p .*LISTEN" > /dev/null ) || all_ok=0
216       elif [ -x /bin/netstat ]; then
217           (netstat -a -n | egrep "0.0.0.0:$p .*LISTEN" > /dev/null ) || all_ok=0
218       fi
219       [ $all_ok -eq 1 ] || {
220           echo "ERROR: $service_name tcp port $p is not responding"
221           exit 1
222       }
223   done
224 }
225
226 ######################################################
227 # check a command returns zero status
228 # usage: ctdb_check_command SERVICE_NAME <command>
229 ######################################################
230 ctdb_check_command() {
231   service_name="$1"
232   wait_cmd="$2"
233   [ -z "$wait_cmd" ] && return;
234   $wait_cmd > /dev/null 2>&1 || {
235       echo "ERROR: $service_name - $wait_cmd returned error"
236       exit 1
237   }
238 }
239
240 ################################################
241 # kill off any TCP connections with the given IP
242 ################################################
243 kill_tcp_connections() {
244     _IP="$1"    
245     _failed=0
246
247     _killcount=0
248     connfile="$CTDB_BASE/state/connections.$_IP"
249     netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
250     netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
251
252     while read dest src; do
253         srcip=`echo $src | sed -e "s/:[^:]*$//"`
254         srcport=`echo $src | sed -e "s/^.*://"`
255         destip=`echo $dest | sed -e "s/:[^:]*$//"`
256         destport=`echo $dest | sed -e "s/^.*://"`
257         echo "Killing TCP connection $srcip:$srcport $destip:$destport"
258         ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
259         case $destport in
260           # we only do one-way killtcp for NFS and CIFS
261           139|445|2049) : ;;
262           # for all others we do 2-way
263           *) 
264                 ctdb killtcp $destip:$destport $srcip:$srcport >/dev/null 2>&1 || _failed=1
265                 ;;
266         esac
267         _killcount=`expr $_killcount + 1`
268      done < $connfile
269     /bin/rm -f $connfile
270
271     [ $_failed = 0 ] || {
272         echo "Failed to send killtcp control"
273         return;
274     }
275     [ $_killcount -gt 0 ] || {
276         return;
277     }
278     _count=0
279     while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
280         sleep 1
281         _count=`expr $_count + 1`
282         [ $_count -gt 3 ] && {
283             echo "Timed out killing tcp connections for IP $_IP"
284             return;
285         }
286     done
287     echo "killed $_killcount TCP connections to released IP $_IP"
288 }
289
290 ########################################################
291 # start/stop the nfs service on different platforms
292 ########################################################
293 startstop_nfs() {
294         PLATFORM="unknown"
295         [ -x /etc/init.d/nfsserver ] && {
296                 PLATFORM="sles"
297         }
298         [ -x /etc/init.d/nfslock ] && {
299                 PLATFORM="rhel"
300         }
301
302         case $PLATFORM in
303         sles)
304                 case $1 in
305                 start)
306                         service nfsserver start
307                         ;;
308                 stop)
309                         service nfsserver stop > /dev/null 2>&1
310                         ;;
311                 esac
312                 ;;
313         rhel)
314                 case $1 in
315                 start)
316                         service nfslock start
317                         service nfs start
318                         ;;
319                 stop)
320                         service nfs stop > /dev/null 2>&1
321                         service nfslock stop > /dev/null 2>&1
322                         ;;
323                 esac
324                 ;;
325         *)
326                 echo "Unknown platform. NFS is not supported with ctdb"
327                 exit 1
328                 ;;
329         esac
330 }
331
332 ########################################################
333 # start/stop the nfs lockmanager service on different platforms
334 ########################################################
335 startstop_nfslock() {
336         PLATFORM="unknown"
337         [ -x /etc/init.d/nfsserver ] && {
338                 PLATFORM="sles"
339         }
340         [ -x /etc/init.d/nfslock ] && {
341                 PLATFORM="rhel"
342         }
343
344         case $PLATFORM in
345         sles)
346                 # for sles there is no service for lockmanager
347                 # so we instead just shutdown/restart nfs
348                 case $1 in
349                 start)
350                         service nfsserver start
351                         ;;
352                 stop)
353                         service nfsserver stop > /dev/null 2>&1
354                         ;;
355                 esac
356                 ;;
357         rhel)
358                 case $1 in
359                 start)
360                         service nfslock start
361                         ;;
362                 stop)
363                         service nfslock stop > /dev/null 2>&1
364                         ;;
365                 esac
366                 ;;
367         *)
368                 echo "Unknown platform. NFS locking is not supported with ctdb"
369                 exit 1
370                 ;;
371         esac
372 }
373
374 ########################################################
375 # load a site local config file
376 ########################################################
377
378 [ -x $CTDB_BASE/rc.local ] && {
379         . $CTDB_BASE/rc.local
380 }
381