Subversion Repositories ALCASAR

Rev

Rev 900 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 900 Rev 904
1
#!/bin/bash
1
#!/bin/bash
2
# $Id: alcasar-load_balancing.sh 900 2012-06-21 16:15:27Z franck $
2
# $Id: alcasar-load_balancing.sh 904 2012-06-23 11:35:25Z franck $
3
 
3
 
4
# alcasar-load_balancing.sh
4
# alcasar-load_balancing.sh
5
# by BOUIJOUX Franck (3abTux) <3abtux@free.fr>
5
# by BOUIJOUX Franck (3abTux) <3abtux@free.fr>
6
# This script is distributed under the Gnu General Public License (GPL)
6
# This script is distributed under the Gnu General Public License (GPL)
7
 
7
 
8
# Équilibrage de charge sur 2 ou plusieurs liens internet (en cours d'expérimentation) 
8
# Équilibrage de charge sur 2 ou plusieurs liens internet (en cours d'expérimentation) 
9
# Définion des poids des routes   :  même poids --> alternance des connexions. Sinon le poids le plus faible est prioritaire
9
# Définion des poids des routes   :  même poids --> alternance des connexions. Sinon le poids le plus faible est prioritaire
10
# Load balancing with 2 or more Internet links (experimental)
10
# Load balancing with 2 or more Internet links (experimental)
11
# Weitght of routes : same --> switch between link. The lowest weight gain the priority.
11
# Weitght of routes : same --> switch between link. The lowest weight gain the priority.
12
 
12
 
-
 
13
EXTIF="eth0"
-
 
14
IPADDR1="192.168.2.10/24"
-
 
15
NETMASK1=`ipcalc -m 192.168.182.2 | cut -d"=" -f2`
-
 
16
GW1="192.168.2.1"
-
 
17
WEIGHT1=1
-
 
18
 
-
 
19
echo $IPADDR1
-
 
20
echo $NETMASK1
-
 
21
echo $GW1
-
 
22
exit 0
-
 
23
 
-
 
24
create_new_interface() {
-
 
25
		if [ "$response" = "n" ] || [ "$response" = "N" ]
-
 
26
		then
-
 
27
			PRIVATE_IP_MASK="0"
-
 
28
			PTN='^\([01]\?[[:digit:]][[:digit:]]\?\|2[0-4][[:digit:]]\|25[0-5]\).\([01]\?[[:digit:]][[:digit:]]\?\|2[0-4][[:digit:]]\|25[0-5]\).\([01]\?[[:digit:]][[:digit:]]\?\|2[0-4][[:digit:]]\|25[0-5]\).\([01]\?[[:digit:]][[:digit:]]\?\|2[0-4][[:digit:]]\|25[0-5]\)/[012]\?[[:digit:]]$'
-
 
29
			until [[ $(expr $PRIVATE_IP_MASK : $PTN) -gt 0 ]]
-
 
30
			do
-
 
31
				if [ $Lang == "fr" ]
-
 
32
					then echo -n "Entrez l'adresse IP d'ALCASAR au format CIDR (a.b.c.d/xx) : "
-
 
33
					else echo -n "Enter ALCASAR IP address in CIDR format (a.b.c.d/xx) : "
-
 
34
				fi
-
 
35
				read PRIVATE_IP_MASK
-
 
36
			done
-
 
37
		else
-
 
38
       			PRIVATE_IP_MASK=$DEFAULT_PRIVATE_IP_MASK
-
 
39
		fi
-
 
40
	else
-
 
41
		PRIVATE_IP_MASK=`grep PRIVATE_IP conf/etc/alcasar.conf|cut -d"=" -f2` 
-
 
42
		rm -rf conf/etc/alcasar.conf
-
 
43
	fi
-
 
44
# Define LAN side global parameters
-
 
45
	hostname $HOSTNAME
-
 
46
	PRIVATE_NETWORK=`/bin/ipcalc -n $PRIVATE_IP_MASK | cut -d"=" -f2`			# private network address (ie.: 192.168.182.0)
-
 
47
	PRIVATE_NETMASK=`/bin/ipcalc -m $PRIVATE_IP_MASK | cut -d"=" -f2`			# private network mask (ie.: 255.255.255.0)
-
 
48
	PRIVATE_IP=`echo $PRIVATE_IP_MASK | cut -d"/" -f1`					# ALCASAR private ip address (consultation LAN side)
-
 
49
	PRIVATE_PREFIX=`/bin/ipcalc -p $PRIVATE_IP_MASK |cut -d"=" -f2`				# network prefix (ie. 24)
-
 
50
	PRIVATE_NETWORK_MASK=$PRIVATE_NETWORK/$PRIVATE_PREFIX					# ie.: 192.168.182.0/24
-
 
51
	classe=$((PRIVATE_PREFIX/8)); classe_sup=`expr $classe + 1`; classe_sup_sup=`expr $classe + 2`		# ie.: 2=classe B, 3=classe C
-
 
52
	PRIVATE_NETWORK_SHORT=`echo $PRIVATE_NETWORK | cut -d"." -f1-$classe`.			# compatibility with hosts.allow et hosts.deny (ie.: 192.168.182.)
-
 
53
	PRIVATE_BROADCAST=`/bin/ipcalc -b $PRIVATE_NETWORK_MASK | cut -d"=" -f2`		# private network broadcast (ie.: 192.168.182.255)
-
 
54
	private_network_ending=`echo $PRIVATE_NETWORK | cut -d"." -f$classe_sup`		# last octet of LAN address
-
 
55
	private_broadcast_ending=`echo $PRIVATE_BROADCAST | cut -d"." -f$classe_sup`		# last octet of LAN broadcast
-
 
56
	PRIVATE_FIRST_IP=`echo $PRIVATE_NETWORK | cut -d"." -f1-3`"."`expr $private_network_ending + 1`		# First network address (ex.: 192.168.182.1)
-
 
57
	PRIVATE_LAST_IP=`echo $PRIVATE_BROADCAST | cut -d"." -f1-3`"."`expr $private_broadcast_ending - 1`	# last network address (ex.: 192.168.182.254)
-
 
58
 
-
 
59
} # end create_new_interface
-
 
