Eventscripts - call ctdb_check_args() instead of doing hand checking
[obnox/samba/samba-obnox.git] / ctdb / config / events.d / 10.interface
1 #!/bin/sh
2
3 #################################
4 # interface event script for ctdb
5 # this adds/removes IPs from your 
6 # public interface
7
8 . $CTDB_BASE/functions
9 loadconfig
10
11 [ -z "$CTDB_PUBLIC_ADDRESSES" ] && {
12         CTDB_PUBLIC_ADDRESSES=$CTDB_BASE/public_addresses
13 }
14
15 [ ! -f "$CTDB_PUBLIC_ADDRESSES" ] && {
16         exit 0
17 }
18
19 mark_up ()
20 {
21     up_interfaces_found=true
22     ctdb setifacelink $1 up >/dev/null 2>&1
23 }
24
25 mark_down ()
26 {
27     fail=true
28     ctdb setifacelink $1 down >/dev/null 2>&1
29 }
30
31 # This sets $all_interfaces as a side-effect.
32 get_all_interfaces ()
33 {
34     # Get all the interfaces listed in the public_addresses file
35     all_interfaces=$(sed -e "s/^[^\t ]*[\t ]*//" -e "s/,/ /g" -e "s/[\t ]*$//" $CTDB_PUBLIC_ADDRESSES)
36
37     # Add some special interfaces if they're defined
38     [ "$CTDB_PUBLIC_INTERFACE" ] && all_interfaces="$CTDB_PUBLIC_INTERFACE $all_interfaces"
39     [ "$CTDB_NATGW_PUBLIC_IFACE" ] && all_interfaces="$CTDB_NATGW_PUBLIC_IFACE $all_interfaces"
40
41     # For all but the 1st line, get the 2nd last field with commas
42     # changes to spaces.
43     ctdb_ifaces=$(ctdb -Y ip -v | sed -e '1d' -e 's/:[^:]*:$//' -e 's/^.*://' -e 's/,/ /g')
44
45     # Add $ctdb_interfaces and uniquify
46     all_interfaces=$(echo $all_interfaces $ctdb_ifaces | tr ' ' '\n' | sort -u)
47 }
48
49 monitor_interfaces()
50 {
51         get_all_interfaces
52
53         fail=false
54         up_interfaces_found=false
55
56         for iface in $all_interfaces ; do
57
58             ip addr show $iface 2>/dev/null >/dev/null || {
59                 echo "WARNING: Interface $iface does not exist but it is used by public addresses."
60                 continue
61             }
62
63             # These interfaces are sometimes bond devices
64             # When we use VLANs for bond interfaces, there will only
65             # be an entry in /proc for the underlying real interface
66             realiface=`echo $iface |sed -e 's/\..*$//'`
67             bi=$(get_proc "net/bonding/$realiface" 2>/dev/null) && {
68                 echo "$bi" | grep -q 'Currently Active Slave: None' && {
69                         echo "ERROR: No active slaves for bond device $realiface"
70                         mark_down $iface
71                         continue
72                 }
73                 echo "$bi" | grep -q '^MII Status: up' || {
74                         echo "ERROR: public network interface $realiface is down"
75                         mark_down $iface
76                         continue
77                 }
78                 echo "$bi" | grep -q '^Bonding Mode: IEEE 802.3ad Dynamic link aggregation' && {
79                         # This works around a bug in the driver where the
80                         # overall bond status can be up but none of the actual
81                         # physical interfaces have a link.
82                         echo "$bi" | grep 'MII Status:' | tail -n +2 | grep -q '^MII Status: up' || {
83                                 echo "ERROR: No active slaves for 802.ad bond device $realiface"
84                                 mark_down $iface
85                                 continue
86                         }
87                 }
88                 mark_up $iface
89                 continue
90             }
91
92             case $iface in
93             lo*)
94                 # loopback is always working
95                 mark_up $iface
96                 ;;
97             ib*)
98                 # we dont know how to test ib links
99                 mark_up $iface
100                 ;;
101             *)
102                 [ -z "$iface" ] || {
103                     [ "$(basename $(readlink /sys/class/net/$iface/device/driver) 2>/dev/null)" = virtio_net ] ||
104                     ethtool $iface | grep -q 'Link detected: yes' || {
105                         # On some systems, this is not successful when a
106                         # cable is plugged but the interface has not been
107                         # brought up previously. Bring the interface up and
108                         # try again...
109                         ip link set $iface up
110                         ethtool $iface | grep -q 'Link detected: yes' || {
111                             echo "ERROR: No link on the public network interface $iface"
112                             mark_down $iface
113                             continue
114                         }
115                     }
116                     mark_up $iface
117                 }
118                 ;;
119             esac
120
121         done
122
123         $fail || return 0
124
125         $up_interfaces_found && \
126             [ "$CTDB_PARTIALLY_ONLINE_INTERFACES" = "yes" ] && \
127             return 0
128
129         return 1
130 }
131
132 ctdb_check_args "$@"
133
134 case "$1" in 
135      #############################
136      # called when ctdbd starts up
137      init)
138         # make sure that we only respond to ARP messages from the NIC where
139         # a particular ip address is associated.
140         get_proc sys/net/ipv4/conf/all/arp_filter >/dev/null 2>&1 && {
141             set_proc sys/net/ipv4/conf/all/arp_filter 1
142         }
143         ;;
144
145      #############################
146      # called after ctdbd has done its initial recovery
147      # and we start the services to become healthy
148      startup)
149         # Assume all links are good initially
150         get_all_interfaces
151         for iface in $all_interfaces ; do
152                 ctdb setifacelink $iface up >/dev/null 2>/dev/null
153         done
154         
155         monitor_interfaces
156
157         ;;
158
159
160      ################################################
161      # called when ctdbd wants to claim an IP address
162      takeip)
163         iface=$2
164         ip=$3
165         maskbits=$4
166
167         add_ip_to_iface $iface $ip $maskbits || {
168                 exit 1;
169         }
170
171         # cope with the script being killed while we have the interface blocked
172         iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
173
174         # flush our route cache
175         set_proc sys/net/ipv4/route/flush 1
176         ;;
177
178
179      ##################################################
180      # called when ctdbd wants to release an IP address
181      releaseip)
182         # releasing an IP is a bit more complex than it seems. Once the IP
183         # is released, any open tcp connections to that IP on this host will end
184         # up being stuck. Some of them (such as NFS connections) will be unkillable
185         # so we need to use the killtcp ctdb function to kill them off. We also
186         # need to make sure that no new connections get established while we are 
187         # doing this! So what we do is this:
188         # 1) firewall this IP, so no new external packets arrive for it
189         # 2) use netstat -tn to find existing connections, and kill them 
190         # 3) remove the IP from the interface
191         # 4) remove the firewall rule
192         iface=$2
193         ip=$3
194         maskbits=$4
195
196         failed=0
197         # we do an extra delete to cope with the script being killed
198         iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
199         iptables -I INPUT -i $iface -d $ip -j DROP
200         kill_tcp_connections $ip
201
202         delete_ip_from_iface $iface $ip $maskbits || {
203                 iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
204                 exit 1;
205         }
206
207         iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
208
209         # flush our route cache
210         set_proc sys/net/ipv4/route/flush 1
211         ;;
212
213      ##################################################
214      # called when ctdbd wants to update an IP address
215      updateip)
216         # moving an IP is a bit more complex than it seems.
217         # First we drop all traffic on the old interface.
218         # Then we try to add the ip to the new interface and before
219         # we finally remove it from the old interface.
220         #
221         # 1) firewall this IP, so no new external packets arrive for it
222         # 2) add the IP to the new interface
223         # 3) remove the IP from the old interface
224         # 4) remove the firewall rule
225         # 5) use ctdb gratiousarp to propagate the new mac address
226         # 6) use netstat -tn to find existing connections, and tickle them
227         oiface=$2
228         niface=$3
229         ip=$4
230         maskbits=$5
231
232         failed=0
233         # we do an extra delete to cope with the script being killed
234         iptables -D INPUT -i $oiface -d $ip -j DROP 2> /dev/null
235         iptables -I INPUT -i $oiface -d $ip -j DROP
236
237         delete_ip_from_iface $oiface $ip $maskbits 2>/dev/null
238         delete_ip_from_iface $niface $ip $maskbits 2>/dev/null
239
240         add_ip_to_iface $niface $ip $maskbits || {
241                 iptables -D INPUT -i $oiface -d $ip -j DROP 2> /dev/null
242                 exit 1;
243         }
244
245         # cope with the script being killed while we have the interface blocked
246         iptables -D INPUT -i $oiface -d $ip -j DROP 2> /dev/null
247
248         # flush our route cache
249         set_proc sys/net/ipv4/route/flush 1
250
251         # propagate the new mac address
252         ctdb gratiousarp $ip $niface
253
254         # tickle all existing connections, so that dropped packets
255         # are retransmited and the tcp streams work
256
257         tickle_tcp_connections $ip
258
259         ;;
260
261
262      ###########################################
263      # called when ctdbd has finished a recovery
264      recovered)
265         ;;
266
267      ####################################
268      # called when ctdbd is shutting down
269      shutdown)
270         ;;
271
272      monitor)
273         monitor_interfaces || exit 1
274         ;;
275     *)
276         ctdb_standard_event_handler "$@"
277         ;;
278 esac
279
280 exit 0
281