网上有很多适用于Linux操作系统的开源网络监视工具.比如说,你可以用命令iftop来检查带宽使用情况. netstat用来查看接口统计报告,还有top监控系统当前运行进程.但是如果你想要找一个能够按进程实时统计网络带宽利用率,那么NetHogs就是你所需要的唯一工具.
1
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包.
- [root@tecmint~]# yum –y install nethogs
- Loaded plugins:fastest mirror,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.i6860: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 28k
- Transaction Summary
- ===========================================================================================================
- Install 1 Package(s)
- Total download size:28k
- Installed size:50k
- Downloading Packages:
- nethogs–0.8.0–1.el6.i686.rpm | 28kB 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.i6860:0.8.0–1.el6
- Complete!
在Ubuntu, Linux mint和Debian下安装NetHogs:
键入apt-get命令安装NetHogs包
- $ sudo apt-get install nethogs
样例输出
- tecmint@tecmint:~$ sudo apt-get install nethogs
- [sudo]password fortecmint:
- 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.1kB of archives.
- After this operation,100kB 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.1kB in 1s (19.8kB/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 forman–db…
- Setting up nethogs(0.8.0–1)…
NetHogs用法
在基于RedHat系统下键入如下命令启动NetHogs工具.
- # nethogs
要执行NetHogs你必须拥有root权限,即如图所示用sudo命令
- $ sudo nethogs
样例预览:
2
CentOS 6.3 下的NetHogs预览
3
Ubuntu 12.10 下的NetHogs预览
正如上图所示, send列和received列显示的是按照每个进程的流量统计.总的收发数据带宽在最下方,而且可以用交互命令控制排序,下面将要讨论这些交互命令:
NetHogs 命令行参数
以下就是NetHogs命令行的参数,用’-d’来添加刷新频率参数,device name
用来检测给定的某个或者某些设备的带宽(默认是eth0).例如:设置5秒钟的刷新频率,键入如下命令即可:
- # nethogs –d 5
- $ sudo nethogs –d 5
如果只用来监视设备(eth0)的网络带宽可以使用如下命令:
- $ sudo nethogs eth0
如果要同时监视eth0和eth1接口,使用以下命令即可:
- # nethogs eth0 eth1
或
- $ sudo nethogs eth0 eth1
其他参数和用法
- –d: delay for refresh rate.
- –h: display available commands usage.
- –p: sniff in promiscious mode (not recommended).
- –t: trace mode.
- –V: prints Version info.
NetHogs 交互控制
以下是NetHogs的一些很有用的交互控制(键盘快捷键)
- –m: Change the units displayed forthe bandwidth inunits like KB/sec->KB->B->MB.
- –r: Sort by magnitude of respectively traffic.
- –s: Sort by magnitude of sent traffic.
- –q: Hit quit tothe shell prompt.
想要NetHogs命令行工具的完整参数列表请参考NetHogs的手册,使用方法是在终端里输入‘man nethogs‘ 或者 ‘sudo man nethogs‘,更多信息请参考NetHogs项目主页.