Просмотр исходного кода

fix(adapters): treat first observation after server restart as baseline for ghost port detection (#406)

After a server restart the PortMonitor has no history. The first sample
was initializing trafficTotal to 0 and hadTraffic to false, so any
non-zero total on the first observation set hadTraffic = true. If the
counter then stayed flat, zeroStreak grew and a ghost-port alert fired
once speedHistory reached ghostMinHistory.

Initialize a fresh portState with trafficTotal set to the first observed
total and hadTraffic = false, so the alert only fires after the monitor
has genuinely seen traffic increase and then remain flat.

Closes #405

Co-authored-by: gnacho <hnacho@proton.me>
Nacho 3 месяцев назад
Родитель
Сommit
ddcbc5ffdb

+ 5 - 1
server-go/internal/adapters/portmonitor.go

@@ -70,7 +70,11 @@ func (pm *PortMonitor) Observe(routerID string, ports []EthPort, engine *alerts.
 		key := portKey{routerID, p.ID}
 		st, ok := pm.states[key]
 		if !ok {
-			st = &portState{up: p.Up}
+			// Tras un reinicio del servidor no hay historial: la primera
+			// observación es la línea de base del contador, no una muestra de
+			// que el puerto "tuvo tráfico". Si no hacemos esto, un contador
+			// congelado en un valor > 0 se interpreta como ghost port.
+			st = &portState{up: p.Up, trafficTotal: p.RxBytes + p.TxBytes}
 			pm.states[key] = st
 		}
 		pm.updateSpeedHistory(st, p)

+ 40 - 0
server-go/internal/adapters/portmonitor_test.go

@@ -372,3 +372,43 @@ func TestPortMonitorGhostDisabled(t *testing.T) {
 	}
 }
 
+// issue #405: un reinicio del servidor pierde el estado; un contador
+// congelado en valor > 0 no debe interpretarse como ghost port.
+func TestPortMonitorGhostNoAlertAfterServerRestart(t *testing.T) {
+	pm := NewPortMonitor(true)
+	engine := alerts.New(nil, nil)
+	now := time.Date(2026, 8, 30, 22, 16, 0, 0, time.UTC)
+	pm.SetClock(func() time.Time { return now })
+
+	// Tras reinicio no hay historial. El puerto reporta un total constante > 0.
+	port := EthPort{ID: "eth0", Label: "LAN3", Up: true, Speed: "1 Gbps", RxBytes: 12345, TxBytes: 6789}
+
+	for i := 0; i < 15; i++ {
+		now = now.Add(15 * time.Second)
+		pm.SetClock(func() time.Time { return now })
+		pm.Observe("gateway", []EthPort{port}, engine)
+	}
+
+	if n, _ := findAlerts(engine, "Ghost port: LAN3 went silent"); n != 0 {
+		t.Fatalf("ghost alerts after server restart = %d, want 0", n)
+	}
+
+	// A partir de aquí el puerto sí muestra tráfico creciente y luego se calla;
+	// ahora sí debe saltar la alerta porque hay historia real de warm-up.
+	for i := 0; i < ghostMinHistory; i++ {
+		port.RxBytes += 1000
+		port.TxBytes += 500
+		now = now.Add(15 * time.Second)
+		pm.SetClock(func() time.Time { return now })
+		pm.Observe("gateway", []EthPort{port}, engine)
+	}
+	for i := 0; i < ghostConsecutive; i++ {
+		now = now.Add(15 * time.Second)
+		pm.SetClock(func() time.Time { return now })
+		pm.Observe("gateway", []EthPort{port}, engine)
+	}
+	if n, _ := findAlerts(engine, "Ghost port: LAN3 went silent"); n != 1 {
+		t.Fatalf("ghost alert after real warm-up = %d, want 1", n)
+	}
+}
+