Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

设置自定义模版,发送出来的还是默认的 #117

Open
levy0519 opened this issue Sep 9, 2020 · 3 comments
Open

设置自定义模版,发送出来的还是默认的 #117

levy0519 opened this issue Sep 9, 2020 · 3 comments

Comments

@levy0519
Copy link

levy0519 commented Sep 9, 2020

`
##alertmanager.base.yml
templates:

  • /srv/alertmanager/temp/default.tmpl

##default.tmpl
{{ define "__subject" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .GroupLabels.SortedPairs.Values | join " " }} {{ if gt (len .CommonLabels) (len .GroupLabels) }}({{ with .CommonLabels.Remove .GroupLabels.Names }}{{ .Values | join " " }}{{ end }}){{ end }}{{ end }}
{{ define "__alertmanagerURL" }}{{ .ExternalURL }}/#/alerts?receiver={{ .Receiver }}{{ end }}
{{ define "__text_alert_list" }}{{ range . }}
Labels
{{ range .Labels.SortedPairs }}> - {{ .Name }}: {{ .Value | html }}
{{ end }}
Annotations
{{ range .Annotations.SortedPairs }}> - {{ .Name }}: {{ .Value | html }}
{{ end }}
Source: [{{ .GeneratorURL }}]({{ .GeneratorURL }})
{{ end }}{{ end }}
{{ define "default.__text_alert_list" }}{{ range . }}

告警级别: {{ .Labels.severity | upper }}
运营团队: {{ .Labels.team | upper }}
触发时间: {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }}
事件信息:
{{ range .Annotations.SortedPairs }}> - {{ .Name }}: {{ .Value | markdown | html }}
{{ end }}
......

[root@f75f3c0030d8 temp]# amtool check-config ../alertmanager.base.yml
Checking '../alertmanager.base.yml' SUCCESS
Found:

  • global config
  • route
  • 0 inhibit rules
  • 2 receivers
  • 1 templates
    SUCCESS

`

@ne1000
Copy link

ne1000 commented Oct 9, 2020

@levy0519 解决了没

@CodyGuo
Copy link

CodyGuo commented Jan 13, 2021

文档有点简陋,我这里有一个例子,可以参考一下:

{{ define "my.__text_alert_list" }}

**告警级别:** \[{{ .Labels.severity | upper }}\] {{ .Annotations.summary }}

**告警应用:** {{ .Labels.source }}

**告警实例:** {{ .Labels.instance }}

**告警描述:** {{ .Annotations.description }}

**Prometheus:** [📈]({{ .GeneratorURL }})

**告警详情:**
{{ range .Labels.SortedPairs }}
  {{- if and (ne (.Name) "severity") (ne (.Name) "summary") }}> - {{ .Name }}: {{ .Value | markdown | html }}
{{ end }}{{ end }}
{{ end }}

{{ define "my.__text_firing_list" }}{{ range . }}
**告警时间:** {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }}
{{ template "my.__text_alert_list" . }}
{{ end }}{{ end }}

{{ define "my.__text_resolved_list" }}{{ range . }}
**恢复时间:** {{ dateInZone "2006.01.02 15:04:05" (.EndsAt) "Asia/Shanghai" }}
{{ template "my.__text_alert_list" . }}
{{ end }}{{ end }}

{{ define "my.title" }}{{ template "__subject" . }}{{ end }}
{{ define "my.content" }}\[{{ if eq .Status "firing" }}PROBLEM:{{ .Alerts.Firing | len }}{{ else }}RECOVERY:{{ .Alerts.Resolved | len }}{{ end }}\] **[{{ index .GroupLabels "alertname" }}]({{ template "__alertmanagerURL" . }})**
{{ if gt (len .Alerts.Firing) 0 -}}
**Alerts Firing**
{{ template "my.__text_firing_list" .Alerts.Firing }}
{{- end }}
{{ if gt (len .Alerts.Resolved) 0 -}}
**Alerts Resolved**
{{ template "my.__text_resolved_list" .Alerts.Resolved }}
{{- end }}
{{- end }}

{{ define "ding.link.title" }}{{ template "my.title" . }}{{ end }}
{{ define "ding.link.content" }}{{ template "my.content" . }}{{ end }}

@KeithTt
Copy link

KeithTt commented Jun 1, 2021

试着改了一下,改文件没用,到底要怎么改默认的消息???

每次发出来的都是这几行:

[RESOLVED] 
Alerts Resolved

[CIRTICAL] cluster m7 pod alg-mid-rec-85f499d764-2lsq9 restarts 9 times.
Description:

Graph: 📈

Details:

alertname: kube_pod_restarts_total
cluster: m7
container: alg-mid-rec
instance: 10.231.3.117:8080
job: kube-state-metrics
namespace: default
pod: xxx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants