Clintm
Clintm

Reputation: 4887

Is there some way to parameterize the slack channel in alertmanager?

Title pretty much says it all... I'm trying to parameterize the slack channel so I don't have a bunch of receivers configured the same way except for the channel.

The docs say it takes a tmpl_string so it should be possible.

Upvotes: 2

Views: 1258

Answers (1)

Clintm
Clintm

Reputation: 4887

Found it... https://ralphmcneal.com/prometheus-alertmanager-dynamic-slack-config/

I can use a label.

channel: '{{ index ((index .Alerts 0).Labels) "slack_channel" }}'

This was my final config:

global:
  resolve_timeout: 5m
  http_config:
    follow_redirects: true
  smtp_hello: localhost
  smtp_require_tls: true
  slack_api_url: <secret>
  pagerduty_url: https://events.pagerduty.com/v2/enqueue
  opsgenie_api_url: https://api.opsgenie.com/
  wechat_api_url: https://qyapi.weixin.qq.com/cgi-bin/
  victorops_api_url: https://alert.victorops.com/integrations/generic/20131114/alert/
route:
  receiver: "null"
  group_by:
  - '...'
  continue: false
  routes:
  - receiver: "null"
    match:
      alertname: Watchdog
    continue: false
  - receiver: slack-from-loki
    match:
      source: loki
    continue: false
  - receiver: slack
    continue: true
  group_wait: 1s
  group_interval: 1s
  repeat_interval: 12h
receivers:
- name: "null"
- name: slack-from-loki
  slack_configs:
  - send_resolved: true
    http_config:
      follow_redirects: true
    api_url: <secret>
    channel: '{{ index ((index .Alerts 0).Labels) "slackChannel" }}'
    username: kube-prometheus-stack
    color: '{{ if eq .Status "firing" }}danger{{ else }}good{{ end }}'
    title: '[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing
      | len }}{{ end }}] Monitoring Event Notification'
    title_link: '{{ template "slack.default.titlelink" . }}'
    pretext: '{{ template "slack.default.pretext" . }}'
    text: |-
      {{ range .Alerts }}
        *Alert:* {{ .Annotations.summary }} - `{{ .Labels.severity }}`
        *Description:* {{ .Annotations.description }}
        *URL:* <{{ .GeneratorURL }}|here>
        *Details:*
        {{ range .Labels.SortedPairs }} • *{{ .Name }}:* `{{ .Value }}`
        {{ end }}
      {{ end }}
    short_fields: false
    footer: '{{ template "slack.default.footer" . }}'
    fallback: '{{ template "slack.default.fallback" . }}'
    callback_id: '{{ template "slack.default.callbackid" . }}'
    icon_emoji: '{{ template "slack.default.iconemoji" . }}'
    icon_url: '{{ template "slack.default.iconurl" . }}'
    link_names: false
- name: slack
  slack_configs:
  - send_resolved: true
    http_config:
      follow_redirects: true
    api_url: <secret>
    channel: '#k8s-alertmanager-till-staging'
    username: kube-prometheus-stack
    color: '{{ if eq .Status "firing" }}danger{{ else }}good{{ end }}'
    title: '[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing
      | len }}{{ end }}] Monitoring Event Notification'
    title_link: '{{ template "slack.default.titlelink" . }}'
    pretext: '{{ template "slack.default.pretext" . }}'
    text: |-
      {{ range .Alerts }}
        *Alert:* {{ .Annotations.summary }} - `{{ .Labels.severity }}`
        *Description:* {{ .Annotations.description }}
        *URL:* <{{ .GeneratorURL }}|here>
        *Details:*
        {{ range .Labels.SortedPairs }} • *{{ .Name }}:* `{{ .Value }}`
        {{ end }}
      {{ end }}
    short_fields: false
    footer: '{{ template "slack.default.footer" . }}'
    fallback: '{{ template "slack.default.fallback" . }}'
    callback_id: '{{ template "slack.default.callbackid" . }}'
    icon_emoji: '{{ template "slack.default.iconemoji" . }}'
    icon_url: '{{ template "slack.default.iconurl" . }}'
    link_names: false
templates:
- /etc/alertmanager/config/*.tmpl

Alert config looks like:

  alerting_groups:
  - name: k8s
    rules:
    - alert: K8sEventsPercentErrors
      annotations:
        description: Error rate from event-exporter is greater than 1%
        summary: Error rate from event-exporter is greater than 1%
      expr: |
        (sum(rate({app="event-exporter"} | json | __error__ != "JSONParserErr"  | type != "Normal" !~ "(?i)probe" [5m])) 
        / sum(rate({app="event-exporter"} [5m])) * 100) > 1
      for: 1m
      labels:
        source: loki
        severity: warning
        slackChannel: '#clint-test'
  - name: till-backend
    rules:
    - alert: ApiErrorRate
      annotations:
        summary: Error rate for till-backend api service is greater than 5%
        description: Error rate for till-backend api service is greater than 5%
      expr: |
        sum(rate({namespace="till-backend-master", app="api"} |~ "(?i)err" !~ "'error': None" [5m])) 
        / sum(rate({namespace="till-backend-master", app="api"} [5m])) * 100 > 5
      for: 2m
      labels:
        severity: warning
        source: loki
        slackChannel: '#clint-test'

Upvotes: 2

Related Questions