Subversion Repositories ALCASAR

Rev

Rev 1078 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 1078 Rev 1469
Line 1... Line 1...
1
#!/bin/bash
1
#!/bin/bash
2
# $Id: alcasar-load_balancing.sh 1078 2013-05-02 16:40:54Z franck $
2
# $Id: alcasar-load_balancing.sh 1469 2014-10-30 21:58:47Z richard $
3
 
3
 
4
# Generic Load balancer for multiple WAN links - version 1.1 (04 Feb 2011)
4
# Generic Load balancer for multiple WAN links - version 1.1 (04 Feb 2011)
5
# (c) 2011 Pau Oliva Fora - http://pof.eslack.org
5
# (c) 2011 Pau Oliva Fora - http://pof.eslack.org
6
#
6
#
7
# Licensed under GPLv3 - for full terms see:
7
# Licensed under GPLv3 - for full terms see:
Line 31... Line 31...
31
CONF_FILE="$DIR_ETC/alcasar.conf"
31
CONF_FILE="$DIR_ETC/alcasar.conf"
32
MULTIWAN=`grep MULTIWAN= $CONF_FILE|cut -d"=" -f2`
32
MULTIWAN=`grep MULTIWAN= $CONF_FILE|cut -d"=" -f2`
33
MULTIWAN=${MULTIWAN:=off}
33
MULTIWAN=${MULTIWAN:=off}
34
FAILOVER=`grep FAILOVER= $CONF_FILE|cut -d"=" -f2`
34
FAILOVER=`grep FAILOVER= $CONF_FILE|cut -d"=" -f2`
35
FAILOVER=${FAILOVER:=30}
35
FAILOVER=${FAILOVER:=30}
-
 
36
INTIF=`grep ^INTIF= $CONF_FILE|cut -d"=" -f2`				# INTernal InterFace
36
 
37
 
37
 
38
 
38
# space separated list of public IPs to ping in watchdog mode
39
# space separated list of public IPs to ping in watchdog mode
39
# set this to some public ip addresses pingable and always on.
40
# set this to some public ip addresses pingable and always on.
40
TESTIPS="8.8.8.8 192.0.32.10"
41
TESTIPS="8.8.8.8 192.0.32.10"
Line 66... Line 67...
66
		WT=`grep "$INT=" $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $5}'`		# WEIGHT
67
		WT=`grep "$INT=" $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $5}'`		# WEIGHT
67
		WT=${WT:-1}
68
		WT=${WT:-1}
68
		IP=`grep "$INT=" $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $3}' | cut -d"/" -f1`	# @IP
69
		IP=`grep "$INT=" $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $3}' | cut -d"/" -f1`	# @IP
69
 
70
 
70
		if [ $i -ne 0 ]; then
71
		if [ $i -ne 0 ]; then
71
			[ -e /etc/sysconfig/network-scripts/ifcfg-eth0:$i ] && ifdown eth0:$i && rm -f /etc/sysconfig/network-scripts/ifcfg-eth0:$i
72
			[ -e /etc/sysconfig/network-scripts/ifcfg-$INTIF:$i ] && ifdown $INTIF:$i && rm -f /etc/sysconfig/network-scripts/ifcfg-$INTIF:$i
72
			IFACE=`grep "$INT=" $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $2}'`	# IFACE
73
			IFACE=`grep "$INT=" $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $2}'`	# IFACE
73
			IP_NET=`grep "^$INT=" $CONF_FILE | awk -F'"' '{print $2}' | awk -F, '{ print $3}'`	# IP
74
			IP_NET=`grep "^$INT=" $CONF_FILE | awk -F'"' '{print $2}' | awk -F, '{ print $3}'`	# IP
74
			NET="`ipcalc -n $IP_NET | cut -d"=" -f2`/`ipcalc -p $IP_NET|cut -d"=" -f2`"
75
			NET="`ipcalc -n $IP_NET | cut -d"=" -f2`/`ipcalc -p $IP_NET|cut -d"=" -f2`"
75
			GW=`grep "$INT=" $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $4}'`		# @GW
76
			GW=`grep "$INT=" $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $4}'`		# @GW
76
			MTU=`grep "$INT=" $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $6}'`	# MTU
