More untested eventscript factorisation.
[metze/ctdb/wip.git] / 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         echo "No public addresses file found. Nothing to do for 10.interfaces"
17         exit 0
18 }
19
20 case $cmd in 
21      #############################
22      # called when ctdbd starts up
23      startup)
24         # make sure that we only respond to ARP messages from the NIC where
25         # a particular ip address is associated.
26         [ -f /proc/sys/net/ipv4/conf/all/arp_filter ] && {
27             echo 1 > /proc/sys/net/ipv4/conf/all/arp_filter
28         }
29         cat "$CTDB_PUBLIC_ADDRESSES" | cut -d/ -f1 | while read _IP; do
30                 _IP_HELD=`/sbin/ip addr show | grep "inet $_IP/"`
31                 [ -z "$_IP_HELD" ] || {
32                         _IFACE=`echo $_IP_HELD | sed -e "s/.*\s//"`
33                         _NM=`echo $_IP_HELD | sed -e "s/.*$_IP\///" -e "s/\s.*//"`
34                         echo "Removing public address $_IP/$_NM from device $_IFACE"
35                         /sbin/ip addr del $_IP/$_NM dev $_IFACE
36                 }
37         done
38         ;;
39
40
41      ################################################
42      # called when ctdbd wants to claim an IP address
43      takeip)
44         if [ $# != 3 ]; then
45            echo "must supply interface, IP and maskbits"
46            exit 1
47         fi
48         iface=$1
49         ip=$2
50         maskbits=$3
51
52         # we make sure the interface is up first
53         /sbin/ip link set $iface up || {
54                  echo "Failed to bringup interface $iface"
55                  exit 1
56         }
57         /sbin/ip addr add $ip/$maskbits brd + dev $iface || {
58                  echo "Failed to add $ip/$maskbits on dev $iface"
59         }
60         # cope with the script being killed while we have the interface blocked
61         iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
62
63         # flush our route cache
64         echo 1 > /proc/sys/net/ipv4/route/flush
65         ;;
66
67
68      ##################################################
69      # called when ctdbd wants to release an IP address
70      releaseip)
71         if [ $# != 3 ]; then
72            echo "must supply interface, IP and maskbits"
73            exit 1
74         fi
75
76         # releasing an IP is a bit more complex than it seems. Once the IP
77         # is released, any open tcp connections to that IP on this host will end
78         # up being stuck. Some of them (such as NFS connections) will be unkillable
79         # so we need to use the killtcp ctdb function to kill them off. We also
80         # need to make sure that no new connections get established while we are 
81         # doing this! So what we do is this:
82         # 1) firewall this IP, so no new external packets arrive for it
83         # 2) use netstat -tn to find existing connections, and kill them 
84         # 3) remove the IP from the interface
85         # 4) remove the firewall rule
86         iface=$1
87         ip=$2
88         maskbits=$3
89
90         failed=0
91         # we do an extra delete to cope with the script being killed
92         iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
93         iptables -I INPUT -i $iface -d $ip -j DROP
94         kill_tcp_connections $ip
95
96         # the ip tool will delete all secondary IPs if this is the primary. To work around
97         # this _very_ annoying behaviour we have to keep a record of the secondaries and re-add
98         # them afterwards. yuck
99         secondaries=""
100         if /sbin/ip addr list dev $iface primary | grep -q "inet $ip/$maskbits " ; then
101             secondaries=`/sbin/ip addr list dev $iface secondary | grep " inet " | awk '{print $2}'`
102         fi
103         /sbin/ip addr del $ip/$maskbits dev $iface || failed=1
104         [ -z "$secondaries" ] || {
105             for i in $secondaries; do
106                 if /sbin/ip addr list dev $iface | grep -q "inet $i" ; then
107                     echo "kept secondary $i on dev $iface"
108                 else 
109                     echo "re-adding secondary address $i to dev $iface"
110                     /sbin/ip addr add $i dev $iface || failed=1         
111                 fi
112             done
113         }
114         iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
115         [ $failed = 0 ] || {
116                  echo "Failed to del $ip on dev $iface"
117                  exit 1
118         }
119
120         # flush our route cache
121         echo 1 > /proc/sys/net/ipv4/route/flush
122         ;;
123
124
125      ###########################################
126      # called when ctdbd has finished a recovery
127      recovered)
128         ;;
129
130      ####################################
131      # called when ctdbd is shutting down
132      shutdown)
133         ;;
134
135      monitor)
136         INTERFACES=`cat $CTDB_PUBLIC_ADDRESSES | 
137                 sed -e "s/^[^\t ]*[\t ]*//" -e "s/[\t ]*$//"`
138
139         [ "$CTDB_PUBLIC_INTERFACE" ] && INTERFACES="$CTDB_PUBLIC_INTERFACE $INTERFACES"
140
141         INTERFACES=`for IFACE in $INTERFACES ; do echo $IFACE ; done | sort | uniq`
142
143         for IFACE in $INTERFACES ; do
144             case $IFACE in 
145             ethX*|bond*)
146                 IFACE=`echo $IFACE |sed -e 's/\....$//'`
147                 grep -q 'Currently Active Slave: None' /proc/net/bonding/$IFACE && {
148                         echo "ERROR: No active slaves for bond device $IFACE"
149                         exit 1
150                 }
151                 grep -q '^MII Status: up' /proc/net/bonding/$IFACE || {
152                         echo "ERROR: public network interface $IFACE is down"
153                         exit 1
154                 }
155                 ;;
156             ib*)
157                 # we dont know how to test ib links
158                 ;;
159             *)
160                 [ -z "$IFACE" ] || {
161                     /usr/sbin/ethtool $IFACE | grep -q 'Link detected: yes' || {
162                         # On some systems, this is not successful when a
163                         # cable is plugged but the interface has not been
164                         # brought up previously. Bring the interface up and
165                         # try again...
166                         /sbin/ip link set $IFACE up
167                         /usr/sbin/ethtool $IFACE | grep -q 'Link detected: yes' || {
168                             echo "ERROR: No link on the public network interface $IFACE"
169                             exit 1
170                         }
171                     }
172                 }
173                 ;;
174             esac
175         done
176         ;;
177     status)
178         ctdb_checkstatus || exit $?
179         ;;
180 esac
181
182 exit 0
183