如何获得Linux / UNIX上的当前networking接口吞吐量统计信息?

诸如MRTG之类的工具为特定接口(例如eth0)上的当前networking利用率提供networking吞吐量/带宽图。 如何在Linux / UNIX的命令行上返回这些信息?

最好这样做不需要安装标准以外的东西。

你可以parsingifconfig的输出

iftop does for network usage what top(1) does for CPU usagehttp://www.ex-parrot.com/~pdw/iftop/

我不知道iftop是如何“标准”的,但是我可以在Fedora上用yum install iftop安装它。

得到了吗? 可能是的,如果你使用RHEL / CentOS。

不需要priv,dorky二进制文件,hacky脚本,libpcap等。Win。

 $ sar -n DEV 1 3 Linux 2.6.18-194.el5 (localhost.localdomain) 10/27/2010 02:40:56 PM IFACE rxpck/s txpck/s rxbyt/s txbyt/s rxcmp/s txcmp/s rxmcst/s 02:40:57 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 02:40:57 PM eth0 10700.00 1705.05 15860765.66 124250.51 0.00 0.00 0.00 02:40:57 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 02:40:57 PM IFACE rxpck/s txpck/s rxbyt/s txbyt/s rxcmp/s txcmp/s rxmcst/s 02:40:58 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 02:40:58 PM eth0 8051.00 1438.00 11849206.00 105356.00 0.00 0.00 0.00 02:40:58 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 02:40:58 PM IFACE rxpck/s txpck/s rxbyt/s txbyt/s rxcmp/s txcmp/s rxmcst/s 02:40:59 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 02:40:59 PM eth0 6093.00 1135.00 8970988.00 82942.00 0.00 0.00 0.00 02:40:59 PM eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 Average: IFACE rxpck/s txpck/s rxbyt/s txbyt/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 8273.24 1425.08 12214833.44 104115.72 0.00 0.00 0.00 Average: eth1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 

我很久以前写过这个笨笨的脚本,除了Perl和Linux≥2.6以外,

 #!/usr/bin/perl use strict; use warnings; use POSIX qw(strftime); use Time::HiRes qw(gettimeofday usleep); my $dev = @ARGV ? shift : 'eth0'; my $dir = "/sys/class/net/$dev/statistics"; my %stats = do { opendir +(my $dh), $dir; local @_ = readdir $dh; closedir $dh; map +($_, []), grep !/^\.\.?$/, @_; }; if (-t STDOUT) { while (1) { print "\033[H\033[J", run(); my ($time, $us) = gettimeofday(); my ($sec, $min, $hour) = localtime $time; { local $| = 1; printf '%-31.31s: %02d:%02d:%02d.%06d%8s%8s%8s%8s', $dev, $hour, $min, $sec, $us, qw(1s 5s 15s 60s) } usleep($us ? 1000000 - $us : 1000000); } } else {print run()} sub run { map { chomp (my ($stat) = slurp("$dir/$_")); my $line = sprintf '%-31.31s:%16.16s', $_, $stat; $line .= sprintf '%8.8s', int (($stat - $stats{$_}->[0]) / 1) if @{$stats{$_}} > 0; $line .= sprintf '%8.8s', int (($stat - $stats{$_}->[4]) / 5) if @{$stats{$_}} > 4; $line .= sprintf '%8.8s', int (($stat - $stats{$_}->[14]) / 15) if @{$stats{$_}} > 14; $line .= sprintf '%8.8s', int (($stat - $stats{$_}->[59]) / 60) if @{$stats{$_}} > 59; unshift @{$stats{$_}}, $stat; pop @{$stats{$_}} if @{$stats{$_}} > 60; "$line\n"; } sort keys %stats; } sub slurp { local @ARGV = @_; local @_ = <>; @_; } 

它每秒钟从/sys/class/net/$dev/statistics读取/sys/class/net/$dev/statistics ,并打印出当前数字和平均变化率:

 $ ./net_stats.pl eth0 rx_bytes : 74457040115259 4369093 4797875 4206554 364088 rx_packets : 91215713193 23120 23502 23234 17616 ... tx_bytes : 90798990376725 8117924 7047762 7472650 319330 tx_packets : 93139479736 23401 22953 23216 23171 ... eth0 : 15:22:09.002216 1s 5s 15s 60s ^ current reading ^-------- averages ---------^ 

你可以parsing/ proc / net / dev 。

  • dstat – 结合vmstat,iostat,ifstat,netstat信息等等
  • iftop – 惊人的networking带宽实用程序来分析您的eth上真正发生的事情
  • netio – 通过TCP / IP测量networking的净吞吐量
  • inq – CLI故障排除实用程序,显示有关存储的信息,通常是Symmetrix。 默认情况下,INQ返回设备名称,Symmetrix ID,Symmetrix LUN和容量。
  • send_arp – 在指定的networking设备上发送一个arp广播(默认为eth0),报告映射到MAC地址的旧的和新的IP地址。
  • EtherApe – 是一个以etherman为模型的Unix的graphics化networking监视器。 具有链路层,IP和TCP模式,以graphics方式显示networking活动。
  • iptraf – 一个IPstream量监视器,显示有关通过您的networking的IPstream量的信息。

更多细节: http : //felipeferreira.net/?p=1194

