网上有很多适用于Linux操作系统的开源网络监视工具.比如说,你可以用命令iftop来检查带宽使用情况. netstat用来查看接口统计报告,还有top监控系统当前运行进程.但是如果你想要找一个能够按进程实时统计网络带宽利用率,那么NetHogs就是你所需要的唯一工具.
NetHogs – 网络带宽监控
NetHogs 是一个开源的命令行工具(类似于Linux的top命令),用来按进程或程序实时统计网络带宽使用率.
来自 NetHogs 项目网站:
NetHogs是一个小型的’net top’工具,不像大多数工具那样拖慢每个协议或者是每个子网的速度而是按照进程进行带宽分组.NetHogs NetHogs不需要依赖载入某个特殊的内核模块. 如果发生了网络阻塞你可以启动NetHogs立即看到哪个PID造成的这种状况.这样就很容易找出哪个程序跑飞了然后突然占用你的带宽.
这篇文章为你讲解了如何在Unix/Linux操作系统下如何安装和使用NetHogs按进程监控网络带宽使用率。
要安装NetHogs,你必须要启用你所用Linux下的EPEL源.然后运行下面的yum命令下载安装NetHogs包.
# 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!
键入apt-get命令安装NetHogs包
$ 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) ...
在基于RedHat系统下键入如下命令启动NetHogs工具.
# nethogs
要执行NetHogs你必须拥有root权限,即如图所示用sudo命令
$ sudo nethogs
CentOS 6.3 下的NetHogs预览
Ubuntu 12.10 下的NetHogs预览
正如上图所示, send列和received列显示的是按照每个进程的流量统计.总的收发数据带宽在最下方,而且可以用交互命令控制排序,下面将要讨论这些交互命令:
以下就是NetHogs命令行的参数,用’-d’来添加刷新频率参数,’device name’ 用来检测给定的某个或者某些设备的带宽(默认是eth0).例如:设置5秒钟的刷新频率,键入如下命令即可:
# nethogs -d 5
$ sudo nethogs -d 5
如果只用来监视设备(eth0)的网络带宽可以使用如下命令:
# nethogs 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 : tracemode. -V : prints Version info.
以下是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.
想要NetHogs命令行工具的完整参数列表请参考NetHogs的手册,使用方法是在终端里输入‘ man nethogs‘ 或者 ‘sudo man nethogs‘,更多信息请参考 NetHogs项目主页.
NetHogs——Linux下按进程实时统计网络带宽利用率,首发于 极客范 - GeekFan.net。