From 76ee1ccd11f6696c4b0a8b61b5c26ed5fbe2b62b Mon Sep 17 00:00:00 2001 From: Gentleelephant <1132960613@qq.com> Date: Mon, 13 May 2024 15:03:15 +0800 Subject: [PATCH] update template --- config/samples/template.yaml | 2 +- helm/templates/template.yaml | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/config/samples/template.yaml b/config/samples/template.yaml index 4f543bb..7d66f55 100644 --- a/config/samples/template.yaml +++ b/config/samples/template.yaml @@ -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" }}{{ .Alerts | len }} {{ if ne (len .Status) 0 }}{{ .Status }} {{ end }}alerts{{ if gt (len .CommonLabels.SortedPairs) 0 }} for {{ range .CommonLabels.SortedPairs }}{{ .Name | translate }}={{ .Value }} {{ 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.default.text" }}{{ range .Alerts }}{{ template "nm.default.message" . }} {{ range .Labels.SortedPairs }} {{ .Name | translate }}: {{ .Value }} diff --git a/helm/templates/template.yaml b/helm/templates/template.yaml index 098bf0f..bf3ebc0 100644 --- a/helm/templates/template.yaml +++ b/helm/templates/template.yaml @@ -5,8 +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\" }}{{ $numAlerts := len .Alerts }}{{ if eq $numAlerts 0 }}Show nothing{{ else if eq $numAlerts 1 }}{{ range .Alerts }}{{ $alertTitle := \"\" }}{{ $alertType := .Labels.alerttype }}{{ $alertName := .Labels.alertname }}{{ $cluster := .Labels.cluster }}{{ $node := .Labels.node }}{{ $pod := .Labels.pod }}{{ $namespace := .Labels.namespace }}{{ if eq $alertType \"metric\" }}{{ $alertTitle = \"[Metrics Alert]\" }}{{ else if eq $alertType \"auditing\" }}{{ $alertTitle = \"[Audit Alert]\" }}{{ else if eq $alertType \"event\" }}{{ $alertTitle = \"[Event Alert]\" }}{{ else }}{{ $alertTitle = \"[Unknown Alert]\" }}{{ end }}{{ $output := printf \"%s\" $alertTitle }}{{ 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 $pod }}{{ $output = printf \"%s | pod=%s\" $output $pod }}{{ if $node }}{{ $output = printf \"%s/node=%s\" $output $node }}{{ end }}{{ else }}{{ if $node }}{{ $output = printf \"%s | node=%s\" $output $node }}{{ end }}{{ end }}{{ $output }}{{ end }}{{ else }}{{ $alertTitle := \"[Aggregation\" }}{{ $alertType := index .GroupLabels \"alerttype\" }}{{ $alertName := index .GroupLabels \"alertname\" }}{{ $cluster := index .GroupLabels \"cluster\" }}{{ $namespace := index .GroupLabels \"namespace\" }}{{ if eq $alertType \"metric\" }}{{ $alertTitle = printf \"%s Metrics Alert]\" $alertTitle }}{{ else if eq $alertType \"auditing\" }}{{ $alertTitle = printf \"%s Audit Alert]\" $alertTitle }}{{ else if eq $alertType \"event\" }}{{ $alertTitle = printf \"%s Event Alert]\" $alertTitle }}{{ else }}{{ $alertTitle = printf \"%s Alert]\" $alertTitle }}{{ end }}{{ $output := printf \"%s\" $alertTitle }}{{ 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 }}{{ $output }}{{ 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.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 }}" }}