Przeglądaj źródła

feat(devices): attribute SNMP switch-port traffic to FDB clients (#1554)

A managed switch has no per-MAC accounting (nlbwmon is an OpenWrt/agent
feature) and switch-local flows (camera -> NVR on the same switch) never cross
the gateway, so clients learned only in a switch's FDB report ~0 traffic.

Add switchporttraffic.go: raise each online client attributed to an SNMP-enabled
router to its FDB port's rx+tx rate (Mbps), split evenly across the online
clients sharing that port, only ever raising (agent-reported and higher figures
untouched). buildDevices keeps a single wiring call; live.go stays wiring-only
per CONTRIBUTING.

Co-authored-by: Bann-DEV <bann.dev@gmail.com>
Bann-DEV 1 miesiąc temu
rodzic
commit
aa89bdb96f

+ 3 - 0
server-go/internal/adapters/live.go

@@ -3462,6 +3462,9 @@ func (l *Live) buildDevices(polled map[string]*routerPolled, wanPort string) []D
 		}
 		devices = append(devices, d)
 	}
+	// Give clients learned only in an SNMP switch's FDB a traffic figure from
+	// that port's counters (see switchporttraffic.go).
+	l.applySnmpSwitchPortTraffic(polled, devices)
 	// Orden estable por MAC (issue #656): buildDevices itera un mapa de Go
 	// (allMacs), cuyo orden es aleatorio entre ticks. Ese orden llega al
 	// frontend y alimenta la asignación de ranuras de los anillos de la

+ 85 - 0
server-go/internal/adapters/switchporttraffic.go

@@ -0,0 +1,85 @@
+// switchporttraffic.go — per-client traffic for wired devices learned only in
+// a managed SNMP switch's FDB (issue #1543).
+//
+// A managed switch has no per-MAC accounting (nlbwmon is an OpenWrt/agent
+// feature), and switch-local flows (e.g. a camera streaming to an NVR on the
+// same switch) never cross the gateway, so the gateway's per-MAC rate reports
+// only the tiny gateway-bound slice and the client looks idle. The switch's own
+// port byte counters (ifTable octets, surfaced as EthPort.RxBps/TxBps) DO see
+// the whole flow, so we attribute a port's rx+tx rate to the clients the
+// switch's FDB learns on it, split evenly so a trunk carrying several MACs is
+// not duplicated onto each. Only SNMP-enabled routers are consulted and a
+// client's rate is only ever raised, so agent-reported clients and any higher
+// existing figure are left untouched.
+package adapters
+
+import "strings"
+
+// applySnmpSwitchPortTraffic raises each online client attributed to an
+// SNMP-enabled router to its FDB port's rate (Mbps), split across the online
+// clients sharing that port. It runs at the end of buildDevices, before
+// inferTopology fills Device.Port, so the client's port is resolved from the
+// router's own FDB (mac -> port, uppercased) rather than Device.Port.
+func (l *Live) applySnmpSwitchPortTraffic(polled map[string]*routerPolled, devices []Device) {
+	type portKey struct{ router, port string }
+
+	// Mbps per (router, port name). Indexed by every alias a port can carry so
+	// the FDB's name (its DisplayName = Label/Iface) resolves.
+	rate := map[portKey]float64{}
+	for rID, p := range polled {
+		if p == nil || !p.cfg.SnmpEnabled {
+			continue
+		}
+		for _, ep := range p.ports {
+			bps := ep.RxBps + ep.TxBps
+			if bps <= 0 {
+				continue
+			}
+			mp := bps / 1e6
+			for _, name := range []string{ep.ID, ep.Label, ep.Iface} {
+				if name != "" {
+					rate[portKey{rID, name}] = mp
+				}
+			}
+		}
+	}
+	if len(rate) == 0 {
+		return
+	}
+
+	// A client's port comes from the switch's own FDB (mac -> port), the same
+	// uppercased-MAC key the topology step uses.
+	resolve := func(d *Device) (portKey, bool) {
+		if !d.Online || d.RouterID == "" {
+			return portKey{}, false
+		}
+		p := polled[d.RouterID]
+		if p == nil || !p.cfg.SnmpEnabled || len(p.fdb) == 0 {
+			return portKey{}, false
+		}
+		port, ok := p.fdb[strings.ToUpper(d.MAC)]
+		if !ok {
+			return portKey{}, false
+		}
+		k := portKey{d.RouterID, port}
+		return k, rate[k] > 0
+	}
+
+	keys := make([]portKey, len(devices))
+	matched := make([]bool, len(devices))
+	share := map[portKey]int{}
+	for i := range devices {
+		if k, ok := resolve(&devices[i]); ok {
+			keys[i], matched[i] = k, true
+			share[k]++
+		}
+	}
+	for i := range devices {
+		if !matched[i] || share[keys[i]] <= 0 {
+			continue
+		}
+		if mp := rate[keys[i]] / float64(share[keys[i]]); mp > devices[i].TrafficMbps {
+			devices[i].TrafficMbps = mp
+		}
+	}
+}

