各网络层问题排查工具

425 阅读2分钟

应用层

HTTP应用排查工具 主要是Chrome的开发者工具

  • 可以找到有问题的服务端IP
  • 辅助排查网页慢的问题
  • 解决失效cookie带来的问题

表示层和会话层

常见的TLS问题排查

  • 一是基于浏览器做初步的检车,主要围绕证书的本省做检查

  • 关于 TLS 握手、密钥交换、密文传输等方面的排查,还是需要用 tcpdump 和 Wireshark 来做

传输层

路径可达性测试

如果我们要测试 TCP 握手,我们有 telnet、nc 这两个常规工具。比如 telnet:


$ telnet www.baidu.com 443
Trying 180.101.49.12...
Connected to www.a.shifen.com.
Escape character is '^]'.

用 nc 呢,可以这样:

$ nc -w 2 -zv www.baidu.com 443
Connection to www.baidu.com 443 port [tcp/https] succeeded!

查看当前连接状况

netstat命令


$ netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp        0    280 10.0.2.15:22            10.0.2.2:56669          ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN

查看当前连接的传输速率

有时候,网络跑得挺繁忙的,但却不知道哪个连接占用了大量的带宽?可以用 iftop

查看丢包和乱序等的统计

其实,用 netstat 除了可以获取实时连接状况,还可以获取历史统计信息。比如,你怀疑一台机器的网络很不稳定,除了用 ping 做简单的测试,你还可以用 netstat -s 来获取更加详细的统计信息。比如,其中的 TCP 丢包和乱序计数值,就能帮助你判断传输层的状况。下面是我截取了一次 netstat -s 命令的输出:

watch --diff netstat -s

这个命令会把发生变化的数值进行高亮,方便我们查看

还有 ss ss 命令是 Iproute2 包里的命令,也是 netstat 的“取代者”。它提供了对 socket 的丰富的统计信息。比如下面这条命令我也经常用,可以查看到当前连接的统计信息:


$ ss -s
Total: 164
TCP:   5 (estab 1, closed 0, orphaned 0, timewait 0)

Transport Total     IP        IPv6
RAW    1         0         1
UDP    2         2         0
TCP    5         4         1
INET    8         6         2
FRAG    0         0         0

网络层

traceroute 和 mtr

查看网络路径状况

traceroute www.baidu.com -I

$ traceroute  www.baidu.com
traceroute to www.a.shifen.com (180.101.49.12), 64 hops max
  1   10.0.2.2  0.133ms  0.131ms  0.087ms
  2   192.168.1.1  3.048ms  1.466ms  1.574ms
  3   100.65.0.1  8.975ms  3.067ms  6.472ms
  4   61.152.53.149  5.644ms  3.691ms  4.624ms
  5   61.152.24.226  5.357ms  4.393ms  4.244ms
  6   202.97.29.122  10.171ms  10.403ms  8.755ms
  7   58.213.94.118  10.707ms  11.880ms  11.441ms
  8   58.213.94.90  9.644ms  *  *
  9   58.213.96.110  12.758ms  12.095ms  11.842ms
 10   *  *  *
 11   *  *  *
 12   *  *  *
 13   *  *  *
 14   *  *  *
 15   *  *  *
 16   *  *  *
 17   *  *  *
 18   *  *  *
 19   *  *  *
 20   *  *  *

traceroute它不能对这个路径做连续多次的探测。于是,mtr 出现了,它可以说是 traceroute 的超集,除了 traceroute 的功能,还能实现丰富的探测报告。尤其是它对每一跳的丢包率的百分比,是用来定位路径中节点问题的重要指标。所以,当你在遇到“连接状况时好时坏的问题”的时候,单纯用一次性的 traceroute 恐怕难以看清楚,那就可以用 mtr,来获取更加全面和动态的链路状态信息了。


$ mtr www.baidu.com -r -c 10
Start: 2022-01-07T04:05:02+0000
HOST: victorebpf                  Loss%   Snt   Last   Avg  Best  Wrst StDev
 1.|-- _gateway                   0.0%    10    0.3   0.4   0.2   1.2   0.3
 2.|-- 192.168.1.1                0.0%    10    1.6   1.8   1.4   3.2   0.5
 3.|-- 100.65.0.1                 0.0%    10    3.8   7.0   3.8  10.3   2.0
 4.|-- 61.152.54.125              0.0%    10    4.0   4.3   3.6   5.1   0.5
 5.|-- 61.152.25.110             30.0%    10    5.0   6.8   4.4  18.9   5.4
 6.|-- 202.97.101.30             20.0%    10    7.8   6.6   5.4   7.8   0.8
 7.|-- 58.213.95.110             80.0%    10   10.0   9.8   9.6  10.0   0.3
 8.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
 9.|-- 58.213.96.74               0.0%    10   10.5  12.7   9.9  24.7   4.9
10.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
11.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
12.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
13.|-- 180.101.49.12              0.0%    10    9.4   9.1   8.3   9.7   0.5

查看路由

route -n


# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.2.2        0.0.0.0         UG    100    0        0 enp0s3
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 enp0s3
10.0.2.2        0.0.0.0         255.255.255.255 UH    100    0        0 enp0s3
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0

netstat -r


$ netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         _gateway        0.0.0.0         UG        0 0          0 enp0s3
10.0.2.0        0.0.0.0         255.255.255.0   U         0 0          0 enp0s3
_gateway        0.0.0.0         255.255.255.255 UH        0 0          0 enp0s3
172.17.0.0      0.0.0.0         255.255.0.0     U         0 0          0 docker0

ip route


$ ip route
default via 10.0.2.2 dev enp0s3 proto dhcp src 10.0.2.15 metric 100
10.0.2.0/24 dev enp0s3 proto kernel scope link src 10.0.2.15
10.0.2.2 dev enp0s3 proto dhcp scope link src 10.0.2.15 metric 100
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown

数据链路层和物理层

ethtool


# ethtool -S enp0s3
NIC statistics:
     rx_packets: 45897
     tx_packets: 9457
     rx_bytes: 59125524
     tx_bytes: 834625
     rx_broadcast: 0
     tx_broadcast: 17
     rx_multicast: 0
     tx_multicast: 59
     rx_errors: 0
     tx_errors: 0
     tx_dropped: 0

它的原理,是网卡驱动会到内核中注册 ethtool 回调函数,然后我们用 ethtool 命令就可以查看这些信息了。由于信息是由网卡驱动提供的,所以十分“接地气”。