天天看點

linux網絡流量檢視指令

  • 網卡流量

1、iftop指令

iftop可以用來監控網卡的實時流量(可以指定網段)、反向解析IP、顯示端口資訊、TCP/IP連接配接等

官網:http://www.ex-parrot.com/~pdw/iftop/

  • 安裝iftop:
[root@Super ~]# yum install flex byacc  libpcap ncurses ncurses-devel libpcap-devel -y
[root@Super ~]# wget http://www.ex-parrot.com/~pdw/iftop/download/iftop-0.17.tar.gz
[root@Super ~]#  tar -zxvf iftop-0.17.tar.gz
[root@Super ~]# cd iftop-0.17
[root@Super iftop-0.17]#    ./configure
[root@Super iftop-0.17]# make
[root@Super iftop-0.17]# make install
[root@Super iftop-0.17]# iftop --help
iftop: unknown option --
iftop: display bandwidth usage on an interface by host

Synopsis: iftop -h | [-npbBP] [-i interface] [-f filter code] [-N net/mask]

   -h                  display this message
   -n                  don't do hostname lookups
   -N                  don't convert port numbers to services
   -p                  run in promiscuous mode (show traffic between other
                       hosts on the same network segment)
   -b                  don't display a bar graph of traffic
   -B                  Display bandwidth in bytes
   -i interface        listen on named interface
   -f filter code      use filter code to select packets to count
                       (default: none, but only IP packets are counted)
   -F net/mask         show traffic flows in/out of network
   -P                  show ports as well as hosts
   -m limit            sets the upper limit for the bandwidth scale
   -c config file      specifies an alternative configuration file

iftop, version 0.17
copyright (c) 2002 Paul Warren <[email protected]> and contributors
[root@Super iftop-0.17]#            

或者安裝好epel源後直接yum install iftop -y安裝

  • 運作iftop

直接運作後效果圖如下

[root@Super soft]# iftop
interface: ens32
IP address is: 192.168.100.127
MAC address is: 00:50:56:a9:1c:49
[root@Super soft]#            
linux網絡流量檢視指令

<= 、=> 左右箭頭,表示流量的方向;

右邊3列,表示的是該通路ip連接配接到本機2秒,10秒和40秒的平均流量

TX:發送流量

RX:接收流量

TOTAL:總流量

Cumm:運作iftop到目前時間的總流量

peak:流量峰值

rates:分别表示過去 2s 10s 40s 的平均流量

  • iftop常用的參數

-i:設定監測的網卡,如:# iftop -i eth1

-B:以bytes為機關顯示流量(預設是bits),如:# iftop -B

-n:使host資訊預設直接都顯示IP,如:# iftop -n

-N:使端口資訊預設直接都顯示端口号,如: # iftop -N

-F:顯示特定網段的進出流量,如# iftop -F 10.10.1.0/24或# iftop -F 10.10.1.0/255.255.255.0

-h:(display this message),幫助,顯示參數資訊

-p:使用這個參數後,中間的清單顯示的本地主機資訊,出現了本機以外的IP資訊;

-b:使流量圖形條預設就顯示;

-f:過濾計算包;

-P:使host資訊及端口資訊預設就都顯示;

-m:設定界面最上邊的刻度的最大值,刻度分五個大段顯示,例:# iftop -m 100M

  • iftop界面常用指令(區分大小寫)

h:切換是否顯示幫助;

n:切換顯示本機的IP或主機名;

s:切換是否顯示本機的host資訊;

d:切換是否顯示遠端目标主機的host資訊;

t:來往的資料合并成一行,切換顯示格式為2行/1行/隻顯示發送流量/隻顯示接收流量;

N:切換顯示端口号或端口服務名稱;

S:切換是否顯示本機的端口資訊;

D:切換是否顯示遠端目标主機的端口資訊;

p:切換是否顯示端口資訊;

P:切換暫停/繼續顯示;

b:切換是否顯示平均流量圖形條;

B:切換計算2秒或10秒或40秒内的平均流量;

T:切換是否顯示每個連接配接的總流量;

l:打開螢幕過濾功能,輸入要過濾的字元,比如ip,按回車後,螢幕就隻顯示這個IP相關的流量資訊;

L:切換顯示畫面上邊的刻度;刻度不同,流量圖形條會有變化;

j或k:可以向上或向下滾動螢幕顯示的連接配接記錄;

1或2或3:可以根據右側顯示的三列流量資料進行排序;

<:根據左邊的本機名或IP排序;