60
 
-
 
61
 
-
 
62
 
-
 
63
 
-
 
64
 
-
 
65
X=1
-
 
66
nbr=3		# nbr interface réseau accédant à l'internet (comprenant celle par défaut eth0)
-
 
67
while [ $X -lt $nbr ]
-
 
68
do
-
 
69
  echo $X
-
 
70
	cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-$EXTIF:${X}
-
 
71
DEVICE=$EXTIF:${X}
-
 
72
BOOTPROTO=static
-
 
73
IPADDR=$IPADDR1
-
 
74
NETMASK=$NETMASK-${X}
-
 
75
ONBOOT=yes
-
 
76
METRIC=10
-
 
77
NOZEROCONF=yes
-
 
78
MII_NOT_SUPPORTED=yes
-
 
79
IPV6INIT=no
-
 
80
IPV6TO4INIT=no
-
 
81
ACCOUNTING=no
-
 
82
USERCTL=no
-
 
83
EOF
-
 
84
 
-
 
85
X=$(( $X + 1))
-
 
86
done
-
 
87
 
-
 
88
exit 0
-
 
89
 
13
WEIGHT1=1
90
WEIGHT1=1
14
WEIGHT2=1
91
WEIGHT2=1
15
#WEIGHT3=3
92
#WEIGHT3=3
16
 
93
 
17
# Définition des interfaces :
94
# Définition des interfaces :
18
DEV1=${1-eth0}  # defaut eth0
95
DEV1=${1-eth0}  # defaut eth0
19
DEV2=${2-eth0:1}  # defaut eth0:1 mais peut être autre chose :-)
96
DEV2=${2-eth0:1}  # defaut eth0:1 mais peut être autre chose :-)
20
#DEV3=${3-eth0:2}  # defaut eth0:2 mais peut être autre chose :-)
97
#DEV3=${3-eth0:2}  # defaut eth0:2 mais peut être autre chose :-)
21
 
98
 
22
# Trouver les adresses pour chaque interface
99
# Trouver les adresses pour chaque interface
23
IP1=`ifconfig $DEV1 | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`
100
IP1=`ifconfig $DEV1 | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`
24
IP2=`ifconfig $DEV2 | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`
101
IP2=`ifconfig $DEV2 | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`
25
#IP3=`ifconfig $DEV3 | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`
102
#IP3=`ifconfig $DEV3 | grep inet | awk '{ print $2 }' | awk -F: '{ print $2 }'`
26
 
103
 
27
# Trouver les passerelles pour chaque interface { ne fonctionne pas bien avec une seule interface } --> forcer les passerelles !
104
# Trouver les passerelles pour chaque interface { ne fonctionne pas bien avec une seule interface } --> forcer les passerelles !
28
GW1=192.168.1.1
105
GW1=192.168.1.1
29
GW2=192.168.2.6
106
GW2=192.168.2.6
30
#GW3=192.168.3.1
107
#GW3=192.168.3.1
31
 
