您的位置:首页 > 理论基础 > 计算机网络

NetHogs——Linux下按进程实时统计网络带宽利用率

2014-04-06 11:06 375 查看
本文由 极客范 -
LQ 翻译自
Ravi Saive。欢迎加入极客翻译小组,同我们一道翻译与分享。转载请参见文章末尾处的要求。


网上有很多适用于Linux操作系统的开源网络监视工具.比如说,你可以用命令iftop来检查带宽使用情况. netstat用来查看接口统计报告,还有top监控系统当前运行进程.但是如果你想要找一个能够按进程实时统计网络带宽利用率,那么NetHogs就是你所需要的唯一工具.






NetHogs – 网络带宽监控

什么是NetHogs?

NetHogs 是一个开源的命令行工具(类似于Linux的top命令),用来按进程或程序实时统计网络带宽使用率.

来自NetHogs项目网站:

NetHogs是一个小型的’net top’工具,不像大多数工具那样拖慢每个协议或者是每个子网的速度而是按照进程进行带宽分组.NetHogs NetHogs不需要依赖载入某个特殊的内核模块. 如果发生了网络阻塞你可以启动NetHogs立即看到哪个PID造成的这种状况.这样就很容易找出哪个程序跑飞了然后突然占用你的带宽.

这篇文章为你讲解了如何在Unix/Linux操作系统下如何安装和使用NetHogs按进程监控网络带宽使用率。

在RHEL,CentOS和Fedora下安装NetHogs:

要安装NetHogs,你必须要启用你所用Linux下的EPEL源.然后运行下面的yum命令下载安装NetHogs包.

# yum install nethogs

1

# yum install nethogs

样例输出

[root@tecmint ~]# yum -y install nethogs

Loaded plugins: fastestmirror, refresh-packagekit
Loading mirror speeds from cached hostfile
* base: mirrors.hns.net.in
* epel: mirror.nus.edu.sg
* extras: mirrors.hns.net.in
* rpmfusion-free-updates: mirrors.ustc.edu.cn
* rpmfusion-nonfree-updates: mirror.de.leaseweb.net
* updates: mirrors.hns.net.in
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package nethogs.i686 0:0.8.0-1.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================
Package Arch Version Repository Size
===========================================================================================================
Installing:
nethogs i686 0.8.0-1.el6 epel 28 k

Transaction Summary
===========================================================================================================
Install 1 Package(s)

Total download size: 28 k
Installed size: 50 k
Downloading Packages:
nethogs-0.8.0-1.el6.i686.rpm | 28 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : nethogs-0.8.0-1.el6.i686 1/1
Verifying : nethogs-0.8.0-1.el6.i686 1/1

Installed:
nethogs.i686 0:0.8.0-1.el6

Complete!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

[root@tecmint
~]# yum -y install nethogs

Loaded
plugins:
fastestmirror,
refresh-packagekit

Loading mirror speeds
from cached hostfile
*
base:
mirrors.hns.net.in

*
epel:
mirror.nus.edu.sg
*
extras:
mirrors.hns.net.in

*
rpmfusion-free-updates:
mirrors.ustc.edu.cn
*
rpmfusion-nonfree-updates:
mirror.de.leaseweb.net

* updates:
mirrors.hns.net.in
Setting
up Install Process

Resolving Dependencies
-->
Running transaction
check

--->
Package nethogs.i686
0:0.8.0-1.el6
will be installed
-->
Finished Dependency
Resolution

Dependencies
Resolved

===========================================================================================================

Package Arch Version Repository Size
===========================================================================================================

Installing:
nethogs i686 0.8.0-1.el6 epel 28
k

Transaction
Summary

===========================================================================================================
Install
1 Package(s)

Total
download size:
28 k

Installed size:
50 k
Downloading
Packages:

nethogs-0.8.0-1.el6.i686.rpm | 28
kB 00:00
Running
rpm_check_debug

Running Transaction Test
Transaction
Test Succeeded

Running Transaction
Installing
: nethogs-0.8.0-1.el6.i686 1/1

Verifying :
nethogs-0.8.0-1.el6.i686 1/1

Installed:
nethogs.i686
0:0.8.0-1.el6

Complete!

在Ubuntu, Linux mint和Debian下安装NetHogs:

键入apt-get命令安装NetHogs包

$ sudo apt-get install nethogs

1

$
sudo apt-get
install nethogs

样例输出

