Kaynağa Gözat

fix(adapters): read agent-only units offline once the pusher is confirmed down

An agent-only unit whose pusher dies kept reading online with frozen vitals while the probe table and the agent-down alert already showed it down. The cached payload is still served so the card keeps its last readings, but the poll is flagged stale past the same down-confirm window the alert uses, and buildRouter then reads offline with health 0 and status_reason agent_stale.

Fresh and recovered units are unaffected. Test covers fresh, stale and recovery.

Co-authored-by: Bann-DEV <hef.fangyuan@gmail.com>
Bann-DEV 2 hafta önce
ebeveyn
işleme
13bd654da3

+ 2 - 1
app/public/locales/en/translation.json

@@ -1413,7 +1413,8 @@
       "pusher_api_not_found": "The device does not expose the expected API endpoint at this address: check the host, the port and the scheme.",
       "pusher_conn_refused": "Connection refused: the device is not answering on the API port. Check the service, the port and the firewall.",
       "pusher_stopped": "The integration pusher is stopped: start it from Settings > Integrations.",
-      "pusher_error": "The integration pusher logged an error; check its details in Settings > Integrations."
+      "pusher_error": "The integration pusher logged an error; check its details in Settings > Integrations.",
+      "agent_stale": "No fresh data from the agent: the unit is shown offline with its last readings. Check the agent service or the device itself."
     }
   },
   "reports": {

+ 2 - 1
app/public/locales/es/translation.json

@@ -1413,7 +1413,8 @@
       "pusher_api_not_found": "El equipo no expone el endpoint esperado de la API en esta dirección: revisa el host, el puerto y el esquema.",
       "pusher_conn_refused": "Conexión rechazada: el equipo no responde en el puerto de la API. Revisa el servicio, el puerto y el cortafuegos.",
       "pusher_stopped": "El pusher de la integración está parado: arráncalo desde Ajustes > Integraciones.",
-      "pusher_error": "El pusher de la integración registró un error; revisa su detalle en Ajustes > Integraciones."
+      "pusher_error": "El pusher de la integración registró un error; revisa su detalle en Ajustes > Integraciones.",
+      "agent_stale": "Sin datos recientes del agent: la unidad se muestra offline con sus últimas lecturas. Revisa el servicio del agent o el dispositivo."
     }
   },
   "reports": {

+ 8 - 1
server-go/internal/adapters/agent.go

@@ -628,7 +628,14 @@ func (l *Live) pollRouterAgent(cfg RouterConfig) (bool, *routerPolled) {
 		}
 		if cfg.AgentOnly {
 			if p != nil {
-				return true, l.polledFromAgent(cfg, p)
+				rp := l.polledFromAgent(cfg, p)
+				// #850 keeps serving cached data so the card never goes blank,
+				// but a pusher dead past the down-confirm window is not
+				// "online": flag it so buildRouter reads offline while keeping
+				// the last readings. Same window as the agent-down alert, so
+				// the status does not flap on a momentary blip.
+				rp.agentStale = reg.StaleFor(slug, confirm)
+				return true, rp
 			}
 		}
 	}

+ 34 - 0
server-go/internal/adapters/agent_match_test.go

@@ -277,3 +277,37 @@ func TestLiveAgentDownAgentOnlyIsUrgentRouter(t *testing.T) {
 		t.Fatalf("alerta recuperación agent-only: %+v", ok)
 	}
 }
+
+// The cached payload keeps the card populated (#850), but a pusher dead past
+// the down-confirm window must read offline, agreeing with the probe table
+// instead of claiming "online" with frozen vitals.
+func TestLiveAgentOnlyStaleCardReadsOffline(t *testing.T) {
+	reg := NewAgentRegistry(50 * time.Millisecond)
+	l := newLiveAgentTest(t, reg)
+	cfg := RouterConfig{ID: "patio", Name: "Patio", Host: "127.0.0.1", AgentOnly: true}
+	reg.Ingest(testPayload())
+	p, err := l.pollRouter(t.Context(), cfg)
+	if err != nil || p == nil {
+		t.Fatalf("fresh poll: p=%v err=%v", p, err)
+	}
+	if r := l.buildRouter(p, nil); r.Status == "offline" {
+		t.Fatalf("fresh agent-only must not read offline: %q", r.Status)
+	}
+	time.Sleep(60 * time.Millisecond)
+	p, err = l.pollRouter(t.Context(), cfg)
+	if err != nil || p == nil {
+		t.Fatalf("stale agent-only still serves cached payload: p=%v err=%v", p, err)
+	}
+	r := l.buildRouter(p, nil)
+	if r.Status != "offline" || r.Health != 0 {
+		t.Fatalf("stale agent-only card must read offline/health 0, got %q/%d", r.Status, r.Health)
+	}
+	if r.StatusReason != "agent_stale" {
+		t.Fatalf("stale agent-only card must carry the agent_stale reason, got %q", r.StatusReason)
+	}
+	reg.Ingest(testPayload())
+	p, _ = l.pollRouter(t.Context(), cfg)
+	if r := l.buildRouter(p, nil); r.Status == "offline" {
+		t.Fatalf("recovered agent-only must not read offline")
+	}
+}

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

@@ -230,6 +230,10 @@ type routerPolled struct {
 	// ("" = sondeo SSH/SNMP directo). "external" = pusher beacon/scraper sin
 	// sección system → sin vitals.
 	agentKind string
+	// agentStale: agent-only unit whose pusher is past the down-confirm
+	// window. The cached payload is still served (#850) but the card reads
+	// offline, not online, so it agrees with the probe table.
+	agentStale bool
 	// selfExpose (#832): el equipo se expone él mismo a Home Assistant por
 	// MQTT (NetGrip con su MQTT activado) → el publisher de flota no publica
 	// sus entidades por router.
@@ -1874,6 +1878,10 @@ func (l *Live) buildRouter(p *routerPolled, history []histPoint) Router {
 	if p.temp > thr || p.cpu > 85 {
 		status = "warn"
 	}
+	if p.agentStale {
+		status = "offline"
+		health = 0
+	}
 	sparkline := make([]float64, 0, len(history))
 	for _, h := range history {
 		sparkline = append(sparkline, h.down)
@@ -1904,6 +1912,13 @@ func (l *Live) buildRouter(p *routerPolled, history []histPoint) Router {
 	// agente en una unidad que se inventaria por SSH.
 	r.InventoryMode = p.cfg.InventoryMode
 	r.InventoryAt = p.polledAt
+	// #2086: la unidad agent-only stale lee offline con sus últimas lecturas.
+	// El motivo: las integraciones pusher reciben su código pusher_* más
+	// específico desde EnrichOverview; un agent nativo no tiene ninguno, así
+	// que lleva agent_stale para que la tarjeta no quede roja sin explicación.
+	if p.agentStale {
+		r.StatusReason = "agent_stale"
+	}
 	// #1741: la serie de 24 h de una unidad sin throughput nativo (SNMP o
 	// pusher external) sale de sus puertos. Se decide aquí, donde se conoce el
 	// agent kind, porque Router no lo expone y VitalsAvailable ya no sirve de