linux系统有许多开源的网络监控工具。比如说,你可以使用iftop命令检查带宽的使用率,netstat命令查看统计情况,又或者是top命令来查看系统上运行的进程。但是,如果你真的要看每个进程使用带宽的情况,NetHogs是唯一你可以使用的工具。
什么是NetHogs?
NetHogs是一个开源的命令行程序(就类似于linux的top命令),它主要用于监控每个进程的实时网络流量带宽。
NetHog项目主页上是这样介绍的:
NetHogs is a small ‘net top’ tool. Instead of breaking the traffic down per protocol or per subnet, like most tools do, it groups bandwidth by process. NetHogs does not rely on a special kernel module to be loaded. If there’s suddenly a lot of network traffic, you can fire up NetHogs and immediately see which PID is causing this. This makes it easy to identify programs that have gone wild and are suddenly taking up your bandwidth.
本文会向你解释如何安装nethogs工具,以及如何使用nethogs工具查看每一个进程的实时网络带宽的使用率。
在RHEL/CentOS/Fedora上安装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!
在Ubuntu/Linux Mint/Debian上安装Nethogs
在Ubuntu/Linux Mint/Debian上安装Nethogs,你只需要输入下面的apt-get命令即可。
$ 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) ...
使用nethogs工具
在基于red-hat的系统上面使用nethogs命令,只需要运行下面的命令即可。
# nethogs
要执行该命令,你不许具有root权限,所以,在运行它的时候,你需要加上sudo命令。
$ sudo nethogs
输出结果示例
正如上面展示的每个进程的send和received速率。累计的sent和received的带宽使用情况会在底部展示。使用一些交互命令,你还可以对输出结果进行排序。
nethogs命令行选项
下面这些是nethogs的命令行选项。使用'-d'选项可以指定刷新频率,'device name'可以指定设备/设备带宽(默认监控的是eth0的网络带宽)。
例如,每五秒中刷新一次,我们可以使用下面的命令。
# nethogs -d 5
$ sudo nethogs -d 5
如果只想监控特定设备的带宽,可以使用:
# nethogs eth0
$ sudo nethogs eth0
如果要同时监控eth0和eth1网络接口的带宽,你可以使用下面的命令.
# nethogs eth0 eth1
$ sudo nethogs eth0 eth1
其他的选项和用法
-d : 刷新频率
-h : 展示命令的用法
-p : 在混杂模式下监听
-t : 跟踪模式
-V : 打印版本信息
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.
如果要查看详细的nethogs的使用说明,可以执行'man nethogs'或'sudo man nethogs',查看更多信息,请访问
nethogs项目主页。
参考文献
1、https://github.com/raboof/nethogs
2、https://www.tecmint.com/nethogs-monitor-per-process-network-bandwidth-usage-in-real-time/