Subversion Repositories ALCASAR

Rev

Rev 1827 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 1827 Rev 2454
Line 1... Line 1...
1
#!/bin/bash
1
#!/bin/bash
2
# $Id: alcasar-load_balancing.sh 1827 2016-04-19 09:47:29Z raphael.pion $
2
# $Id: alcasar-load_balancing.sh 2454 2017-12-09 18:59:31Z tom.houdayer $
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 46... Line 46...
46
# CONFIGURATION ENDS HERE
46
# CONFIGURATION ENDS HERE
47
###############################
47
###############################
48
 
48
 
49
 
49
 
50
if [ $(whoami) != "root" ]; then
50
if [ $(whoami) != "root" ]; then
51
        echo "You must be root to run this!" ; echo ; exit 1
51
	echo "You must be root to run this!" ; echo ; exit 1
52
fi
52
fi
53
 
53
 
54
# Adapter for ALCASAR project
54
# Adapter for ALCASAR project
55
CONF_FILE="/usr/local/etc/alcasar.conf"
55
CONF_FILE="/usr/local/etc/alcasar.conf"
56
 
56
 
Line 102... Line 102...
102
			GW=`grep "^GW=" $CONF_FILE | awk -F= '{print $2}'`				# @GW
102
			GW=`grep "^GW=" $CONF_FILE | awk -F= '{print $2}'`				# @GW
103
#			MTU=`grep "^PUBLIC_MTU=" $CONF_FILE | awk -F= '{print $2}'`			# MTU
103
#			MTU=`grep "^PUBLIC_MTU=" $CONF_FILE | awk -F= '{print $2}'`			# MTU
104
		fi # End
104
		fi # End
105
 
105
 
106
		NET="`ipcalc -n $IP_NET | cut -d"=" -f2`/`ipcalc -p $IP_NET|cut -d"=" -f2`"
106
		NET="`ipcalc -n $IP_NET | cut -d"=" -f2`/`ipcalc -p $IP_NET|cut -d"=" -f2`"
107
		if [ "$PARAM" == "add" ]; then	
107
		if [ "$PARAM" == "add" ]; then
108
			set -x
108
			set -x
109
			table=$(($i + 1))
109
			table=$(($i + 1))
110
			ip route ${PARAM} ${NET} dev ${IFACE} src ${IP} table $table
110
			ip route ${PARAM} ${NET} dev ${IFACE} src ${IP} table $table
111
			ip route ${PARAM} default via ${GW} table $table
111
			ip route ${PARAM} default via ${GW} table $table
112
			ip rule ${PARAM} from ${IP} table $table
112
			ip rule ${PARAM} from ${IP} table $table
Line 122... Line 122...
122
		echo
122
		echo
123
		routecmd="${routecmd} nexthop via ${GW} dev ${IFACE} weight ${WT}"
123
		routecmd="${routecmd} nexthop via ${GW} dev ${IFACE} weight ${WT}"
124
		i=$(($i + 1))
124
		i=$(($i + 1))
125
	done # End While
125
	done # End While
126
 
126
 
127
	if [ "$PARAM" == "add" ]; then	
127
	if [ "$PARAM" == "add" ]; then
128
		echo "[] Balanced routing:"
128
		echo "[] Balanced routing:"
129
		# suppress default route
129
		# suppress default route
130
		ip route del default scope global
130
		ip route del default scope global
131
		set -x
131
		set -x
132
		${routecmd}
132
		${routecmd}
133
		set +x
133
		set +x
134
		echo
134
		echo
135
	fi
135
	fi
136
	
136
 
137
} # end create_eth
137
} # end create_eth
138
 
138
 
