Skip to content

Commit

Permalink
The firing and resolved status are supported in the title
Browse files Browse the repository at this point in the history
  • Loading branch information
Gentleelephant committed May 22, 2024
1 parent 5427718 commit 1cb5646
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/samples/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ data:
{{ define "nm.default.message.cn" }}{{ if ne (len .Status) 0 }}[{{ .Status | translate }}] {{ end }}{{ .MessageCN }}{{ end }}
{{ define "nm.default.subject" }}{{ if eq (len .Alerts) 1 }}{{ range .Alerts }}{{ template "nm.default.message" . }}{{ end }}{{ else }}{{ .Alerts | len }} {{ if ne (len .Status) 0 }}{{ .Status }} {{ end }}alerts{{ if gt (len .GroupLabels.SortedPairs) 0 }} for {{ range .GroupLabels.SortedPairs }}{{ .Name | translate }}={{ .Value }} {{ end }}{{ end }}{{ end }}{{ end }}
{{ define "nm.subject" -}}{{- $rawStatus := .Status -}}{{- $severity := index .CommonLabels "severity" -}}{{- $status := "" -}}{{- if eq $rawStatus "firing" -}}{{- $status = "FIRING" -}}{{- else if eq $rawStatus "resolved" -}}{{- $status = "RESOLVED" -}}{{- else -}}{{- $status = "UNKNOWN" -}}{{- end -}}{{- $numAlerts := len .Alerts -}}{{- if eq $numAlerts 0 -}}Show nothing{{- else if eq $numAlerts 1 -}}{{- range .Alerts -}}{{- $alertType := .Labels.alerttype -}}{{- $alertName := .Labels.alertname -}}{{- $cluster := .Labels.cluster -}}{{- $node := .Labels.node -}}{{- $pod := .Labels.pod -}}{{- $namespace := .Labels.namespace -}}{{- $output := printf "[%s]" $status -}}{{- if $severity -}}{{- $output = printf "%s | severity=%s" $output $severity -}}{{- end -}}{{- if $alertType -}}{{- $output = printf "%s | alerttype=%s" $output $alertType -}}{{- end -}}{{- if $alertName -}}{{- $output = printf "%s | alertname=%s" $output $alertName -}}{{- end -}}{{- if $cluster -}}{{- $output = printf "%s | cluster=%s" $output $cluster -}}{{- end -}}{{- if $node -}}{{- $output = printf "%s | node=%s" $output $node -}}{{- end -}}{{- if $namespace -}}{{- $output = printf "%s | namespace=%s" $output $namespace -}}{{- end -}}{{- if $pod -}}{{- $output = printf "%s | pod=%s" $output $pod -}}{{- end -}}{{- $output -}}{{- end -}}{{- else -}}{{- $alertType := index .GroupLabels "alerttype" -}}{{- $alertName := index .GroupLabels "alertname" -}}{{- $cluster := index .GroupLabels "cluster" -}}{{- $namespace := index .GroupLabels "namespace" -}}{{- $node := index .GroupLabels "node" -}}{{- $output := printf "[%s]" $status -}}{{- if $severity -}}{{- $output = printf "%s | severity=%s" $output $severity -}}{{- end -}}{{- if $alertType -}}{{- $output = printf "%s | alerttype=%s" $output $alertType -}}{{- end -}}{{- if $alertName -}}{{- $output = printf "%s | alertname=%s" $output $alertName -}}{{- end -}}{{- if $cluster -}}{{- $output = printf "%s | cluster=%s" $output $cluster -}}{{- end -}}{{- if $namespace -}}{{- $output = printf "%s | namespace=%s" $output $namespace -}}{{- end -}}{{- if $node -}}{{- $output = printf "%s | node=%s" $output $node -}}{{- end -}}{{- $output -}}{{- end -}}{{- end -}}
{{ define "nm.subject" -}}{{- $rawStatus := .Status -}}{{- $severity := index .CommonLabels "severity" -}}{{- $status := "" -}}{{- $numAlerts := len .Alerts -}}{{- if eq $numAlerts 0 -}}Show nothing{{- else if eq $numAlerts 1 -}}{{- range .Alerts -}}{{- $alertType := .Labels.alerttype -}}{{- $alertName := .Labels.alertname -}}{{- $cluster := .Labels.cluster -}}{{- $node := .Labels.node -}}{{- $pod := .Labels.pod -}}{{- $namespace := .Labels.namespace -}}{{- if eq $rawStatus "firing" -}}{{- $status = "[FIRING]" -}}{{- else if eq $rawStatus "resolved" -}}{{- $status = "[RESOLVED]" -}}{{- else -}}{{- $status = "[UNKNOWN]" -}}{{- end -}}{{- $output := $status -}}{{- if $severity -}}{{- $output = printf "%s | severity=%s" $output $severity -}}{{- end -}}{{- if $alertType -}}{{- $output = printf "%s | alerttype=%s" $output $alertType -}}{{- end -}}{{- if $alertName -}}{{- $output = printf "%s | alertname=%s" $output $alertName -}}{{- end -}}{{- if $cluster -}}{{- $output = printf "%s | cluster=%s" $output $cluster -}}{{- end -}}{{- if $node -}}{{- $output = printf "%s | node=%s" $output $node -}}{{- end -}}{{- if $namespace -}}{{- $output = printf "%s | namespace=%s" $output $namespace -}}{{- end -}}{{- if $pod -}}{{- $output = printf "%s | pod=%s" $output $pod -}}{{- end -}}{{- $output -}}{{- end -}}{{- else -}}{{- $hasFiring := false -}}{{- $hasResolved := false -}}{{- range .Alerts -}}{{- if eq .Status "firing" -}}{{- $hasFiring = true -}}{{- end -}}{{- if eq .Status "resolved" -}}{{- $hasResolved = true -}}{{- end -}}{{- end -}}{{- if and $hasFiring $hasResolved -}}{{- $status = "[FIRING|RESOLVED]" -}}{{- else if $hasFiring -}}{{- $status = "[FIRING]" -}}{{- else if $hasResolved -}}{{- $status = "[RESOLVED]" -}}{{- else -}}{{- $status = "[UNKNOWN]" -}}{{- end -}}{{- $alertType := index .GroupLabels "alerttype" -}}{{- $alertName := index .GroupLabels "alertname" -}}{{- $cluster := index .GroupLabels "cluster" -}}{{- $namespace := index .GroupLabels "namespace" -}}{{- $node := index .GroupLabels "node" -}}{{- $output := $status -}}{{- if $severity -}}{{- $output = printf "%s | severity=%s" $output $severity -}}{{- end -}}{{- if $alertType -}}{{- $output = printf "%s | alerttype=%s" $output $alertType -}}{{- end -}}{{- if $alertName -}}{{- $output = printf "%s | alertname=%s" $output $alertName -}}{{- end -}}{{- if $cluster -}}{{- $output = printf "%s | cluster=%s" $output $cluster -}}{{- end -}}{{- if $namespace -}}{{- $output = printf "%s | namespace=%s" $output $namespace -}}{{- end -}}{{- if $node -}}{{- $output = printf "%s | node=%s" $output $node -}}{{- end -}}{{- $output -}}{{- end -}}{{- end -}}
{{ define "nm.default.text" }}{{ range .Alerts }}{{ template "nm.default.message" . }}
{{ range .Labels.SortedPairs }} {{ .Name | translate }}: {{ .Value }}
{{ end }}{{ range .Annotations.SortedPairs.Filter "runbook_url" "message" "summary" "summary_cn" }} {{ .Name | translate }}: {{ .Value }}
Expand Down
2 changes: 1 addition & 1 deletion helm/templates/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ data:
{{ "{{ define \"nm.default.message.cn\" }}{{ if ne (len .Status) 0 }}[{{ .Status | translate }}] {{ end }}{{ .MessageCN }}{{ end }}" }}
{{ "{{ define \"nm.default.subject\" }}{{ if eq (len .Alerts) 1 }}{{ range .Alerts }}{{ template \"nm.default.message\" . }}{{ end }}{{ else }}{{ .Alerts | len }} {{ if ne (len .Status) 0 }}{{ .Status }} {{ end }}alerts{{ if gt (len .GroupLabels.SortedPairs) 0 }} for {{ range .GroupLabels.SortedPairs }}{{ .Name | translate }}={{ .Value }} {{ end }}{{ end }}{{ end }}{{ end }}" }}
{{ "{{ define \"nm.subject\" -}}{{- $rawStatus := .Status -}}{{- $severity := index .CommonLabels \"severity\" -}}{{- $status := \"\" -}}{{- if eq $rawStatus \"firing\" -}}{{- $status = \"FIRING\" -}}{{- else if eq $rawStatus \"resolved\" -}}{{- $status = \"RESOLVED\" -}}{{- else -}}{{- $status = \"UNKNOWN\" -}}{{- end -}}{{- $numAlerts := len .Alerts -}}{{- if eq $numAlerts 0 -}}Show nothing{{- else if eq $numAlerts 1 -}}{{- range .Alerts -}}{{- $alertType := .Labels.alerttype -}}{{- $alertName := .Labels.alertname -}}{{- $cluster := .Labels.cluster -}}{{- $node := .Labels.node -}}{{- $pod := .Labels.pod -}}{{- $namespace := .Labels.namespace -}}{{- $output := printf \"[%s]\" $status -}}{{- if $severity -}}{{- $output = printf \"%s | severity=%s\" $output $severity -}}{{- end -}}{{- if $alertType -}}{{- $output = printf \"%s | alerttype=%s\" $output $alertType -}}{{- end -}}{{- if $alertName -}}{{- $output = printf \"%s | alertname=%s\" $output $alertName -}}{{- end -}}{{- if $cluster -}}{{- $output = printf \"%s | cluster=%s\" $output $cluster -}}{{- end -}}{{- if $node -}}{{- $output = printf \"%s | node=%s\" $output $node -}}{{- end -}}{{- if $namespace -}}{{- $output = printf \"%s | namespace=%s\" $output $namespace -}}{{- end -}}{{- if $pod -}}{{- $output = printf \"%s | pod=%s\" $output $pod -}}{{- end -}}{{- $output -}}{{- end -}}{{- else -}}{{- $alertType := index .GroupLabels \"alerttype\" -}}{{- $alertName := index .GroupLabels \"alertname\" -}}{{- $cluster := index .GroupLabels \"cluster\" -}}{{- $namespace := index .GroupLabels \"namespace\" -}}{{- $node := index .GroupLabels \"node\" -}}{{- $output := printf \"[%s]\" $status -}}{{- if $severity -}}{{- $output = printf \"%s | severity=%s\" $output $severity -}}{{- end -}}{{- if $alertType -}}{{- $output = printf \"%s | alerttype=%s\" $output $alertType -}}{{- end -}}{{- if $alertName -}}{{- $output = printf \"%s | alertname=%s\" $output $alertName -}}{{- end -}}{{- if $cluster -}}{{- $output = printf \"%s | cluster=%s\" $output $cluster -}}{{- end -}}{{- if $namespace -}}{{- $output = printf \"%s | namespace=%s\" $output $namespace -}}{{- end -}}{{- if $node -}}{{- $output = printf \"%s | node=%s\" $output $node -}}{{- end -}}{{- $output -}}{{- end -}}{{- end -}}" }}
{{ "{{ define \"nm.subject\" -}}{{- $rawStatus := .Status -}}{{- $severity := index .CommonLabels \"severity\" -}}{{- $status := \"\" -}}{{- $numAlerts := len .Alerts -}}{{- if eq $numAlerts 0 -}}Show nothing{{- else if eq $numAlerts 1 -}}{{- range .Alerts -}}{{- $alertType := .Labels.alerttype -}}{{- $alertName := .Labels.alertname -}}{{- $cluster := .Labels.cluster -}}{{- $node := .Labels.node -}}{{- $pod := .Labels.pod -}}{{- $namespace := .Labels.namespace -}}{{- if eq $rawStatus \"firing\" -}}{{- $status = \"[FIRING]\" -}}{{- else if eq $rawStatus \"resolved\" -}}{{- $status = \"[RESOLVED]\" -}}{{- else -}}{{- $status = \"[UNKNOWN]\" -}}{{- end -}}{{- $output := $status -}}{{- if $severity -}}{{- $output = printf \"%s | severity=%s\" $output $severity -}}{{- end -}}{{- if $alertType -}}{{- $output = printf \"%s | alerttype=%s\" $output $alertType -}}{{- end -}}{{- if $alertName -}}{{- $output = printf \"%s | alertname=%s\" $output $alertName -}}{{- end -}}{{- if $cluster -}}{{- $output = printf \"%s | cluster=%s\" $output $cluster -}}{{- end -}}{{- if $node -}}{{- $output = printf \"%s | node=%s\" $output $node -}}{{- end -}}{{- if $namespace -}}{{- $output = printf \"%s | namespace=%s\" $output $namespace -}}{{- end -}}{{- if $pod -}}{{- $output = printf \"%s | pod=%s\" $output $pod -}}{{- end -}}{{- $output -}}{{- end -}}{{- else -}}{{- $hasFiring := false -}}{{- $hasResolved := false -}}{{- range .Alerts -}}{{- if eq .Status \"firing\" -}}{{- $hasFiring = true -}}{{- end -}}{{- if eq .Status \"resolved\" -}}{{- $hasResolved = true -}}{{- end -}}{{- end -}}{{- if and $hasFiring $hasResolved -}}{{- $status = \"[FIRING|RESOLVED]\" -}}{{- else if $hasFiring -}}{{- $status = \"[FIRING]\" -}}{{- else if $hasResolved -}}{{- $status = \"[RESOLVED]\" -}}{{- else -}}{{- $status = \"[UNKNOWN]\" -}}{{- end -}}{{- $alertType := index .GroupLabels \"alerttype\" -}}{{- $alertName := index .GroupLabels \"alertname\" -}}{{- $cluster := index .GroupLabels \"cluster\" -}}{{- $namespace := index .GroupLabels \"namespace\" -}}{{- $node := index .GroupLabels \"node\" -}}{{- $output := $status -}}{{- if $severity -}}{{- $output = printf \"%s | severity=%s\" $output $severity -}}{{- end -}}{{- if $alertType -}}{{- $output = printf \"%s | alerttype=%s\" $output $alertType -}}{{- end -}}{{- if $alertName -}}{{- $output = printf \"%s | alertname=%s\" $output $alertName -}}{{- end -}}{{- if $cluster -}}{{- $output = printf \"%s | cluster=%s\" $output $cluster -}}{{- end -}}{{- if $namespace -}}{{- $output = printf \"%s | namespace=%s\" $output $namespace -}}{{- end -}}{{- if $node -}}{{- $output = printf \"%s | node=%s\" $output $node -}}{{- end -}}{{- $output -}}{{- end -}}{{- end -}}" }}
{{ "{{ define \"nm.default.text\" }}{{ range .Alerts }}{{ template \"nm.default.message\" . }}" }}
{{ "{{ range .Labels.SortedPairs }} {{ .Name | translate }}: {{ .Value }}" }}
{{ "{{ end }}{{ range .Annotations.SortedPairs.Filter \"runbook_url\" \"message\" \"summary\" \"summary_cn\" }} {{ .Name | translate }}: {{ .Value }}" }}
Expand Down

0 comments on commit 1cb5646

Please sign in to comment.