View the method of linux server traffic summary

  

To view the Linux server traffic is a little troublesome and not intuitive and ready-made methods, there are two methods of summarization, install third-party and homemade shell script tools to view NIC traffic.

Method 1:

Commonly used for Linux systems, but the installation method is different, iftop installation method under centos system, execute

yum install iftop

Installation, if the installation is not successful, the prompt fails. Compile and install, please compile and install, please confirm whether to install GCC, if the new system is not installed by default, run

yum install gcc

Compile and install iftop:

yum -y install flex byacc libpcap ncurses ncurses-devel libpcap-develwget http://www.ex-parrot.com/pdw/iftop/download/iftop-0.17.tar.gztar zxvf iftop-0.17.tar.gzcd iftop -0.17./configuremake && make install

debian/ubuntu system installation method, execute

apt-get install iftop

installation method: < Br>

iftop -i eth0

TX, send traffic; RX, receive traffic; TOTAL, total traffic; Cumm, run during iftop; peak, traffic peak; rates, representing 2 seconds, 10 Average flow in seconds and 40 seconds. The interface can use the shortcut keys: h help, n switch to display the IP host name, s whether to display the local information, d whether to display the remote information, N switch port service name, b switch whether the hour traffic graph bar.

Method 2:


1) You can use the ifconfig command to check the usage of NIC eth0

ifconfig eth0

eth0 Link encap :Ethernet HWaddr 00:50:56:B2:1D:65

inet addr:192.168.0.2 Bcast:192.168.0.255 Mask:255.255.255.0

inet6 addr: 2001:da8:20d :31:2::/64 Scope:Global

inet6 addr: 2001:da8:20d:31:250:56ff:feb2:1d65/64 Scope:Global

inet6 addr: fe80 ::250:56ff:feb2:1d65/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:949925 errors:0 dropped:0 overruns:0 frame :0

TX packets:476662 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:1350085212 (1.2 GiB) TX bytes:33019912 (31.4 MiB)


2) The last line can check the NIC receiving and sending bytes

ifconfig eth0 |  Grep bytes

RX bytes:1350100537 (1.2 GiB) TX bytes:33023756 (31.4 MiB)

3) The number of bytes is increasing over time, see current reception The number of bytes can be as follows

ifconfig eth0 | Grep bytes |  Awk '{print $2}' |  Awk -F ":" '{print $2}'

1350116823

ifconfig eth0 | Grep bytes |  Awk '{print $2}' |  Awk -F ":" '{print $2}'

1350120591


4) As the number of bytes grows, the value is taken every 1 second. Subtraction is the speed value of the second

ifconfig eth0 | Grep bytes |  Awk '{print $2}' |  Awk -F ":" '{print $2}' ; sleep 1s ; ifconfig eth0 | Grep bytes |  Awk '{print $2}' |  Awk -F ":" '{print $2}'

1350168724

1350168876


5) The return values ​​of the two commands are subtracted, Need to use script to do

vi RX.sh

Enter the following content

RX0=$(ifconfig eth0 | Grep bytes |  Awk '{print $2}' |  Awk -F ":" '{print $2}')

sleep 1s

RX1=$(ifconfig eth0 | Grep bytes |  Awk '{print $2}' |  Awk -F ":" '{print $2}')

echo $((RX1-RX0))

sh RX.sh

240

The obtained 240 is the real-time receiving network speed of the current network card is 240B/s


6) If you need to display the unit in KB/s or MB/s, you need to The original value is divided by 1024, but the default use echo division can only display integers, can not display decimals, here you need to use the awk command

vi RX.sh

modified to the following content

RX0=$(ifconfig eth0 | Grep bytes |  Awk '{print $2}' |  Awk -F ":" '{print $2}')

sleep 1s

RX1=$(ifconfig eth0 | Grep bytes |  Awk '{print $2}' |  Awk -F ":" '{print $2}')

awk "BEGIN{print ($RX1-$RX0)/1024}"

sh RX.sh < Br>

0.117188


7) Similarly, you can use the script to get the real-time receiving and real-time sending speed

vi Rb.sh

Enter the following content

RX0=$(ifconfig eth0 | Grep bytes |  Awk '{print $2}' |  Awk -F ":" '{print $2}')

TX0=$(ifconfig eth0 | Grep bytes |  Awk '{print $6}' |  Awk -F ":" '{print $2}')

sleep 1s

RX1=$(ifconfig eth0 | Grep bytes |  Awk '{print $2}' |  Awk -F ":" '{print $2}')

TX1=$(ifconfig eth0 | Grep bytes |  Awk '{print $6}' |  Awk -F ":" '{print $2}')

awk "BEGIN{print ($RX1-$RX0)/1024}" ; awk "BEGIN{print ($TX1- $TX0)/1024}"

sh Rb.sh

97.0039

11547.2

This is when I download the file from the client from the client , the resulting network speed, receiving 97.0039KB /s, sending 11547.2 KB /s

Copyright © Windows knowledge All Rights Reserved