139
###########################
139
###########################
140
# Fonction virtual Interfaces deleting
140
# Fonction virtual Interfaces deleting
141
###########################
141
###########################
142
delete_eth () {
142
delete_eth () {
143
	IFACE_COUNT=`ls -l /etc/sysconfig/network-scripts/ifcfg-$EXTIF:* | wc -l`
143
	IFACE_COUNT=`ls -l /etc/sysconfig/network-scripts/ifcfg-$EXTIF:* | wc -l`
144
	echo $IFACE_COUNT
144
	echo $IFACE_COUNT
145
	while [ $IFACE_COUNT -ne 0 ]
145
	while [ $IFACE_COUNT -ne 0 ]
146
	do
146
	do
147
		i=$IFACE_COUNT	
147
		i=$IFACE_COUNT
148
		echo "ifdown $EXTIF:$i"
148
		echo "ifdown $EXTIF:$i"
149
		ifdown $EXTIF:$i
149
		ifdown $EXTIF:$i
150
		rm -f /etc/sysconfig/network-scripts/ifcfg-$EXTIF:$i
150
		rm -f /etc/sysconfig/network-scripts/ifcfg-$EXTIF:$i
151
		IFACE_COUNT=$(($IFACE_COUNT - 1))
151
		IFACE_COUNT=$(($IFACE_COUNT - 1))
152
	done
152
	done
153
	ip route del default scope global
153
	ip route del default scope global
154
#	ip route add default gw 192.168.1.1
154
#	ip route add default gw 192.168.1.1
155
}
155
}
156
	
156
 
157
 
157
 
158
# do not modify below this line unless you know what you're doing :)
158
# do not modify below this line unless you know what you're doing :)
159
function getvalue() {
159
function getvalue() {
160
        index=$1
160
	index=$1
161
        VAR=$2
161
	VAR=$2
162
 
162
 
163
        n=1
163
	n=1
164
        for f in ${VAR} ; do
164
	for f in ${VAR} ; do
165
                if [ "${n}" == "${index}" ]; then
165
		if [ "${n}" == "${index}" ]; then
166
                        echo "$f"
166
			echo "$f"
167
                        break
167
			break
168
                fi
168
		fi
169
                n=$(($n++))
169
		n=$(($n++))
170
        done
170
	done
171
}
171
}
172
 
172
 
