- 论坛徽章:
- 0
|
相信现在很多公司或网吧开始考虑双线接入,来实现网络的均衡负载或冗余(备份)线路,保持网络的不间断性。但是如何才能检测某条线路断了呢?有以下几种办法:
1. 物理连接中断(比如网线出现问题)
这个可以通过SNMP来检测(需要现安装 net-snmp 包),如下:
- 先查看各网卡的设备描述编号:
- # snmpwalk -v 1 -c public localhost|grep ifDescr
- IF-MIB::ifDescr.1 = STRING: lo
- IF-MIB::ifDescr.2 = STRING: dummy0
- IF-MIB::ifDescr.3 = STRING: eth0
- IF-MIB::ifDescr.4 = STRING: eth1
- IF-MIB::ifDescr.5 = STRING: tunl0
- IF-MIB::ifDescr.6 = STRING: gre
- 上面显示 eth0 的编号是3
- 查看 eth0 的状态:
- # snmpwalk -v 1 -c public localhost IF-MIB::ifOperStatus.3
- IF-MIB::ifOperStatus.3 = INTEGER: up(1)
- 如果显示 up 表示连接正常,显示 down 表示网络中断了。
复制代码
补充:(感谢 孙轩 兄的建议)
更简单的办法是利用 ethtool 工具,下载地址:<url>http://directory.fsf.org/All_Packages_in_Directory/ethtool.html</url>
编译安装:
- # ./configure && make && make install
复制代码
测试:
- # ethtool eth0
- ....
- Link detected: yes
复制代码
如果最后一行显示 yes 表示连接正常,显示 no 表示网络中断了。
2. 逻辑连接中断(比如网关的线路出了问题等)
可以通过每10s发送一个小的ping包到网关,看是否有回应,一般网关都是允许ping的。
这个可以通过一个expect程序(netwatch.exp)来完成,下面这个脚本发送一个字节为1的ping包给网关,并分析ping的输出信息,如果为 0% packet loss,表示正常,以0退出。如果超时(1秒钟),以1退出。
- #!/usr/bin/expect
- set target [lindex $argv 0]
- set timeout 1
- spawn ping -c 1 -s 1 $target
- expect {
- " 0% packet loss" {
- exit 0
- }
- timeout {
- exit 1
- }
- }
复制代码
比如网关1为192.168.1.1,网关2为192.168.2.1,那么要监测这两个网关的话,可以这样:
- #!/bin/sh
- netwatch.exp 192.168.1.1
- result1=$?
- netwatch.exp 192.168.2.1
- result2=$?
- if [ "$result1" eq 0 ] && [ "$result2" eq 0 ] ; then
- # 如果上次检测时连接丢失,而此次恢复正常
- if [ -e /var/run/lost-connection ] ; then
- do some recovery action here ...
- rm /var/run/lost-connection
- fi
- # 如果网关1不可达
- elif [ "$result1" ne 0 ] ; then
- change default route to 192.168.2.1
- do some action here, such as iptables NAT
- # 创建连接丢失标识
- touch /var/run/lost-connection
- # 如果网关2不可达
- elif [ "$result2" ne 0 ] ; then
- change default route to 192.168.1.1
- do some action here, such as iptables NAT
- # 创建连接丢失标识
- touch /var/run/lost-connection
- fi
复制代码
虽然可以通过iptables的MASQUERADE来实现自动切换,但是如果加了策略路由,就不行了。
以上只是个人的想法,希望起到抛砖引玉的作用,如果大家有更好的办法或建议,请不要吝啬提出来哦!
[ 本帖最后由 coolend 于 2005-12-21 11:53 编辑 ] |
|