tecmint@tecmint:~$ sudo apt-get install nethogs

[sudo] password for tecmint:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
nethogs
0 upgraded, 1 newly installed, 0 to remove and 318 not upgraded.
Need to get 27.1 kB of archives.
After this operation, 100 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu/ quantal/universe nethogs i386 0.8.0-1 [27.1 kB]
Fetched 27.1 kB in 1s (19.8 kB/s)
Selecting previously unselected package nethogs.
(Reading database ... 216058 files and directories currently installed.)
Unpacking nethogs (from .../nethogs_0.8.0-1_i386.deb) ...
Processing triggers for man-db ...
Setting up nethogs (0.8.0-1) ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

tecmint@tecmint:~$
sudo apt-get
install nethogs

[sudo]
password for
tecmint:

Reading package
lists...
Done
Building
dependency tree

Reading state information...
Done
The
following NEW
packages will
be installed:

nethogs
0
upgraded,
1 newly
installed,
0 to
remove and
318 not
upgraded.

Need to
get 27.1
kB of archives.
After
this operation,
100 kB
of additional
disk space will
be used.

Get:1 http://in.archive.ubuntu.com/ubuntu/ quantal/universe nethogs i386 0.8.0-1 [27.1 kB]
Fetched
27.1 kB
in 1s
(19.8
kB/s)

Selecting previously
unselected package
nethogs.
(Reading
database ...
216058 files
and directories
currently installed.)

Unpacking nethogs
(from
.../nethogs_0.8.0-1_i386.deb)
...
Processing
triggers for
man-db
...

Setting up nethogs
(0.8.0-1)
...

NetHogs用法

在基于RedHat系统下键入如下命令启动NetHogs工具.

# nethogs

1

# nethogs

要执行NetHogs你必须拥有root权限,即如图所示用sudo命令

$ sudo nethogs

1

$
sudo nethogs

样例预览:




CentOS 6.3 下的NetHogs预览





Ubuntu 12.10 下的NetHogs预览
正如上图所示, send列和received列显示的是按照每个进程的流量统计.总的收发数据带宽在最下方,而且可以用交互命令控制排序,下面将要讨论这些交互命令:

NetHogs 命令行参数

以下就是NetHogs命令行的参数,用’-d’来添加刷新频率参数,’device name’ 用来检测给定的某个或者某些设备的带宽(默认是eth0).例如:设置5秒钟的刷新频率,键入如下命令即可:

# nethogs -d 5

1

# nethogs -d 5

$ sudo nethogs -d 5

1

$
sudo nethogs
-d
5

如果只用来监视设备(eth0)的网络带宽可以使用如下命令:

# nethogs eth0

1

# nethogs eth0

$ sudo nethogs eth0

1

$
sudo nethogs eth0

如果要同时监视eth0和eth1接口,使用以下命令即可:

# nethogs eth0 eth1

1

# nethogs eth0 eth1

$ sudo nethogs eth0 eth1

1

$
sudo nethogs eth0
eth1

其他参数和用法

-d : delay for refresh rate.
-h : display available commands usage.
-p : sniff in promiscious mode (not recommended).
-t : tracemode.
-V : prints Version info.

1
2
3
4
5

-d
: delay
for refresh
rate.

-h
: display
available commands
usage.
-p
: sniff
in promiscious
mode (not
recommended).

-t
: tracemode.
-V
: prints
Version info.

NetHogs 交互控制

以下是NetHogs的一些很有用的交互控制(键盘快捷键)

-m : Change the units displayed for the bandwidth in units like KB/sec -> KB -> B-> MB.
-r : Sort by magnitude of respectively traffic.
-s : Sort by magnitude of sent traffic.
-q : Hit quit to the shell prompt.

1
2
3
4

-m
: Change
the units displayed
for the
bandwidth in
units like KB/sec
-> KB
-> B->
MB.

-r
: Sort
by magnitude of
respectively traffic.
-s
: Sort
by magnitude of
sent traffic.

-q
: Hit
quit to
the shell prompt.

想要NetHogs命令行工具的完整参数列表请参考NetHogs的手册,使用方法是在终端里输入‘man nethogs‘ 或者 ‘sudo man nethogs‘,更多信息请参考NetHogs项目主页.

原文链接:
Ravi Saive 翻译: 极客范 -
LQ

译文链接: http://www.geekfan.net/8019/

[ 转载请保留原文出处、译者和译文链接。]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: