nethogs安装

昨天开发反馈应用服务器网络带宽占用很大,需要查看进程占用的流量,百度了一下nethogs可以查看。下载了nethogs-0.8.0.tar.gz在测试环境安装一下。

操作系统版本7.4

[root@ora11g nethogs]# make && make install
g++ -g -Wall -Wextra -c process.cpp
process.cpp:4:21: fatal error: ncurses.h: No such file or directory
#include <ncurses.h>
^
compilation terminated.
make: *** [process.o] Error 1

需要安装yum install ncurses-devel ncurses,配置了aliyun的yum源。

[root@ora11g nethogs]# yum install ncurses-devel ncurses
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package ncurses.x86_64 0:5.9-13.20130511.el7 will be updated
---> Package ncurses.x86_64 0:5.9-14.20130511.el7_4 will be an update
--> Processing Dependency: ncurses-libs(x86-64) = 5.9-14.20130511.el7_4 for package: ncurses-5.9-14.20130511.el7_4.x86_64
---> Package ncurses-devel.x86_64 0:5.9-14.20130511.el7_4 will be installed
--> Running transaction check
---> Package ncurses-libs.x86_64 0:5.9-13.20130511.el7 will be updated
---> Package ncurses-libs.x86_64 0:5.9-14.20130511.el7_4 will be an update
--> Processing Dependency: ncurses-base = 5.9-14.20130511.el7_4 for package: ncurses-libs-5.9-14.20130511.el7_4.x86_64
--> Running transaction check
---> Package ncurses-base.noarch 0:5.9-13.20130511.el7 will be updated
---> Package ncurses-base.noarch 0:5.9-14.20130511.el7_4 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================================
Package Arch Version Repository Size
============================================================================================================================================================================================
Installing:
ncurses-devel x86_64 5.9-14.20130511.el7_4 rhel 712 k
Updating:
ncurses x86_64 5.9-14.20130511.el7_4 rhel 304 k
Updating for dependencies:
ncurses-base noarch 5.9-14.20130511.el7_4 rhel 68 k
ncurses-libs x86_64 5.9-14.20130511.el7_4 rhel 316 k

Transaction Summary
============================================================================================================================================================================================
Install 1 Package
Upgrade 1 Package (+2 Dependent packages)

Total download size: 1.4 M
Is this ok [y/d/N]: y
Downloading packages:
No Presto metadata available for rhel
(1/4): ncurses-base-5.9-14.20130511.el7_4.noarch.rpm | 68 kB 00:00:00
(2/4): ncurses-devel-5.9-14.20130511.el7_4.x86_64.rpm | 712 kB 00:00:00
(3/4): ncurses-libs-5.9-14.20130511.el7_4.x86_64.rpm | 316 kB 00:00:00
(4/4): ncurses-5.9-14.20130511.el7_4.x86_64.rpm | 304 kB 00:00:00
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 4.3 MB/s | 1.4 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating : ncurses-base-5.9-14.20130511.el7_4.noarch 1/7
Updating : ncurses-libs-5.9-14.20130511.el7_4.x86_64 2/7
Installing : ncurses-devel-5.9-14.20130511.el7_4.x86_64 3/7
Updating : ncurses-5.9-14.20130511.el7_4.x86_64 4/7
Cleanup : ncurses-5.9-13.20130511.el7.x86_64 5/7
Cleanup : ncurses-libs-5.9-13.20130511.el7.x86_64 6/7
Cleanup : ncurses-base-5.9-13.20130511.el7.noarch 7/7
Verifying : ncurses-devel-5.9-14.20130511.el7_4.x86_64 1/7
Verifying : ncurses-libs-5.9-14.20130511.el7_4.x86_64 2/7
Verifying : ncurses-5.9-14.20130511.el7_4.x86_64 3/7
Verifying : ncurses-base-5.9-14.20130511.el7_4.noarch 4/7
Verifying : ncurses-5.9-13.20130511.el7.x86_64 5/7
Verifying : ncurses-libs-5.9-13.20130511.el7.x86_64 6/7
Verifying : ncurses-base-5.9-13.20130511.el7.noarch 7/7

Installed:
ncurses-devel.x86_64 0:5.9-14.20130511.el7_4

Updated:
ncurses.x86_64 0:5.9-14.20130511.el7_4

Dependency Updated:
ncurses-base.noarch 0:5.9-14.20130511.el7_4 ncurses-libs.x86_64 0:5.9-14.20130511.el7_4

Complete!

