Linux using shell scripts to measure high-speed network traffic

  
                

In Linux system operation, tools can be used to monitor network traffic, but there are certain limitations on the monitoring of high-speed networks. For high-speed network traffic monitoring, shell scripts can be used. The following small series will introduce you to Linux. A script that monitors high-speed network traffic.

In this article we describe a simple Shell script that can monitor network traffic and does not rely on slow libpcap library. These scripts support high-speed network interfaces of the size above Gb. If you are interested in the network traffic of "aggregation type", they can count the traffic on each network interface.

Scripts are primarily based on the sysfs virtual file system, a mechanism used by the kernel to output device or driver related information to user space. The relevant analysis data of the network interface will be output through “/sys/class/net/"ethX"/statistics”.

For example, the analysis report on the eth0 network port will be output to these files:

/sys/class/net/eth0/statistics/rx_packets: Received packet data

/sys/class/net/eth0/statistics/tx_packets: Number of packets transmitted

/sys/class/net/eth0/statistics/rx_bytes: Number of bytes received

/sys/class/net/eth0/statistics/tx_bytes: Number of bytes transferred

/sys/class/net/eth0/statistics/rx_dropped: The amount of data dropped when a packet is received < Br>

/sys/class/net/eth0/statistics/tx_dropped: The amount of data dropped by the transport packet

These data are automatically refreshed when the kernel data changes. Therefore, you can write a series of scripts to analyze and calculate traffic statistics. Here is the script like this (thanks to joemiller). The first script is to count the amount of data per second, including receive (RX) or send (TX). The latter is a description of the receive (RX) transmit (TX) bandwidth in network transmission. No tools are required for installation in these scripts.

Measuring network port data per second:

#! /bin/bash

INTERVAL=“1” # update interval in seconds

if [ -z “$1” ]; then

echo

echo usage: $0 [network-interface]

echo

echo eg $0 eth0

echo

echo shows packets-per-second

exit

fi

IF=$1

while true

do

R1=`cat /sys/class /net/$1/statistics/rx_packets`

T1=`cat /sys/class/net/$1/statistics/tx_packets`

sleep $INTERVAL

R2=` Cat /sys/class/net/$1/statistics/rx_packets`

T2=`cat /sys/class/net/$1/statistics/tx_packets`

TXPPS=`expr $T2 - $T1`

RXPPS=`expr $R2 - $R1`

echo “TX $1: $TXPPS pkts/s RX $1: $RXPPS pkts/s”

done
Previous12Next Total 2 Pages

Copyright © Windows knowledge All Rights Reserved