在Linux中netstat -su命令在哪里获取统计信息?

Y.L*_*.L. -1 linux netstat

在我运行的linux服务器中,我netstat -su可以得到udp数据包的统计信息,如下所示:

netstat -su 
IcmpMsg:
    InType0: 10827
    InType3: 42792
    InType8: 298795
    InType13: 2
    OutType0: 298795
    OutType3: 328120
    OutType8: 10827
    OutType14: 2 
Udp:
    232862733 packets received
    12074334 packets to unknown port received.
    555474 packet receive errors
    8650718 packets sent 
UdpLite: IpExt:
    InBcastPkts: 375
    InOctets: 169855997552
    OutOctets: 60497003017
    InBcastOctets: 144080
Run Code Online (Sandbox Code Playgroud)

netstat命令从哪里获取这些统计信息?我可以清除缓冲区以使它们从零开始吗?

小智 7

您无需离开终端即可找到这些事情的答案.

让我们自己看看:

# strace netstat -su &> netstat_strace
Run Code Online (Sandbox Code Playgroud)

它将是一个'open'和'read',因为它从某个地方获取数据(但是grep out无法读取/打开的地方):

# grep -E 'open|read' netstat_strace  | grep -v ENOENT
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\37\2\0\0\0\0\0"..., 832) = 832
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
open("/proc/meminfo", O_RDONLY|O_CLOEXEC) = 3
read(3, "MemTotal:        3854816 kB\nMemF"..., 1024) = 1024
open("/proc/net/snmp", O_RDONLY)        = 3
read(3, "Ip: Forwarding DefaultTTL InRece"..., 4096) = 1261
open("/usr/share/locale/locale.alias", O_RDONLY|O_CLOEXEC) = 4
read(4, "# Locale name alias data base.\n#"..., 4096) = 2570
read(4, "", 4096)                       = 0
read(3, "", 4096)                       = 0
open("/proc/net/netstat", O_RDONLY)     = 3
read(3, "TcpExt: SyncookiesSent Syncookie"..., 4096) = 2158
read(3, "", 4096)                       = 0
Run Code Online (Sandbox Code Playgroud)

从检查strace输出,我们可以看到它正在写一个字符串:

write(1, "IcmpMsg:\n    InType0: 11\n    InT"..., 373IcmpMsg:
    InType0: 11
Run Code Online (Sandbox Code Playgroud)

好吧,这很有趣.让我们看看手册页netstat:

man netstat
Run Code Online (Sandbox Code Playgroud)

如果你看下FILES:

FILES

   /etc/services -- The services translation file

   /proc -- Mount point for the proc filesystem, which gives access to kernel status information via the following files.

   /proc/net/dev -- device information

   /proc/net/raw -- raw socket information

   /proc/net/tcp -- TCP socket information

   /proc/net/udp -- UDP socket information

   /proc/net/igmp -- IGMP multicast information

...
Run Code Online (Sandbox Code Playgroud)

你可以从上面看到它为什么open编辑read.在搜索"清除"或"重置"(或读取)时,您会发现这些不是命令的选项.

下一步是签出man proc,它将自己描述为"进程信息伪文件系统".

从这里开始,您可以了解如果您修改了netstat读取的文件,您可以更改netstat的输出(/proc/net/netstat在我看来看起来特别有趣) - 你可以 - 但我建议让这个读取只要.