diff --git a/hosts/idols-aquamarine/monitoring/alert.nix b/hosts/idols-aquamarine/monitoring/alert.nix index 8983e17e..d5ff69ff 100644 --- a/hosts/idols-aquamarine/monitoring/alert.nix +++ b/hosts/idols-aquamarine/monitoring/alert.nix @@ -126,6 +126,13 @@ # Message template # Telegram limits a message to 4096 chars, so only render the first 5 alerts # in a group and keep the fields minimal (no full label dump). + # + # WARNING: never use template variables ($i, $a, ...) here. The NixOS + # alertmanager module pipes the generated config through `envsubst` + # (to inject $TELEGRAM_BOT_TOKEN etc.), which silently replaces any + # $var in this template with an empty string and breaks rendering at + # notify time. Use `{{ range .Alerts }}` + dot, and `define`/`template` + # for reuse instead. message = '' {{- if eq .Status "firing" }} 🟑 ε‘Šθ­¦θ§¦ε‘ [{{ .CommonLabels.severity | title }}] {{ .CommonLabels.alertname }} (ε…± {{ len .Alerts }} 村) @@ -137,18 +144,21 @@ β€’ Cluster: {{ with .CommonLabels.cluster }}{{ . }}{{ else }}N/A{{ end }} β€’ Env: {{ with .CommonLabels.env }}{{ . }}{{ else }}N/A{{ end }} β€’ Namespace: {{ with .CommonLabels.namespace }}{{ . }}{{ else }}N/A{{ end }} - {{- range $i, $a := .Alerts }} - {{- if lt $i 5 }} + {{- define "alert" }} ━━━━━━━━ - πŸ“Œ {{ with $a.Annotations.summary }}{{ . }}{{ else }}{{ $a.Labels.instance }}{{ end }} - {{- with $a.Labels.nodename }} + πŸ“Œ {{ with .Annotations.summary }}{{ . }}{{ else }}{{ .Labels.instance }}{{ end }} + {{- with .Labels.nodename }} 🏠 {{ . }} {{- end }} - πŸ”— Grafana Explore Β· ⏱ {{ $a.StartsAt.Format "2006-01-02 15:04:05" }} + πŸ”— Grafana Explore Β· ⏱ {{ .StartsAt.Format "2006-01-02 15:04:05" }} {{- end }} + {{- if le (len .Alerts) 5 }} + {{- range .Alerts }}{{ template "alert" . }} + {{- end }} + {{- else }} + {{- range slice .Alerts 0 5 }}{{ template "alert" . }} {{- end }} - {{- if gt (len .Alerts) 5 }} ⚠️ ε¦ζœ‰ {{ len (slice .Alerts 5) }} ζ‘εŒη»„ε‘Šθ­¦ζœͺεˆ—ε‡ΊοΌŒθ―·ζŸ₯ηœ‹ Alertmanager。 {{- end }}