77
			MTU=`grep "$INT=" $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $6}'`	# MTU
77
 
78
 
78
			# Config eth0:$i (Internet)
79
			# Config $INTIF:$i (Internet)
79
			cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-eth0:$i
80
			cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-$INTIF:$i
80
DEVICE=$IFACE
81
DEVICE=$IFACE
81
BOOTPROTO=static
82
BOOTPROTO=static
82
IPADDR=`echo $IP | cut -d"/" -f1`
83
IPADDR=`echo $IP | cut -d"/" -f1`
83
NETMASK=`ipcalc -m $IP_NET | cut -d= -f2`
84
NETMASK=`ipcalc -m $IP_NET | cut -d= -f2`
84
NETWORK=`ipcalc -n $IP_NET | cut -d= -f2`
85
NETWORK=`ipcalc -n $IP_NET | cut -d= -f2`
Line 89... Line 90...
89
IPV6INIT=no
90
IPV6INIT=no
90
IPV6TO4INIT=no
91
IPV6TO4INIT=no
91
ACCOUNTING=no
92
ACCOUNTING=no
92
USERCTL=no
93
USERCTL=no
93
EOF
94
EOF
94
			echo "ifup eth0:$i"
95
			echo "ifup $INTIF:$i"
95
			ifup eth0:$i
96
			ifup $INTIF:$i
96
			NET="`ipcalc -n $IP_NET | cut -d"=" -f2`/`ipcalc -p $IP_NET|cut -d"=" -f2`"
97
			NET="`ipcalc -n $IP_NET | cut -d"=" -f2`/`ipcalc -p $IP_NET|cut -d"=" -f2`"
97
		else
98
		else
98
			IFACE="eth0"
99
			IFACE="$INTIF"
99
			IP_NET=`grep "^PUBLIC_IP=" $CONF_FILE | awk -F'=' '{print $2}'`			# IP/MSK
100
			IP_NET=`grep "^PUBLIC_IP=" $CONF_FILE | awk -F'=' '{print $2}'`			# IP/MSK
100
			IP=`grep "^PUBLIC_IP=" $CONF_FILE | awk -F= '{ print $2 }' | cut -d"/" -f1`	# @IP
101
			IP=`grep "^PUBLIC_IP=" $CONF_FILE | awk -F= '{ print $2 }' | cut -d"/" -f1`	# @IP
101
			GW=`grep "^GW=" $CONF_FILE | awk -F= '{print $2}'`				# @GW
102
			GW=`grep "^GW=" $CONF_FILE | awk -F= '{print $2}'`				# @GW
102
#			MTU=`grep "^PUBLIC_MTU=" $CONF_FILE | awk -F= '{print $2}'`			# MTU
103
#			MTU=`grep "^PUBLIC_MTU=" $CONF_FILE | awk -F= '{print $2}'`			# MTU
103
		fi # End
104
		fi # End
Line 137... Line 138...
137
 
138
 
138
###########################
139
###########################
139
# Fonction virtual Interfaces deleting
140
# Fonction virtual Interfaces deleting
140
###########################
141
###########################
141
delete_eth () {
142
delete_eth () {
142
	IFACE_COUNT=`ls -l /etc/sysconfig/network-scripts/ifcfg-eth0:* | wc -l`
143
	IFACE_COUNT=`ls -l /etc/sysconfig/network-scripts/ifcfg-$INTIF:* | wc -l`
143
	echo $IFACE_COUNT
144
	echo $IFACE_COUNT
144
	while [ $IFACE_COUNT -ne 0 ]
145
	while [ $IFACE_COUNT -ne 0 ]
145
	do
146
	do
146
		i=$IFACE_COUNT	
147
		i=$IFACE_COUNT	
147
		echo "ifdown eth0:$i"
148
		echo "ifdown $INTIF:$i"
148
		ifdown eth0:$i
149
		ifdown $INTIF:$i
149
		rm -f /etc/sysconfig/network-scripts/ifcfg-eth0:$i
150
		rm -f /etc/sysconfig/network-scripts/ifcfg-$INTIF:$i
150
		IFACE_COUNT=$(($IFACE_COUNT - 1))
151
		IFACE_COUNT=$(($IFACE_COUNT - 1))
151
	done
152
	done
152
	ip route del default scope global
153
	ip route del default scope global
153
#	ip route add default gw 192.168.1.1
154
#	ip route add default gw 192.168.1.1
154
}
155
}
Line 180... Line 181...
180
	