[root@ora11g nethogs]# make
g++ -g -Wall -Wextra -c process.cpp
g++ -g -Wall -Wextra -c refresh.cpp
refresh.cpp:9:6: warning: unused parameter ‘i’ [-Wunused-parameter]
void alarm_cb (int i)
^
cc -g -Wall -Wextra -c decpcap.c
decpcap.c: In function ‘dp_open_live’:
decpcap.c:60:2: warning: passing argument 5 of ‘pcap_open_live’ discards ‘const’ qualifier from pointer target type [enabled by default]
pcap_t * temp = pcap_open_live(device, snaplen, promisc, to_ms, ebuf);
^
In file included from /usr/include/pcap.h:45:0,
from decpcap.c:7:
/usr/include/pcap/pcap.h:349:9: note: expected ‘char *’ but argument is of type ‘const char *’
pcap_t *pcap_open_live(const char *, int, int, int, char *);
^
g++ -g -Wall -Wextra -c cui.cpp -DVERSION="0" -DSUBVERSION="8" -DMINORVERSION="0"
cui.cpp: In function ‘void do_refresh()’:
cui.cpp:308:6: warning: variable ‘row’ set but not used [-Wunused-but-set-variable]
int row; // number of terminal rows
^
g++ -g -Wall -Wextra -c inode2prog.cpp
g++ -g -Wall -Wextra -c conninode.cpp
g++ -c -o devices.o devices.cpp
g++ -g -Wall -Wextra nethogs.cpp packet.o connection.o process.o refresh.o decpcap.o cui.o inode2prog.o conninode.o devices.o -o nethogs -lpcap -lm -lncurses -DVERSION="0" -DSUBVERSION="8" -DMINORVERSION="0"
g++ -g -Wall -Wextra decpcap_test.cpp decpcap.o -o decpcap_test -lpcap -lm
[root@ora11g nethogs]# make install
install -d -m 755 /usr/local/sbin
install -m 755 nethogs /usr/local/sbin
install -d -m 755 /usr/local/share/man/man8/
install -m 644 nethogs.8 /usr/local/share/man/man8/

[root@ora11g nethogs]# nethogs
creating socket failed while establishing local IP - are you root?
错误提示说我不是root账号,但是我确实是root执行的,为毛还是这个错呢?!
百度一下,说是0.8.0的一个bug,安装0.8.1就可以了。
[root@ora11g opt]# wget -c https://github.com/raboof/nethogs/archive/v0.8.1.tar.gz
--2021-10-26 15:17:55-- https://github.com/raboof/nethogs/archive/v0.8.1.tar.gz
Resolving github.com (github.com)... 20.205.243.166
Connecting to github.com (github.com)|20.205.243.166|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/raboof/nethogs/tar.gz/v0.8.1 [following]
--2021-10-26 15:17:55-- https://codeload.github.com/raboof/nethogs/tar.gz/v0.8.1
Resolving codeload.github.com (codeload.github.com)... 20.205.243.165
Connecting to codeload.github.com (codeload.github.com)|20.205.243.165|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/x-gzip]
Saving to: ‘v0.8.1.tar.gz’

[ <=> ] 35,986 --.-K/s in 0.08s