108
 
32
echo "Acces internet depuis $DEV1: IP=$IP1  par la Gateway=$GW1"
109
echo "Acces internet depuis $DEV1: IP=$IP1  par la Gateway=$GW1"
33
echo "            et depuis $DEV2: IP=$IP2  par la Gateway=$GW2"
110
echo "            et depuis $DEV2: IP=$IP2  par la Gateway=$GW2"
34
#echo "            et depuis $DEV3: IP=$IP3  par la GW=$GW3"
111
#echo "            et depuis $DEV3: IP=$IP3  par la GW=$GW3"
35
 
112
 
36
#############################
113
#############################
37
## Mise en place des routes 
114
## Mise en place des routes 
38
 
115
 
39
# Tester si les tables existent sinon les créer
116
# Tester si les tables existent sinon les créer
40
if [ -z "`cat /etc/iproute2/rt_tables | grep '^252'`" ] ; then
117
if [ -z "`cat /etc/iproute2/rt_tables | grep '^252'`" ] ; then
41
        echo "252       rt_dev1" >> /etc/iproute2/rt_tables
118
        echo "252       rt_dev1" >> /etc/iproute2/rt_tables
42
fi
119
fi
43
 
120
 
44
if [ -z "`cat /etc/iproute2/rt_tables | grep '^251'`" ] ; then
121
if [ -z "`cat /etc/iproute2/rt_tables | grep '^251'`" ] ; then
45
        echo "251       rt_dev2" >> /etc/iproute2/rt_tables
122
        echo "251       rt_dev2" >> /etc/iproute2/rt_tables
46
fi
123
fi
47
#if [ -z "`cat /etc/iproute2/rt_tables | grep '^250'`" ] ; then
124
#if [ -z "`cat /etc/iproute2/rt_tables | grep '^250'`" ] ; then
48
#       echo "250       rt_dev3" >> /etc/iproute2/rt_tables
125
#       echo "250       rt_dev3" >> /etc/iproute2/rt_tables
49
#fi
126
#fi
50
 
127
 
51
# Tables de routage
128
# Tables de routage
52
ip route add default via $GW1 table rt_dev1
129
ip route add default via $GW1 table rt_dev1
53
ip route add default via $GW2 table rt_dev2
130
ip route add default via $GW2 table rt_dev2
54
#ip route add default via $GW3 table rt_dev3
131
#ip route add default via $GW3 table rt_dev3
55
 
132
 
56
# Création des règles
133
# Création des règles
57
ip rule add from $IP1 table rt_dev1
134
ip rule add from $IP1 table rt_dev1
58
ip rule add from $IP2 table rt_dev2
135
ip rule add from $IP2 table rt_dev2
59
#ip rule add from $IP3 table rt_dev3
136
#ip rule add from $IP3 table rt_dev3
60
 
137
 
61
# Effacer la route par défaut existante
138
# Effacer la route par défaut existante
62
ip route del default
139
ip route del default
63
if [ ! -z "`ip route show table main | grep 'nexthop'`" ] ; then
140
if [ ! -z "`ip route show table main | grep 'nexthop'`" ] ; then
64
        ip route del default scope global
141
        ip route del default scope global
65
fi
142
fi
66
 
143
 
67
# Alterne les liens basés sur chaque route
144
# Alterne les liens basés sur chaque route
68
ip route add default scope global nexthop via $GW1 dev $DEV1 weight $WEIGHT1 \
145
ip route add default scope global nexthop via $GW1 dev $DEV1 weight $WEIGHT1 \
69
                                nexthop via $GW2 dev $DEV2 weight $WEIGHT2
146
                                nexthop via $GW2 dev $DEV2 weight $WEIGHT2
70
#                               nexthop via $GW3 dev $DEV3 weight $WEIGHT3 
147
#                               nexthop via $GW3 dev $DEV3 weight $WEIGHT3 
71
 
148
 
72
 
149
 
73
# Purge le cache
150
# Purge le cache
74
ip route flush cache
151
ip route flush cache
75
 
152
 
76
# Fin de alcasar-load_balancing.sh
153
# Fin de alcasar-load_balancing.sh
77
 
154
 
78

Generated by GNU Enscript 1.6.6.
155

Generated by GNU Enscript 1.6.6.
79
 
156
 
80
 
157
 
81
 
158