Application of script three: real-time monitoring network card traffic

Source: Internet
Author: User
Tags clear screen

function: real-time monitor the rate of network card receiving and sending, as well as the total amount of data, and set alarm value.

beta version:CentOS Series

implementation: through the ifconfig command to obtain data, the calculation of the same position after the cycle of printing information, once a second; tput bel command Alarm

Insufficient: only one network card can be monitored at a time;

Since 7.x is different from 5.x and 6.x,ifconfig, the relevant parameters need to be specified manually according to the system version, 7.x please set the first position parameter to "7", the other version is "6" (default, not specified)


Demonstrate:


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/F0/wKiom1cNoGKzcM72AABKdjhG9g0957.png "title=" 1.png " alt= "Wkiom1cnogkzcm72aabkdjhg9g0957.png"/>

The script will automatically list the discovered network card, select the network card to monitor, you need to specify the network card name, not serial number!

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7E/ED/wKioL1cNob-TZ5nJAAAiMSidclE626.png "title=" 2.png " alt= "Wkiol1cnob-tz5njaaaimsidcle626.png"/>

HR and HT are alarm values set in the script, with a sound alarm when the packet rate exceeds any of the corresponding values, in Kbps

Script 6s Clear screen once, correct user error button caused by the display confusion:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/ED/wKioL1cNoteyN0xRAAAxsOXM3ac218.png "title=" 3.png " alt= "Wkiol1cnoteyn0xraaaxsoxm3ac218.png"/>


Code area:

#/bin/bash#  ly#  ------------------#  copyright 2016.4.13, lingyi   ([email protected])  QQ:1519952564#   "Monitor network traffic" #系统版本选择 # centos 5.x 6.x ->  ' func6x ' #CentOS  7.x ->  ' func7x ' func=${1:-func6x}[[  $1 ==  ' 6 '  ]] && func=func6x[[ $1 ==  ' 7 '  ]]  && func=func7x# Alarm parameter Setting #kbpsspeed_alarm_rx=${2:-10000}speed_alarm_tx=${3:-10000}alarm_times=2# secondalarm_speed=0.5[[  $func  ==  ' func6x '  | |   $func  ==  ' func7x '  ]] | |  { echo -e  "variable func wrong !! \N$0&NBSP;[6|7] "&NBSP;&AMP;&AMP;&NBSP;EXIT;&NBSP; #工具安装检查for  tool in net-tools bc;  doif ! rpm -q --quiet  $tool; thenecho -n  "Install  $tool,  WAIT&NBSP, yum install -y  $tool  >/dev/null 2>&1[[ $? -eq 0 ]] && echo ok | |  { echo fail && exit 1; }fidone# print NIC list, receive user input [[ $# -ne  0 ]] && ifconfig $1 >/dev/null 2>&1 &&  setnic=1 && Nic=$1while [[  $setnic  -ne 1 ]]; doifconfig  | grep  ' ^[a-za-z0-9] ' |awk  ' {print  "[", NR, "]", $ ' read -p  "input the  Nic:   Nic[[ -z  $Nic  ]] && continueifconfig  $Nic   >/dev/null 2>&1 && breakdone# uses no function to obtain traffic-related values for different system versions func6x () {rx_sum=$ (  ifconfig $1 | grep -i tx | grep -i rx | awk  ' { print $3,$4} '  | tr -d  ' [()] '  )         tx _sum=$ ( ifconfig $1 | grep -i tx | grep -i Rx | awk  ' {print $7,$8} '  | tr  -d  ' [()] '  ]         rx_before=$ ( ifconfig $1  | grep -i tx | grep -i Rx | awk  ' {print $2} '  | tr -c -d  ' 0-9\n '  )         tx_before=$ ( ifconfig $1 | grep -i tx | grep -i rx | awk   ' {print $6} '  | tr -c -d  ' 0-9\n '  )          sleep 1        rx_after=$ ( ifconfig $1  | grep -i tx | grep -i Rx | awk  ' {print $2} '  | tr -c -d  ' 0-9\n '  )         tx_after=$ (  ifconfig $1 | grep -i tx | grep -i rx | awk  ' {print $6} '  | tr  -c -d  ' 0-9\n '  )         rx=$ ( echo  " scale=3;  ( ${Rx_After} - ${Rx_Before} )  / 1024 " | bc )          tx=$ ( echo  "scale=3; " ( ${tx_after} - $ {tx_before} )  / 1024 " | bc )}func7x () {         rx_sum=$ ( ifconfig eth0 | grep -i  ' rx packets '  | awk  ' {print $6,$7} ' |tr -d  ' [()] '  )         tx_sum=$ (  ifconfig eth0 | grep -i  ' tx packets '  | awk  ' {print $6, $7} ' |tr -d  ' [()] '  )         rx_before=$ ( ifconfig  eth0 | grep -i  ' rx packets '  | awk  ' {print $5} '  )          tx_before=$ ( ifconfig eth0 | grep -i  ' tx packets '  | awk  ' {print $5} '  )         sleep 1         rx_after=$ ( ifconfig eth0 | grep -i   ' rx packets '  | awk  ' {print $5} '  )          tx_after=$ ( ifconfig eth0 | grep -i  ' tx packets '  |  awk  ' {print $5} '  )         rx=$ ( echo  scale=3 ;  ( ${Rx_After} - ${Rx_Before} )  / 1024 " | bc )          tx=$ ( echo  "scale=3; " ( ${tx_after} - ${tx_ before} )  / 1024 " | bc )} #报警函数Alarm () {    [[ $ (awk -v n1=$1 -v n2=${ speed_alarm_rx}  ' Begin{print (n1>=n2)? " 0 ":" 1 "} ')  -ne 0 ]] && {    [[ $ (awk -v n1= $2 -v n2=${speed_alarm_tx}  ' Begin{print (n1>=n2)? " 0 ":" 1 "} ')  -ne 0 ]] && return; }    for ((i=1; i <=alarm_times; i++)); do tput bel; sleep  $Alarm _speed; done &}# Signal capture, processing "CTRL + C" Operation tput cup 0 0; tput ed; tput civis; trap  ' clear;  tput cnorm; exit 0 '  2set  $Nic;  i=1while true; dotput cup  0 0$func $1# determines whether the receive and send rates exceed the threshold [[ $ (Echo ${rx} | cut -b 1)  ==   "."  ]] && rx=0.$ (echo ${rx} | awk -f  ".")   ' {print $2} ') [[ $ (echo ${tx} | cut -b 1)  ==  "."  ]] && tx=0.$ (echo ${tx} | awk -f  ".")   ' {print $2} ') #6秒清屏一次 [[ $ ( expr ${i} % 7 )  -eq 6 ]]  && tput edAlarm  $Rx   $Txprintf   " [ ${1} ]\thr: ${ speed_alarm_rx}  ht:${speed_alarm_tx}\n "printf "%50s\n " " " | tr   "   '-'   printf  '  %-5s%-11s%-4s\n %-5s%-11s%-4s\n '  rx: ${rx} kb/s  Tx: ${Tx} kB/secho -e  " rx:  ${rx_sum}\n tx:  ${tx_sum }\n\t\t\t$ ( date  "+%h:%m:%s %y.%m.%d"  ) "printf "%50s\n " " "|tr  '   '   '-' let i++done




This article is from the "retrograde person" blog, please be sure to keep this source http://lingyi.blog.51cto.com/2837715/1763281

Application of script three: real-time monitoring network card traffic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.