linux網卡掉包或掛掉解決辦法


最近自己公司網站老出現掉包問題之前以為是網絡問題或機房問題,經過N久的排查發現是linux網卡掉包了,下面我來分享我的解決辦法。
 


之前公司的系統由於網卡問題,經常出現掉包(掉包排除攻擊的 因素)或者突然掛掉,

嚴重影響服務的正常提供,因此想在出現此問題時自動解決。

思路:對網卡檢測掉包情況,或者用ping檢測,出現了,就重啟哈網卡 。瞧是很簡單吧!

代碼如下:

 代碼如下 復制代碼
#安裝幫助: 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    #修改為你的交互機或者路由器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
 


程序運行至目前的日志結果如下:

 代碼如下 復制代碼
20130319 12:23:01: eth0 1 ping 100% loss
20130319 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
 


呵呵,自動解決了網卡掉包或掛掉的煩惱!您是不是很爽!
忘記了說鳥,此程序運行環境Red Hat Enterprise Linux Server release 5.4或者Centos6.3
其他linux環境估計問題不大,這里就不測試鳥!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM