Browse Source

feat(alerts): quote the reject reason in the auto-rearm alert

The alert said the agent had not pushed again when the pushes were being
rejected. It now names the reason and what to check: clock, agent build or
token.

Part of #2211
gnacho 6 ngày trước cách đây
mục cha
commit
ae1defa2ae

+ 40 - 1
server-go/internal/rearmer/rearmer.go

@@ -706,17 +706,56 @@ func (s *Supervisor) emitFail(slug string) {
 		s.failIDs[slug] = id
 	}
 	s.mu.Unlock()
+	desc := fmt.Sprintf("The supervisor restarted netpulse-agent on %s but the agent has not pushed again; next attempt in %d s", slug, int(s.cooldown.Seconds()))
+	// #2211: si el servidor está rechazando los pushes, el motivo manda sobre
+	// "reiniciado y no volvió". Un reloj desfasado se ve como agente muerto y
+	// ningún rearme lo arregla, así que la alerta tiene que decirlo.
+	if reason := s.rejectReason(slug); reason != "" {
+		desc = fmt.Sprintf("The supervisor restarted netpulse-agent on %s but its pushes are being rejected (401 %s). %s Next attempt in %d s",
+			slug, reason, rejectHint(reason), int(s.cooldown.Seconds()))
+	}
 	s.engine.EmitOrUpdate(alerts.AlertEvent{
 		ID:       id,
 		Category: alerts.CatSystem, Urgent: false, Severity: "warn",
 		Title:       fmt.Sprintf("Auto-rearm without recovery on %s", slug),
-		Description: fmt.Sprintf("The supervisor restarted netpulse-agent on %s but the agent has not pushed again; next attempt in %d s", slug, int(s.cooldown.Seconds())),
+		Description: desc,
 		Type:        alerts.TypeAutoRearmFailed,
 		Vars:        map[string]string{"router": slug, "cooldown": fmt.Sprintf("%d", int(s.cooldown.Seconds()))},
 		Time:        "just now", RouterID: slug,
 	})
 }
 
+// rejectReasonPrefix: duplicado de httpapi.agentRejectReasonKeyPrefix a
+// propósito (httpapi importa rearmer, no al revés). Valor: motivo del último
+// push rechazado con 401 (#2211).
+const rejectReasonPrefix = "agent.reject_reason."
+
+// rejectReason devuelve el motivo del último rechazo del slug, o "" si no hay.
+func (s *Supervisor) rejectReason(slug string) string {
+	if s.db == nil {
+		return ""
+	}
+	var v string
+	if err := s.db.QueryRow("SELECT value FROM kv WHERE key = ?", rejectReasonPrefix+slug).Scan(&v); err != nil {
+		return ""
+	}
+	return v
+}
+
+// rejectHint traduce el motivo del ingest a la acción que lo arregla.
+func rejectHint(reason string) string {
+	switch reason {
+	case "stale_payload":
+		return "The payload timestamp is outside the server's freshness window, so check the unit clock before anything else."
+	case "invalid_signature":
+		return "The signature does not match, which usually means the agent build is older than this server."
+	case "unauthorized":
+		return "The token does not match; the rearm has already rotated it, so the unit is not picking the new one up."
+	default:
+		return "Check the token, the agent version and the unit clock."
+	}
+}
+
 // closeFailID: borra el ID de fallo abierto del slug (incidente resuelto).
 func (s *Supervisor) closeFailID(slug string) {
 	s.mu.Lock()

+ 29 - 0
server-go/internal/rearmer/rearmer_test.go

@@ -587,3 +587,32 @@ func TestRearmNetgripFreshRechazado(t *testing.T) {
 		t.Fatalf("netgrip fresh no debe ejecutar SSH, tuve %d", env.ssh.count())
 	}
 }
+
+// TestSupervisorAlertaConMotivoDelRechazo (#2211): si el servidor está
+// rechazando los pushes, la alerta dice por qué. Un stale_payload es un reloj
+// desfasado, que ningún rearme arregla, así que la descripción tiene que
+// mandar a mirar el reloj y no a "reiniciado y no volvió".
+func TestSupervisorAlertaConMotivoDelRechazo(t *testing.T) {
+	env := makeRearmEnv(t, "patio")
+	env.pushViejo("patio")
+	env.expira(time.Minute)
+	env.arm.SetPollWait(50 * time.Millisecond)
+	if _, err := env.d.Exec("INSERT INTO kv (key, value) VALUES (?, ?)",
+		"agent.reject_reason.patio", "stale_payload"); err != nil {
+		t.Fatalf("kv motivo: %v", err)
+	}
+
+	sup := rearmer.NewSupervisor(env.arm, env.reg, env.d.DB, env.engine, time.Hour, time.Hour)
+	sup.CheckOnce()
+
+	for _, ev := range env.engine.events() {
+		if ev.Title != "Auto-rearm without recovery on patio" {
+			continue
+		}
+		if !strings.Contains(ev.Description, "stale_payload") || !strings.Contains(ev.Description, "clock") {
+			t.Fatalf("la alerta debe citar el motivo y el reloj, fue: %q", ev.Description)
+		}
+		return
+	}
+	t.Fatalf("no se emitió la alerta de fallo, eventos: %d", env.engine.count())
+}