“>:”根據遠端目标主機的主機名或IP排序

o:切換是否固定隻顯示目前的連接配接;

f:可以編輯過濾代碼,這是翻譯過來的說法,我還沒用過這個!

!:可以使用shell指令,這個沒用過!沒搞明白啥指令在這好用呢!

q:退出監控。

2、iptraf指令

iptraf是一款互動式、色彩鮮豔的IP區域網路監控工具。它可以顯示每個連接配接以及主機之間傳輸的資料量。以生成各種網絡統計資料,包括TCP資訊、UDP統計、ICMP和OSPF資訊、以太網負載資訊、節點統計、IP校驗和錯誤和其它一些資訊。

[root@localhost ~]# yum install -y epel-release
[root@localhost ~]# yum install iptraf-ng -y            

-i iface 網絡接口:立即在指定網絡接口上開啟IP流量監視,iface為all指監視所有的網絡接口,iface指相應的interface

-g 立即開始生成網絡接口的概要狀态資訊

-d iface 網絡接口:在指定網絡接口上立即開始監視明細的網絡流量資訊,iface指相應的interface

-s iface 網絡接口:在指定網絡接口上立即開始監視TCP和UDP網絡流量資訊,iface指相應的interface

-z iface 網絡接口:在指定網絡接口上顯示包計數,iface指相應的interface

-l iface 網絡接口:在指定網絡接口上立即開始監視區域網路工作站資訊,iface指相應的interface

-t timeout 時間:指定iptraf指令監視的時間,timeout指監視時間的minute數

-B 将标注輸出重新定向到“/dev/null”,關閉标注輸入,将程式作為背景程序運作

-L logfile 指定一個檔案用于記錄所有指令行的log,預設檔案是位址:/var/log/iptraf

-I interval 指定記錄log的時間間隔(機關是minute),不包括IP traffic monitor

-u 允許使用不支援的接口作為以太網裝置

-f 清空所有計數器

-h 顯示幫助資訊

linux網絡流量檢視指令
  • Configure

    在總菜單指令中點選“Configure” menu,即進入如上指令菜單,進行适當的配置可以讓統計的結果更直覺,資訊更豐富。

1)Reverse DNS lookups:檢視連接配接的ip所對應的域名,在IP traffic monitor的pkt captured對話框中就可以看到域名結果,這個不是很直覺,開啟後會有點點影響抓包性能。

2)TCP/UDP service names:在有端口的地方都會把端口号換成相應的服務名,非常有用,很直覺。

3)Activity mode:顯示流量是按Kbits/s還是Kbytes/s,建議改成後面的更符合習慣。

4)Additional ports:按端口号監控所額外需要監控的端口,預設隻監控小于1024的。

  • Filters

這個預設就行了,除非有特殊需要。

  • IP traffic monitor

根據連接配接檢視網絡流量,這個最好讓他跑一段時間看統計總量的結構,如果單個連接配接占用大量帶寬,就很容易看出來。同時根據IP還可以很容易分辨是和内網還是外網伺服器進行互動。pkt captured可以看到mac位址。

3、sar指令

  • sar(System Activity Reporter系統活動情況報告)是目前 Linux 上最為全面的系統性能分析工具之一,可以從多方面對系統的活動進行報告,包括:檔案的讀寫情況、系統調用的使用情況、磁盤I/O、CPU效率、記憶體使用狀況、程序活動及IPC有關的活動等。
  • sar指令包含在sysstat工具包中,提供系統的衆多統計資料。其在不同的系統上指令有些差異,某些系統提供的sar支援基于網絡接口的資料統計,也可以檢視裝置上每秒收發包的個數和流量。
[root@Super soft]# which sar
/usr/bin/which: no sar in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/soft/node-v6.10.2-linux-x64/bin/node:/root/bin)
[root@Super soft]# yum -y install sysstat
[root@Super soft]# which sar
/bin/sar
[root@Super soft]# sar -n DEV 1 2
Linux 3.10.0-693.21.1.el7.x86_64 (Super.Admin)  08/11/2018  _x86_64_    (16 CPU)

04:54:14 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
04:54:15 PM     ens32     18.00      2.00      1.45      0.12      0.00      0.00      0.00
04:54:15 PM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:54:15 PM   docker0      0.00      0.00      0.00      0.00      0.00      0.00      0.00

04:54:15 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
04:54:16 PM     ens32     15.00      3.00      1.44      0.60      0.00      0.00      0.00
04:54:16 PM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:54:16 PM   docker0      0.00      0.00      0.00      0.00      0.00      0.00      0.00

