如何获取 Linux/UNIX 上的当前网络接口吞吐量统计信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/596590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the current network interface throughput statistics on Linux/UNIX?
提问by DavidM
Tools such as MRTG provide network throughput / bandwidth graphs for the current network utilisation on specific interfaces, such as eth0. How can I return that information at the command line on Linux/UNIX?
MRTG 等工具为特定接口(例如 eth0)上的当前网络利用率提供网络吞吐量/带宽图。如何在 Linux/UNIX 上的命令行中返回该信息?
Preferably this would be without installing anything other than what is available on the system as standard.
最好不要安装除系统上可用的标准之外的任何东西。
采纳答案by Mehrdad Afshari
You can parse the output of ifconfig
您可以解析输出 ifconfig
回答by Johannes Weiss
回答by codelogic
You could parse /proc/net/dev.
你可以解析/proc/net/dev。
回答by ephemient
I wrote this dumb script a long time ago, it depends on nothing but Perl and Linux≥2.6:
我很久以前写过这个笨脚本,它只依赖于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 "3[H3[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 @_ = <>;
@_;
}
It just reads from /sys/class/net/$dev/statistics
every second, and prints out the current numbers and the average rate of change:
它只是/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 ---------^
回答by Philip Durbin
iftop does for network usage what top(1) does for CPU usage
-- http://www.ex-parrot.com/~pdw/iftop/
iftop does for network usage what top(1) does for CPU usage
-- http://www.ex-parrot.com/~pdw/iftop/
I don't know how "standard" iftop is, but I was able to install it with yum install iftop
on Fedora.
我不知道 iftop 的“标准”如何,但我可以yum install iftop
在 Fedora 上安装它。
回答by Philip Durbin
I find dstat to be quite good. Has to be installed though. Gives you way more information than you need. Netstat will give you packet rates but not bandwith also. netstat -s
我发现 dstat 非常好。不过必须安装。为您提供比您需要的更多的信息。Netstat 将为您提供数据包速率,但不会提供带宽。网络统计 -s
回答by miguel
Besides iftop and iptraf, also check:
除了 iftop 和 iptraf,还要检查:
bwm-ng
(Bandwidth Monitor Next Generation)
bwm-ng
(下一代带宽监视器)
and/or
和/或
cbm
(Color Bandwidth Meter)
cbm
(颜色带宽计)
ref: http://www.powercram.com/2010/01/bandwidth-monitoring-tools-for-ubuntu.html
参考:http: //www.powercram.com/2010/01/bandwidth-monitoring-tools-for-ubuntu.html
回答by filler
Got sar? Likely yes if youre using RHEL/CentOS.
有萨尔吗?如果您使用 RHEL/CentOS,可能是的。
No need for priv, dorky binaries, hacky scripts, libpcap, etc. Win.
不需要 priv、dorky 二进制文件、hacky 脚本、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
回答by Matthieu
I got another quick'n'dirty bash script for that:
为此,我得到了另一个 quick'n'dirty bash 脚本:
#!/bin/bash
IF=
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
It's considering that sleep 1
will actually last exactly one second, which is not true, but good enough for a rough bandwidth assessment.
它正在考虑sleep 1
实际上将持续一秒钟,这不是真的,但足以进行粗略的带宽评估。
Thanks to @ephemient for the /sys/class/net/<interface>
! :)
感谢@ephemient 的/sys/class/net/<interface>
!:)
回答by Xoroz
dstat
- Combines vmstat, iostat, ifstat, netstat information and moreiftop
- Amazing network bandwidth utility to analyse what is really happening on your ethnetio
- Measures the net throughput of a network via TCP/IPinq
- CLI troubleshooting utility that displays info on storage, typically Symmetrix. By default, INQ returns the device name, Symmetrix ID, Symmetrix LUN, and capacity.send_arp
- Sends out an arp broadcast on the specified network device (defaults to eth0), reporting an old and new IP address mapping to a MAC address.EtherApe
- is a graphical network monitor for Unix modeled after etherman. Featuring link layer, IP and TCP modes, it displays network activity graphically.iptraf
- An IP traffic monitor that shows information on the IP traffic passing over your network.
dstat
- 结合 vmstat、iostat、ifstat、netstat 信息等iftop
- 惊人的网络带宽实用程序,用于分析您的 eth 上真正发生的事情netio
- 通过 TCP/IP 测量网络的净吞吐量inq
- 显示存储信息的 CLI 故障排除实用程序,通常是 Symmetrix。默认情况下,INQ 返回设备名称、Symmetrix ID、Symmetrix LUN 和容量。send_arp
- 在指定的网络设备上发送 arp 广播(默认为 eth0),报告新旧 IP 地址映射到 MAC 地址。EtherApe
- 是模仿 etherman 的 Unix 图形网络监视器。它具有链路层、IP 和 TCP 模式,以图形方式显示网络活动。iptraf
- 一个 IP 流量监视器,显示有关通过您的网络的 IP 流量的信息。
More details: http://felipeferreira.net/?p=1194