Recently, the company's web site of the old swap problem before thought is a network problem or computer room problems, after n-long troubleshooting found that the Linux network card switched, I would like to share my solution.
Before the company's system due to network card problems, often a swap (swap to exclude the attack factor) or suddenly hung up,
Severely affects the normal delivery of the service, so you want to resolve it automatically when this problem occurs.
Idea: the network card detection swap situation, or with ping detection, appeared, restart the HA network card. Look, it's very simple!
The code is as follows:
The code is as follows |
Copy Code |
#安装帮助: MKDIR/CYC #/cyc/renetwork.log for log files
#!/bin/bash # 2013-2-17 @ Bump Mann # 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 $"
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 program runs to the current log results 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
|
Oh, automatically solve the network card swap or hang up the trouble! You are not very cool!
Forget to say Bird, this program runs the environment Red Hat Enterprise Linux Server release 5.4 or Centos6.3
Other Linux environments do not have a big problem, so don't test birds here!
Linux network adapter swap or hang up solution