在警报管理器接收器中使用警报注释

时间:2016-09-08 11:14:50

标签: prometheus

我收到了这样的警告:

ALERT InstanceDown
IF up == 0
FOR 30s
ANNOTATIONS {
    summary = "Server {{ $labels.Server }} is down.",
    description = "{{ $labels.Server }} ({{ $labels.job }}) is down for more than 30 seconds."
}

松弛的接收器看起来像这样:

receivers:
- name: general_receiver
  slack_configs:
  - api_url: https://hooks.slack.com/services/token
    channel: "#telemetry"
    title: "My summary"
    text: "My description"

是否可以在我的接收器中使用注释? This github comment表明它是,但我还没有能够从中获得任何工作。

2 个答案:

答案 0 :(得分:10)

你需要定义自己的模板(我只是走路去那条路)。有关简要示例,请参阅:https://prometheus.io/blog/2016/03/03/custom-alertmanager-templates/

此处的所有提醒至少包含summaryrunbook注释。 runbook包含Wiki网址。我已经定义了以下模板(如上面的博客文章中所述),以将它们包含在松弛消息体中:

{{ define "__slack_text" }}                                                                                                                                                
{{ range .Alerts }}{{ .Annotations.summary }}                                                                                                                                    
{{ if gt (len .Labels) (len .GroupLabels) }}({{ with .Labels.Remove .GroupLabels.Names }}{{ .Values | join " " }}{{ end }}){{ end }}                                             
{{ end }}<{{ (index .Alerts 0).GeneratorURL }}|Source> | {{ if .CommonAnnotations.runbook }}<{{ .CommonAnnotations.runbook }}|:notebook_with_decorative_cover: Runbook>{{ else }}<https://wiki.some.where/Runbooks|:exclamation:*NO RUNBOOK*:exclamation:>{{ end }}                                                                                
{{ end }}   
{{ define "slack.default.text" }}{{ template "__slack_text" . }}{{ end }}  

模板会覆盖slack.default.text,因此无需在receiver配置中引用它。

默认值可以在源代码中找到,也可以在&#34;文档&#34;:

中找到

有关golang模板语言的基础知识和详细信息:

为了完整性&#39;为了最终的模板:

{{ define "__slack_text" }}                                                                                                                                                
{{ range .Alerts }}{{ .Annotations.text }}{{ end }}                                                                                
{{ end }}

{{ define "__slack_title" }}                                                                                                                                                
{{ range .Alerts }}{{ .Annotations.title }}{{ end }}                                                                                
{{ end }}

{{ define "slack.default.text" }}{{ template "__slack_text" . }}{{ end }}
{{ define "slack.default.title" }}{{ template "__slack_title" . }}{{ end }}

答案 1 :(得分:1)

我知道这种响应是在多年之后的,但是我的方法是利用.CommonAnnotations和标签显示信息,并使用Alertname查询运行手册-

alertmanager.yml

  slack_configs:
  - api_url: 
    channel: '#XXXXXXXXXXXXXXXX'
    color: '{{ template "SLACK_MSG_COLOR" . }}'
    send_resolved: true
    title: '{{ template "SLACK_MSG_TITLE" . }}'
    text: '{{ template "SLACK_MSG_TEXT" . }}'


  pagerduty_configs:
  - routing_key: '{{ template "Global_PD_Service_Key" . }}'
    description: '{{ template "PAGERDUTY_DESCRIPTION" . }}'
    severity: '{{ if .CommonLabels.severity }}{{ .CommonLabels.severity | toLower }}{{ else }}critical{{ end }}'
    links: 
    - text: 'Prometheus'
      href: '{{ (index .Alerts 0).GeneratorURL }}'
    - text: 'Search Runbooks'
      href: '{{ template "RUNBOOK_SEARCH" . }}'

和.tmpl

################  
# Runbook  
################ 
# Runbook Search
{{ define "RUNBOOK_SEARCH" }}https://dsmith73.github.io/101-docs/search/?q={{ .CommonLabels.alertname }}{{ end }}



################  
# Slack  
################  
# Slack Color
{{ define "SLACK_MSG_COLOR" }}{{ if eq .Status "firing" }}{{ if eq .CommonLabels.severity "critical" }}danger{{ else if eq .CommonLabels.severity "error" }}danger{{ else if eq .CommonLabels.severity "warning" }}warning{{ else }}#439FE0{{ end }}{{ else}}good{{ end }}{{ end }}


# Slack Text  
{{define "SLACK_MSG_TEXT" }}
      <!here> - {{ .CommonAnnotations.description }}
      `View:` :chart_with_upwards_trend:*<{{ (index .Alerts 0).GeneratorURL }}|Prometheus>* or :notebook:*<{{ template "RUNBOOK_SEARCH" . }}|Runbook>*

      *Details:*
      {{ range .CommonLabels.SortedPairs }}• *{{ .Name }}:* `{{ .Value }}`
    {{ end }}
{{ end }}

#Slack Summary
{{ define "SLACK_TITLE_SUMMARY" -}}
    {{- if .CommonAnnotations.summary -}}
        {{- .CommonAnnotations.summary -}}
    {{- else -}}
        {{- with index .Alerts 0 -}}
            {{- .Annotations.summary -}}
        {{- end -}}
    {{- end -}}
{{- end -}}


# Slack Title  
{{ define "SLACK_MSG_TITLE" }}
    {{ if eq .Status "resolved" }}
        {{- .Status | toUpper }} : {{ template "SLACK_TITLE_SUMMARY" . }}
    {{ else if eq .Status "firing" }}
        {{ .CommonLabels.severity | toUpper }} : {{ template "SLACK_TITLE_SUMMARY" . }}
    {{ end }}
{{ end }}

对我来说,这是将警报绑定到运行手册的好方法,而不是在规则或其他位置中定义警报...

相关问题