2015-10-15 49 views
-1

在我的Linux服务器当我运行netstat -su我能UDP报文的统计数据是这样的:在Linux中,netstat -su命令获取统计信息?

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 

哪里netstat命令从得到这些统计数字?我可以清除缓冲区以使它们从零开始?

回答

5

你可以在不离开你的终端的情况下找到这些东西的答案。

让我们来看看自己:

# strace netstat -su &> netstat_strace 

这将是一个“开放”和“读”,因为它正从某个数据(但用grep出它无法读取/开):

# 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 

,并从检查strace输出,我们可以看到它写的字符串:

write(1, "IcmpMsg:\n InType0: 11\n InT"..., 373IcmpMsg: 
    InType0: 11 

好吧,那很有趣。让我们来看看男人页netstat

man netstat 

如果你看看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 

... 

你可以看到为什么它open版,并从上述read。在搜索“清除”或“重置”(或阅读它)时,您会发现这些不是该命令的选项。

下一步将检查man proc,它将自己描述为“过程信息伪文件系统”。

从这里,你可以得到这样的想法,如果你修改了netstat读取的文件,你可以改变netstat的输出(在我看来,/proc/net/netstat看起来特别有趣) - 而且你可以 - 但是我会建议让它只读。

+1

我喜欢这个很聪明的“逆向工程”类方法 – Marged

1

计数器被设计为不被重置作为一项规则,如果它们被重置,它将失去作为计数器的目的。计数器的要点是数据的使用者可以轮询它们并计算速率,或者计算自某个时间以前的增量,但轮询的频率并不重要。数据可能会有很多不同的消费者,如果计数器发生故障(可以说零),则消费者可以丢弃数据时间段或假定他们已经翻身(可能导致错误报告)。 (例如,运行脚本获取统计数据,查看其当前值,并提供扣除这些初始值的后续读数)。