为什么 ping 可以给我快速的 rtt 值,但每次响应都需要很长时间才能返回?

为什么 ping 可以给我快速的 rtt 值,但每次响应都需要很长时间才能返回?

我这里有几个关于 ping 的基本网络问题。

当 ping 完全不同的子网上的特定主机时,我收到如下响应:

PING myhost.example.com (1.2.3.4) 56(84) bytes of data.
64 bytes from 1.2.3.4: icmp_req=1 ttl=115 time=1.88 ms
64 bytes from 1.2.3.4: icmp_req=2 ttl=115 time=1.66 ms
64 bytes from 1.2.3.4: icmp_req=3 ttl=115 time=1.96 ms
64 bytes from 1.2.3.4: icmp_req=4 ttl=115 time=1.95 ms

--- myhost.example.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 48191ms
rtt min/avg/max/mdev = 1.669/1.868/1.969/0.127 ms

这最初看起来正常,但随后您会看到获得这 4 个响应总共需要 48 秒。当 ping 运行时,在打印每行之前会有一个大约 10 秒的明显间隔。但是,rtt 几乎符合我的预期。

这造成了一些问题,因为我正尝试使用 nagios 监控此主机,而主机检查目前收到“网络不可达”错误。网络可达,rtt 正常,但我不禁怀疑总时间缓慢是否与此有关。

该主机距离有 14 个跳数,并且跟踪路由如下所示(我已将中间步骤匿名化,它们都是不同的 IP!):

traceroute to myhost (1.2.3.4), 30 hops max, 60 byte packets
 1  10.A.B.C (10.A.B.C)  0.680 ms  0.729 ms  0.833 ms
 2  10.A.B.C (10.A.B.C)  0.636 ms  0.678 ms  0.759 ms
 3  10.A.B.C (10.A.B.C)  0.803 ms  0.867 ms  0.923 ms
 4  10.A.B.C (10.A.B.C)  0.702 ms  0.738 ms  0.782 ms
 5  10.A.B.C (10.A.B.C)  1.027 ms  1.251 ms  1.342 ms
 6  10.A.B.C (10.A.B.C)  2.688 ms  1.436 ms  1.484 ms
 7  10.A.B.C (10.A.B.C)  2.960 ms  3.475 ms  3.527 ms
 8  10.A.B.C (10.A.B.C)  1.284 ms  1.310 ms  1.393 ms
 9  10.A.B.C (10.A.B.C)  1.990 ms  1.865 ms  1.964 ms
10  10.A.B.C (10.A.B.C)  1.750 ms  1.841 ms  1.748 ms
11  10.A.B.C (10.A.B.C)  1.849 ms  1.614 ms  1.628 ms
12  10.A.B.C (10.A.B.C)  1.997 ms  2.150 ms  2.119 ms
13  10.A.B.C (10.A.B.C)  2.442 ms  2.454 ms  2.560 ms
14  1.2.3.4 (1.2.3.4)  1.978 ms * *

什么原因造成这种情况?

答案1

这有点猜测……

您正在通过 DNS 名称执行 ping 操作。myhost.example.com
的 DNS 查找需要那么长时间才能解决吗?
如果通过 ip 地址执行 ping 操作会发生什么情况?

答案2

尝试编辑/etc/nsswitch.conf。mDNS给我带来了同样的问题。

如果你有:

hosts:  files mdns4_minimal [NOTFOUND=return] dns mdns4

尝试替换为:

hosts:  files dns

答案3

除了DNS查询问题外,也可能是ping包间隔问题导致的:

-i interval
          Wait  interval  seconds  between  sending each packet.  The default is to wait for one second between each packet normally, or not to wait in
          flood mode. Only super-user may set interval to values less 0.2 seconds.

如果你通过 IP ping google 的服务器:

# ping -c 3 74.125.130.101
PING 74.125.130.101 (74.125.130.101) 56(84) bytes of data.
64 bytes from 74.125.130.101: icmp_seq=1 ttl=50 time=2.51 ms
64 bytes from 74.125.130.101: icmp_seq=2 ttl=50 time=2.52 ms
64 bytes from 74.125.130.101: icmp_seq=3 ttl=50 time=2.72 ms

--- 74.125.130.101 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2006ms
rtt min/avg/max/mdev = 2.511/2.589/2.729/0.114 ms

总时间仍然比 rtts 的总和大得多。

但是如果你在洪水模式下使用 ping:

# ping -c 3 -f 74.125.130.101
PING 74.125.130.101 (74.125.130.101) 56(84) bytes of data.

--- 74.125.130.101 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 8ms
rtt min/avg/max/mdev = 2.474/2.744/3.140/0.286 ms, ipg/ewma 4.230/2.657 ms

或将间隔设置为 0:

# ping -c 3 -i 0 74.125.130.101
PING 74.125.130.101 (74.125.130.101) 56(84) bytes of data.
64 bytes from 74.125.130.101: icmp_seq=1 ttl=50 time=2.83 ms
64 bytes from 74.125.130.101: icmp_seq=2 ttl=50 time=2.87 ms
64 bytes from 74.125.130.101: icmp_seq=3 ttl=50 time=2.96 ms

--- 74.125.130.101 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 9ms
rtt min/avg/max/mdev = 2.834/2.891/2.966/0.083 ms, ipg/ewma 4.510/2.855 ms

总时间几乎等于 rtts 的总和。

相关内容