【问题标题】:Prometheus Alertmanager Slack Configuration: send_resolved as flexible valuePrometheus Alertmanager Slack 配置:send_resolved 作为灵活值
【发布时间】:2020-01-30 13:51:52
【问题描述】:

我想根据警报向 slack 发送解决通知。因此,我为每个警报创建了一个注释(“resolve”),它应该定义是否应该发送已解决的通知。

这是我的警报的外观:

        - alert: JobsFailing
          expr: rate(failed_jobs_submitter{job="my_job"}[10m]) > 0
          for: 5m
          labels:
            severity: slack_critical
            service: my_service
          annotations:
            identifier: 'my_service_jobs_failing'
            description: 'Jobs are failing'
            resolve: true

这是我的 alertmanager.yaml:

    global:
      resolve_timeout: 5m
      slack_api_url: 'https://hooks.slack.com/services/...'
      http_config:
        proxy_url: 'some_proxy'

    templates:
      - /etc/config/notifications.tpl

    route:
      receiver: 'default-receiver'
      group_wait: 5s
      group_interval: 1m
      repeat_interval: 6h
      routes:
      - receiver: slack_critical
        match:
          severity: slack_critical

    receivers:
    - name: default-receiver

    - name: slack_critical
      slack_configs:
      - channel: '#alert_channel'
        username: "Alert"
        icon_emoji: ":monkey:"
        send_resolved: {{ .Annotations.resolve }}
        title: '{{ template "custom_title" . }}'
        text: '{{ template "custom_slack_message" . }}'

我尝试对 .Annotations.resolve 使用字符串而不是布尔值

我尝试使用模板:

{{ define resolve_alert }}{{ if eq .Annotations.resolve "true"}}{{ else }}{{ if eq .Annotations.resolve "false" }}{{ end }}

send_resolved: {{ template resolve_alert . }}

我尝试了 if 子句:

{{ if eq .Annotations.resolve true }}send_resolved: true{{else}}send_resolved: false{{end}}
send_resolved: {{ if or .Annotations.resolve | default false }}

我希望 slack 配置能够获取值 .Annotations.resolve 并根据警报中定义的值向 slack 发送已解决的消息。使用 .Annotations.resolve 时得到的结果:

level=error ts=2019-10-01T09:08:12.284Z caller=coordinator.go:124 component=configuration msg="Loading configuration file failed" file=/etc/config/alertmanager.yml err="yaml: unmarshal errors:\n  line 28: cannot unmarshal !!map into bool"

使用模板时得到的结果:

level=error ts=2019-09-30T14:39:29.294Z caller=coordinator.go:124 component=configuration msg="Loading configuration file failed" file=/etc/config/alertmanager.yml err="yaml: line 27: did not find expected key"

任何意见都非常感谢...

【问题讨论】:

    标签: alert prometheus slack


    【解决方案1】:

    send_resolved 是一个布尔值,而不是一个可模板化的字符串。你想要的是一个标签,当有一个路由到一个接收器和另一个路由到另一个时,每个路由都有不同的布尔值。

    无法根据注释发送警报,只能根据标签。

    【讨论】:

    • 使用标签也不能解决问题,错误保持不变:cannot unmarshal !!map into bool 如果我使用 {{ .Labels.resolved }}did not find expected key 如果我使用 if 子句:{{ if .Labels.resolve == "test" }}true{{else}}false{{end}}
    • 我也尝试定义一个额外的路由+接收器来实现不同的send_resolved,但这也没有成功......
    猜你喜欢
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多