Linux常用命令-ifstat

命令

ifstat

描述

handy utility to read network interface statistics
查看网络接口统计信息

用法

1
ifstat [ OPTIONS ] [ INTERFACE_LIST ]

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Options:
-a, --ignore 忽略历史文件数据,即显示当前的数据
-d, --scan=SECS 指定每几秒抽样一次统计数据,注意默认为后台运行
-e, --errors 显示错误
-j, --json 显示为JSON格式
-n, --nooutput 不显示任何输出,只更新历史文件
-p, --pretty 优化显示格式
-r, --reset 重置历史
-s, --noupdate 不要更新历史
-t, --interval=SECS 显示过去几秒钟的平均值
-z, --zeros 显示活动为零的条目
-x, --extended=TYPE 显示指定类型的扩展统计信息,TYPE参数cpu_hits

默认历史文件路径/tmp/.ifstat.u$UID,如/tmp/.ifstat.u0

注意

示例

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
$ ifstat -V
ifstat utility, iproute2-ss170501

# 首次执行会显示当前汇总统计,并写入到/tmp/.ifstat.u0文件中
$ ifstat
#kernel
Interface RX Pkts/Rate TX Pkts/Rate RX Data/Rate TX Data/Rate
RX Errs/Drop TX Errs/Drop RX Over/Rate TX Coll/Rate
lo 465914 0 465914 0 625885K 0 625885K 0
0 0 0 0 0 0 0 0
eth0 8088K 0 829431 0 1527M 0 1036M 0
0 162 0 0 0 0 0 0
# 该文件为上一次的统计数据
$ more /tmp/.ifstat.u0
#kernel
1 lo 466027 0 466027 0 625892330 0 625892330 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
2 eth0 8092621 0 830397 0 1527932937 0 1036848657 0 0 0 0 0 162 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 再次执行,默认情况下仅显示上次调用和当前调用之间的差异,并更新/tmp/.ifstat.u0文件内容
$ ifstat
#kernel
Interface RX Pkts/Rate TX Pkts/Rate RX Data/Rate TX Data/Rate
RX Errs/Drop TX Errs/Drop RX Over/Rate TX Coll/Rate
lo 2 0 2 0 104 0 104 0
0 0 0 0 0 0 0 0
eth0 5 0 4 0 360 0 392 0
0 0 0 0 0 0 0 0

# -a 显示当前的统计数据
$ ifstat -a
# -j 显示为json格式
$ ifstat -j
{"kernel":{"lo":{"rx_packets":466070,"tx_packets":466070,"rx_bytes":625895786,"tx_bytes":625895786,"rx_errors":0,"tx_errors":0,"rx_dropped":0,"tx_dropped":0,"multicast":0,"collisions":0},"eth0":{"rx_packets":8093534,"tx_packets":830501,"rx_bytes":1527989911,"tx_bytes":1036857851,"rx_errors":0,"tx_errors":0,"rx_dropped":162,"tx_dropped":0,"multicast":0,"collisions":0}}}

# ifstat获取的数据和ifconfig查看的网卡数据是一致的
$ ifstat -j
{"kernel":{"lo":{"rx_packets":466171,"tx_packets":466171,"rx_bytes":625902258,"tx_bytes":625902258,"rx_errors":0,"tx_errors":0,"rx_dropped":0,"tx_dropped":0,"multicast":0,"collisions":0},"eth0":{"rx_packets":8096750,"tx_packets":831115,"rx_bytes":1528196531,"tx_bytes":1036914347,"rx_errors":0,"tx_errors":0,"rx_dropped":162,"tx_dropped":0,"multicast":0,"collisions":0}}}
$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.31.80 netmask 255.255.255.0 broadcast 192.168.31.255
inet6 fe80::2daa:d1ea:5915:ce9f prefixlen 64 scopeid 0x20<link>
ether 00:50:56:2f:07:60 txqueuelen 1000 (Ethernet)
RX packets 8096769 bytes 1528197941 (1.4 GiB)
RX errors 0 dropped 162 overruns 0 frame 0
TX packets 831126 bytes 1036915361 (988.8 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 466171 bytes 625902258 (596.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 466171 bytes 625902258 (596.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0