2021-10-26 15:17:56 (433 KB/s) - ‘v0.8.1.tar.gz’ saved [35986]
[root@ora11g opt]# rm nethogs-0.8.0.tar.gz
rm: remove regular file ‘nethogs-0.8.0.tar.gz’? y
[root@ora11g opt]# rm nethogs
rm: cannot remove ‘nethogs’: Is a directory
[root@ora11g opt]# y
bash: y: command not found...
[root@ora11g opt]# rm nethogs -fr
[root@ora11g opt]# tar -zxf v0.8.1.tar.gz
[root@ora11g opt]# cd nethogs-0.8.1/
[root@ora11g nethogs-0.8.1]# ll
total 192
-rw-rw-r-- 1 root root 2602 Dec 21 2015 Changelog
-rw-rw-r-- 1 root root 5444 Dec 21 2015 connection.cpp
-rw-rw-r-- 1 root root 2597 Dec 21 2015 connection.h
-rw-rw-r-- 1 root root 5971 Dec 21 2015 conninode.cpp
-rw-rw-r-- 1 root root 855 Dec 21 2015 conninode.h
-rw-rw-r-- 1 root root 18092 Dec 21 2015 COPYING
-rw-rw-r-- 1 root root 12365 Dec 21 2015 cui.cpp
-rw-rw-r-- 1 root root 929 Dec 21 2015 cui.h
-rw-rw-r-- 1 root root 8697 Dec 21 2015 decpcap.c
-rw-rw-r-- 1 root root 2281 Dec 21 2015 decpcap.h
-rw-rw-r-- 1 root root 1455 Dec 21 2015 decpcap_test.cpp
-rw-rw-r-- 1 root root 1207 Dec 21 2015 DESIGN
-rw-rw-r-- 1 root root 1888 Dec 21 2015 devices.cpp
-rw-rw-r-- 1 root root 1198 Dec 21 2015 devices.h
-rw-rw-r-- 1 root root 5639 Dec 21 2015 inode2prog.cpp
-rw-rw-r-- 1 root root 1214 Dec 21 2015 inode2prog.h
-rw-rw-r-- 1 root root 138 Dec 21 2015 INSTALL
-rw-rw-r-- 1 root root 5007 Dec 21 2015 main.cpp
-rw-rw-r-- 1 root root 2394 Dec 21 2015 Makefile
-rw-rw-r-- 1 root root 1421 Dec 21 2015 nethogs.8
-rw-rw-r-- 1 root root 6002 Dec 21 2015 nethogs.cpp
-rw-rw-r-- 1 root root 4110 Dec 21 2015 nethogs.h
-rw-rw-r-- 1 root root 8317 Dec 21 2015 packet.cpp
-rw-rw-r-- 1 root root 2250 Dec 21 2015 packet.h
-rw-rw-r-- 1 root root 7506 Dec 21 2015 process.cpp
-rw-rw-r-- 1 root root 2861 Dec 21 2015 process.h
-rw-rw-r-- 1 root root 2635 Dec 21 2015 README.decpcap.txt
-rw-rw-r-- 1 root root 1638 Dec 21 2015 README.md
-rw-rw-r-- 1 root root 1080 Dec 21 2015 refresh.cpp
-rw-rw-r-- 1 root root 811 Dec 21 2015 refresh.h
[root@ora11g nethogs-0.8.1]# make
g++ -Wall -Wextra -c packet.cpp
g++ -Wall -Wextra -c connection.cpp
g++ -Wall -Wextra -c process.cpp
g++ -Wall -Wextra -c refresh.cpp
cc -Wall -Wextra -c decpcap.c
g++ -Wall -Wextra -c cui.cpp -DVERSION="0" -DSUBVERSION="8" -DMINORVERSION="1"
g++ -Wall -Wextra -c inode2prog.cpp
g++ -Wall -Wextra -c conninode.cpp
g++ -Wall -Wextra -c -o devices.o devices.cpp
g++ -Wall -Wextra main.cpp packet.o connection.o process.o refresh.o decpcap.o cui.o inode2prog.o conninode.o devices.o -o nethogs -lpcap -lm -lncurses -DVERSION="0" -DSUBVERSION="8" -DMINORVERSION="1"
g++ -Wall -Wextra decpcap_test.cpp decpcap.o -o decpcap_test -lpcap -lm
[root@ora11g nethogs-0.8.1]# make install
install -d -m 755 /usr/local/sbin
install -m 755 nethogs /usr/local/sbin
install -d -m 755 /usr/local/share/man/man8/
install -m 644 nethogs.8 /usr/local/share/man/man8/

使用还是很方便的

直接输入nethogs即可。
参数: [-d seconds] [device(s)]
-d seconds 是数据刷新时间 如nethogs -d 1 就是每秒刷新一次
device(s) 是指定监控的网卡,如nethogs eth1 就是监控eth1网卡。默认是监控 eth0 网卡,如果要监控多网卡的话则必须进行指定。

[root@ora11g nethogs-0.8.1]# nethogs -d 2 ens192
Ethernet link detected
Waiting for first packet to arrive (see sourceforge.net bug 1019381)
NetHogs version 0.8.1

PID USER    PROGRAM               DEV         SENT                RECEIVED
18014 root     sshd: root@pts/0   ens192        0.045               0.012 KB/sec
?          root unknown                   TCP               0.000          0.000 KB/sec

 

NetHogs提供交互式控制指令:

m : Cycle between display modes (kb/s, kb, b, mb) 切换网速显示单位

r : Sort by received. 按接收流量排序

s : Sort by sent. 按发送流量排序

q : Quit and return to the shell prompt. 退出NetHogs命令工具

常用的参数:

-d delay for refresh rate. 数据刷新时间 如nethogs -d 1 就是每秒刷新一次

-h display available commands usage. 显示命名帮助、使用信息

-p sniff in promiscious mode (not recommended).

-t tracemode.

-V prints Version info.

原文地址:https://www.cnblogs.com/historynote/p/15466212.html