项目作者: prometheus-msteams

项目描述 :
Forward Prometheus Alert Manager notifications to Microsoft Teams.
高级语言: Go
项目地址: git://github.com/prometheus-msteams/prometheus-msteams.git
创建时间: 2018-03-04T19:55:38Z
项目社区:https://github.com/prometheus-msteams/prometheus-msteams

开源协议:MIT License

下载


GitHub tag
Build Status
codecov
Go Report Card

Overview

A lightweight Go Web Server that receives POST alert messages from Prometheus Alert Manager and sends it to a Microsoft Teams Channel using an incoming webhook url. How light? See the docker image!

Synopsis

Alertmanager doesn’t support sending to Microsoft Teams out of the box. Fortunately, they allow you to use a generic webhook_config for cases like this. This project was inspired from idealista’s prom2teams which was written in Python.

Why choose Go? Not Python or Ruby or Node?

Why use Go? A Go binary is statically compiled unlike the other simple language (python, ruby, node). Having a static binary means that there is no need for you to install your program’s dependencies and these dependencies takes up a lot of space in your docker image! Try it out DevOps folks!

Table of Contents

Getting Started (Quickstart)

How it works.

Installation

We always recommend to use the latest stable release!

OPTION 1: Run using docker.

  1. docker run -d -p 2000:2000 \
  2. --name="promteams" \
  3. -e TEAMS_INCOMING_WEBHOOK_URL="https://example.webhook.office.com/webhookb2/xxx" \
  4. -e TEAMS_REQUEST_URI=alertmanager \
  5. quay.io/prometheusmsteams/prometheus-msteams

OPTION 2: Run using binary.

Download the binary for your platform and the default card template from RELEASES, then run the binary in the same directory as you have stored the default-message-card.tmpl like the following:

  1. ./prometheus-msteams -teams-request-uri alertmanager \
  2. -teams-incoming-webhook-url "https://example.webhook.office.com/webhookb2/xxx"

OPTION 3: If you are going to deploy this in a Kubernetes cluster, checkout the Kubernetes Deployment Guide.

Setting up Prometheus Alert Manager

