邮件,钉钉,企业微信告警模板

alertmanager.yml

global:
  resolve_timeout: 5m
  # smtp config
  smtp_from: "prometheus-alert@example.com"
  smtp_smarthost: 'email-smtp.us-west-2.amazonaws.com:25'
  smtp_auth_username: "1"
  smtp_auth_password: "2"
  #smtp_require_tls: true
templates:
  - '/data/alertmanager/template/*.tmpl'
route:
  receiver: webhook1
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 1h
  group_by: [alertname]
  routes:

    # team1 webhook
    - receiver: webhook1
      group_wait: 10s
      match:
        team: team1
    # team2 webhook
    - receiver: webhook2
      group_wait: 10s
      match:
        team: team2

receivers:
  - name: webhook1
    email_configs:
      - to: 'xxxxxxxx@qq.com'
        headers: { Subject: "[Team1] 报警邮件"} # 接收邮件的标题
    webhook_configs:
      - url: http://localhost:8060/dingtalk/ops/send
  - name: webhook2
    email_configs:
      - to: 'xxxxxxxx@qq.com'
        send_resolved: true
        headers: { Subject: "[Team2] 报警邮件"}
    webhook_configs:
      - url: http://localhost:8060/dingtalk/ops/send
    wechat_configs:
      - corp_id: 'wwxxxxxxxxxxxxxx86'
        to_party: '2'
        agent_id: '1000002'
        api_secret: '8i7EHfxxxxxxxxxxxxxxxxiofUQM0'
        send_resolved: true

wechat.tmpl

 {{ define "wechat.default.message" }}
  {{ if gt (len .Alerts.Firing) 0 -}}
  ☸ Alerts Firing ✖️ ‼️ :
  {{ range .Alerts }}
  ☎️ 触发警报 ☔ ☠️ : {{ .Labels.alertname }}
  ☞名称空间: {{ .Labels.namespace }}
  ☞主机: {{ .Labels.instance }}
  ☞job: {{ .Labels.job }}
  ->涉及容器名称: {{ .Labels.container }}
  ->Pod名称: {{ .Labels.pod }}
  告警级别: {{ .Labels.severity }}
  告警详情: {{ .Annotations.description }}
  触发时间⏱: {{ .StartsAt.Format "2006-01-02 15:04:05" }}
  警报链接: {{ .GeneratorURL }}
  ✍️ 备注详情❄️: {{ .Annotations.runbook_url }}
  -------------------->END<--------------------
  {{- end }}
  {{- end }}
  {{ if gt (len .Alerts.Resolved) 0 -}}
  ☸ Alerts Resolved ✔️:
  {{ range .Alerts }}
  ☎️ 触发警报 ☫ : {{ .Labels.alertname }}
  ♥️ 名称空间 ✝️ : {{ .Labels.namespace }}
  ♥️ ->涉及容器名称: {{ .Labels.container }}
  ♥️ ->Pod名称☸: {{ .Labels.pod }}
  ♥️ 告警级别: {{ .Labels.severity }}
  ♥️ 告警详情: {{ .Annotations.description }}
  ♥️ 触发时间 ⏱ : {{ .StartsAt.Format "2006-01-02 15:04:05" }}
  ♥️ 恢复时间 ⏲ : {{ .EndsAt.Format "2006-01-02 15:04:05" }}
  ♥️ 备注详情: {{ .Annotations.runbook_url }}
  {{- end }}
  {{- end }}
  {{- end }}

email.tmpl

  {{ define "email.to.html" }}
  {{ if gt (len .Alerts.Firing) 0 -}}
  ? Alerts Firing ?? ?? : <br>
  <br>
  <br>
  {{ range .Alerts }}
 -------------------->START<-------------------- <br>
  ?? 触发警报 ? ?? : {{ .Labels.alertname }} <br>
  ?? 告警级别: {{ .Labels.severity }} <br>
  ?? 触发时间?: {{ (.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }} <br>
  ?? 告警详情: {{ .Annotations.message }} <br>
  ?? 警报链接: {{ .GeneratorURL }}  <br>
  ?? 备注详情??: {{ .Annotations.runbook_url }} <br>
  -------------------->END<-------------------- <br>
  <br>
  <br>
  {{- end }}
  {{- end }}
  {{ if gt (len .Alerts.Resolved) 0 -}}
  ? Alerts Resolved ??: <br>
  {{ range .Alerts }}
  -------------------->START<-------------------- <br>
  ?? 触发警报 ? : {{ .Labels.alertname }} <br>
  ?? 告警级别: {{ .Labels.severity }} <br>
  ? 触发时间 ?: {{ (.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }} <br>
  ? 恢复时间 ?: {{ (.EndsAt.Add 28800e9).Format "2006-01-02 15:04:05" }} <br>
  ?? 告警详情: {{ .Annotations.message }} <br>
  ?? 警报链接: {{ .GeneratorURL }}  <br>
  ?? 备注详情: {{ .Annotations.runbook_url }} <br>
  -------------------->END<-------------------- <br>
  <br>
  <br>
  {{- end }}
  {{- end }}
  {{- end }}

dingtalk_custom_tempalte.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 | markdown | html }}
{{ end }}
**Annotations**
{{ range .Annotations.SortedPairs }}> - {{ .Name }}: {{ .Value | markdown | html }}
{{ end }}
**Source:** [{{ .GeneratorURL }}]({{ .GeneratorURL }})
{{ end }}{{ end }}

{{ define "default.__text_alert_list" }}{{ range . }}
---
**告警级别:** {{ .Labels.severity | upper }}

**概览:** {{ .Annotations.summary }}

**Trigger Time:** {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }}

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

**图表:** [查看图表]({{ .GeneratorURL }})

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

{{/* Default */}}
{{ define "default.title" }}{{ template "__subject" . }}{{ end }}
{{ define "default.content" }}#### \[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}\] **[{{ index .GroupLabels "alertname" }}]({{ template "__alertmanagerURL" . }})**
{{ if gt (len .Alerts.Firing) 0 -}}
**发生告警**
{{ template "default.__text_alert_list" .Alerts.Firing }}
{{- end }}
{{ if gt (len .Alerts.Resolved) 0 -}}
**告警恢复**
{{ template "default.__text_alert_list" .Alerts.Resolved }}
{{- end }}
{{- end }}

{{/* Legacy */}}
{{ define "legacy.title" }}{{ template "__subject" . }}{{ end }}
{{ define "legacy.content" }}#### \[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}\] **[{{ index .GroupLabels "alertname" }}]({{ template "__alertmanagerURL" . }})**
{{ template "__text_alert_list" .Alerts.Firing }}
{{- end }}

{{/* Following names for compatibility */}}
{{ define "ding.link.title" }}{{ template "default.title" . }}{{ end }}
{{ define "ding.link.content" }}{{ template "default.content" . }}{{ end }}

posted @ 2021-11-23 09:45  哈喽哈喽111111  阅读(1006)  评论(0编辑  收藏  举报