181
	
181
	DOWNCOUNT_BAK=0
182
	DOWNCOUNT_BAK=0
182
	DOWN_BAK=""
183
	DOWN_BAK=""
183
	NBIFACE=`grep "^WAN" $CONF_FILE | wc -l`	# Nbre interfaces virtuelles
184
	NBIFACE=`grep "^WAN" $CONF_FILE | wc -l`	# Nbre interfaces virtuelles
184
	echo "Nombre interfaces =  "$NBIFACE
185
	echo "Nombre interfaces =  "$NBIFACE
185
	WANIFACE[0]="eth0"	# eth0 par défaut
186
	WANIFACE[0]="$INTIF"
186
	c=0
187
	c=0
187
	while [ $c -le $NBIFACE ]; do
188
	while [ $c -le $NBIFACE ]; do
188
		ITH=(`grep "WAN$c=" $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $2}'`)	# IFACE
189
		ITH=(`grep "WAN$c=" $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $2}'`)	# IFACE
189
		echo $ITH
190
		echo $ITH
190
		WANIFACE="${WANIFACE} $ITH"
191
		WANIFACE="${WANIFACE} $ITH"
Line 204... Line 205...
204
		DOWN=""			# liste des interfaces down
205
		DOWN=""			# liste des interfaces down
205
		DOWNCOUNT=0		# nombre d'interface down
206
		DOWNCOUNT=0		# nombre d'interface down
206
		for iface in $WANIFACE ; do
207
		for iface in $WANIFACE ; do
207
			COUNT=0		# compteur de test
208
			COUNT=0		# compteur de test
208
			FAIL=0		# Nombre de fois down
209
			FAIL=0		# Nombre de fois down
209
			# Recup de l'adresse IP dynamiquement          A tester avec le tableau ... ip=${ETH[$i:2]} basé sur iface=${ETH[$i:1]}
210
			# Recup de l'adresse IP dynamiquement
210
			IP=`ifconfig $iface |grep "inet adr" |cut -f 2 -d ":" |awk '{print $1}'`
211
			IP=`ifconfig $iface |grep "inet adr" |cut -f 2 -d ":" |awk '{print $1}'`
211
			if [ $i -ne 0 ]; then
212
			if [ $i -ne 0 ]; then
212
				GW=`grep "$iface," $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $4}'`		# @GW
213
				GW=`grep "$iface," $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $4}'`		# @GW
213
				WT=`grep "$iface," $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $5}'`		# @WT
214
				WT=`grep "$iface," $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $5}'`		# @WT
214
			else
215
			else
Line 290... Line 291...
290
						fi
291
						fi
291
					done # End linkdown in DOWN
292
					done # End linkdown in DOWN
292
					# Interface en etat normal --> rajout de la règle en mode nexthop
293
					# Interface en etat normal --> rajout de la règle en mode nexthop
293
					if [ $FAILIF -eq 0 ]; then
294
					if [ $FAILIF -eq 0 ]; then
294
						IP=`ifconfig $iface |grep "inet adr" |cut -f 2 -d ":" |awk '{print $1}'`
295
						IP=`ifconfig $iface |grep "inet adr" |cut -f 2 -d ":" |awk '{print $1}'`
295
						if [ $iface != "eth0" ]; then
296
						if [ $iface != "$INTIF" ]; then
296
							GW=`grep "$iface," $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $4}'`		# @GW
297
							GW=`grep "$iface," $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $4}'`		# @GW
297
							WT=`grep "$iface," $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $5}'`		# @GW
298
							WT=`grep "$iface," $CONF_FILE | awk -F'"' '{ print $2 }' | awk -F, '{ print $5}'`		# @GW
298
						else
299
						else
299
							GW=`grep "^GW=" $CONF_FILE | awk -F= '{print $2}'`			# @GW
300
							GW=`grep "^GW=" $CONF_FILE | awk -F= '{print $2}'`			# @GW
300
						fi	
301
						fi