侧边栏壁纸
Linux入门自学网博主等级

每日学一条Linux命令,终成Linux大神

  • 累计撰写 725 篇文章
  • 累计创建 143 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Linux lnstat命令教程:如何查看网络统计信息(附实例详解和注意事项)

Linux lnstat命令介绍

lnstat(Linux Network Statistics)是一个通用的和更加功能完善的替代旧的rtstat程序。它可以定期打印内核导出的各种统计值,除了路由缓存统计,它还支持内核通过/proc/net/stat/文件导出的任何类型的统计。

Linux lnstat命令适用的Linux版本

lnstat命令适用于大多数Linux发行版,如Ubuntu,Debian,Fedora,CentOS等。如果你的系统没有安装lnstat命令,你可以使用以下命令来安装:

[linux@bashcommandnotfound.cn ~]$ sudo apt-get install iproute2 # Ubuntu/Debian
[linux@bashcommandnotfound.cn ~]$ sudo yum install iproute # CentOS 7
[linux@bashcommandnotfound.cn ~]$ sudo dnf install iproute # CentOS 8/Fedora

Linux lnstat命令的基本语法

lnstat命令的基本语法如下:

lnstat [options]

Linux lnstat命令的常用选项或参数说明

lnstat命令支持以下常用选项或参数:

选项参数说明
-h显示帮助信息
-V显示程序版本
-c打印个间隔
-d打印可用的文件/键列表
-f使用指定的统计文件,可以多次指定,默认使用/proc/net/stat下的所有文件
-i设置间隔为秒
-j以JSON格式显示结果
-kk,k,k,...只显示指定的键,每个键k的格式为[file:]key,如果指定,则只在该文件中搜索键,否则使用第一个包含该键的文件
-s[0-2]指定标题/主题的显示,'0'表示不显示标题,'1'表示只在程序开始时打印标题,'2'表示每20行打印一次标题
-wn,n,n,...指定每个字段的宽度

Linux lnstat命令的实例

实例1:获取支持的统计文件列表

[linux@bashcommandnotfound.cn ~]$ lnstat -d
arp_cache
entries
allocs
destroys
hash_grows
lookups
...
out_martian_dst
out_martian_src

这个实例显示了lnstat支持的统计文件和键的列表,每个文件下有一些键,表示不同的统计项。

实例2:打印指定的文件和键的统计值

[linux@bashcommandnotfound.cn ~]$ lnstat -f arp_cache -k entries,lookups,hits
arp_cache:entries arp_cache:lookups arp_cache:hits
0                0                0
0                0                0
0                0                0
...

这个实例显示了arp_cache文件下的entries,lookups和hits键的统计值,每秒打印一次,每个值表示与上次的差值。

实例3:设置间隔为10秒

[linux@bashcommandnotfound.cn ~]$ lnstat -i 10
arp_cache:entries arp_cache:lookups arp_cache:hits ndisc_cache:entries ndisc_cache:lookups ndisc_cache:hits ip_conntrack:entries ip_conntrack:searched ip_conntrack:found ip_conntrack:new ip_conntrack:invalid ip_conntrack:ignore ip_conntrack:delete ip_conntrack:delete_list
    ...

实例4:打印指定的文件和键的统计值,并设置间隔为5秒,打印10个间隔

[linux@bashcommandnotfound.cn ~]$ lnstat -f nf_conntrack -k entries,searched,found -i 5 -c 10
nf_conntrack:entries nf_conntrack:searched nf_conntrack:found
0                    0                     0
0                    0                     0
0                    0                     0
...

这个实例显示了nf_conntrack文件下的entries,searched和found键的统计值,每5秒打印一次,共打印10个间隔。

实例5:以JSON格式显示结果

[linux@bashcommandnotfound.cn ~]$ lnstat -j
{"arp_cache":{"entries":0,"lookups":0,"hits":0,"res_failed":0,"rcv_probes_mcast":0,"rcv_probes_ucast":0,"periodic_gc_runs":0,"forced_gc_runs":0,"unres_probes":0,"table_fulls":0},"ndisc_cache":{"entries":0,"lookups":0,"hits":0,"res_failed":0,"rcv_probes_mcast":0,"rcv_probes_ucast":0,"periodic_gc_runs":0,"forced_gc_runs":0,"unres_probes":0,"table_fulls":0},"ip_conntrack":{"entries":0,"searched":0,"found":0,"new":0,"invalid":0,"ignore":0,"delete":0,"delete_list":0,"insert":0,"insert_failed":0,"drop":0,"early_drop":0,"icmp_error":0,"expect_new":0,"expect_create":0,"expect_delete":0,"search_restart":0},"nf_conntrack":{"entries":0,"searched":0,"found":0,"new":0,"invalid":0,"ignore":0,"delete":0,"delete_list":0,"insert":0,"insert_failed":0,"drop":0,"early_drop":0,"icmp_error":0,"expect_new":0,"expect_create":0,"expect_delete":0,"search_restart":0},"rt_cache":{"in_hit":0,"in_slow_tot":0,"in_slow_mc":0,"in_no_route":0,"in_brd":0,"in_martian_dst":0,"in_martian_src":0,"in_hlist_search":0,"out_hit":0,"out_slow_tot":0,"out_slow_mc":0,"gc_total":0,"gc_ignored":0,"gc_goal_miss":0,"gc_dst_overflow":0,"in_redirect":0,"in_discard":0,"out_discard":0,"drop_redirect":0,"in_no_policy":0,"out_no_policy":0,"out_brd":0,"out_martian_dst":0,"out_martian_src":0}}

这个实例以JSON格式显示了所有文件和键的统计值,每个文件和键都是一个JSON对象的属性,属性值是一个数字。

实例6:设置每个字段的宽度为10

[linux@bashcommandnotfound.cn ~]$ lnstat -w 10,10,10
arp_cache:entries arp_cache:lookups arp_cache:hits
0                0                0
0                0                0
0                0                0
...

这个实例设置了每个字段的宽度为10个字符,如果字段名或值超过10个字符,会被截断。

Linux lnstat命令的注意事项

  • lnstat命令只能显示内核导出的统计值,如果内核没有导出某些文件或键,lnstat命令无法显示。
  • lnstat命令显示的统计值是与上次的差值,而不是累计值,如果要显示累计值,可以使用-c 1选项。
  • 如果系统没有安装lnstat命令,会出现bash: lnstat: command not found的错误,可以按照上面的方法安装iproute2包来解决。
0

评论区