173
######################
173
######################
174
# Fonction de FailOver
174
# Fonction de FailOver
175
######################
175
######################
176
function failover () {
176
function failover () {
177
 
177
 
178
	echo "[] Watchdog started"
178
	echo "[] Watchdog started"
179
	# 0 == all links ok, 1 == some link down
179
	# 0 == all links ok, 1 == some link down
180
	STATE=0
180
	STATE=0
181
	
181
 
182
	DOWNCOUNT_BAK=0
182
	DOWNCOUNT_BAK=0
183
	DOWN_BAK=""
183
	DOWN_BAK=""
184
	NBIFACE=`grep "^WAN" $CONF_FILE | wc -l`	# Nbre interfaces virtuelles
184
	NBIFACE=`grep "^WAN" $CONF_FILE | wc -l`	# Nbre interfaces virtuelles
185
	echo "Nombre interfaces =  "$NBIFACE
185
	echo "Nombre interfaces =  "$NBIFACE
186
	WANIFACE[0]="$EXTIF"
186
	WANIFACE[0]="$EXTIF"
Line 193... Line 193...
193
		c=$(($c + 1))
193
		c=$(($c + 1))
194
	done
194
	done
195
	echo "Liste des interfaces : "${WANIFACE[*]}
195
	echo "Liste des interfaces : "${WANIFACE[*]}
196
	# Failover test
196
	# Failover test
197
	while : ; do
197
	while : ; do
198
	
198
 
199
		if [ $VERBOSE -eq 1 ]; then
199
		if [ $VERBOSE -eq 1 ]; then
200
			echo "[] Sleeping, state=$STATE"
200
			echo "[] Sleeping, state=$STATE"
201
		fi
201
		fi
202
		sleep $FAILOVER
202
		sleep $FAILOVER
203
	
203
 
204
		IFINDEX=1
204
		IFINDEX=1
205
		DOWN=""			# liste des interfaces down
205
		DOWN=""			# liste des interfaces down
206
		DOWNCOUNT=0		# nombre d'interface down
206
		DOWNCOUNT=0		# nombre d'interface down
207
		for iface in $WANIFACE ; do
207
		for iface in $WANIFACE ; do
208
			COUNT=0		# compteur de test
208
			COUNT=0		# compteur de test
Line 212... Line 212...
212
			if [ $i -ne 0 ]; then
212
			if [ $i -ne 0 ]; then
213
				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
214
				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
215
			else
215
			else
216
				GW=`grep "^GW=" $CONF_FILE | awk -F= '{print $2}'`			# @GW
216
				GW=`grep "^GW=" $CONF_FILE | awk -F= '{print $2}'`			# @GW
217
			fi	
217
			fi
218
			for TESTIP in $TESTIPS ; do
218
			for TESTIP in $TESTIPS ; do
219
				COUNT=$(($COUNT + 1))
219
				COUNT=$(($COUNT + 1))
220
				ping -W 3 -I $IP -c 1 $TESTIP > /dev/null 2>&1
220
				ping -W 3 -I $IP -c 1 $TESTIP > /dev/null 2>&1
221
#				ping -W 3 -I $IP -c 1 $TESTIP
221
#				ping -W 3 -I $IP -c 1 $TESTIP
222
				# Si ping de la première adresse --> ok  --> stop du test pour l'interface testée
222
				# Si ping de la première adresse --> ok  --> stop du test pour l'interface testée
223
				if [ $? -eq 0 ]; then
223
				if [ $? -eq 0 ]; then
224
					break
224
					break
225
				else 
225
				else
226
					# sinon on compte une erreur
226
					# sinon on compte une erreur
227
					FAIL=$(($FAIL + 1))
227
					FAIL=$(($FAIL + 1))
228
				fi
228
				fi
229
			done # End of test sur un serveur Internet
229
			done # End of test sur un serveur Internet
230
			# Affichage du nombre de down
230
			# Affichage du nombre de down
Line 246... Line 246...
246
			fi
246
			fi
247
			IFINDEX=$(($IFINDEX + 1))
247
			IFINDEX=$(($IFINDEX + 1))
248
			echo "IFINDEX =$IFINDEX"
248
			echo "IFINDEX =$IFINDEX"
249
		done # End Test Interface in WANIFACE
249
		done # End Test Interface in WANIFACE
250
 
250
 
251
		#  0 Passerelle down et état précédent différent (retour à la normale)) --> mise à la normale des passerelles 
251
		#  0 Passerelle down et état précédent différent (retour à la normale)) --> mise à la normale des passerelles
252
#		if [ $DOWNCOUNT -eq 0 ] && [ $DOWNCOUNT -ne $DOWNCOUNT_BAK ]; then
252
#		if [ $DOWNCOUNT -eq 0 ] && [ $DOWNCOUNT -ne $DOWNCOUNT_BAK ]; then
253
		if [ $DOWNCOUNT -eq 0 ] ; then
253
		if [ $DOWNCOUNT -eq 0 ] ; then
254
			if [ $STATE -eq 1 ]; then
254
			if [ $STATE -eq 1 ]; then
255
				echo
255
				echo
256
				echo "[] All links up and running :)"
256
				echo "[] All links up and running :)"
Line 277... Line 277...
277
				for iface in $WANIFACE ; do
277
				for iface in $WANIFACE ; do
278
					echo "-------------------------"
278
					echo "-------------------------"
279
					echo "iface=$iface"
279
					echo "iface=$iface"
280
					echo "Index = " $IFINDEX
280
					echo "Index = " $IFINDEX
281
					FAILIF=0
281
					FAILIF=0
282
					# Pour chaque interface down --> 
282
					# Pour chaque interface down -->
283
					echo "Interfaces DOWN = $DOWN"
283
					echo "Interfaces DOWN = $DOWN"
284
					for lnkdwn in $DOWN ; do
284
					for lnkdwn in $DOWN ; do
285
						echo "LINKDOWN = "$lnkdown
285
						echo "LINKDOWN = "$lnkdown
286
						if [ $lnkdwn -eq $IFINDEX ]; then
286
						if [ $lnkdwn -eq $IFINDEX ]; then
287
							FAILIF=1
287
							FAILIF=1
288
							break			
288
							break
289
						else
289
						else
290
							continue
290
							continue
291
						fi
291
						fi
292
					done # End linkdown in DOWN
292
					done # End linkdown in DOWN
293
					# 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
Line 296... Line 296...
296
						if [ $iface != "$EXTIF" ]; then
296
						if [ $iface != "$EXTIF" ]; then
297
							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
298
							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
299
						else
299
						else
300
							GW=`grep "^GW=" $CONF_FILE | awk -F= '{print $2}'`			# @GW
300
							GW=`grep "^GW=" $CONF_FILE | awk -F= '{print $2}'`			# @GW
301
						fi	
301
						fi
302
						echo "GW=$GW"
302
						echo "GW=$GW"
303
						echo "WT=$WT"
303
						echo "WT=$WT"
304
						echo "suffix=$sufix"
304
						echo "suffix=$sufix"
305
						suffix="${suffix} nexthop via ${GW} dev ${iface} weight ${WT:-1}"
305
						suffix="${suffix} nexthop via ${GW} dev ${iface} weight ${WT:-1}"
306
					fi # End interface = noFAIL
306
					fi # End interface = noFAIL
307
					IFINDEX=$(($IFINDEX + 1))
307
					IFINDEX=$(($IFINDEX + 1))
308
				done # End  iface IN WANIFACE
308
				done # End  iface IN WANIFACE
309
				# Commande globale
309
				# Commande globale
310
				cmd="ip route replace default scope global $suffix"
310
				cmd="ip route replace default scope global $suffix"
311
			
311
 
312
				if [ $VERBOSE -eq 1 ]; then
312
				if [ $VERBOSE -eq 1 ]; then
313
					set -x
313
					set -x
314
			#		echo "Avec commentaire : " ${cmd}
314
			#		echo "Avec commentaire : " ${cmd}
315
					${cmd}
315
					${cmd}
316
					set +x
316
					set +x
Line 319... Line 319...
319
					${cmd} 2>/dev/null
319
					${cmd} 2>/dev/null
320
					echo ${cmd}
320
					echo ${cmd}
321
				fi # end Application de la commande de routage globale
321
				fi # end Application de la commande de routage globale
322
			fi #
322
			fi #
323
			DOWN_BAK=$DOWN	# Enregistrement de l'etat
323
			DOWN_BAK=$DOWN	# Enregistrement de l'etat
324
		fi # End 
324
		fi # End
325
	done
325
	done
326
} # End of Failover
326
} # End of Failover
327
 
