Linux self papers - several ways to achieve traffic monitoring

1.sar -n DEV 1 2

sysstat sar command is included in the kit, the system provides a number of statistics. Its orders on different systems are some differences, sar some systems provide statistical data to support web-based interface, you can also view the number on the device and send and receive traffic packets per second.

1 sar -n DEV 1 2 1 after the command means 2: 1 takes every second value, taken twice. DEV display network interface information
2 fetching sar -n 1 2 EDEV display statistics on network errors.
3 sar -n NFS 1 2 Information NFS Client NFS statistical activities.
4 sar –n NFSD 1 2 Statistical information NFSD NFS server
5 sar –n SOCK 1 2 SOCK display socket information
6 sar –n ALL 1 2 ALL show 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
IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
LAN interface Packets received per second Data packets sent per second The number of bytes received per second The number of bytes transmitted per second Compressed data packets received per second Compressed packets sent per second Per second received multicast packet
rxerr / s txerr / s coll/s rxdrop / s txdrop/s txcarr/s rxfram/s rxfifo / s txfifo/s
Bad packets received per second Bad packets sent per second Per second conflict Because the buffer is full, the received data per the number of dropped packets Because the buffer is filled, per the number of discarded packets transmitted data When transmitting packets, the number of errors per second carrier Frame received packets per second alignment errors Receiving data packets over the number of errors per second FIFO speed FIFO data packet transmitted over the number of errors per second speed
2. Real-time monitoring script
#!/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 need to install additional software and check the interface can be customized to be accurate to one decimal, flexible display unit according to the size of the flow, the default collection interval is 1 second.

Usage is:

1. Save the script as an executable script file, such as call net.sh.

2, chmod + x ./net.sh files into executable scripts.

3, sh net.sh eth0 to start eth0 monitor traffic interfaces, press ctrl + c to exit.

Script is real-time data network file system / proc / net / dev is run by reading time, and obtained by simple calculation. About the directory / proc / net / dev, please see below.

3.cat /proc/net/dev

Linux kernel provides a mechanism for the / proc file system access internal kernel data structures at runtime, change the kernel settings. proc file system is a pseudo file system exists only among the memory, external memory without taking up space. It is the operating system kernel to access the data in a manner that provides an interface to the file system. Users and applications can be obtained by the information system proc, certain parameters may be changed and the kernel. Due to information systems, such as the process is dynamically changing, so the user or application reads the proc file, proc file system is a dynamic system kernel is read out from the required information and submit. / Proc filesystem contains a number of directories, which / proc / net / dev save the network adapter 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 represents the name of the interface, Receive, acknowledged the package, Transmit represents sending packets;

bytes packets errs drop
Indicates the number of bytes received and transmitted Data transmitting and receiving the correct amount of packet Data transmitting and receiving an error packet amount Represents an amount of packets dropped transceiver

In fact, many view real-time traffic card command we usually often used, is by reading the real-time traffic in the directory, and by simply calculated.

4. Use the watch command, with ifconfig, more / proc / net / dev, cat / proc / net / dev to real-time monitoring.

For example, to perform watch -n 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 can help you monitor the operating results of a command, to save you manually run over and over again. In Linux, watch the next periodic programs and full screen displays the result.

Finally, in addition to a few, there are many ways to look at the card can be provided above the flow of the current system, I will not repeat all of the way if you can not meet your needs, please google it yourself now.

2 According to its ease of use and readability, it is strongly recommended Tier 1 and Tier. I want to help learning, I hope you will support script home.

发布了32 篇原创文章 · 获赞 181 · 访问量 2万+

Guess you like

Origin blog.csdn.net/weixin_45728976/article/details/104092116