Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取Linux/UNIX上当前的网络接口吞吐量统计数据?_Linux_Networking_Bandwidth - Fatal编程技术网

如何获取Linux/UNIX上当前的网络接口吞吐量统计数据?

如何获取Linux/UNIX上当前的网络接口吞吐量统计数据?,linux,networking,bandwidth,Linux,Networking,Bandwidth,MRTG等工具提供特定接口(如eth0)上当前网络利用率的网络吞吐量/带宽图。在Linux/UNIX上,如何在命令行返回该信息 最好不要安装系统上可用的标准配置以外的任何东西。我喜欢,但您可能必须安装它,而且它似乎不再被主动维护。您可以解析可以解析的ifconfig的输出。我很久以前写过这个愚蠢的脚本,它只依赖于Perl和Linux≥2.6: #!/usr/bin/perl use strict; use warnings; use POSIX qw(strftime); use Time:

MRTG等工具提供特定接口(如eth0)上当前网络利用率的网络吞吐量/带宽图。在Linux/UNIX上,如何在命令行返回该信息


最好不要安装系统上可用的标准配置以外的任何东西。

我喜欢,但您可能必须安装它,而且它似乎不再被主动维护。

您可以解析可以解析的
ifconfig

的输出。

我很久以前写过这个愚蠢的脚本,它只依赖于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 @_ = <>;
    @_;
}

iftop对网络使用的作用与top(1)对CPU使用的作用相同


我不知道iftop有多“标准”,但我可以在Fedora上使用
yum install-iftop
安装它。

我发现dstat非常好。但必须安装。为您提供比您需要的更多的信息。Netstat将提供数据包速率,但不会同时提供bandwith。netstat-s

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

  • bwm ng
    (下一代带宽监视器)
和/或

  • cbm
    (彩色带宽计)

参考:

得到sar了吗?如果您使用的是RHEL/CentOS,则可能是

不需要priv、呆板的二进制文件、黑客脚本、libpcap等

$ 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

我得到了另一个快速的“n”脏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
实际上只会持续一秒钟,这是不正确的,但足以进行粗略的带宽评估

感谢@ephemient提供的
/sys/class/net/
!:)

  • dstat
    -结合了vmstat、iostat、ifstat、netstat等信息
  • iftop
    -惊人的网络带宽实用程序,用于分析eth上真正发生的情况
  • netio
    -通过TCP/IP测量网络的净吞吐量
  • inq
    —CLI疑难解答实用程序,用于显示有关存储(通常是Symmetrix)的信息。默认情况下,INQ返回设备名称、Symmetrix ID、Symmetrix LUN和容量
  • send_arp
    -在指定的网络设备上发送arp广播(默认为eth0),报告到MAC地址的新旧IP地址映射
  • EtherApe
    -是一种模仿etherman的Unix图形网络监视器。它采用链路层、IP和TCP模式,以图形方式显示网络活动
  • iptraf
    -一种IP流量监视器,显示通过网络的IP流量信息
更多详情:

是实时监控带宽的好工具,通过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
另一个很好的工具是,也很容易获得:

             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
191Mb 381Mb 572Mb 763Mb 954Mb
└────────────┴──────────┴─────────────────────┴───────────┴──────────────────────
box4.local=>box-2.local 91.0Mb 27.0Mb 15.1Mb
box.local 560b 26.8kb 27.7kb
userify.com 0b 11.4kb 8.01kb
b、 Resolver.Level3.net 0b 58b 168b
stackoverflow.com 0b 42b 21b
224.0.0.251 0b 0b 179b
box-2.local 0b 0b 0b 0b
box.local 0b 0b 0b 0b

您可以使用iperf对网络性能(最大可能吞吐量)进行基准测试。 有关详细信息,请参阅以下链接:


如果您只想获得值,可以使用简单的shell oneliner,如下所示:

S=10; F=/sys/class/net/eth0/statistics/rx_bytes; X=`cat $F`; sleep $S; Y=`cat $F`; BPS="$(((Y-X)/S))"; echo $BPS
它将显示10秒期间的平均“每秒接收字节数”(您可以通过更改
S=10
参数来更改周期,也可以通过使用
tx_字节
而不是
rx_字节
来测量传输的BPS而不是接收的BPS)。不要忘记将
eth0
更改为要监视的网络设备

当然,您并不局限于显示平均速率(如其他答案中所述,还有其他工具可以向您显示更好的输出),但是这个解决方案可以轻松地编写脚本来执行其他操作

例如,以下shell脚本(为了可读性分成多行)仅在5分钟平均传输速度降至10kBPS以下时(可能是在其他占用带宽的进程完成时)才会执行offlineimap进程:


请注意,
/sys/class/…
是特定于Linux的(提交者确实选择了
Linux
标记),并且需要非旧式内核。Shell代码本身与/bin/sh兼容(因此不仅bash可以使用,dash和其他/bin/sh实现也可以使用),而且/bin/sh实际上一直都在安装。

我无法让parse-ifconfig脚本在AMI上运行,所以我可以用它来测量平均超过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

如果希望(ifconfig-a)的输出为json格式,则可以使用(python)

/proc。并非每个UNIX上都存在。由于Linux标记的缘故,假设为True,该OP只对Linux感兴趣。在3.17内核Seems上没有这样的文件等同于像ephemient脚本中那样解析
/sys/class/net/$dev/statistics
。有一个iptraf ng,对它们之间的差异有什么建议吗?iptraf ng是原始项目的一个分支。它应该具有所有原始功能和更多功能。这假设根访问可用(
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