# 通过 Slack 接收告警

[Slack](https://slack.com) 作为 IM 办公软件，简单好用，在国外用的特别多，那如何用它来接收 Prometheus 的告警信息，让咱们的运维看上去高大上。

## 目的

* 使用 slack 接受消息。
* 消息能够带有 url， 自动跳转到 prometheus 对应 graph 查询页面。
* 能自定义颜色。
* 能够 @ 某人

## 准备工作

已注册了 slack 账号，并创建了一个 #test 频道。

## 配置步骤

Step1: 为 #test 频道创建一个 incomming webhooks 应用。

* 点击频道标题，选择 `Add an app or integration`

![Add an app or integration](https://962234814-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8oPJUWJoRcTOnREKHd%2Fsync%2F26e59d3c6c35b406f845bd8b79c3c023545b6c62.png?generation=1591091025992800\&alt=media)

* 在 app store 中搜索 `incomming webhooks`，选择第一个

![incomming webhooks](https://962234814-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8oPJUWJoRcTOnREKHd%2Fsync%2F9114e0ecc0c3a590006605cfbfb780b63a60e1b5.png?generation=1591091026490179\&alt=media)

创建成功以后，拷贝 app webhook 地址，后面会用到。

Step2: 修改 prometheus rules，在 ANNOTATIONS 中添加特定字段。

v1.x rule 写法：

```
ALERT InstanceStatus
 IF up {job="node"}== 0
 FOR 15s
 LABELS {
   instance = "",
 }
 ANNOTATIONS {
   summary = "服务器运行状态",
   description = 服务器已当机超过多少时间",
   link="http://xxx",
   color="#xxx",
   username="@sjy"
 }
```

v2.x alert rule 写法：

```
- alert: InstanceStatus
  expr: up {job="node"} == 0
  for: 15s
  labels:
    instance: ""
  annotations:
    summary: "服务器运行状态"
    description: 服务器已当机超过多少时间"
    link: "http://xxx"
    color: "#xxx"
    username: "@sjy"
```

说明：我们在 rule 的 ANNOTATIONS 中添加了 link, color, username 三个字段，用它们来表示 `消息外链地址`，`消息颜色`和需要 `@` 的人。

Step3: 修改 Alertmanager 配置。

使用 `slack_configs` 来配置 slack 的告警接收渠道：

```
receivers:
  - name: 'slack'
    slack_configs:
      - api_url: "xxx"
        channel: "#test"
        text: "{{ range .Alerts }} {{ .Annotations.description}}\n {{end}} {{ .CommonAnnotations.username}} <{{.CommonAnnotations.link}}| click here>"
        title: "{{.CommonAnnotations.summary}}"
        title_link: "{{.CommonAnnotations.link}}"
        color: "{{.CommonAnnotations.color}}"
```

配置说明：

* 按 alertname 分组。
* slack\_configs 配置中，使用了 template 语句，通过 CommonAnnotations 查找字段。
* 插入外链不仅可以使用 title\_link, 还可以使用 slack link 标记语法 。

更多 slack 配置，请参考 [incoming-webhooks](https://api.slack.com/incoming-webhooks)。

经过以上配置，我们收到的消息是这样：

![slack.alert.demo](https://962234814-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8oPJUWJoRcTOnREKHd%2Fsync%2Ffa8178394eb0f8769c15ba916fc9bbb6f0e20324.png?generation=1591091026203347\&alt=media)

点击 title 或者 Click here，即可跳转到 Prometheus graph 页面：

![prometheus graph](https://962234814-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8oPJUWJoRcTOnREKHd%2Fsync%2Ff353fc9ad4efa2144ea02c0ebe20460008f2e0cb.png?generation=1591091025895383\&alt=media)

这样就很方便了，再也不用担心多个 Prometheus 节点，切换查询带来的烦恼。
