使用 nethogs 查看进程占用带宽

netstat –statistics 可以查看非常详细的统计信息,但是不包含进程信息,只包含没有计算的原始数据。

nethogs 是一款简洁的终端网络监控软件,可以输出进程PID,用户名,程序名或IP端口信息,收发速率,非常直观。 可以使用 -d 参数调整刷新的频率。Web服务器套用了nginx, ruby, c 系统调用等,所以PID信息没有抓取到,可以看到下面80/8080端口的行没有PID。

使用iptables规则限制下载amosk.info服务器文件速率,每秒10个包
# iptables -A INPUT -d amosk.info -m limit --limit 1/s -j ACCEPT

更多iptables使用:http://www.cnblogs.com/JemBai/archive/2009/03/19/1416364.html

# nethogs -h
usage: nethogs [-V] [-b] [-d seconds] [-t] [-p] [device [device [device ...]]]
		-V : prints version.
		-d : delay for update refresh rate in seconds. default is 1.
		-t : tracemode.
		-b : bughunt mode - implies tracemode.
		-p : sniff in promiscious mode (not recommended).
		device : device(s) to monitor. default is eth0

When nethogs is running, press:
 q: quit
 m: switch between total and kb/s mode

# nethogs -d 5 eth1
NetHogs version 0.8.0

  PID USER PROGRAM                                 DEV    SENT  RECEIVED       
24784 root ruby                                    eth1   0.949  12.052 KB/sec
25966 root sshd: root@pts/4                        eth1   5.996   1.775 KB/sec
?     root 12.12.123.212:8080-113.74.211.34:64983        31.991   1.676 KB/sec
?     root 12.12.123.212:80-27.150.134.139:57327         39.763   1.468 KB/sec
?     root 12.12.123.212:8080-113.74.211.34:64984        26.885   1.243 KB/sec
?     root 12.12.123.212:8080-27.150.134.139:57328        1.937   0.219 KB/sec
?     root 12.12.123.212:8080-114.102.76.57:35705         0.180   0.167 KB/sec
7808  root sshd: root@pts/2                        eth1   0.089   0.141 KB/sec
737   newr /usr/sbin/nrsysmond                     eth1   0.496   0.114 KB/sec
19887 root sshd: root@pts/5                        eth1   0.143   0.064 KB/sec
22187 root sshd: root@pts/0                        eth1   0.488   0.013 KB/sec
?     root 12.12.123.212:8080-114.102.76.57:35704         0.000   0.000 KB/sec
?     root 12.12.123.212:8080-113.74.211.34:64982         0.000   0.000 KB/sec
?     root 12.12.123.212:8080-114.102.76.57:35703         0.000   0.000 KB/sec
1310  root /usr/local/aegis/aegis_client/aegis_00  eth1   0.000   0.000 KB/sec
?     root 12.12.123.212:8080-27.36.218.38:34520          0.000   0.000 KB/sec
?     root 12.12.123.212:8080-113.74.211.34:64981         0.000   0.000 KB/sec
?     root 12.12.123.212:8080-113.74.211.34:64980         0.000   0.000 KB/sec
?     root 12.12.123.212:8080-113.74.211.34:64979         0.000   0.000 KB/sec
?     root 12.12.123.212:8080-113.74.211.34:64978         0.000   0.000 KB/sec
?     root 12.12.123.212:80-114.102.76.57:35702           0.000   0.000 KB/sec
?     root 12.12.123.212:80-27.156.33.66:55921            0.000   0.000 KB/sec
24982 root sshd: root@pts/1                        eth1   0.013   0.000 KB/sec
?     root unknown TCP                                    0.000   0.000 KB/sec

  TOTAL                                                 108.928  18.932 KB/sec

# netstat -s
Ip:
    1196603371 total packets received
    0 forwarded
    33 with unknown protocol
    0 incoming packets discarded
    1196603338 incoming packets delivered
    821308850 requests sent out
Icmp:
    33261 ICMP messages received
    877 input ICMP message failed.
    ICMP input histogram:
        destination unreachable: 27959
        timeout in transit: 83
        source quenches: 2
        echo requests: 5208
        echo replies: 2
        timestamp request: 2
    9638 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 4428
        echo replies: 5208
        timestamp replies: 2
IcmpMsg:
        InType0: 2
        InType3: 27959
        InType4: 2
        InType8: 5208
        InType11: 83
        InType13: 2
        OutType0: 5208
        OutType3: 4428
        OutType14: 2
Tcp:
    9725137 active connections openings
    1240953 passive connection openings
    34063 failed connection attempts
    17180 connection resets received
    31 connections established
    1196269750 segments received
    830752456 segments send out
    1311497 segments retransmited
    3178 bad segments received.
    199120 resets sent
Udp:
    295790 packets received
    4522 packets to unknown port received.
    3 packet receive errors
    297048 packets sent
UdpLite:
TcpExt:
    10828 invalid SYN cookies received
    9148 resets received for embryonic SYN_RECV sockets
    8 packets pruned from receive queue because of socket buffer overrun
    1373290 TCP sockets finished time wait in fast timer
    23367 packets rejects in established connections because of timestamp
    20165100 delayed acks sent
    4209 delayed acks further delayed because of locked socket
    Quick ack mode was activated 2611513 times
    50928669 packets directly queued to recvmsg prequeue.
    15261568 bytes directly in process context from backlog
    1694004723 bytes directly received in process context from prequeue
    714008734 packet headers predicted
    798736 packets header predicted and directly queued to user
    35195159 acknowledgments not containing data payload received
    119615013 predicted acknowledgments
    56 times recovered from packet loss due to fast retransmit
    29431 times recovered from packet loss by selective acknowledgements
    273 bad SACK blocks received
    Detected reordering 63 times using FACK
    Detected reordering 530 times using SACK
    Detected reordering 822 times using time stamp
    1977 congestion windows fully recovered without slow start
    10742 congestion windows partially recovered using Hoe heuristic
    832 congestion windows recovered without slow start by DSACK
    515352 congestion windows recovered without slow start after partial ack
    37356 TCP data loss events
    TCPLostRetransmit: 4355
    12 timeouts after reno fast retransmit
    4324 timeouts after SACK recovery
    2700 timeouts in loss state
    92192 fast retransmits
    8830 forward retransmits
    47669 retransmits in slow start
    923558 other TCP timeouts
    3 classic Reno fast retransmits failed
    2498 SACK retransmits failed
    18 times receiver scheduled too late for direct processing
    530 packets collapsed in receive queue due to low socket buffer
    2604726 DSACKs sent for old packets
    253239 DSACKs sent for out of order packets
    5286 DSACKs received
    1 DSACKs for out of order packets received
    13385 connections reset due to unexpected data
    458 connections reset due to early user close
    45406 connections aborted due to timeout
    TCPSACKDiscard: 11
    TCPDSACKIgnoredOld: 20
    TCPDSACKIgnoredNoUndo: 921
    TCPSpuriousRTOs: 2853
    TCPSackShifted: 190479
    TCPSackMerged: 152819
    TCPSackShiftFallback: 251450
    TCPBacklogDrop: 1
IpExt:
    InOctets: -654005353
    OutOctets: 884338795

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.