带有常见问题网址的Prometheus Slack警报

时间:2018-07-12 08:48:42

标签: slack prometheus go-templates prometheus-alertmanager

我有这样的普罗米修斯警报:

- alert: NginxCrashLoop
  annotations:
    description: Nginx at {{ $labels.pod }} is in Crash Loop
    identifier: '{{ $labels.node }}'
    runbook_url: https://sites.google.com/a/domain.com/bi/faq/nginx
  expr: |
    rate(kube_pod_container_status_restarts_total{container="frontend"}[2m]) * on(namespace, pod) group_left(node) kube_pod_info * 3600 > 3
  labels:
    severity: critical

和用于Slack自定义警报的Go模板:

{{ define "__single_message_title" }}{{ range .Alerts.Firing }}{{ .Labels.alertname }} at {{ .Annotations.identifier }}{{ end }}{{ range .Alerts.Resolved }}{{ .Labels.alertname }} at {{ .Annotations.identifier }}{{ end }}{{ end }}

{{ define "custom_title" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ if or (and (eq (len .Alerts.Firing) 1) (eq (len .Alerts.Resolved) 0)) (and (eq (len .Alerts.Firing) 0) (eq (len .Alerts.Resolved) 1)) }}{{ template "__single_message_title" . }}{{ end }}{{ end }}

{{ define "faqURL" }}{{ range .Alerts.Firing }}{{ .Annotations.runbook_url }}
{{ end }}{{ end }}

{{ define "custom_slack_message" }}
{{ if or (and (eq (len .Alerts.Firing) 1) (eq (len .Alerts.Resolved) 0)) (and (eq (len .Alerts.Firing) 0) (eq (len .Alerts.Resolved) 1)) }}
{{ range .Alerts.Firing }}{{ .Annotations.description }}{{ end }}{{ range .Alerts.Resolved }}{{ .Annotations.description }}{{ end }}
{{ else }}
{{ if gt (len .Alerts.Firing) 0 }}
*Alerts Firing:*
{{ range .Alerts.Firing }}- {{ .Annotations.identifier }}: {{ .Annotations.description }}: <{{ template "faqURL" . }}|Quick FAQ>
{{ end }}{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}
*Alerts Resolved:*
{{ range .Alerts.Resolved }}- {{ .Annotations.identifier }}: {{ .Annotations.description }}
{{ end }}{{ end }}
{{ end }}
{{ end }}

我希望它能够向我发送指向FAQ文档的链接,以减轻问题的影响,该描述如下所示。但是,松弛消息仍然不包含链接。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

这是我的用法

我有一个自定义模板,其松弛文本定义如下

{{`{{ define "slack.custom.text" }}`}}
{{`{{ range .Alerts }}`}}
*Alert:* {{`{{ .Annotations.summary }}`}} - `{{`{{ .Labels.severity }}`}}`
*Description:* {{`{{ .Annotations.description }}`}}
*Runbook:* {{`{{ .Annotations.runbook}}`}}
*Details:*
{{`{{ range .Labels.SortedPairs }}`}} • *{{`{{ .Name }}`}}:* `{{`{{ .Value }}`}}`
{{`{{ end }}`}}
{{`{{ end }}`}}
{{`{{ end }}`}}

然后在alert_manager_config中调用它,如

slack_configs:
      - channel: slack-channel-id
        title: '{{`{{ template "slack.default.title" . }}`}}'
        text: '{{`{{ template "slack.custom.text" . }}`}}'

templates:
- '/etc/alertmanager/config/custom.tmpl'