nload是一个实时监控带宽的好工具,可以通过sudo apt-get install nload轻松安装在Ubuntu或Debian中。

 Device eth0 [10.10.10.5] (1/2): ===================================================================================== Incoming: . ...| # ####| .. |#| ... #####. .. Curr: 2.07 MBit/s ###.### #### #######|. . ## | Avg: 1.41 MBit/s ########|#########################. ### Min: 1.12 kBit/s ........ ################################### .### Max: 4.49 MBit/s .##########. |###################################|##### Ttl: 1.94 GByte Outgoing: ########## ########### ########################### ########## ########### ########################### ##########. ########### .########################### ########### ########### ############################# ########### ###########..############################# ############ ########################################## ############ ########################################## ############ ########################################## Curr: 63.88 MBit/s ############ ########################################## Avg: 32.04 MBit/s ############ ########################################## Min: 0.00 Bit/s ############ ########################################## Max: 93.23 MBit/s ############## ########################################## Ttl: 2.49 GByte 

另一个优秀的工具是iftop ,也很容易获得:

  191Mb 381Mb 572Mb 763Mb 954Mb └────────────┴──────────┴─────────────────────┴───────────┴────────────────────── box4.local => box-2.local 91.0Mb 27.0Mb 15.1Mb <= 1.59Mb 761kb 452kb box4.local => box.local 560b 26.8kb 27.7kb <= 880b 31.3kb 32.1kb box4.local => userify.com 0b 11.4kb 8.01kb <= 1.17kb 2.39kb 1.75kb box4.local => b.resolvers.Level3.net 0b 58b 168b <= 0b 83b 288b box4.local => stackoverflow.com 0b 42b 21b <= 0b 42b 21b box4.local => 224.0.0.251 0b 0b 179b <= 0b 0b 0b 224.0.0.251 => box-2.local 0b 0b 0b <= 0b 0b 36b 224.0.0.251 => box.local 0b 0b 0b <= 0b 0b 35b ───────────────────────────────────────────────────────────────────────────────── TX: cum: 37.9MB peak: 91.0Mb rates: 91.0Mb 27.1Mb 15.2Mb RX: 1.19MB 1.89Mb 1.59Mb 795kb 486kb TOTAL: 39.1MB 92.6Mb 92.6Mb 27.9Mb 15.6Mb 

不要忘记旧版* nix上的经典和强大的sar和netstat实用程序!

除了iftop和iptraf之外,还请检查:

  • bwm-ng (下一代带宽监视器)

和/或

  • cbm (彩色带宽表)

ref: http : //www.powercram.com/2010/01/bandwidth-monitoring-tools-for-ubuntu.html

我得到了另一个quick'n'dirty bash脚本:

 #!/bin/bash IF=$1 if [ -z "$IF" ]; then IF=`ls -1 /sys/class/net/ | head -1` fi RXPREV=-1 TXPREV=-1 echo "Listening $IF..." while [ 1 == 1 ] ; do RX=`cat /sys/class/net/${IF}/statistics/rx_bytes` TX=`cat /sys/class/net/${IF}/statistics/tx_bytes` if [ $RXPREV -ne -1 ] ; then let BWRX=$RX-$RXPREV let BWTX=$TX-$TXPREV echo "Received: $BWRX B/s Sent: $BWTX B/s" fi RXPREV=$RX TXPREV=$TX sleep 1 done 

这是考虑sleep 1实际上会持续一秒钟,这是不正确的,但足够的宽带带宽评估。

感谢/sys/class/net/<interface> @ephemient! 🙂

我喜欢iptraf但你可能不得不安装它,似乎没有被主动维护了。

如果你只是想获得价值,你可以使用简单的shell oneliner像这样:

 S=10; F=/sys/class/net/eth0/statistics/rx_bytes; X=`cat $F`; sleep $S; Y=`cat $F`; BPS="$(((YX)/S))"; echo $BPS 

它将向您显示10秒钟的平均“每秒接收字节数”(您可以通过更改S=10参数来更改周期,并且可以使用tx_bytes而不是rx_bytes来测量传输的BPS,而不是接收的BPS。 不要忘记将eth0更改为您要监控的networking设备。

当然,你并不限于显示平均速度(正如其他答案中提到的,还有其他工具可以显示更好的输出),但是这个解决scheme很容易编写其他的东西。

例如,下面的shell脚本(为了便于阅读而分成多行)只有当5分钟的平均传输速度下降到10kBPS以下时(大概是其他带宽消耗过程结束时)才会执行offlineimap进程:

 #!/bin/sh S=300; F=/sys/class/net/eth0/statistics/tx_bytes BPS=999999 while [ $BPS -gt 10000 ] do X=`cat $F`; sleep $S; Y=`cat $F`; BPS="$(((YX)/S))"; echo BPS is currently $BPS done offlineimap 

请注意, /sys/class/...是特定于Linux的(这是可以的,因为提交者确实select了linux标签),并且需要非古老的内核。 Shell代码本身是/ bin / sh兼容的(所以不仅bash,而且破折号和其他/ bin / sh实现将工作)和/ bin / sh是真的总是安装。

我觉得dstat是相当不错的。 必须安装。 给你更多的信息比你需要的。 Netstat会给你包率,但不带宽带也。 netstat -s

您可以使用iperf来评估networking性能( 最大可能吞吐量 )。 详细信息请参阅以下链接:

http://en.wikipedia.org/wiki/Iperf

https://iperf.fr/

https://code.google.com/p/iperf/

我不能得到parsingifconfig脚本为我在AMI工作,所以得到这个工作测量收到的stream量平均超过10秒

 date && rxstart=`ifconfig eth0 | grep bytes | awk '{print $2}' | cut -d : -f 2` && sleep 10 && rxend=`ifconfig eth0 | grep bytes | awk '{print $2}' | cut -d : -f 2` && difference=`expr $rxend - $rxstart` && echo "Received `expr $difference / 10` bytes per sec" 

对不起,它是如此便宜和讨厌,但它的工作!

 ifconfig -a ip -d link ls -l /sys/class/net/ (physical and virtual devices) route -n 

如果你想以json格式输出(ifconfig -a),你可以使用这个 (python)