Average:        IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
Average:        ens32     16.50      2.50      1.45      0.36      0.00      0.00      0.00
Average:           lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:      docker0      0.00      0.00      0.00      0.00      0.00      0.00      0.00
[root@Super soft]# 
           

1 2 表示:間隔時間(秒)、次數,即每一秒鐘取1次值,取2次。

-n參數有6個開關:

DEV:顯示網絡接口資訊。

EDEV:顯示關于網絡錯誤的統計資料。

NFS:統計活動的NFS用戶端的資訊。

NFSD:統計NFS伺服器的資訊

SOCK:顯示套接字資訊

ALL:顯示所有5個開關

IFACE:LAN接口

rxpck/s:每秒鐘接收的資料包

txpck/s:每秒鐘發送的資料包

rxbyt/s:每秒鐘接收的位元組數

txbyt/s:每秒鐘發送的位元組數

rxcmp/s:每秒鐘接收的壓縮資料包

txcmp/s:每秒鐘發送的壓縮資料包

rxmcst/s:每秒鐘接收的多點傳播資料包

rxerr/s:每秒鐘接收的壞資料包

txerr/s:每秒鐘發送的壞資料包

coll/s:每秒沖突數

rxdrop/s:因為緩沖充滿,每秒鐘丢棄的已接收資料包數

txdrop/s:因為緩沖充滿,每秒鐘丢棄的已發送資料包數

txcarr/s:發送資料包時,每秒載波錯誤數

rxfram/s:每秒接收資料包的幀對齊錯誤數

rxfifo/s:接收的資料包每秒FIFO過速的錯誤數

txfifo/s:發送的資料包每秒FIFO過速的錯誤數

  • 實時監控腳本
[root@Super soft]# cat net.sh 
#!/bin/bash
export LANG="en_US.UTF-8"
#export LANG="zh_CN.UTF8"
source /etc/rc.d/init.d/functions
[ -f /etc/profile ] && . /etc/profile
[ -f ~/.bash_profile ] && . ~/.bash_profile
ethn=$1
while true
do
 RX_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
 TX_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
 sleep 1
 RX_next=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
 TX_next=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')

 clear
 echo -e "time \tInterface\t RX   \t   TX"

 RX=$((${RX_next}-${RX_pre}))
 TX=$((${TX_next}-${TX_pre}))

 if [[ $RX -lt 1024 ]];then
 RX="${RX}B/s"
 elif [[ $RX -gt 1048576 ]];then
 RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
 else
 RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
 fi

 if [[ $TX -lt 1024 ]];then
 TX="${TX}B/s"
 elif [[ $TX -gt 1048576 ]];then
 TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
 else
 TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
 fi

 echo -e "`date +%k:%M:%S` $ethn\t\t$RX $TX" 
done
[root@Super soft]# ./net.sh ens32
time    Interface    RX        TX
17:16:29 ens32      1.1543KB/s 254B/s
time    Interface    RX        TX
17:16:30 ens32      1.03711KB/s 0B/s           

腳本是通過讀取運作時檔案系統/proc/net/dev中的網絡實時資料,并通過簡單計算得到的。

Linux 核心提供了一種通過 /proc 檔案系統,在運作時通路核心内部資料結構、改變核心設定的機制。proc檔案系統是一個僞檔案系統,它隻存在記憶體當中,而不占用外存空間。它以檔案系統的方式為通路系統核心資料的操作提供接口。使用者和應用程式可以通過proc得到系統的資訊,并可以改變核心的某些參數。由于系統的資訊,如程序,是動态改變的,是以使用者或應用程式讀取proc檔案時,proc檔案系統是動态從系統核心讀出所需資訊并送出的。/proc檔案系統中包含了很多目錄,其中/proc/net/dev 儲存了網絡擴充卡及統計資訊。

linux網絡流量檢視指令

Receive表示收包,

Transmit表示發送包

bytes表示收發的位元組數;

packets表示收發正确的包量;

errs表示收發錯誤的包量;

drop表示收發丢棄的包量;

4、nload指令

檢視總帶寬使用情況,可以分開來監控入站流量和出站流量。它還可以繪制圖表以顯示入站流量和出站流量,視圖比例可以調整。用起來很簡單,不支援許多選項。

CentOS需要從Epel軟體庫獲得nload。

[root@localhost ~]# yum install -y epel-release
[root@localhost ~]# yum install nload -y
[root@localhost ~]# nload -h

nload version 0.7.4
Copyright (C) 2001 - 2012 by Roland Riegel <[email protected]>
nload comes with ABSOLUTELY NO WARRANTY. This is free software, and you are
welcome to redistribute it under certain conditions. For more details see the
GNU General Public License Version 2 (http://www.gnu.org/copyleft/gpl.html).

Command line syntax:
nload [options] [devices]
nload --help|-h

Options:
-a period       Sets the length in seconds of the time window for average
                calculation.
                Default is 300.    #平均計算時間長度
-i max_scaling  Specifies the 100% mark in kBit/s of the graph indicating the
                incoming bandwidth usage. Ignored if max_scaling is 0 or the
                switch -m is given.
                Default is 10240.    #指定入口流量最大值 機關是kBit/s
-m              Show multiple devices at a time; no traffic graphs.   #同時顯示多個裝置統計資訊
-o max_scaling  Same as -i but for the graph indicating the outgoing bandwidth
                usage.
                Default is 10240.   #和-i相同,但是是出口流量
-t interval     Determines the refresh interval of the display in milliseconds.
                Default is 500.    #重新整理間隔,機關是毫秒,
-u h|b|k|m|g    Sets the type of unit used for the display of traffic numbers.
   H|B|K|M|G    h: auto, b: Bit/s, k: kBit/s, m: MBit/s etc.
                H: auto, B: Byte/s, K: kByte/s, M: MByte/s etc.
                Default is h.    #設定右邊Curr、Avg、Min、Max的資料機關,預設是自動變的.注意大小寫機關不同!
-U h|b|k|m|g    Same as -u, but for a total amount of data (without "/s").
   H|B|K|M|G    Default is H.    #和-u相同,但是為總流量
devices         Network devices to use.
                Default is to use all auto-detected devices.    # 指定網卡,預設是全部監控的,使用左右鍵切換。
--help
-h              Print this help.

example: nload -t 200 -i 1024 -o 128 -U M

The options above can also be changed at run time by pressing the 'F2' key.     #運作時使用F2可以檢視此頁

[root@localhost ~]# 
           
linux網絡流量檢視指令

目前流量(Curr),平均流量(Avg),最小流量(Min),最大流量(Max),總和流量(Ttl)

程序的流量

1、nethogs指令

nethogs用來按程序或程式實時統計網絡帶寬使用率,不依賴核心中的子產品。可以顯示每個程序所使用的帶寬,并對清單排序,将耗用帶寬最多的程序排在最上面。yum安裝NetHogs需要配置EPEL源,或者https://sourceforge.net/projects/nethogs/ 下載下傳

[root@Super ~]# yum install -y epel-release
[root@Super ~]# yum install libpcap nethogs -y
[root@Super ~]# nethogs --help
nethogs: invalid option -- '-'
usage: nethogs [-V] [-h] [-b] [-d seconds] [-v mode] [-c count] [-t] [-p] [-s] [device [device [device ...]]]
        -V : prints version.
        -h : prints this help.
        -b : bughunt mode - implies tracemode.
        -d : delay for update refresh rate in seconds. default is 1.    #重新整理時間,機關秒,預設1秒
        -v : view mode (0 = KB/s, 1 = total KB, 2 = total B, 3 = total MB). default is 0.
        -c : number of updates. default is 0 (unlimited).
        -t : tracemode.       #跟蹤模式
        -p : sniff in promiscious mode (not recommended).
        -s : sort output by sent column.
   -a : monitor all devices, even loopback/stopped ones.
        device : device(s) to monitor. default is all interfaces up and running excluding loopback    #監聽的裝置,預設是eth0,也就是網卡裝置名稱

When nethogs is running, press:
 q: quit
 s: sort by SENT traffic     #按send進行排序
 r: sort by RECEIVE traffic   #按received進行排序
 m: switch between total (KB, B, MB) and KB/s mode   #按 m 鍵,切換機關或顯示程序占用速度或已統計使用的流量。切換順序是KB/sec->KB->B->MB
[root@Super ~]#            

源碼方式安裝:

yum install gcc-c++ libpcap-devel.x86_64 libpcap.x86_64 ncurses*

git clone https://github.com/raboof/nethogs

cd nethogs/

make

make install

  • 直接運作nethogs 就可以檢視目前占用帶寬的程序
  • 監視裝置(eth0)的網絡帶寬
[root@Super ~]# nethogs eth0           
  • 同時監視eth0和eth1接口
[root@Super ~]# nethogs eth0 eth1           

繼續閱讀