By default, prometheus-msteams creates a static uri handler /alertmanager and a dynamic uri handler /_dynamicwebhook/*.

static uri handler (e.g. /alertmanager)

  1. route:
  2. group_by: ['alertname']
  3. group_interval: 30s
  4. repeat_interval: 30s
  5. group_wait: 30s
  6. receiver: 'prometheus-msteams'
  7. receivers:
  8. - name: 'prometheus-msteams'
  9. webhook_configs: # https://prometheus.io/docs/alerting/configuration/#webhook_config
  10. - send_resolved: true
  11. url: 'http://localhost:2000/alertmanager' # the prometheus-msteams proxy

dynamic uri handler /_dynamicwebhook/*

The dynamic webhook handler allows you to pass the webhook url to prometheus-msteams proxy directly from alertmanager.

By default the passed URL is not validated. If validation is needed, pass flag -validate-webhook-url to prometheus-msteams on start.
A valid url matches the regular expression ^[a-z0-9]+\.webhook\.office\.com/webhookb2/[a-z0-9\-]+@[a-z0-9\-]+/IncomingWebhook/[a-z0-9]+/[a-z0-9\-]+(/[a-zA-Z0-9\-]+)?$.

  1. route:
  2. group_by: ['alertname']
  3. group_interval: 30s
  4. repeat_interval: 30s
  5. group_wait: 30s
  6. receiver: 'prometheus-msteams'
  7. receivers:
  8. - name: 'prometheus-msteams'
  9. webhook_configs:
  10. - send_resolved: true
  11. url: 'http://localhost:2000/_dynamicwebhook/example.webhook.office.com/webhookb2/xxx' # the prometheus-msteams proxy + "/_dynamicwebhook/" + webhook url (without prefix "https://")

Alternatively you can also use the webhook as a means of authorization:

  1. route:
  2. group_by: ['alertname']
  3. group_interval: 30s
  4. repeat_interval: 30s
  5. group_wait: 30s
  6. receiver: 'prometheus-msteams'
  7. receivers:
  8. - name: 'prometheus-msteams'
  9. webhook_configs:
  10. - send_resolved: true
  11. http_Config:
  12. authorization:
  13. type: 'webhook'
  14. credentials: 'example.webhook.office.com/webhookb2/xxx' # webhook url (without prefix "https://")
  15. url: 'http://localhost:2000/_dynamicwebhook/' # the prometheus-msteams proxy + "/_dynamicwebhook/"

This provides the added benefit that alertmanager will treat the Incoming
Webhook as a credential and hides it from view in the Web UI.

If you don’t have Prometheus running yet and you wan’t to try how this works,
try stefanprodan’s Prometheus in Docker to help you install a local Prometheus setup quickly in a single machine.

Simulating a Prometheus Alerts to Teams Channel

Create the following json data as prom-alert.json.

  1. {
  2. "version": "4",
  3. "groupKey": "{}:{alertname=\"high_memory_load\"}",
  4. "status": "firing",
  5. "receiver": "teams_proxy",
  6. "groupLabels": {
  7. "alertname": "high_memory_load"
  8. },
  9. "commonLabels": {
  10. "alertname": "high_memory_load",
  11. "monitor": "master",
  12. "severity": "warning"
  13. },
  14. "commonAnnotations": {
  15. "summary": "Server High Memory usage"
  16. },
  17. "externalURL": "http://docker.for.mac.host.internal:9093",
  18. "alerts": [
  19. {
  20. "labels": {
  21. "alertname": "high_memory_load",
  22. "instance": "10.80.40.11:9100",
  23. "job": "docker_nodes",
  24. "monitor": "master",
  25. "severity": "warning"
  26. },
  27. "annotations": {
  28. "description": "10.80.40.11 reported high memory usage with 23.28%.",
  29. "summary": "Server High Memory usage"
  30. },
  31. "startsAt": "2018-03-07T06:33:21.873077559-05:00",
  32. "endsAt": "0001-01-01T00:00:00Z"
  33. }
  34. ]
  35. }
  1. curl -X POST -d @prom-alert.json http://localhost:2000/alertmanager

The teams channel should received a message.

Sending Alerts to Multiple Teams Channel

You can configure this application to serve 2 or more request path and each path can use a unique Teams channel webhook url to post.

multiChannel

This can be achieved by supplying the application a configuration file.

Creating the Configuration File

Create a yaml file with the following format.

  1. connectors:
  2. - high_priority_channel: "https://example.webhook.office.com/webhookb2/xxxx/aaa/bbb"
  3. - low_priority_channel: "https://example.webhook.office.com/webhookb2/xxxx/aaa/ccc"

NOTE: high_priority_channel and low_priority_channel are example handler or request path names.

When running as a docker container, mount the config file in the container and set the CONFIG_FILE environment variable.

  1. docker run -d -p 2000:2000 \
  2. --name="promteams" \
  3. -v /tmp/config.yml:/tmp/config.yml \
  4. -e CONFIG_FILE="/tmp/config.yml" \
  5. quay.io/prometheusmsteams/prometheus-msteams:v1.5.1

When running as a binary, use the -config-file flag.

  1. ./prometheus-msteams server \
  2. -l localhost \
  3. -p 2000 \
  4. -config-file /tmp/config.yml

This will create the request uri handlers /high_priority_channel and /low_priority_channel.

To validate your configuration, see the /config endpoint of the application.

  1. curl localhost:2000/config
  2. [
  3. {
  4. "high_priority_channel": "https://example.webhook.office.com/webhookb2/xxxx/aaa/bbb"
  5. },
  6. {
  7. "low_priority_channel": "https://example.webhook.office.com/webhookb2/xxxx/aaa/ccc"
  8. }
  9. ]

Setting up Prometheus Alert Manager

Considering the prometheus-msteams config file settings, your Alert Manager would have a configuration like the following.

  1. route:
  2. group_by: ['alertname']
  3. group_interval: 30s
  4. repeat_interval: 30s
  5. group_wait: 30s
  6. receiver: 'low_priority_receiver' # default/fallback request handler
  7. routes:
  8. - receiver: high_priority_receiver
  9. match:
  10. severity: critical
  11. - receiver: low_priority_receiver
  12. match:
  13. severity: warning
  14. receivers:
  15. - name: 'high_priority_receiver'
  16. webhook_configs:
  17. - send_resolved: true
  18. url: 'http://localhost:2000/high_priority_channel' # request handler 1
  19. - name: 'low_priority_receiver'
  20. webhook_configs:
  21. - send_resolved: true
  22. url: 'http://localhost:2000/low_priority_channel' # request handler 2

Customise Messages to MS Teams

This application uses a default Microsoft Teams Message card template to convert incoming Prometheus alerts to teams message cards. This template can be customised. Simply create a new file that you want to use as your custom template. It uses the Go Templating Engine and the Prometheus Alertmanager Notification Template. Also see the Office 365 Connector Card Reference and some examples for more information to construct your template. Apart from that, you can use the Message Card Playground to form the basic structure of your card.

When running as a docker container, mount the template file in the container and set the TEMPLATE_FILE environment variable.

  1. docker run -d -p 2000:2000 \
  2. --name="promteams" \
  3. -e TEAMS_INCOMING_WEBHOOK_URL="https://example.webhook.office.com/webhookb2/xxx" \
  4. -v /tmp/card.tmpl:/tmp/card.tmpl \
  5. -e TEMPLATE_FILE="/tmp/card.tmpl" \
  6. quay.io/prometheusmsteams/prometheus-msteams

When running as a binary, use the -template-file flag.

  1. ./prometheus-msteams server \
  2. -l localhost \
  3. -p 2000 \
  4. -template-file /tmp/card.tmpl

Customise Messages per MS Teams Channel

You can also use a custom template per webhook by using the connectors_with_custom_templates.

  1. # alerts in the connectors here will use the default template.
  2. connectors:
  3. - alert1: <webhook>
  4. # alerts in the connectors here will use template_file specified.
  5. connectors_with_custom_templates:
  6. - request_path: /alert2
  7. template_file: ./default-message-card.tmpl
  8. webhook_url: <webhook>
  9. escape_underscores: true # get the effect of -auto-escape-underscores.

Use Template functions to improve your templates

You can use

Configuration

All configuration from flags can be overwritten using environment variables.

E.g, -config-file is CONFIG_FILE, -debug is DEBUG, -log-format is LOG_FORMAT.

  1. Usage of prometheus-msteams:
  2. -auto-escape-underscores
  3. Automatically replace all '_' with '\_' from texts in the alert.
  4. -config-file string
  5. The connectors configuration file.
  6. -debug
  7. Set log level to debug mode. (default true)
  8. -http-addr string
  9. HTTP listen address. (default ":2000")
  10. -idle-conn-timeout duration
  11. The HTTP client idle connection timeout duration. (default 1m30s)
  12. -jaeger-agent string
  13. Jaeger agent endpoint (default "localhost:6831")
  14. -jaeger-trace
  15. Send traces to Jaeger.
  16. -log-format string
  17. json|fmt (default "json")
  18. -max-idle-conns int
  19. The HTTP client maximum number of idle connections (default 100)
  20. -teams-incoming-webhook-url string
  21. The default Microsoft Teams webhook connector.
  22. -teams-request-uri string
  23. The default request URI path where Prometheus will post to.
  24. -template-file string
  25. The Microsoft Teams Message Card template file. (default "./default-message-card.tmpl")
  26. -tls-handshake-timeout duration
  27. The HTTP client TLS handshake timeout. (default 30s)
  28. -max-retry-count int
  29. The retry maximum for sending requests to the webhook. (default 3)
  30. -validate-webhook-url
  31. Enforce strict validation of webhook url. (default false)

Kubernetes Deployment

See Helm Guide.

Contributing

See Contributing Guide