当前位置: > Linux服务器 > 监控工具 >

CentOS6.5下使用NetHogs监控进程网络使用情况

时间:2014-10-28 13:47来源:linux.it.net.cn 作者:it
  • Nethogs 是一个终端下的网络流量监控工具,它的特别之处在于可以显示每个进程的带宽占用情况,这样可以更直观获取网络使用情况。它支持 IPv4 和 IPv6 协议、支持本地网卡及 PPP 链接。

    下载:

    从SourceForge上下载nethogs-0.8.0.tar.gz

    使用wget下载:

     

    wget http://sourceforge.net/projects/nethogs/files/nethogs/0.8/nethogs-0.8.0.tar.gz/download

    安装:

    安装之前需要先 yum install ncurses*

    view sourceprint?
    01.[root@localhost Desktop]# yum install ncurses*
    02.Loaded plugins: fastestmirror, refresh-packagekit, security
    03.Loading mirror speeds from cached hostfile
    04.* base: mirrors.skyshe.cn
    05.* extras: mirrors.skyshe.cn
    06.* updates: mirrors.skyshe.cn
    07.Setting up Install Process
    08.Package ncurses-5.7-3.20090208.el6.x86_64 already installed and latest version
    09.Package ncurses-libs-5.7-3.20090208.el6.x86_64 already installed and latest version
    10.Package ncurses-base-5.7-3.20090208.el6.x86_64 already installed and latest version
    11.Resolving Dependencies
    12.--> Running transaction check
    13.---> Package ncurses-devel.x86_64 0:5.7-3.20090208.el6 will be installed
    14.---> Package ncurses-static.x86_64 0:5.7-3.20090208.el6 will be installed
    15.---> Package ncurses-term.x86_64 0:5.7-3.20090208.el6 will be installed
    16.--> Finished Dependency Resolution
    17. 
    18.Dependencies Resolved
    19. 
    20.========================================================================================================================================================================
    21.Package                                    Arch                               Version                                           Repository                        Size
    22.========================================================================================================================================================================
    23.Installing:
    24.ncurses-devel                              x86_64                             5.7-3.20090208.el6                                base                             642 k
    25.ncurses-static                             x86_64                             5.7-3.20090208.el6                                base                             546 k
    26.ncurses-term                               x86_64                             5.7-3.20090208.el6                                base                             547 k
    27. 
    28.Transaction Summary
    29.========================================================================================================================================================================
    30.Install       3 Package(s)
    31. 
    32.Total download size: 1.7 M
    33.Installed size: 6.8 M
    34.Is this ok [y/N]: y
    35.Downloading Packages:
    36.(1/3): ncurses-devel-5.7-3.20090208.el6.x86_64.rpm                                                                                               | 642 kB     00:01    
    37.(2/3): ncurses-static-5.7-3.20090208.el6.x86_64.rpm                                                                                              | 546 kB     00:00    
    38.(3/3): ncurses-term-5.7-3.20090208.el6.x86_64.rpm                                                                                                | 547 kB     00:02    
    39.------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    40.Total                                                                                                                                   284 kB/s | 1.7 MB     00:06    
    41.Running rpm_check_debug
    42.Running Transaction Test
    43.Transaction Test Succeeded
    44.Running Transaction
    45.Installing : ncurses-devel-5.7-3.20090208.el6.x86_64                                                                                                              1/3
    46.Installing : ncurses-static-5.7-3.20090208.el6.x86_64                                                                                                             2/3
    47.Installing : ncurses-term-5.7-3.20090208.el6.x86_64                                                                                                               3/3
    48.Verifying  : ncurses-term-5.7-3.20090208.el6.x86_64                                                                                                               1/3
    49.Verifying  : ncurses-static-5.7-3.20090208.el6.x86_64                                                                                                             2/3
    50.Verifying  : ncurses-devel-5.7-3.20090208.el6.x86_64                                                                                                              3/3
    51. 
    52.Installed:
    53.ncurses-devel.x86_64 0:5.7-3.20090208.el6              ncurses-static.x86_64 0:5.7-3.20090208.el6              ncurses-term.x86_64 0:5.7-3.20090208.el6             
    54. 
    55.Complete!
    解压文件:

    tar -zxvf nethogs-0.8.0.tar.gz

    切换目录:

    cd nethogs

    编译安装:

     

    make && make install

    make出错了:

     

    view sourceprint?
    01.[root@localhost nethogs]# make && make install
    02.cc -g -Wall -Wextra -c decpcap.c
    03.decpcap.c:7:18: error: pcap.h: No such file or directory
    04.In file included from decpcap.c:8:
    05.decpcap.h:34: error: expected specifier-qualifier-list before ‘pcap_t’
    06.decpcap.c:14: error: expected ‘)’ before ‘*’ token
    07.decpcap.c: In function ‘dp_open_offline’:
    08.decpcap.c:48: error: ‘pcap_t’ undeclared (first use in this function)
    09.decpcap.c:48: error: (Each undeclared identifier is reported only once
    10.decpcap.c:48: error: for each function it appears in.)
    11.decpcap.c:48: error: ‘temp’ undeclared (first use in this function)
    12.decpcap.c:48: warning: implicit declaration of function ‘pcap_open_offline’
    13.decpcap.c:55: warning: implicit declaration of function ‘dp_fillhandle’
    14.decpcap.c: In function ‘dp_open_live’:
    15.decpcap.c:60: error: ‘pcap_t’ undeclared (first use in this function)
    16.decpcap.c:60: error: ‘temp’ undeclared (first use in this function)
    17.decpcap.c:60: warning: implicit declaration of function ‘pcap_open_live’
    18.decpcap.c: In function ‘dp_addcb’:
    19.decpcap.c:74: error: ‘struct dp_handle’ has no member named ‘callback’
    20.decpcap.c: In function ‘dp_parse_tcp’:
    21.decpcap.c:84: error: ‘struct dp_handle’ has no member named ‘callback’
    22.decpcap.c:86: error: ‘struct dp_handle’ has no member named ‘callback’
    23.decpcap.c:87: error: ‘struct dp_handle’ has no member named ‘userdata’
    24.decpcap.c: In function ‘dp_parse_ip’:
    25.decpcap.c:99: error: dereferencing pointer to incomplete type
    26.decpcap.c:103: error: ‘struct dp_handle’ has no member named ‘callback’
    27.decpcap.c:105: error: ‘struct dp_handle’ has no member named ‘callback’
    28.decpcap.c:106: error: ‘struct dp_handle’ has no member named ‘userdata’
    29.decpcap.c: In function ‘dp_parse_ip6’:
    30.decpcap.c:126: error: ‘struct dp_handle’ has no member named ‘callback’
    31.decpcap.c:128: error: ‘struct dp_handle’ has no member named ‘callback’
    32.decpcap.c:129: error: ‘struct dp_handle’ has no member named ‘userdata’
    33.decpcap.c: In function ‘dp_parse_ethernet’:
    34.decpcap.c:150: error: ‘struct dp_handle’ has no member named ‘callback’
    35.decpcap.c:152: error: ‘struct dp_handle’ has no member named ‘callback’
    36.decpcap.c:153: error: ‘struct dp_handle’ has no member named ‘userdata’
    37.decpcap.c: In function ‘dp_parse_ppp’:
    38.decpcap.c:196: error: ‘struct dp_handle’ has no member named ‘callback’
    39.decpcap.c:198: error: ‘struct dp_handle’ has no member named ‘callback’
    40.decpcap.c:199: error: ‘struct dp_handle’ has no member named ‘userdata’
    41.decpcap.c: In function ‘dp_parse_linux_cooked’:
    42.decpcap.c:238: error: ‘struct dp_handle’ has no member named ‘callback’
    43.decpcap.c:240: error: ‘struct dp_handle’ has no member named ‘callback’
    44.decpcap.c:241: error: ‘struct dp_handle’ has no member named ‘userdata’
    45.decpcap.c: In function ‘dp_pcap_callback’:
    46.decpcap.c:270: error: ‘struct dp_handle’ has no member named ‘userdata_size’
    47.decpcap.c:271: error: ‘struct dp_handle’ has no member named ‘userdata’
    48.decpcap.c:271: error: ‘struct dp_handle’ has no member named ‘userdata_size’
    49.decpcap.c:273: error: ‘struct dp_handle’ has no member named ‘linktype’
    50.decpcap.c:274: error: ‘DLT_EN10MB’ undeclared (first use in this function)
    51.decpcap.c:277: error: ‘DLT_PPP’ undeclared (first use in this function)
    52.decpcap.c:280: error: ‘DLT_LINUX_SLL’ undeclared (first use in this function)
    53.decpcap.c:283: error: ‘DLT_RAW’ undeclared (first use in this function)
    54.decpcap.c:284: error: ‘DLT_NULL’ undeclared (first use in this function)
    55.decpcap.c:289: error: ‘struct dp_handle’ has no member named ‘linktype’
    56.decpcap.c: In function ‘dp_dispatch’:
    57.decpcap.c:296: error: ‘struct dp_handle’ has no member named ‘userdata’
    58.decpcap.c:297: error: ‘struct dp_handle’ has no member named ‘userdata_size’
    59.decpcap.c:298: warning: implicit declaration of function ‘pcap_dispatch’
    60.decpcap.c:298: error: ‘struct dp_handle’ has no member named ‘pcap_handle’
    61.decpcap.c: In function ‘dp_setnonblock’:
    62.decpcap.c:302: warning: implicit declaration of function ‘pcap_setnonblock’
    63.decpcap.c:302: error: ‘struct dp_handle’ has no member named ‘pcap_handle’
    64.decpcap.c: In function ‘dp_geterr’:
    65.decpcap.c:307: warning: implicit declaration of function ‘pcap_geterr’
    66.decpcap.c:307: error: ‘struct dp_handle’ has no member named ‘pcap_handle’
    67.make: *** [decpcap.o] Error 1
    还是缺少依赖的东西,执行以下命令安装:

    yum install libpcap-dev libncurses5-dev

    view sourceprint?
    01.[root@localhost nethogs]# yum install libpcap-dev libncurses5-dev
    02.Loaded plugins: fastestmirror, refresh-packagekit, security
    03.Loading mirror speeds from cached hostfile
    04.* base: mirrors.skyshe.cn
    05.* extras: mirrors.skyshe.cn
    06.* updates: mirrors.skyshe.cn
    07.Setting up Install Process
    08.No package libpcap-dev available.
    09.No package libncurses5-dev available.
    10.Error: Nothing to do
    11.[root@localhost nethogs]# yum  -y install libpcap-devel  ncurses-devel
    12.Loaded plugins: fastestmirror, refresh-packagekit, security
    13.Loading mirror speeds from cached hostfile
    14.* base: mirrors.skyshe.cn
    15.* extras: mirrors.skyshe.cn
    16.* updates: mirrors.skyshe.cn
    17.Setting up Install Process
    18.Package ncurses-devel-5.7-3.20090208.el6.x86_64 already installed and latest version
    19.Resolving Dependencies
    20.--> Running transaction check
    21.---> Package libpcap-devel.x86_64 14:1.4.0-1.20130826git2dbcaa1.el6 will be installed
    22.--> Finished Dependency Resolution
    23. 
    24.Dependencies Resolved
    25. 
    26.===============================================================================================================================================================================================
    27.Package                                      Arch                                  Version                                                          Repository                           Size
    28.===============================================================================================================================================================================================
    29.Installing:
    30.libpcap-devel                                x86_64                                14:1.4.0-1.20130826git2dbcaa1.el6                                base                                114 k
    31. 
    32.Transaction Summary
    33.===============================================================================================================================================================================================
    34.Install       1 Package(s)
    35. 
    36.Total download size: 114 k
    37.Installed size: 160 k
    38.Downloading Packages:
    39.libpcap-devel-1.4.0-1.20130826git2dbcaa1.el6.x86_64.rpm                                                                                                                 | 114 kB     00:00    
    40.Running rpm_check_debug
    41.Running Transaction Test
    42.Transaction Test Succeeded
    43.Running Transaction
    44.Installing : 14:libpcap-devel-1.4.0-1.20130826git2dbcaa1.el6.x86_64                                                                                                                      1/1
    45.Verifying  : 14:libpcap-devel-1.4.0-1.20130826git2dbcaa1.el6.x86_64                                                                                                                      1/1
    46. 
    47.Installed:
    48.libpcap-devel.x86_64 14:1.4.0-1.20130826git2dbcaa1.el6                                                                                                                       
    49.Complete!
    再执行:make && make install
    view sourceprint?
    01.[root@localhost nethogs]# make && make install
    02.cc -g -Wall -Wextra -c decpcap.c
    03.decpcap.c: In function ‘dp_open_live’:
    04.decpcap.c:60: warning: passing argument 5 of ‘pcap_open_live’ discards qualifiers from pointer target type
    05./usr/include/pcap/pcap.h:349: note: expected ‘char *’ but argument is of type ‘constchar *’
    06.g++ -g -Wall -Wextra -c cui.cpp -DVERSION=\"0\" -DSUBVERSION=\"8\" -DMINORVERSION=\"0\"
    07.g++ -g -Wall -Wextra -c inode2prog.cpp
    08.g++ -g -Wall -Wextra -c conninode.cpp
    09.g++    -c -o devices.o devices.cpp
    10.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\"
    11.g++ -g -Wall -Wextra decpcap_test.cpp decpcap.o -o decpcap_test -lpcap -lm
    12.install -d -m 755 /usr/local/sbin
    13.install -m 755 nethogs /usr/local/sbin
    14.install -d -m 755 /usr/local/share/man/man8/
    15.install -m 644 nethogs.8 /usr/local/share/man/man8/
    安装成功了!

    执行:nethogs

     

    view sourceprint?
    01.[root@localhost nethogs]# nethogs
    02.Waiting for first packet to arrive (see sourceforge.net bug 1019381)
    03. 
    04.NetHogs version 0.8.0
    05. 
    06.PID USER     PROGRAM                                                                                                                                     DEV        SENT      RECEIVED       
    07.3227  root     sshd: root@pts/2                                                                                                                            eth0       0.666   0.059 KB/sec
    08.?     root     unknown TCP                                                                                                                                            0.000   0.000 KB/sec
    09. 
    10.TOTAL                                                                                                                                                               0.666       0.059 KB/sec

     

    下图显示各进程当前网络使用情况:

    \
    按“m”键可以切换到统计视图,显示各进程总的网络使用情况

    \
    按“Ctrl+C”或“q”退出监控

    使用帮助:

     

    view sourceprint?
    01.[root@localhost ~]# nethogs --help
    02.nethogs: invalid option -- '-'
    03.usage: nethogs [-V] [-b] [-d seconds] [-t] [-p] [device [device [device ...]]]
    04.-V : 显示版本信息,注意是大写字母V.
    05.-d : 延迟更新刷新速率,以秒为单位。默认值为 1.
    06.-t : 跟踪模式.
    07.-b : bug 狩猎模式 — — 意味着跟踪模式.
    08.-p : 混合模式(不推荐).
    09.设备 : 要监视的设备名称. 默认为 eth0
    10. 
    11.当 nethogs 运行时, 按:
    12.q: 退出
    13.m: 总数和当前使用情况模式之间切换

(责任编辑:IT)
------分隔线----------------------------