Detailed Linux view of the real time network card traffic several ways

  • 2020-06-03 09:09:11
  • OfStack

At work, we often need to check the real-time network card traffic of the server. These are some of the ways we typically look at real-time network card traffic for Linux servers.

1. sar -n DEV 1 2

The sar command is included in the sysstat toolkit and provides many statistics for the system. Some systems offer sar that supports data statistics based on a network interface, as well as the number of packets and traffic per second on the device.


 sar  � n DEV 1 2

After the command 1, 2 means: take the value once every 1 second, take the value twice.

DEV displays network interface information

In addition, the -n parameter is useful, it has 6 different switches: DEV | EDEV | NFS | NFSD | SOCK | ALL, which represents the following meanings:

DEV displays network interface information. EDEV displays statistics about network errors. NFS statistics the NFS client information for activities. NFSD statistics NFS server information SOCK displays socket information ALL displays all five switches

[sre@CDVM-213017031 ~]$ sar -n DEV 1 2
Linux 2.6.32-431.el6.x86_64 (CDVM-213017031)  05/04/2017  _x86_64_ (4 CPU)

08:05:30 PM  IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
08:05:31 PM  lo  0.00  0.00  0.00  0.00  0.00  0.00  0.00
08:05:31 PM  eth0 1788.00 1923.00 930.47 335.60  0.00  0.00  0.00

08:05:31 PM  IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
08:05:32 PM  lo  0.00  0.00  0.00  0.00  0.00  0.00  0.00
08:05:32 PM  eth0 1387.00 1469.00 652.12 256.98  0.00  0.00  0.00

Average:  IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
Average:   lo  0.00  0.00  0.00  0.00  0.00  0.00  0.00
Average:   eth0 1587.50 1696.00 791.29 296.29  0.00  0.00  0.00

Parameter description:

IFACE: The LAN interface rxpck/s: Packets received per second txpck/s: Packets sent per second rxbyt/s: Number of bytes received per second txbyt/s: Number of bytes sent per second rxcmp/s: Compressed packets received per second txcmp/s: Compressed packets sent per second rxmcst/s: Multicast packets received per second rxerr/s: Bad packets per second received txerr/s: Bad packets sent per second coll/s: Collisions per second rxdrop/s: Number of received packets dropped per second due to full buffer txdrop/s: Number of sent packets dropped per second because of full buffer txcarr/s: Number of carrier errors per second when sending a packet rxfram/s: Number of frame alignment errors per second received rxfifo/s: Number of errors per second for FIFO overspeed of received packets txfifo/s: Number of FIFO overspeed errors per second per packet sent

This method is simple, intuitive and recommended.

2. Real-time monitoring scripts


#!/bin/bash

ethn=$1

while true
do
 RX_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
 TX_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
 sleep 1
 RX_next=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
 TX_next=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')

 clear
 echo -e "\t RX `date +%k:%M:%S` TX"

 RX=$((${RX_next}-${RX_pre}))
 TX=$((${TX_next}-${TX_pre}))

 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 "$ethn \t $RX $TX "

done

This script does not require additional software installation, and can be customized to view the interface, accurate to decimal, according to the size of the traffic can display units flexibly, the default acquisition interval of 1 second.

Usage:

1. Save the script as an executable file called ES83en.sh.

chmod + x. / net. sh change the file to an executable script.

3, sh ES94en. sh eth0 can start monitoring interface eth0 traffic, press ctrl+c to exit.

The script is generated by reading real-time network data from the runtime file system /proc/net/dev and doing simple calculations. For details of the directory /proc/net/dev, please see below.

3. cat /proc/net/dev

The Linux kernel provides a mechanism to access internal kernel data structures and change kernel Settings at runtime via the /proc file system. The proc file system is a pseudo-file system that exists only in memory, not in external memory. It provides an interface for operations that access system kernel data in the form of a file system. Users and applications can get information about the system through proc and can change some parameters of the kernel. Because system information, such as processes, changes dynamically, the proc file system is dynamically read and committed from the system kernel when a user or application reads the proc file. The /proc file system contains many directories where /proc/net/dev holds network adapters and statistics.


[sre@CDVM-213017031 ~]$ 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:137052296 108029 0 0 0  0   0   0 137052296 108029 0 0 0  0  0   0
 eth0:13661574714188 31346790620 0 0 0  0   0   0 5097461049535 27671144304 0 0 0  0  0   0

The leftmost interface is the name of the interface, Receive is packet receiving, Transmit is packet sending;

bytes represents the number of bytes sent and received; packets means the correct number of packets sent and received; errs represents the number of packets for sending and receiving errors; drop represents the number of packets sent and received and discarded;

In fact, we often use a lot of view network card real-time traffic command, are through reading the directory under the real-time traffic, and through simple calculation.

4. Use watch command to cooperate with ifconfig, more /proc/net/dev and cat /proc/net/dev for real-time monitoring. So watch-ES147en 1 "ifconfig eth0"


Every 1.0s: ifconfig eth0Thu May 4 20:26:45 2017

eth0  Link encap:Ethernet HWaddr FA:16:3E:7E:55:D1
   inet addr:10.213.17.31 Bcast:10.213.23.255 Mask:255.255.248.0
   inet6 addr: fe80::f816:3eff:fe7e:55d1/64 Scope:Link
   UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
   RX packets:31350149703 errors:0 dropped:0 overruns:0 frame:0
   TX packets:27674701465 errors:0 dropped:0 overruns:0 carrier:0
   collisions:0 txqueuelen:1000
   RX bytes:13663400883450 (12.4 TiB) TX bytes:5098104759633 (4.6 TiB)

watch helps you monitor the results of a single command without having to run it manually for a second time. Under Linux, watch periodically executes the next program and displays the results in full screen.

Finally, in addition to the above several, there are many ways to see the current system network card traffic, I will not repeat 11, if the above ways can not meet your needs, please google1.


Related articles: