[精讚] [會員登入]
175

[Centos] 查看網路的流量

介紹 sar, iftop, nethogs, 和 vnstat 等流量監控程式

分享此文連結 //n.sfs.tw/16491

分享連結 [Centos] 查看網路的流量@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
最後編修
2025-02-25 16:09:20 By 張○○
 

自動目錄

這近的需求是要查看linux裡面網路的使用量,以前可以用ifconfig看即時的上下傳再除頻寬,現在沒有這個指令。

於是參考[1][2]安裝幾個程式來用

sar

安裝

# yum install sysstat -y

執行如下命令,使用sar每1秒統計一次網路介面的活動狀況,連續統計5次。

# sar -n DEV 1 5

14時57分42秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
14時57分42秒    ens192     66.00     22.00      4.35      7.35      0.00      0.00      0.00
14時57分42秒 br-9a04a3103406      0.00      0.00      0.00      0.00      0.00      0.00      0.00
14時57分42秒 br-bb96b8cee087      0.00      0.00      0.00      0.00      0.00      0.00      0.00
14時57分42秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
14時57分42秒 br-ff4045892b6c      0.00      0.00      0.00      0.00      0.00      0.00      0.00
14時57分42秒   docker0      0.00      0.00      0.00      0.00      0.00      0.00      0.00

<中間省略3筆>

14時57分45秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
14時57分46秒    ens192     70.00     28.00      5.24      6.80      0.00      0.00      0.00
14時57分46秒 br-9a04a3103406      0.00      0.00      0.00      0.00      0.00      0.00      0.00
14時57分46秒 br-bb96b8cee087      0.00      0.00      0.00      0.00      0.00      0.00      0.00
14時57分46秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
14時57分46秒 br-ff4045892b6c      0.00      0.00      0.00      0.00      0.00      0.00      0.00
14時57分46秒   docker0      0.00      0.00      0.00      0.00      0.00      0.00      0.00

平均時間:     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
平均時間:    ens192     69.40     31.80      6.04      7.76      0.00      0.00      0.00   <== 查看這個介面統計資料
平均時間: br-9a04a3103406      0.00      0.00      0.00      0.00      0.00      0.00      0.00
平均時間: br-bb96b8cee087      0.00      0.00      0.00      0.00      0.00      0.00      0.00
平均時間:        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
平均時間: br-ff4045892b6c      0.00      0.00      0.00      0.00      0.00      0.00      0.00
平均時間:   docker0      0.00      0.00      0.00      0.00      0.00      0.00      0.00

這個程式功能很強大,可以查看諸如中斷、cpu、記憶體、各種網路流量,但是顯示太多了,眼花僚亂。

 

iftop

安裝

# yum install iftop

執行

# iftop -i ens192 -P

# iftop -i ens192 -t

-t 只會顯示文字,-P會顯示介面全部的流量,-p會顯示埠號。

   # Host name (port/service if enabled)            last 2s   last 10s   last 40s cumulative
--------------------------------------------------------------------------------------------
   1 n.sfs.tw                                 =>     33.1Kb     18.2Kb     18.2Kb     13.7KB
     211.75.194.243                           <=     7.14Kb     4.04Kb     4.04Kb     3.03KB
    <中間省略>
  10 n.sfs.tw.                                =>         0b       923b       923b       692B
     2001:500:f0::63                          <=         0b     1.16Kb     1.16Kb       888B
--------------------------------------------------------------------------------------------
Total send rate:                                     57.6Kb     38.7Kb     38.7Kb
Total receive rate:                                  34.0Kb     28.7Kb     28.7Kb
Total send and receive rate:                         91.6Kb     67.4Kb     67.4Kb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     57.6Kb     49.5Kb      101Kb
Cumulative (sent/received/total):                    29.0KB     21.5KB     50.6KB

會顯示每一個連線的流量,然後會做summary,有點像 ss ,但ss不會顯示流量。

 

 

nethogs

安裝

# yum install nethogs -y

執行

# nethogs ens192 -v 0

-v 是用KB來顯示流量

主要是by pid來看即時流量,因為數據會一直跳,不太好看。

 

vnstat

安裝

# yum install vnstat

執行

# vnstat  -i ens192 -l

-l 會持續監聽

Monitoring ens192...    (press CTRL-C to stop)
  rx:       61 kbit/s    92 p/s          tx:      841 kbit/s    52 p/s
^C <按ctrl-c中斷>
 ens192  /  traffic statistics
                           rx         |       tx
--------------------------------------+------------------
  bytes                      890 KiB  |        2.59 MiB
--------------------------------------+------------------
          max            1.41 Mbit/s  |      849 kbit/s
      average          222.54 kbit/s  |   663.75 kbit/s
          min              24 kbit/s  |       11 kbit/s
--------------------------------------+------------------
  packets                       3132  |            1496
--------------------------------------+------------------
          max                200 p/s  |          84 p/s
      average                 97 p/s  |          46 p/s
          min                 48 p/s  |           6 p/s
--------------------------------------+------------------
  time                    32 seconds

# vnstat  -i ens192 -tr 60

用-tr 指定60秒取樣

3569 packets sampled in 60 seconds
Traffic average for ens192

      rx        28.40 kbit/s            49 packets/s
      tx        17.65 kbit/s             9 packets/s

 

結論

要看每個連線流量,推薦 iftop

sar 可以看各種流量、cpu或其它資訊,有點複雜。

要看每個pid的流量,用 nethogs

要看統計的流量,用 vnstat

 

參考資料

[1] https://blog.csdn.net/sumengnan/article/details/108142494

[2] https://www.alibabacloud.com/help/tc/ecs/support/query-and-case-analysis-of-linux-network-traffic-load

[3] https://www.globo.tech/learning-center/install-sar-centos/

 

END

你可能感興趣的文章

[Centos7] 安裝sphinx+php7 在centos7上安裝sphinx,在PHP7上運作正確

[CENTOS7 8] 查看記憶體 linux 中查看記憶體狀態指令整理

EXCEL+phpPgAdmin 使用phpPgAdmin 匯出資料 直接使用phpPgAmin下載資料的方法

[SELINUX] php 呼叫系統程式(使用exec, system等)執行網路服務 這近想透過PHP呼叫系統的程式來執行網路的服務,怎麼測試都失敗…

[Centos] opentftp + selinux 安裝及設定 Centos 安裝微型ftp伺服器:tftp伺服器

使用dnstop 來觀察dns運作的情形 dnstop是非常方便的工具,可以觀察即時的dns查詢。 安裝 yum install dnstop

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

[jQuery] textarea 的取值和給值 HTML 的 TEXTAREA 標籤若要用 jquery 取值,不能使用 .text() 或 .html() ,使用 .

[jQuery] select 元件的取值及給值 html中的元件select,在jquery中要如何使用?

SELinux 常用指令和檔案 在Redhat系列中,Centos5以後加入了selinux,他並沒有這麼可怕,不必每次看到Selinux ,就想把他

[札記] 2016.7~12月札記 札記,只是札記

[Win7] 燒錄 iso 檔 在Windows7 中內建燒錄程式,可以直接把檔案拉到光碟機裡,再執行燒錄。