Use ctx.States() for alert cards in HTML report

Build alert cards from rule states when available; fall back to
raw-data analysis (resolve failures, CNAME violations, unreachable
targets) when states are absent.
This commit is contained in:
nemunaire 2026-05-18 10:57:48 +08:00
commit 0a41c706aa

View file

@ -256,6 +256,32 @@ func (p *srvProvider) GetHTMLReport(ctx sdk.ReportContext) (string, error) {
rd.Records = append(rd.Records, rec) rd.Records = append(rd.Records, rec)
} }
// Build alerts from rule states when available; fall back to raw-data
// analysis when the host hasn't threaded rule output through yet.
states := ctx.States()
if len(states) > 0 {
for _, st := range states {
sev := ""
switch st.Status {
case sdk.StatusCrit, sdk.StatusError:
sev = "crit"
case sdk.StatusWarn:
sev = "warn"
case sdk.StatusInfo:
sev = "info"
default:
continue
}
alert := reportAlert{
Severity: sev,
Title: st.Message,
}
if fix, ok := st.Meta["fix"].(string); ok && fix != "" {
alert.Body = template.HTML(template.HTMLEscapeString(fix))
}
rd.Alerts = append(rd.Alerts, alert)
}
} else {
if len(resolveFails) > 0 { if len(resolveFails) > 0 {
rd.Alerts = append(rd.Alerts, reportAlert{ rd.Alerts = append(rd.Alerts, reportAlert{
Severity: "crit", Severity: "crit",
@ -303,6 +329,7 @@ func (p *srvProvider) GetHTMLReport(ctx sdk.ReportContext) (string, error) {
"If you expected clients to reach this service, replace the null target with a real hostname."), "If you expected clients to reach this service, replace the null target with a real hostname."),
}) })
} }
}
var buf strings.Builder var buf strings.Builder
if err := htmlTpl.Execute(&buf, rd); err != nil { if err := htmlTpl.Execute(&buf, rd); err != nil {