Linux shell script Real-time monitor network card traffic

Source: Internet
Author: User
Tags time interval netspeed


Through the/proc/net/dev can be viewed to the network card traffic, according to the changes in the value of the file in conjunction with while loop write a real-time monitoring network card script, by specifying the network card name as parameters to monitor the traffic of the specified network card

#!/bin/bash
While ["1"]
Todo
Eth=$1
rxpre=$ (Cat/proc/net/dev | grep $eth | tr: "" | awk ' {print $} ')
txpre=$ (Cat/proc/net/dev | grep $eth | tr: "" | awk ' {print $} ')
Sleep 1
rxnext=$ (Cat/proc/net/dev | grep $eth | tr: "" | awk ' {print $} ')
txnext=$ (Cat/proc/net/dev | grep $eth | tr: "" | awk ' {print $} ')
Clear
Echo-e "T RX ' date +%k:%m:%s ' TX"
rx=$ ((${rxnext}-${rxpre}))
tx=$ ((${txnext}-${txpre}))
if [[$RX-lt 1024]];then
rx= "${RX}B/S"
elif [[$RX-gt 1048576]];then
rx=$ (echo $RX | awk ' {print $1/1048576 ' MB/S "} ')
Else
rx=$ (echo $RX | awk ' {print $1/1024 ' kb/s '} ')
Fi
if [[$TX-lt 1024]];then
tx= "${TX}B/S"
elif [[$TX-gt 1048576]];then
tx=$ (echo $TX | awk ' {print $1/1048576 ' MB/S "} ')
Else
tx=$ (echo $TX | awk ' {print $1/1024 ' kb/s '} ')
Fi
Echo-e "$eth t $RX $TX"
Done

Example 2, awk implementation of real-time monitoring network card traffic script (Common Application II)

Implementation principle:

[Chengmo@localhost ~]$ Cat/proc/net/dev
inter-|  Receive | Transmit
Face |bytes packets errs drop FIFO frame compressed multicast|bytes packets errs Drop FIFO colls carrier compressed
lo:1068205690 1288942839 0 0 0 0 0 0 1068205690 1288942839 0 0 0 0-0 0
eth0:91581844 334143895 0 0 0 0 0 145541676 4205113078 3435231517 0 0 0 0-0 0

The Proc/net/dev file saves the total traffic information of the NIC, and adds the network card to the record through interval interval. The actual rate is obtained before subtracting.

Program code:

awk ' begin{
Ofmt= "%.3f";
Devf= "/proc/net/dev";
while (("Cat" DEVF) | Getline)
{
if ($ ~/:/&& ($10+0) > 0)
{
Split ($1,tarr, ":");
NET[TARR[1]]=$10+TARR[2];
Print tarr[1],$10+tarr[2];
}
}
Close (DEVF);
while (System (' Sleep 1 ') >=0)
{
System ("clear");
while (Getline < DEVF)
{
if ($ ~/:/&& ($10+0) > 0)
{
Split ($1,tarr, ":");
if (tarr[1] in net)
{
Print tarr[1], ":", ($10+tarr[2]-net[tarr[1]]) *8/1024, "kb/s";
NET[TARR[1]]=$10+TARR[2];
}
}
}
Close (DEVF);
}
}'

Description: The first while is to obtain the total initial value, is the network card out of traffic, $ is the network card into traffic. The 2nd while will be started one time interval of 1 seconds. Calculating the total flow difference gets the average flow per second.

Note: Close is required to read the file line by Getline. Otherwise, the data cannot be obtained in the 2nd while loop.


Example 3


Network Card monitoring footsteps:

A script command to monitor the real-time traffic of the network card.

Script code content:

#!/bin/bash
If [-Z "$"]; Then
Echo
echo Usage: $ network-interface
Echo
echo e.g. $ eth0
Echo
Exit
Fi
If=$1
While True
Todo
r1= ' Cat/sys/class/net/$1/statistics/rx_bytes '
t1= ' Cat/sys/class/net/$1/statistics/tx_bytes '
Sleep 1
R2= ' Cat/sys/class/net/$1/statistics/rx_bytes '
T2= ' Cat/sys/class/net/$1/statistics/tx_bytes '
tbps= ' Expr $T 2-$T 1 '
rbps= ' Expr $R 2-$R 1 '
tkbps= ' expr $TBPS/1024 '
rkbps= ' expr $RBPS/1024 '
Echo "TX $: $TKBPS kb/s Rx $: $RKBPS kb/s"
Done

Footstep Use Method:

1. In order for the script file to be convenient to use, it is placed in the/sbin directory.


#cd/sbin
#vim Netspeed
Enter Footstep content to save exit.


#chmod +x Netspeed

2. Use Footsteps command:

#netspeed eth0

(This is based on the host network card information input adapter name, if not clear network card information, you can use the Ifconfig command to view)

Related Article

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.