SuperAPI AI 工具集

network-interface-health

部署与运维

查看项目

更新于 10-15 08:38

技能介绍

用途

当网络症状可能由物理链路、交换机端口、线缆、光模块、双工设置或拥塞接口引起时,使用本技能。具体适用场景包括:

  • 某主机或 VLAN 出现丢包、延迟尖峰或间歇性可达性问题。
  • 交换机或路由器接口出现 CRC、runts、giants、drops、resets 或 flaps。
  • 在更换硬件之前,需要对比链路两端的情况。
  • 变更窗口需要接口计数器的前后对比证据。
  • 监控报告 ifInErrors、ifOutErrors 或 ifOutDiscards 持续上升。

主要能力

本技能围绕接口计数器展开诊断。计数器是证据,但趋势比绝对值更重要:先采集基线,等待一个测量间隔,再次采集,然后比较增量。

计数器参考:

  • CRC:接收帧校验和失败,常见原因为线缆损坏、光纤脏污、光模块故障、双工不匹配。
  • input errors:接收侧错误汇总,下结论前需检查子计数器。
  • runts:小于以太网最小尺寸的帧,常见于双工不匹配、冲突域、网卡故障。
  • giants:大于预期 MTU 的帧,常见于 MTU 不匹配或巨帧边界问题。
  • input drops:设备无法接收入站数据包,常见于突发、超订、CPU 路径、队列压力。
  • output drops:出站队列丢弃数据包,常见于拥塞、QoS 策略、上行链路带宽不足。
  • resets:接口硬件重置,常见于抖动、keepalive、驱动、光模块、电源问题。
  • collisions:以太网冲突计数,常见于半双工或协商不匹配。

使用方式

诊断流程按问题类型分为三条路径。

CRC 或 input errors:确认计数器在增长而非仅为历史值;检查链路两端,接收侧错误通常指向信号到达的那一侧;更换跳线或清洁/更换光纤和光模块;确认两端速率/双工设置匹配;检查同一时间戳附近的日志是否有抖动事件。

Drops:区分 input drops 与 output drops;对比接口速率与容量;检查 QoS 策略、队列计数器以及链路是否为超订上行;队列调优是次要手段,首先证明链路是否拥塞。

双工与速率:现代以太网链路在两端都支持时优先使用自动协商;若一端必须固定,则两端都显式配置并记录原因;切勿一端固定速率/双工而另一端自动协商。

常用命令:

show interfaces <interface>
show interfaces <interface> status
show logging | include <interface>|changed state|line protocol

Linux 主机上:

ip -s link show <interface>
ethtool <interface>
ethtool -S <interface>

检查双工与速率:

show interfaces <interface> | include duplex|speed

安全解析示例

从每个接口块的头部切到下一个头部,不要使用任意字符窗口;大型接口块可能导致计数器遗漏或归属错误端口。

import re
from typing import Any

HEADER_RE = re.compile(
    r"^(?P<name>\S+) is (?P<status>(?:administratively )?down|up), "
    r"line protocol is (?P<protocol>up|down)",
    re.I | re.M,
)
ERROR_RE = re.compile(r"(?P<input>\d+) input errors, (?P<crc>\d+) CRC", re.I)
DROP_RE = re.compile(r"(?P<output>\d+) output errors", re.I)
DUPLEX_RE = re.compile(r"(?P<duplex>Full|Half|Auto)-duplex,\s+(?P<speed>[^,]+)", re.I)

def parse_show_interfaces(raw: str) -> list[dict[str, Any]]:
    headers = list(HEADER_RE.finditer(raw))
    interfaces = []
    for index, header in enumerate(headers):
        end = headers[index + 1].start() if index + 1 < len(headers) else len(raw)
        block = raw[header.start():end]
        errors = ERROR_RE.search(block)
        drops = DROP_RE.search(block)
        duplex = DUPLEX_RE.search(block)
        interfaces.append({
            "name": header.group("name"),
            "status": header.group("status"),
            "protocol": header.group("protocol"),
            "duplex": duplex.group("duplex") if duplex else "unknown",
            "speed": duplex.group("speed").strip() if duplex else "unknown",
            "input_errors": int(errors.group("input")) if errors else 0,
            "crc_errors": int(errors.group("crc")) if errors else 0,
            "output_errors": int(drops.group("output")) if drops else 0,
        })
    return interfaces

适用场景示例

交换机端口出现 CRC:在本地端口采集计数器;在相连的远端端口采集计数器;在更改路由或防火墙规则之前先更换线缆或光模块;仅在记录基线后才清除计数器;固定间隔后复查。

互联网慢但局域网正常:检查 WAN 接口的 drops/errors;检查 LAN 上行利用率和 output drops;若 WAN 链路干净但吞吐仍低,检查网关 CPU;在归咎上游服务前先对比有线和无线测试。

限制与反模式

  • 在保存基线之前清除计数器。
  • 只看链路一端。
  • 在没有时间窗口的情况下假设所有历史 CRC 都是活跃问题。
  • 一端自动协商而另一端固定速率/双工。
  • 在检查拥塞之前把 output drops 当作线缆问题处理。

相关资源:Agent: network-troubleshooter;Skill: network-config-validation;Skill: homelab-network-setup。