Код: Выделить всё
f0s@ubuntu9:/etc/init.d$ uname -a
Linux ubuntu9 2.6.28-16-server #55-Ubuntu SMP Tue Oct 20 20:37:10 UTC 2009 x86_64 GNU/Linux
Код: Выделить всё
f0s@ubuntu9:/etc/init.d$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:22:15:d8:2c:76
inet addr:192.168.51.254 Bcast:192.168.51.255 Mask:255.255.255.0
inet6 addr: fe80::222:15ff:fed8:2c76/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:134890 errors:0 dropped:0 overruns:0 frame:0
TX packets:135985 errors:0 dropped:0 overruns:0 carrier:1
collisions:0 txqueuelen:1000
RX bytes:32683486 (32.6 MB) TX bytes:67833163 (67.8 MB)
Interrupt:253
eth1 Link encap:Ethernet HWaddr 00:21:91:12:0b:b2
inet addr:91.111.23.114 Bcast:81.211.23.255 Mask:255.255.255.0
inet6 addr: fe80::221:91ff:fe12:bb2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:127603 errors:0 dropped:0 overruns:0 frame:0
TX packets:128347 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:60980358 (60.9 MB) TX bytes:32724434 (32.7 MB)
Interrupt:16 Base address:0xa800
eth2 Link encap:Ethernet HWaddr 00:21:91:11:e0:5b
inet addr:192.168.55.254 Bcast:192.168.55.255 Mask:255.255.255.0
inet6 addr: fe80::221:91ff:fe11:e05b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:13921 errors:0 dropped:0 overruns:0 frame:0
TX packets:3248 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2636440 (2.6 MB) TX bytes:440076 (440.0 KB)
Interrupt:19 Base address:0x4c00
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:790 errors:0 dropped:0 overruns:0 frame:0
TX packets:790 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:308140 (308.1 KB) TX bytes:308140 (308.1 KB)
tun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:192.168.251.4 P-t-P:192.168.251.2 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:9032 errors:0 dropped:0 overruns:0 frame:0
TX packets:8923 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:5095696 (5.0 MB) TX bytes:2778973 (2.7 MB)
Код: Выделить всё
f0s@ubuntu9:/etc/init.d$ netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
77.221.149.163 192.168.55.253 255.255.255.255 UGH 0 0 0 eth2
192.168.251.2 0.0.0.0 255.255.255.255 UH 0 0 0 tun0
192.168.240.0 192.168.251.2 255.255.255.0 UG 0 0 0 tun0
192.168.55.0 0.0.0.0 255.255.255.0 U 0 0 0 eth2
192.168.53.0 192.168.251.2 255.255.255.0 UG 0 0 0 tun0
192.168.3.0 192.168.251.2 255.255.255.0 UG 0 0 0 tun0
192.168.81.0 192.168.251.2 255.255.255.0 UG 0 0 0 tun0
192.168.50.0 192.168.251.2 255.255.255.0 UG 0 0 0 tun0
192.168.80.0 192.168.251.2 255.255.255.0 UG 0 0 0 tun0
192.168.51.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.1.0 192.168.251.2 255.255.255.0 UG 0 0 0 tun0
192.168.0.0 192.168.251.2 255.255.255.0 UG 0 0 0 tun0
192.168.82.0 192.168.251.2 255.255.255.0 UG 0 0 0 tun0
192.168.250.0 192.168.251.2 255.255.255.0 UG 0 0 0 tun0
192.168.251.0 192.168.251.2 255.255.255.0 UG 0 0 0 tun0
192.168.252.0 192.168.251.2 255.255.255.0 UG 0 0 0 tun0
192.168.253.0 192.168.251.2 255.255.255.0 UG 0 0 0 tun0
91.111.23.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
0.0.0.0 91.111.23.113 0.0.0.0 UG 0 0 0 eth1
есть скрипт, который пингует по очереди каждый шлюз, и если основной упал, меняет на запасной.. если основной появился, то должен менять обратно на него, т.к. он предпочитаемый.. вообщем вот сам скрипт:
Код: Выделить всё
#!/bin/sh
# Указываем путь у log-файлу
log="/var/log/routing.log"
# Задаем периодичность опроса шлюзов.
# 30s - 30 секунд
# 10m - 10 минут
# 1h - 1 час
check_period="30s"
# Если log-файл не найден, создаем его, иначе отчищаем
clear_log ()
{
if [ ! -f ${log} ]
then
touch ${log}
else
cat /dev/null > ${log}
fi
}
# Инициализируем переменные
init ()
{
# шлюз основного инета
gw1="91.111.23.113"
gw2="192.168.55.253"
prefered_gw=${gw1}
dev_if1="eth1"
dev_if2="eth2"
if1=`ifconfig ${dev_if1} | awk -F ' *|:' '/inet addr/{print $4}'`
if2=`ifconfig ${dev_if2} | awk -F ' *|:' '/inet addr/{print $4}'`
# Текущие статусы шлюзов: 0 - шлюз недоступен, 1 - шлюз работает
gw1_curr_status=0
gw2_curr_status=0
# Текущие потери пакетов на шлюзе
gw1_curr_packet_loss=0
gw2_curr_packet_loss=0
# Максимальный процент потерь пакетов, при превышении данного числа, шлюз считается недоступным
gw1_max_packet_loss=35
gw2_max_packet_loss=40
# Отчищаем log-файл и сохраняем значения основных переменных
clear_log
echo `date +"%T %d.%m.%Y"`." Init environment OK. Check gateways status every ${check_period}." >> ${log}
echo `date +"%T %d.%m.%Y"`." ISP1 [if1=${if1}, dev_if1=${dev_if1}, gw1=${gw1}], ISP2 [if2=${if2}, dev_if2=${dev_if2}, gw2=${gw2}]" >> ${log}
}
# Данная функция определяет текущее состояние каждого из шлюзов.
# Если текущий процент потерь меньше максимально допустимого, то считаем, что шлюз доступен.
get_current_status ()
{
echo `date +"%T %d.%m.%Y"`." Get current status ISP gateways." >> ${log}
# Get current status default gateway ISP1
gw1_curr_packet_loss=`ping -I ${dev_if1} -c20 -l20 -q -W3 ${gw1} | grep loss | awk '{print $(NF-4)}' | cut -d"%" -f1`
if [ ${gw1_curr_packet_loss} -le ${gw1_max_packet_loss} ]
then
echo `date +"%T %d.%m.%Y"`. "ISP1. [STATUS - OK]. Current packet loss on ${gw1} via ${dev_if1} is ${gw1_curr_packet_loss}%." >> ${log}
gw1_curr_status=1
else
echo `date +"%T %d.%m.%Y"`. "ISP1. [STATUS - CRITICAL]. Current packet loss on ${gw1} via ${dev_if1} is ${gw1_curr_packet_loss}%." >> ${log}
gw1_curr_status=0
fi
# Get current status default gateway ISP2 (lissyara.su - 77.221.149.163)
gw2_curr_packet_loss=`ping -I ${dev_if2} -c20 -l20 -q -W3 77.221.149.163 | grep loss | awk '{print $(NF-4)}' | cut -d"%" -f1`
if [ ${gw2_curr_packet_loss} -le ${gw2_max_packet_loss} ]
then
echo `date +"%T %d.%m.%Y"`. "ISP2. [STATUS - OK]. Current packet loss on ${gw2} via ${dev_if2} is ${gw2_curr_packet_loss}%." >> ${log}
gw2_curr_status=1
else
echo `date +"%T %d.%m.%Y"`. "ISP2. [STATUS - CRITICAL]. Current packet loss on ${gw2} via ${dev_if2} is ${gw2_curr_packet_loss}%." >> ${log}
gw2_curr_status=0
fi
}
# Данная функция производит переключение шлюза по умолчанию в зависимости от результатов полученных в get_current_status ()
# На данный момент предпочитаемым шлюзом может быть только ${gw1}
switch_default_gw ()
{
curr_gw=`ip route show | grep default | awk '{print $3}'`
if [ ${curr_gw} = ${prefered_gw} -a ${gw1_curr_status} -eq 1 ]
then
echo `date +"%T %d.%m.%Y"`. "[STATUS - OK]. Current default gateway is prefered and running up. Nothing to do." >> ${log}
return
fi
if [ ${curr_gw} = ${prefered_gw} -a ${gw1_curr_status} -eq 0 -a ${gw2_curr_status} -eq 1 ]
then
echo `date +"%T %d.%m.%Y"`. "Prefered gw ${gw1} is down. Change default gw to ${gw2}" >> ${log}
echo `date +"%T %d.%m.%Y"`. "ip route replace default via ${gw2} dev ${dev_if2}" >> ${log}
/sbin/ip route replace default via ${gw2} dev ${dev_if2}
return
fi
if [ ${curr_gw} != ${prefered_gw} -a ${gw1_curr_status} -eq 1 ]
then
echo `date +"%T %d.%m.%Y"`. "Prefered gw ${prefered_gw} is running up now. Change default gw ${curr_gw} to prefered ${prefered_gw}" >> ${log}
echo `date +"%T %d.%m.%Y"`. "ip route replace default via ${gw1} dev ${dev_if1}" >> ${log}
/sbin/ip route replace default via ${gw1} dev ${dev_if1}
return
fi
if [ ${curr_gw} != ${prefered_gw} -a ${gw1_curr_status} -eq 0 -a ${gw2_curr_status} -eq 1 ]
then
echo `date +"%T %d.%m.%Y"`. "Prefered gw ${prefered_gw} is still down. Current default gw is ${gw2}" >> ${log}
return
fi
if [ ${gw1_curr_status} -eq 0 -a ${gw2_curr_status} -eq 0 ]
then
echo "*************************************************************************" >> ${log}
echo `date +"%T %d.%m.%Y"`. "CRITICAL. Two gateways is down. Try again later." >> ${log}
echo "*************************************************************************" >> ${log}
return
fi
}
# Инициализируем переменные
init
# "Заходим" в вечный цикл, в котором по очереди вызываем функции get_current_status () и switch_default_gw ()
# После этого делаем паузу, время паузы задается в переменной ${check_period}
while [ 1 ]
do
get_current_status
switch_default_gw
sleep ${check_period}
done
запускаем. смотрим логи.. пока все нормально:
Код: Выделить всё
12:59:35 24.11.2009. Init environment OK. Check gateways status every 30s.
12:59:35 24.11.2009. ISP1 [if1=91.111.23.113, dev_if1=eth1, gw1=91.111.23.113], ISP2 [if2=192.168.55.254, dev_if2=eth2, gw2=192.168.55.253]
12:59:35 24.11.2009. Get current status ISP gateways.
12:59:35 24.11.2009. ISP1. [STATUS - OK]. Current packet loss on 91.111.23.113 via eth1 is 0%.
12:59:36 24.11.2009. ISP2. [STATUS - OK]. Current packet loss on 192.168.55.253 via eth2 is 0%.
12:59:36 24.11.2009. [STATUS - OK]. Current default gateway is prefered and running up. Nothing to do.
Код: Выделить всё
13:00:06 24.11.2009. Get current status ISP gateways.
13:00:06 24.11.2009. ISP1. [STATUS - OK]. Current packet loss on 91.111.23.113 via eth1 is 0%.
13:00:07 24.11.2009. ISP2. [STATUS - OK]. Current packet loss on 192.168.55.253 via eth2 is 0%.
13:00:07 24.11.2009. [STATUS - OK]. Current default gateway is prefered and running up. Nothing to do.
13:00:37 24.11.2009. Get current status ISP gateways.
13:00:37 24.11.2009. ISP1. [STATUS - OK]. Current packet loss on 91.111.23.113 via eth1 is 0%.
13:00:37 24.11.2009. ISP2. [STATUS - OK]. Current packet loss on 192.168.55.253 via eth2 is 0%.
13:00:37 24.11.2009. [STATUS - OK]. Current default gateway is prefered and running up. Nothing to do.
13:01:08 24.11.2009. Get current status ISP gateways.
13:01:08 24.11.2009. ISP1. [STATUS - OK]. Current packet loss on 91.111.23.113 via eth1 is 0%.
13:01:08 24.11.2009. ISP2. [STATUS - OK]. Current packet loss on 192.168.55.253 via eth2 is 0%.
13:01:08 24.11.2009. [STATUS - OK]. Current default gateway is prefered and running up. Nothing to do.
Код: Выделить всё
13:01:38 24.11.2009. Get current status ISP gateways.
13:01:41 24.11.2009. ISP1. [STATUS - CRITICAL]. Current packet loss on 91.111.23.113 via eth1 is 100%.
13:01:41 24.11.2009. ISP2. [STATUS - OK]. Current packet loss on 192.168.55.253 via eth2 is 0%.
13:01:41 24.11.2009. Prefered gw 91.111.23.113 is down. Change default gw to 192.168.55.253
13:01:41 24.11.2009. ip route replace default via 192.168.55.253 dev eth2
Код: Выделить всё
13:02:11 24.11.2009. Get current status ISP gateways.
13:02:14 24.11.2009. ISP1. [STATUS - CRITICAL]. Current packet loss on 91.111.23.113 via eth1 is 100%.
13:02:17 24.11.2009. ISP2. [STATUS - OK]. Current packet loss on 192.168.55.253 via eth2 is 5%.
13:02:47 24.11.2009. Get current status ISP gateways.
13:02:50 24.11.2009. ISP1. [STATUS - CRITICAL]. Current packet loss on 91.111.23.113 via eth1 is 100%.
13:02:53 24.11.2009. ISP2. [STATUS - OK]. Current packet loss on 192.168.55.253 via eth2 is 5%.
13:03:24 24.11.2009. Get current status ISP gateways.
13:03:27 24.11.2009. ISP1. [STATUS - CRITICAL]. Current packet loss on 91.111.23.113 via eth1 is 85%.
13:03:30 24.11.2009. ISP2. [STATUS - OK]. Current packet loss on 192.168.55.253 via eth2 is 5%.
Код: Выделить всё
13:04:00 24.11.2009. Get current status ISP gateways.
13:04:00 24.11.2009. ISP1. [STATUS - OK]. Current packet loss on 91.111.23.113 via eth1 is 0%.
13:04:03 24.11.2009. ISP2. [STATUS - OK]. Current packet loss on 192.168.55.253 via eth2 is 5%.
13:04:33 24.11.2009. Get current status ISP gateways.
13:04:33 24.11.2009. ISP1. [STATUS - OK]. Current packet loss on 91.111.23.113 via eth1 is 0%.
13:04:33 24.11.2009. ISP2. [STATUS - OK]. Current packet loss on 192.168.55.253 via eth2 is 0%.
13:05:03 24.11.2009. Get current status ISP gateways.
13:05:03 24.11.2009. ISP1. [STATUS - OK]. Current packet loss on 91.111.23.113 via eth1 is 0%.
13:05:06 24.11.2009. ISP2. [STATUS - OK]. Current packet loss on 192.168.55.253 via eth2 is 10%.
13:05:36 24.11.2009. Get current status ISP gateways.
13:05:37 24.11.2009. ISP1. [STATUS - OK]. Current packet loss on 91.111.23.113 via eth1 is 0%.
13:05:37 24.11.2009. ISP2. [STATUS - OK]. Current packet loss on 192.168.55.253 via eth2 is 0%.
13:06:07 24.11.2009. Get current status ISP gateways.
13:06:07 24.11.2009. ISP1. [STATUS - OK]. Current packet loss on 91.111.23.113 via eth1 is 0%.
13:06:07 24.11.2009. ISP2. [STATUS - OK]. Current packet loss on 192.168.55.253 via eth2 is 0%.
Код: Выделить всё
13:06:37 24.11.2009. Get current status ISP gateways.
13:06:37 24.11.2009. ISP1. [STATUS - OK]. Current packet loss on 91.111.23.113 via eth1 is 0%.
13:06:40 24.11.2009. ISP2. [STATUS - CRITICAL]. Current packet loss on 192.168.55.253 via eth2 is 100%.
13:07:11 24.11.2009. Get current status ISP gateways.
13:07:11 24.11.2009. ISP1. [STATUS - OK]. Current packet loss on 91.111.23.113 via eth1 is 0%.
13:07:14 24.11.2009. ISP2. [STATUS - CRITICAL]. Current packet loss on 192.168.55.253 via eth2 is 100%.
13:07:44 24.11.2009. Get current status ISP gateways.
13:07:44 24.11.2009. ISP1. [STATUS - OK]. Current packet loss on 91.111.23.113 via eth1 is 0%.
13:07:47 24.11.2009. ISP2. [STATUS - CRITICAL]. Current packet loss on 192.168.55.253 via eth2 is 100%.
13:08:17 24.11.2009. Get current status ISP gateways.
13:08:17 24.11.2009. ISP1. [STATUS - OK]. Current packet loss on 91.111.23.113 via eth1 is 0%.
13:08:20 24.11.2009. ISP2. [STATUS - CRITICAL]. Current packet loss on 192.168.55.253 via eth2 is 100%.
13:08:50 24.11.2009. Get current status ISP gateways.
13:08:50 24.11.2009. ISP1. [STATUS - OK]. Current packet loss on 91.111.23.113 via eth1 is 0%.
13:08:53 24.11.2009. ISP2. [STATUS - CRITICAL]. Current packet loss on 192.168.55.253 via eth2 is 100%.
13:09:23 24.11.2009. Get current status ISP gateways.
13:09:23 24.11.2009. ISP1. [STATUS - OK]. Current packet loss on 91.111.23.113 via eth1 is 0%.
13:09:26 24.11.2009. ISP2. [STATUS - CRITICAL]. Current packet loss on 192.168.55.253 via eth2 is 100%.
13:09:56 24.11.2009. Get current status ISP gateways.
13:09:56 24.11.2009. ISP1. [STATUS - OK]. Current packet loss on 91.111.23.113 via eth1 is 0%.
13:09:59 24.11.2009. ISP2. [STATUS - CRITICAL]. Current packet loss on 192.168.55.253 via eth2 is 100%.
в чем может быть тут косяк.. я так думаю может быть дело в том, что после того, как переключается шлюз на второй, то как-то неверно отрабатывает команда:
Код: Выделить всё
sudo ip route show | grep default | awk '{print $3}'