71274c56783b6f09a1b18d37a65fe9733753c560
[sahlberg/ctdb.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 "$1" in 
21      #############################
22      # called when ctdbd starts up
23      init)
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 [ $# != 4 ]; then
45            echo "must supply interface, IP and maskbits"
46            exit 1
47         fi
48         iface=$2
49         ip=$3
50         maskbits=$4
51
52         add_ip_to_iface $iface $ip $maskbits || {
53                 exit 1;
54         }
55
56         # cope with the script being killed while we have the interface blocked
57         iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
58
59         # flush our route cache
60         echo 1 > /proc/sys/net/ipv4/route/flush
61         ;;
62
63
64      ##################################################
65      # called when ctdbd wants to release an IP address
66      releaseip)
67         if [ $# != 4 ]; then
68            echo "must supply interface, IP and maskbits"
69            exit 1
70         fi
71
72         # releasing an IP is a bit more complex than it seems. Once the IP
73         # is released, any open tcp connections to that IP on this host will end
74         # up being stuck. Some of them (such as NFS connections) will be unkillable
75         # so we need to use the killtcp ctdb function to kill them off. We also
76         # need to make sure that no new connections get established while we are 
77         # doing this! So what we do is this:
78         # 1) firewall this IP, so no new external packets arrive for it
79         # 2) use netstat -tn to find existing connections, and kill them 
80         # 3) remove the IP from the interface
81         # 4) remove the firewall rule
82         iface=$2
83         ip=$3
84         maskbits=$4
85
86         failed=0
87         # we do an extra delete to cope with the script being killed
88         iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
89         iptables -I INPUT -i $iface -d $ip -j DROP
90         kill_tcp_connections $ip
91
92         delete_ip_from_iface $iface $ip $maskbits || {
93                 iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
94                 exit 1;
95         }
96
97         iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
98
99         # flush our route cache
100         echo 1 > /proc/sys/net/ipv4/route/flush
101         ;;
102
103
104      ###########################################
105      # called when ctdbd has finished a recovery
106      recovered)
107         ;;
108
109      ####################################
110      # called when ctdbd is shutting down
111      shutdown)
112         ;;
113
114      monitor)
115         INTERFACES=`cat $CTDB_PUBLIC_ADDRESSES | 
116                 sed -e "s/^[^\t ]*[\t ]*//" -e "s/[\t ]*$//"`
117
118         [ "$CTDB_PUBLIC_INTERFACE" ] && INTERFACES="$CTDB_PUBLIC_INTERFACE $INTERFACES"
119         [ "$CTDB_NATGW_PUBLIC_IFACE" ] && INTERFACES="$CTDB_NATGW_PUBLIC_IFACE $INTERFACES"
120
121         INTERFACES=`for IFACE in $INTERFACES ; do echo $IFACE ; done | sort | uniq`
122
123         for IFACE in $INTERFACES ; do
124             # These interfaces are sometimes bond devices
125             # When we use VLANs for bond interfaces, there will only
126             # be an entry in /proc for the underlying real interface
127             REALIFACE=`echo $IFACE |sed -e 's/\..*$//'`
128             [ -f /proc/net/bonding/$REALIFACE ] && {
129                 grep -q 'Currently Active Slave: None' /proc/net/bonding/$REALIFACE && {
130                         echo "ERROR: No active slaves for bond device $REALIFACE"
131                         exit 1
132                 }
133                 grep -q '^MII Status: up' /proc/net/bonding/$REALIFACE || {
134                         echo "ERROR: public network interface $REALIFACE is down"
135                         exit 1
136                 }
137                 exit 0;
138             }
139
140             case $IFACE in 
141             ib*)
142                 # we dont know how to test ib links
143                 ;;
144             *)
145                 [ -z "$IFACE" ] || {
146                     /usr/sbin/ethtool $IFACE | grep -q 'Link detected: yes' || {
147                         # On some systems, this is not successful when a
148                         # cable is plugged but the interface has not been
149                         # brought up previously. Bring the interface up and
150                         # try again...
151                         /sbin/ip link set $IFACE up
152                         /usr/sbin/ethtool $IFACE | grep -q 'Link detected: yes' || {
153                             echo "ERROR: No link on the public network interface $IFACE"
154                             exit 1
155                         }
156                     }
157                 }
158                 ;;
159             esac
160         done
161         ;;
162     *)
163         ctdb_standard_event_handler "$@"
164         ;;
165 esac
166
167 exit 0
168