分類:
linux(40)
版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。
Nethogs 是一個終端下的網絡流量監控工具,它的特别之處在于可以顯示每個程序的帶寬占用情況,這樣可以更直覺擷取網絡使用情況。它支援 IPv4 和 IPv6 協定、支援本地網卡及 PPP 連結。
有些時候伺服器莫名其妙的向外發送大量的包,占用大量帶寬,導緻其他伺服器受到影響。那麼在流量異常的情況下,如何檢視每個程序使用的帶寬呢?可以使用nethogs工具來檢視。
1、下載下傳
下載下傳安裝包:nethogs-0.8.0.tar.gz (38.0 kB)
2、安裝
yum install ncurses* tar -zxvf nethogs-0.8.0.tar.gz cd nethogs make && make install
如果報錯如下:
[root@localhost nethogs]# make && make install g++ -g -Wall -Wextra -c packet.cpp g++ -g -Wall -Wextra -c connection.cpp g++ -g -Wall -Wextra -c process.cpp g++ -g -Wall -Wextra -c refresh.cpp refresh.cpp:9: warning: unused parameter ‘i’ cc -g -Wall -Wextra -c decpcap.c decpcap.c:7:18: error: pcap.h: No such file or directory In file included from decpcap.c:8: decpcap.h:34: error: expected specifier-qualifier-list before ‘pcap_t’ decpcap.c:14: error: expected ‘)’ before ‘*’ token decpcap.c: In function ‘dp_open_offline’: decpcap.c:48: error: ‘pcap_t’ undeclared (first use in this function) decpcap.c:48: error: (Each undeclared identifier is reported only once decpcap.c:48: error: for each function it appears in.) decpcap.c:48: error: ‘temp’ undeclared (first use in this function) decpcap.c:48: warning: implicit declaration of function ‘pcap_open_offline’ decpcap.c:55: warning: implicit declaration of function ‘dp_fillhandle’ decpcap.c: In function ‘dp_open_live’: decpcap.c:60: error: ‘pcap_t’ undeclared (first use in this function) decpcap.c:60: error: ‘temp’ undeclared (first use in this function) decpcap.c:60: warning: implicit declaration of function ‘pcap_open_live’ decpcap.c: In function ‘dp_addcb’: decpcap.c:74: error: ‘struct dp_handle’ has no member named ‘callback’ decpcap.c: In function ‘dp_parse_tcp’: decpcap.c:84: error: ‘struct dp_handle’ has no member named ‘callback’ decpcap.c:86: error: ‘struct dp_handle’ has no member named ‘callback’ decpcap.c:87: error: ‘struct dp_handle’ has no member named ‘userdata’ decpcap.c: In function ‘dp_parse_ip’: decpcap.c:99: error: dereferencing pointer to incomplete type decpcap.c:103: error: ‘struct dp_handle’ has no member named ‘callback’ decpcap.c:105: error: ‘struct dp_handle’ has no member named ‘callback’ decpcap.c:106: error: ‘struct dp_handle’ has no member named ‘userdata’ decpcap.c: In function ‘dp_parse_ip6’: decpcap.c:126: error: ‘struct dp_handle’ has no member named ‘callback’ decpcap.c:128: error: ‘struct dp_handle’ has no member named ‘callback’ decpcap.c:129: error: ‘struct dp_handle’ has no member named ‘userdata’ decpcap.c: In function ‘dp_parse_ethernet’: decpcap.c:150: error: ‘struct dp_handle’ has no member named ‘callback’ decpcap.c:152: error: ‘struct dp_handle’ has no member named ‘callback’ decpcap.c:153: error: ‘struct dp_handle’ has no member named ‘userdata’ decpcap.c: In function ‘dp_parse_ppp’: decpcap.c:196: error: ‘struct dp_handle’ has no member named ‘callback’ decpcap.c:198: error: ‘struct dp_handle’ has no member named ‘callback’ decpcap.c:199: error: ‘struct dp_handle’ has no member named ‘userdata’ decpcap.c: In function ‘dp_parse_linux_cooked’: decpcap.c:238: error: ‘struct dp_handle’ has no member named ‘callback’ decpcap.c:240: error: ‘struct dp_handle’ has no member named ‘callback’ decpcap.c:241: error: ‘struct dp_handle’ has no member named ‘userdata’ decpcap.c: In function ‘dp_pcap_callback’: decpcap.c:270: error: ‘struct dp_handle’ has no member named ‘userdata_size’ decpcap.c:271: error: ‘struct dp_handle’ has no member named ‘userdata’ decpcap.c:271: error: ‘struct dp_handle’ has no member named ‘userdata_size’ decpcap.c:273: error: ‘struct dp_handle’ has no member named ‘linktype’ decpcap.c:274: error: ‘DLT_EN10MB’ undeclared (first use in this function) decpcap.c:277: error: ‘DLT_PPP’ undeclared (first use in this function) decpcap.c:280: error: ‘DLT_LINUX_SLL’ undeclared (first use in this function) decpcap.c:283: error: ‘DLT_RAW’ undeclared (first use in this function) decpcap.c:284: error: ‘DLT_NULL’ undeclared (first use in this function) decpcap.c:289: error: ‘struct dp_handle’ has no member named ‘linktype’ decpcap.c: In function ‘dp_dispatch’: decpcap.c:296: error: ‘struct dp_handle’ has no member named ‘userdata’ decpcap.c:297: error: ‘struct dp_handle’ has no member named ‘userdata_size’ decpcap.c:298: warning: implicit declaration of function ‘pcap_dispatch’ decpcap.c:298: error: ‘struct dp_handle’ has no member named ‘pcap_handle’ decpcap.c: In function ‘dp_setnonblock’: decpcap.c:302: warning: implicit declaration of function ‘pcap_setnonblock’ decpcap.c:302: error: ‘struct dp_handle’ has no member named ‘pcap_handle’ decpcap.c: In function ‘dp_geterr’: decpcap.c:307: warning: implicit declaration of function ‘pcap_geterr’ decpcap.c:307: error: ‘struct dp_handle’ has no member named ‘pcap_handle’ make: * [decpcap.o] Error 1
執行指令
yum install libpcap-dev* libncurses5-dev*
然後再重新編譯安裝,如下:
decpcap.c:60: warning: passing argument 5 of ‘pcap_open_live’ discards qualifiers from pointer /usr/include/pcap/pcap.h:349: note: expected ‘char ’ but argument is of type ‘const char ’ g++ -g -Wall -Wextra -c cui.cpp -DVERSION=\”0\” -DSUBVERSION=\”8\” -DMINORVERSION=\”0\” 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 inodee.o devices.o -o nethogs -lpcap -lm -lncurses -DVERSION=\”0\” -DSUBVERSION=\”8\” -DMINORVERSION= g++ -g -Wall -Wextra decpcap_test.cpp decpcap.o -o decpcap_test -lpcap -lm 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 即可檢視應用的寬帶占用情況。
下圖是我開啟了2個指令視窗做的測試: