Recently, my company thought it was a network problem or a data center problem when a packet loss problem occurred on its website. after a long time of troubleshooting, I found that the linux network card was out of the package. Let's share my solution.
In the past, the company's system often experienced packet loss due to network card problems (packet loss exclusion factors) or sudden failure,
It seriously affects the normal provision of services, so we want to automatically solve this problem when it occurs.
Train of Thought: Check the packet loss status of the NIC, or use ping detection. If yes, restart the NIC. It's easy!
The Code is as follows:
The Code is as follows: |
Copy code |
# Installation help: mkdir/cyc #/Cyc/renetwork. log is a log file
#! /Bin/bash #2013-2-17 @ man # Ping timeout restart network # Lastmodtime 2013-3-19 PingNetwork (){ Local c = $1 Local pcount = $2 Local dst = 192.168.0.143 # modify the ip address of your Interactive Machine or router 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 |
The log results of running the program till now are as follows:
The Code is as follows: |
Copy code |
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 |
Haha, it automatically solves the problem of NIC packet loss or disconnection! Are you so nice!
If you forget to mention it, the program runs in Red Hat Enterprise Linux Server release 5.4 or Centos6.3.
It is estimated that the problem is not serious in other linux environments. We will not test it here!