+ 73 - 0
server-go/internal/adapters/switchporttraffic_test.go

@@ -0,0 +1,73 @@
+package adapters
+
+import (
+	"math"
+	"testing"
+)
+
+// applySnmpSwitchPortTraffic raises a switch client's traffic to its FDB port's
+// byte-counter rate (switch-local flows the gateway's per-MAC nlbwmon misses),
+// splits a port across its clients, and never lowers an existing higher figure.
+func TestApplySnmpSwitchPortTraffic(t *testing.T) {
+	polled := map[string]*routerPolled{
+		"sw": {
+			cfg: RouterConfig{ID: "sw", SnmpEnabled: true},
+			ports: []EthPort{
+				{ID: "snmp-3", Label: "1/g3", RxBps: 14e6, TxBps: 0.14e6},
+				{ID: "snmp-4", Label: "1/g4", RxBps: 5e6, TxBps: 0},
+			},
+			fdb: map[string]string{
+				"AA:01": "1/g3",
+				"AA:02": "1/g3",
+				"AA:03": "1/g4",
+				"AA:04": "1/g3",
+			},
+		},
+	}
+	devices := []Device{
+		// Two cameras sharing port 1/g3 (14.14 Mbps total → 7.07 each). Lowercase
+		// MACs also prove the FDB uppercased-key lookup.
+		{MAC: "aa:01", Online: true, RouterID: "sw", TrafficMbps: 0.0008},
+		{MAC: "aa:02", Online: true, RouterID: "sw", TrafficMbps: 0},
+		// One camera on 1/g4 (5 Mbps); its existing 20 must not be lowered.
+		{MAC: "aa:03", Online: true, RouterID: "sw", TrafficMbps: 20},
+		// Offline client is ignored (not counted toward the port split).
+		{MAC: "aa:04", Online: false, RouterID: "sw", TrafficMbps: 0},
+	}
+
+	l := &Live{}
+	l.applySnmpSwitchPortTraffic(polled, devices)
+
+	if d := devices[0]; math.Abs(d.TrafficMbps-7.07) > 0.01 {
+		t.Fatalf("AA:01 = %.4f, want ~7.07 (port 14.14 split over 2 online)", d.TrafficMbps)
+	}
+	if d := devices[1]; math.Abs(d.TrafficMbps-7.07) > 0.01 {
+		t.Fatalf("AA:02 = %.4f, want ~7.07", d.TrafficMbps)
+	}
+	if d := devices[2]; d.TrafficMbps != 20 {
+		t.Fatalf("AA:03 = %.4f, want 20 (never lowered from a higher existing figure)", d.TrafficMbps)
+	}
+	if d := devices[3]; d.TrafficMbps != 0 {
+		t.Fatalf("offline AA:04 = %.4f, want 0", d.TrafficMbps)
+	}
+}
+
+// Non-SNMP routers are untouched: a client on an OpenWrt/agent router keeps
+// whatever per-MAC rate it had (no switch port to attribute).
+func TestApplySnmpSwitchPortTrafficSkipsAgentRouters(t *testing.T) {
+	polled := map[string]*routerPolled{
+		"gw": {
+			cfg:   RouterConfig{ID: "gw"}, // SnmpEnabled false
+			ports: []EthPort{{ID: "lan1", Label: "LAN 1", RxBps: 100e6}},
+			fdb:   map[string]string{"BB:01": "lan1"},
+		},
+	}
+	devices := []Device{{MAC: "BB:01", Online: true, RouterID: "gw", TrafficMbps: 1}}
+
+	l := &Live{}
+	l.applySnmpSwitchPortTraffic(polled, devices)
+
+	if devices[0].TrafficMbps != 1 {
+		t.Fatalf("agent-router client changed to %.4f, want 1 (helper is SNMP-only)", devices[0].TrafficMbps)
+	}
+}