Prometheus 实战
  • 前言
  • 修订记录
  • 如何贡献
  • Prometheus 简介
    • Prometheus 是什么
    • 为什么选择 Prometheus
  • Prometheus 安装
    • 二进制包安装
    • Docker 安装
  • 基础概念
    • 数据模型
    • 指标类型
    • 作业与实例
  • PromQL
    • PromQL 基本使用
    • 与 SQL 对比
  • 数据可视化
    • Web Console
    • Grafana
    • Promlens
  • Prometheus 配置
    • 全局配置
    • 告警配置
    • 规则配置
    • 数据拉取配置
    • 远程可写存储
    • 远程可读存储
    • 服务发现
    • 配置样例
  • 服务发现
    • 静态服务发现
    • 文件服务发现
    • HTTP服务发现
    • Consul服务发现
    • moby服务发现
    • kubernetes服务发现
  • Exporter
    • 文本格式
    • Sample Exporter
    • Node Exporter 安装使用
    • Node Exporter 常用查询
    • 其他 Exporter 介绍
  • Pushgateway
    • Pushgateway 是什么
    • 如何使用 Pushgateway
  • 数据存储
    • Local Store
    • Remote Store
  • 告警/记录规则
    • 如何配置
    • 触发逻辑
  • Alertmanager
    • Alertmanager 是什么
    • 配置详情
    • 通过 Email 接收告警
    • 通过企业微信接收告警
    • 通过 Slack 接收告警
    • 通过 Webhook 接收告警
    • 其他告警接收方案
  • Prometheus 工具
    • Promtool 介绍和使用
    • Client SDK
  • Prometheus 性能调优
    • Metrics 仪表盘
    • 启动参数优化
    • 日志查询
  • Prometheus 与容器
    • Docker
    • Kubernetes
  • 高可用方案探讨
    • Prometheus Server 的高可靠
    • AlertManager 的高可靠
  • 实战练习
    • NodeExporter
    • 配置告警规则
    • Grafana 集成
    • Alertmanager 告警
  • 常见问题收录
    • 如何热加载新配置
    • 如何通过认证后拉取数据
Powered by GitBook
On this page
  • 目的
  • 准备工作
  • 配置步骤

Was this helpful?

  1. Alertmanager

通过 Slack 接收告警

Previous通过企业微信接收告警Next通过 Webhook 接收告警

Last updated 4 years ago

Was this helpful?

作为 IM 办公软件,简单好用,在国外用的特别多,那如何用它来接收 Prometheus 的告警信息,让咱们的运维看上去高大上。

目的

  • 使用 slack 接受消息。

  • 消息能够带有 url, 自动跳转到 prometheus 对应 graph 查询页面。

  • 能自定义颜色。

  • 能够 @ 某人

准备工作

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

配置步骤

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

  • 点击频道标题,选择 Add an app or integration

  • 在 app store 中搜索 incomming webhooks,选择第一个

创建成功以后,拷贝 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 标记语法 。

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

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

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

更多 slack 配置,请参考 。

incoming-webhooks
Slack
Add an app or integration
incomming webhooks
slack.alert.demo
prometheus graph