曾經遇到過一個很奇怪的問題,一臺服務器運行一段時間後網卡流量變爲0,就是說網卡沒法傳輸數據了。可是重啓網絡服務後恢復正常,形成該問題的緣由多是系統內核的問題,也多是網卡硬件的問題。python
當時解決的思路是,先嚐試從新安裝操做系統,看問題是否能夠解決,若是問題依然存在而後嘗試更換網卡。因爲服務器上的業務不能中斷,因此重裝操做系統或者更換網卡都沒法在近期實現。臨時解決辦法是寫一個shell腳本監控網卡流量,當流量爲0時,重啓網絡服務。shell
需求以下:centos
1)一分鐘監控一次網卡流量bash
2)當網卡流量爲0時,重啓網卡服務器
3)假設網卡名字爲eth0網絡
知識點一:查看網卡流量運維
在案例八中,使用了sar -n DEV查看網卡流量,其實還有一種更直觀的方法,使用nload命令查看網卡流量。要使用該工具,須要安裝nload包,在centos系統中,須要先安裝epel-release包。 tcp
若是想要查看另外一塊網卡的流量,按一下右方向鍵便可。使用nload -m能夠同時顯示所有網卡的實時流量。可是,nload這個工具在shell腳本中沒法使用,由於它是動態的。ide
# nload Device ens33 [192.168.93.130] (1/2): ==================================================== Incoming: Curr: 944.00 Bit/s Avg: 1.12 kBit/s Min: 944.00 Bit/s Max: 1.86 kBit/s Ttl: 15.59 MByte Outgoing: Curr: 8.58 kBit/s Avg: 8.44 kBit/s Min: 4.27 kBit/s Max: 9.03 kBit/s Ttl: 1.05 MByte
知識點二:重啓網絡服務工具
centos系統中,查看網卡和ip的命令是ip addr,網卡配置文件所在路徑爲:/etc/sysconfig/network-scripts/,在這個目錄下有幾個帶有網卡名字的配置文件,例如ifcfg-ens33,ifcfg-lo。要想更改IP地址,修改對應的配置文件便可,修改完後須要重啓網絡服務才能生效:
# systemctl restart network //centos7系統
若是是centos6系統,命令爲:
# service network restart
也能夠單獨重啓指定網卡,例如:
# ifdown ens33 && ifup ens33
知識點三:抓包
在平時平常運維工做中,若是流量異常(一般是流量變大),須要咱們分析究竟是什麼樣的流量,能夠用抓包工具用來完成該需求。在centos系統裏有一個經常使用的抓包工具tcpdump,若系統沒有這個命令,安裝一下tcpdump包。
經常使用的用法是:
# tcpdump -nn -i eth0 -c 100
說明:
-nn:以數字的形式顯示IP和Port;
-i:指定網卡名字;
-c:指定抓包數量。
該用法在屏幕上顯示數據包的流向,即顯示來源IP、端口和目標IP、端口。要想抓到真正的數據包,須要加上-w選項。
# tcpdump -nn -i eth0 -c 100 -w /tmp/1.cap
這樣會抓100個數據包存入到/tmp/1.cap裏,這個文件是二進制的,能夠用Windows版的wireshark工具(圖形化)查看也能夠用tcpdump查看:
# tcpdunp -r /tmp/1.cap
另外,tcpdump也能夠指定IP、port及協議:
# tcpdump -nn tcp and host 1.1.1.1 and port 80
知識點四:if判斷多個條件
在shell腳本中,if判斷條件常常會有多個,要麼同時知足要麼只知足一個。用法以下:
# if [ option1 -a option2 ] //同時知足option1和option2 # if [ option1 ] && [ option2 ] //同時知足option1和option2 # if [ option1 -o option2 ] //知足option1或option2 # if [ option1 ] || [ option2 ] //知足option1或option2
本案例參考腳本
#!/bin/bash #監控網卡流量,當流量爲0,重啓網卡 #做者: #日期: #版本:v0.2 #設定語言爲英文 LANG=en #斷定系統是否已經安裝sysstat包,該包裏有sar命令 if ! rpm -q sysstat &>/dev/null then yum install -y sysstat fi #將10秒的網卡流量寫入到一個臨時文件裏 sar -n DEV 1 10 |grep 'ens33' > /tmp/ens33_sar.log #入口網卡流量 net_in=`grep '^Average:' /tmp/ens33_sar.log|awk '{print $5}'` #出口網卡流量 net_out=`grep '^Average:' /tmp/ens33_sar.log|awk '{print $6}'` #當入口和出口流量同時爲0時,說明網卡異常 if [ $net_in == "0.00" -a $net_out == "0.00" ] then echo "`date` ens33網卡出現異常,重啓網卡。">> /tmp/net.log ifdown ens33 && ifup ens33 fi
一分鐘執行一次,寫一個任務計劃便可。
需求擴展
網卡出問題,流量跌爲0的狀況很是罕見,但流量忽然大幅增漲的狀況卻是很廣泛。如今的需求是,當網卡(eth0)流量增幅超過1倍時須要抓1000個數據包存入一個以日期、時間爲名字的文件中,而且須要發郵件給admin@admin.com(不考慮告警收斂)。
參考腳本以下:
#!/bin/bash #監控網卡流量增幅超過一倍告警 #做者: #日期: #版本:v0.1 mail_user=admin@admin.com dir=/tmp/netlog [ -d $dir ] || mkdir $dir s_m=`lsattr -d $dir|awk '{print $1}' |sed 's/[^a]//g'` if [ $s_m != "a" ] then chattr +a $dir fi if ! rpm -q sysstat &>/dev/null then yum install -y sysstat fi sar -n DEV 1 10 |grep 'ens33' > /tmp/ens33_sar.log net_in=`grep '^Average:' /tmp/ens33_sar.log|awk '{print $5}'` net_out=`grep '^Average:' /tmp/ens33_sar.log|awk '{print $6}'` if [ ! -f $dir/net.log ] then echo "net_in $net_in" >> $dir/net.log echo "net_out $net_out" >> $dir/net.log exit 0 fi net_in_last=`tail -2 $dir/net.log|grep 'net_in'` net_out_last=`tail -2 $dir/net.log|grep 'net_out'` net_in_diff=`$[$net_in-$net_in_last]` net_out_diff=`$[$net_out-$net_out_last]` if [ $net_in_diff -gt $net_in_last ] then python mail.py $mail_user "網卡入口流量增幅異常" "增幅$net_in_diff" #這裏的mail.py參考案例二的知識點四 fi if [ $net_out_diff -gt $net_out_last ] then python mail.py $mail_user "網卡出口流量增幅異常" "增幅$net_out_diff" fi echo "net_in $net_in" >> $dir/net.log echo "net_out $net_out" >> $dir/net.log