最近自己公司網(wǎng)站老出現(xiàn)掉包問題之前以為是網(wǎng)絡(luò)問題或機(jī)房問題,經(jīng)過N久的排查發(fā)現(xiàn)是linux網(wǎng)卡掉包了,下面我來分享我的解決辦法,
linux網(wǎng)卡掉包或掛掉解決辦法linux操作系統(tǒng)
。之前公司的系統(tǒng)由于網(wǎng)卡問題,經(jīng)常出現(xiàn)掉包(掉包排除攻擊的 因素)或者突然掛掉,
嚴(yán)重影響服務(wù)的正常提供,因此想在出現(xiàn)此問題時(shí)自動(dòng)解決。
思路:對(duì)網(wǎng)卡檢測(cè)掉包情況,或者用ping檢測(cè),出現(xiàn)了,就重啟哈網(wǎng)卡 。瞧是很簡(jiǎn)單吧!
代碼如下:
代碼如下復(fù)制代碼#安裝幫助: mkdir /cyc#/cyc/renetwork.log為日志文件
#!/bin/bash
# 2013-2-17 @凹凸曼
# ping timeout restart network
# lastmodtime 2013-3-19
pingNetwork(){
local c=$1
local pcount=$2
local dst=192.168.0.143 #修改為你的交互機(jī)或者路由器ip
if [[ ! $pcount =~ ^[0-9]+$ ]];then
pcount=5
fi
loss=`ping -c $pcount $dst|grep loss|awk '{print $6}'|cut -d% -f 1 `
if [[ ! $loss =~ ^[0-9]+$ ]];then
loss=100
fi
if [ $loss -gt 0 ]; then
ifdown $c
sleep 3
ifup $c
echo `date "+%Y%m%d %T"`": $pcount ping $loss% loss">>/cyc/renetwork.log
sleep 30
fi
}
#start dst eth
stopEth(){
local c=$1
local et=`service network status |tail -1|grep "$c"`
if [ x"" == x"$et" ]; then
ifup $c
echo `date "+%Y%m%d %T"`":$c stop">>/cyc/renetwork.log
sleep 30
fi
}
#drop found
NetDrop(){
local c=$1
#$6 RX-DRP
local redp=`netstat -i|grep "$c"|awk 'END{print $6}'`
#$10 TX-DRP
local txdp=`netstat -i|grep "$c"|awk 'END{print $10}'`
if [ $redp -gt 0 ];then
ifdown $c
sleep 3
ifup $c
echo `date "+%Y%m%d %T"`":$c RX-DRP drop">>/cyc/renetwork.log
sleep 30
fi
sleep 2
if [ $txdp -gt 0 ];then
ifdown $c
sleep 3
ifup $c
echo `date "+%Y%m%d %T"`":$c TX-DRP drop">>/cyc/renetwork.log
sleep 30
fi
}
wtime=$2
eth=$1
if [[ ! $wtime =~ ^[0-9]+$ ]];then
wtime=3
fi
if [ x"" == x"$eth" ];then
echo "please enter the NIC name!"
exit
fi
while [ 1 ]
do
stopEth $eth
sleep $wtime
NetDrop $eth
sleep $wtime
pingNetwork $eth 1
done
程序運(yùn)行至目前的日志結(jié)果如下:
代碼如下復(fù)制代碼20130319 12:23:01: eth0 1 ping 100% loss20130319 13:43:50: eth0 1 ping 100% loss
20130319 18:11:18: eth0 1 ping 100% loss
20130323 08:04:59: eth0 1 ping 100% loss
20130323 14:41:04: eth0 1 ping 100% loss
20130324 12:49:04: eth0 1 ping 100% loss
20130327 13:15:47: eth0 1 ping 100% loss
20130401 11:17:42: eth0 1 ping 100% loss
20130402 09:02:14: eth0 1 ping 100% loss
20130403 08:49:30: eth0 1 ping 100% loss
20130404 20:21:46: eth0 1 ping 100% loss
20130407 18:44:57: eth0 1 ping 100% loss
20130408 10:46:53: eth0 1 ping 100% loss
20130408 14:41:06: eth0 1 ping 100% loss
20130408 14:47:24: eth0 1 ping 100% loss
20130408 15:23:02:eth0 RX-DRP drop
20130415 08:30:09: eth0 1 ping 100% loss
20130415 11:16:16: eth0 1 ping 100% loss
20130417 08:05:41: eth0 1 ping 100% loss
20130419 08:04:19: eth0 1 ping 100% loss
20130419 10:40:51: eth0 1 ping 100% loss
20130419 11:33:14: eth0 1 ping 100% loss
20130419 19:03:03: eth0 1 ping 100% loss
20130421 16:10:55: eth0 1 ping 100% loss
20130422 07:47:34: eth0 1 ping 100% loss
20130423 07:15:07: eth0 1 ping 100% loss
呵呵,自動(dòng)解決了網(wǎng)卡掉包或掛掉的煩惱!您是不是很爽!
忘記了說鳥,此程序運(yùn)行環(huán)境Red Hat Enterprise Linux Server release 5.4或者Centos6.3
其他linux環(huán)境估計(jì)問題不大,這里就不測(cè)試?guó)B!