查看流量 watch -n 1 "/sbin/ifconfig eth0 | grep bytes"
終端下網速監測工具 -- Speedometer
Speedometer 可以同時檢測多個網絡接口、文件,並在一起顯示出來,只要你的屏幕夠大~
Speedmeter 的主頁: http://excess.org/speedometer/
./speedometer.py -t eth0 發
./speedometer.py -r eth0 收
1. # iptraf -g //這個最直觀
2. iperf Iperf 是一個網絡性能測試工具。Iperf可以測試最大TCP和UDP帶寬性能。Iperf具有多種參數和UDP特性,可以根據需要調整。Iperf可以報告帶寬,延遲抖動和數據包丟失
TCP:
server(pc): iperf -s -i 1
client(ls1b): iperf -t 20 -i 1 -c 192.168.1.167 (pc-ip)
UDP:
server(pc): iperf -u -s -i 1
client(ls1b): iperf -t 10 -i 1 -u -b 1000M -c 192.168.1.90
3. # ifconfig -a
eth0:Link encap:Ethernet HWaddr 00:1e:64:63:ab:fe
inet addr:192.168.3.103 Bcast:192.168.3.255 Mask:255.255.255.0
inet6 addr: fe80::21e:64ff:fe63:abfe/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:70283 errors:0 dropped:0 overruns:0 frame:0
TX packets:46701 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:73276012 (73.2 MB) TX bytes:6267375 (6.2 MB)
4. # mii-tool -w -v //更詳細的用法 man mii-tool
5. ethtool是用來顯示和更改網卡設置的工具 更詳細的用法 man ethtool
# ethtool eth0 //顯示網絡端口設置功能
Settings for eth0:
Supported ports: [ TP MII ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Link partner advertised link modes: Not reported
Link partner advertised pause frame use: No
Link partner advertised auto-negotiation: No
Speed: 10Mb/s
Duplex: Half
Port: MII
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
Supports Wake-on: pumbg
Wake-on: g
Current message level: 0x00000033 (51)
Link detected: no
# ethtool -i eth0 //顯示跟硬件相關的信息
driver: r8169
version: 2.3LK-NAPI
firmware-version:
bus-info: 0000:05:00.0
6. # watch -n 1 "/sbin/ifconfig eth0 | grep bytes"
1. sar -n DEV 1 2
sar命令包含在sysstat工具包中,提供系統的眾多統計數據。其在不同的系統上命令有些差異,某些系統提供的sar支持基於網絡接口的數據統計,也可以查看設備上每秒收發包的個數和流量。
1
|
sar –n DEV 1 2
|
命令后面1 2 意思是:每一秒鍾取1次值,取2次。
DEV顯示網絡接口信息
另外,-n參數很有用,他有6個不同的開關:DEV | EDEV | NFS | NFSD | SOCK | ALL ,其代表的含義如下:
- DEV顯示網絡接口信息。
- EDEV顯示關於網絡錯誤的統計數據。
- NFS統計活動的NFS客戶端的信息。
- NFSD統計NFS服務器的信息
- SOCK顯示套接字信息
- ALL顯示所有5個開關
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[sre@CDVM-213017031 ~]$ sar -n DEV 1 2
Linux 2.6.32-431.el6.x86_64 (CDVM-213017031) 05
/04/2017
_x86_64_ (4 CPU)
08:05:30 PM IFACE rxpck
/s
txpck
/s
rxkB
/s
txkB
/s
rxcmp
/s
txcmp
/s
rxmcst
/s
08:05:31 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
08:05:31 PM eth0 1788.00 1923.00 930.47 335.60 0.00 0.00 0.00
08:05:31 PM IFACE rxpck
/s
txpck
/s
rxkB
/s
txkB
/s
rxcmp
/s
txcmp
/s
rxmcst
/s
08:05:32 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
08:05:32 PM eth0 1387.00 1469.00 652.12 256.98 0.00 0.00 0.00
Average: IFACE rxpck
/s
txpck
/s
rxkB
/s
txkB
/s
rxcmp
/s
txcmp
/s
rxmcst
/s
Average: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: eth0 1587.50 1696.00 791.29 296.29 0.00 0.00 0.00
|
參數說明:
- 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過速的錯誤數
這種方式簡單,直觀,推薦使用。
2.實時監控腳本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#!/bin/bash
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
"\t RX `date +%k:%M:%S` 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
"$ethn \t $RX $TX "
done
|
此腳本不需要額外再安裝軟件,並且可自定義欲查看接口,精確到小數,可根據流量大小靈活顯示單位,默認采集間隔為1秒。
用法為:
1、將腳本保存為可執行腳本文件,比如叫net.sh。
2、chmod +x ./net.sh 將文件改成可執行腳本。
3、sh net.sh eth0即可開始監看接口eth0流量,按ctrl+c退出。
腳本是通過讀取運行時文件系統/proc/net/dev中的網絡實時數據,並通過簡單計算得到的。關於目錄/proc/net/dev,請參考下文。
3. cat /proc/net/dev
Linux 內核提供了一種通過 /proc 文件系統,在運行時訪問內核內部數據結構、改變內核設置的機制。proc文件系統是一個偽文件系統,它只存在內存當中,而不占用外存空間。它以文件系統的方式為訪問系統內核數據的操作提供接口。用戶和應用程序可以通過proc得到系統的信息,並可以改變內核的某些參數。由於系統的信息,如進程,是動態改變的,所以用戶或應用程序讀取proc文件時,proc文件系統是動態從系統內核讀出所需信息並提交的。/proc文件系統中包含了很多目錄,其中/proc/net/dev 保存了網絡適配器及統計信息。
1
2
3
4
5
|
[sre@CDVM-213017031 ~]$
cat
/proc/net/dev
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
lo:137052296 108029 0 0 0 0 0 0 137052296 108029 0 0 0 0 0 0
eth0:13661574714188 31346790620 0 0 0 0 0 0 5097461049535 27671144304 0 0 0 0 0 0
|
最左邊的表示接口的名字,Receive表示收包,Transmit表示發送包;
- bytes表示收發的字節數;
- packets表示收發正確的包量;
- errs表示收發錯誤的包量;
- drop表示收發丟棄的包量;
其實,我們平時經常用的很多查看網卡實時流量的命令,都是通過讀取該目錄下的實時流量,並通過簡單計算得到的。
4. 使用watch命令,配合ifconfig、more /proc/net/dev、cat /proc/net/dev來實時監控。比如執行 watch -n 1 "ifconfig eth0"
1
2
3
4
5
6
7
8
9
10
|
Every 1.0s: ifconfig eth0Thu May 4 20:26:45 2017
eth0 Link encap:Ethernet HWaddr FA:16:3E:7E:55:D1
inet addr:10.213.17.31 Bcast:10.213.23.255 Mask:255.255.248.0
inet6 addr: fe80::f816:3eff:fe7e:55d1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:31350149703 errors:0 dropped:0 overruns:0 frame:0
TX packets:27674701465 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:13663400883450 (12.4 TiB) TX bytes:5098104759633 (4.6 TiB)
|
watch可以幫你監測一個命令的運行結果,省得你一遍遍的手動運行。在Linux下,watch是周期性的執行下個程序,並全屏顯示執行結果。