327
 
328
 
328
 
329
#################
329
#################
Line 334... Line 334...
334
echo "[] (c) 2011 Pau Oliva Fora <pof> @eslack.org"
334
echo "[] (c) 2011 Pau Oliva Fora <pof> @eslack.org"
335
echo "[] (c) 2013 3abtux ALCASAR  <3abtux> @alcasar.net"
335
echo "[] (c) 2013 3abtux ALCASAR  <3abtux> @alcasar.net"
336
echo
336
echo
337
 
337
 
338
case $1 in
338
case $1 in
339
	create) 
339
	create)
340
		create_eth  		
340
		create_eth
341
	;;
341
	;;
342
	delete) 
342
	delete)
343
		delete_eth  		
343
		delete_eth
344
	;;
344
	;;
345
	start) 
345
	start)
346
                if [ "$MULTIWAN" != "on" ] && [ "$MULTIWAN" != "On" ]; then 
346
		if [ "$MULTIWAN" != "on" ] && [ "$MULTIWAN" != "On" ]; then
347
		    echo "The MultiGateway is not activated !"
347
		    echo "The MultiGateway is not activated !"
348
		    exit 0
348
		    exit 0
349
		fi
349
		fi
350
                PARAM="add"
350
		PARAM="add"
351
                create_eth
