From 24263a7439eac607384d22f42964ca810604af5f Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 26 Apr 2026 03:39:01 +0800 Subject: [PATCH] =?UTF-8?q?Debug:=20=E5=A2=9E=E5=BC=BA=E6=8B=93=E6=89=91?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E8=B0=83=E8=AF=95=E6=97=A5=E5=BF=97=E5=92=8C?= =?UTF-8?q?MAC=E5=9C=B0=E5=9D=80=E6=A0=87=E5=87=86=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 打印所有设备的MAC地址数据库 - 详细输出每个邻居的匹配过程 - 添加normalizeMAC函数标准化MAC地址格式 - 解决MAC地址格式不一致导致匹配失败的问题 --- internal/topology/builder.go | 57 +++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/internal/topology/builder.go b/internal/topology/builder.go index ece453d..aa5c59e 100644 --- a/internal/topology/builder.go +++ b/internal/topology/builder.go @@ -2,6 +2,7 @@ package topology import ( "fmt" + "strings" "network-topology-discovery/pkg/models" ) @@ -54,24 +55,44 @@ func (b *Builder) Build() models.TopologyGraph { // 构建边(基于邻居信息) edgeMap := make(map[string]bool) // 用于去重 + + // 打印所有设备的MAC地址,用于调试 + fmt.Println("\n========== MAC Address Database ==========") + for _, d := range b.devices { + if len(d.MACAddresses) > 0 { + fmt.Printf("Device %s (%s): %d MAC addresses\n", d.Hostname, d.IP, len(d.MACAddresses)) + for i, mac := range d.MACAddresses { + if i < 5 { // 只打印前5个 + fmt.Printf(" MAC[%d]: %s\n", i, mac) + } + } + if len(d.MACAddresses) > 5 { + fmt.Printf(" ... and %d more\n", len(d.MACAddresses)-5) + } + } + } + fmt.Println("==========================================\n") + for _, device := range b.devices { fmt.Printf("Building edges for device %s: %d neighbors, %d MAC addresses\n", device.IP, len(device.Neighbors), len(device.MACAddresses)) for _, neighbor := range device.Neighbors { + fmt.Printf(" Processing neighbor on %s: RemoteMAC=%s, RemoteDevice=%s, RemoteIP=%s\n", + neighbor.LocalInterface, neighbor.RemoteMAC, neighbor.RemoteDevice, neighbor.RemoteIP) + // 尝试匹配邻居设备 targetIP := neighbor.RemoteIP matchMethod := "IP" // 如果没有IP,尝试通过设备名匹配 if targetIP == "" && neighbor.RemoteDevice != "" { - fmt.Printf(" Neighbor %s has no IP, trying hostname match: %s\n", - neighbor.LocalInterface, neighbor.RemoteDevice) + fmt.Printf(" Trying hostname match: %s\n", neighbor.RemoteDevice) for _, d := range b.devices { if d.Hostname == neighbor.RemoteDevice { targetIP = d.IP matchMethod = "hostname" - fmt.Printf(" Matched by hostname: %s -> %s\n", d.Hostname, d.IP) + fmt.Printf(" ✓ Matched by hostname: %s -> %s\n", d.Hostname, d.IP) break } } @@ -79,14 +100,18 @@ func (b *Builder) Build() models.TopologyGraph { // 如果还是没有,尝试通过MAC地址匹配(新增) if targetIP == "" && neighbor.RemoteMAC != "" { - fmt.Printf(" Neighbor %s has no IP/hostname, trying MAC match: %s\n", - neighbor.LocalInterface, neighbor.RemoteMAC) + fmt.Printf(" Trying MAC match: %s\n", neighbor.RemoteMAC) for _, d := range b.devices { for _, mac := range d.MACAddresses { - if mac == neighbor.RemoteMAC { + // 标准化MAC地址进行比较(去除分隔符,转小写) + normalizedNeighborMAC := normalizeMAC(neighbor.RemoteMAC) + normalizedDeviceMAC := normalizeMAC(mac) + + if normalizedNeighborMAC == normalizedDeviceMAC { targetIP = d.IP matchMethod = "MAC" - fmt.Printf(" Matched by MAC: %s -> %s\n", mac, d.IP) + fmt.Printf(" ✓ Matched by MAC: %s (device) == %s (neighbor) -> %s\n", + mac, neighbor.RemoteMAC, d.IP) break } } @@ -97,12 +122,11 @@ func (b *Builder) Build() models.TopologyGraph { } if targetIP == "" { - fmt.Printf(" Neighbor %s could not be matched (RemoteIP=%s, RemoteMAC=%s, RemoteDevice=%s)\n", - neighbor.LocalInterface, neighbor.RemoteIP, neighbor.RemoteMAC, neighbor.RemoteDevice) + fmt.Printf(" ✗ Could not match neighbor on %s\n", neighbor.LocalInterface) continue } - fmt.Printf(" Creating edge: %s (%s) -> %s via %s, matched by %s\n", + fmt.Printf(" ✓ Creating edge: %s (%s) -> %s via %s, matched by %s\n", device.IP, neighbor.LocalInterface, targetIP, neighbor.RemoteInterface, matchMethod) // 创建唯一的边ID @@ -131,6 +155,19 @@ func (b *Builder) Build() models.TopologyGraph { return graph } +// normalizeMAC 标准化MAC地址(去除分隔符,转小写) +func normalizeMAC(mac string) string { + // 去除所有分隔符(-、:、.) + result := "" + for _, c := range mac { + if c != '-' && c != ':' && c != '.' { + result += string(c) + } + } + // 转小写 + return strings.ToLower(result) +} + // getDeviceIcon 获取设备图标 func getDeviceIcon(deviceType models.DeviceType) string { switch deviceType {