351
		create_eth
352
                ip route flush cache
352
		ip route flush cache
353
                if [ $FAILOVER -eq 0 ]; then 
353
		if [ $FAILOVER -eq 0 ]; then
354
		      echo "The MultiWAN Mode is actived but not failover connectivity !"
354
		      echo "The MultiWAN Mode is actived but not failover connectivity !"
355
		      exit 0
355
		      exit 0
356
		fi
356
		fi
357
                echo "Starting down $prog: "
357
		echo "Starting down $prog: "
358
                pid=`pidof -x "alcasar-load_balancing.sh"`
358
		pid=`pidof -x "alcasar-load_balancing.sh"`
359
                if [ $pid != "" ]; then
359
		if [ $pid != "" ]; then
360
                        echo $pid > $pidfile
360
			echo $pid > $pidfile
361
                fi
361
		fi
362
                touch /var/lock/subsys/alcasar-load_balancing
362
		touch /var/lock/subsys/alcasar-load_balancing
363
                failover
363
		failover
364
	;;
364
	;;
365
	stop) 
365
	stop)
366
		PARAM="del"
366
		PARAM="del"
367
		echo "Shutting down $prog: "
367
		echo "Shutting down $prog: "
368
                if [ -f $pidfile ]; then
368
		if [ -f $pidfile ]; then
369
                        pid=`cat $pidfile`
369
			pid=`cat $pidfile`
370
                        kill -9 $pid
370
			kill -9 $pid
371
                else
371
		else
372
                        echo "$prog is not running."
372
			echo "$prog is not running."
373
                        exit 1
373
			exit 1
374
                fi
374
		fi
375
                RETVAL=$?
375
		RETVAL=$?
376
                echo
376
		echo
377
                [ $RETVAL -eq 0 ] && rm -f $pidfile && rm -f /var/lock/subsys/alcasar-load_balancing
377
		[ $RETVAL -eq 0 ] && rm -f $pidfile && rm -f /var/lock/subsys/alcasar-load_balancing
378
                echo "Delete of virtual interfaces"
378
		echo "Delete of virtual interfaces"
379
                delete_eth
379
		delete_eth
380
                echo "Network restart"
380
		echo "Network restart"
381
                service network restart 2>&1 > /dev/null
381
		service network restart 2>&1 > /dev/null
382
                ip route
382
		ip route
383
       
383
 
384
	;;
384
	;;
385
	status)
385
	status)
386
                echo "Checking  $prog : "
386
		echo "Checking  $prog : "
387
                if [ -f $pidfile ]; then
387
		if [ -f $pidfile ]; then
388
                        pid=`cat $pidfile`
388
			pid=`cat $pidfile`
389
                        CHECK=`ps -p $pid --no-heading | awk {'printf $1'}`
389
			CHECK=`ps -p $pid --no-heading | awk {'printf $1'}`
390
                        if [ "$CHECK" = "" ]; then
390
			if [ "$CHECK" = "" ]; then
391
                                echo "$prog is NOT running."
391
				echo "$prog is NOT running."
392
                        else
392
			else
393
                                echo "$prog is running !"
393
				echo "$prog is running !"
394
                        fi
394
			fi
395
                else
395
		else
396
                                echo "$prog is Not running."
396
				echo "$prog is Not running."
397
                fi	
397
		fi
398
	;;
398
	;;
399
	fail) 
399
	fail)
400
		failover 		
400
		failover
401
	;;
401
	;;
402
	*) 
402
	*)
403
		echo "Usage: $0 [start|stop|status|create|delete]" ; echo ; exit 1
403
		echo "Usage: $0 [start|stop|status|create|delete]" ; echo ; exit 1
404
	;;
404
	;;
405
esac
405
esac
406
 
406
 
407
exit 0
407
exit 0