「Alertmanager」- 发送测试通知(=> ALERTING/Clients)

问题描述

当进行 Alertmanager 设置之后,我们希望能够发送告警消息进行测试,以验证配置已生效。但是我们又不能直接关闭服务等待 Prometheus 触发告警消息 :-)

解决方案

好在 Alertmanager 提供告警接口,允许我们以 HTTP 协议进行调用,来要求其发送告警信息。 当 Alertmanager 启动后,将监听 HTTP 9093 端口,Prometheus 将通过 HTTP 调用该接口来发送告警消息参数。而除此之外,我们能构建自己的告警参数,来发送自定义的 HTTP 请求以产生告警消息。

补充说明:
通过调用 API 告警的方式,仅作为补充手段,如果需要进行 Prometheus 告警,官方建议通过 Alert Rule 实现,不建议实现额外的客户端。

更多内容,参考 Clients | Prometheus 文档

告警脚本:send-alerts.sh

该部分将演示,在 Shell 中,调用 Alertmanager 服务来发送通知的方法,以及相关问题的处理方法。

下面是发送告警的简单示例:

#!/usr/bin/env bash

alerts_message='[
  {
    "labels": {
       "alertname": "DiskRunningFull",
       "dev": "sda1",
       "instance": "example1",
       "msgtype": "testing"
     },
     "annotations": {
        "info": "The disk sda1 is running full",
        "summary": "please check the instance example1"
      }
  },
  {
    "labels": {
       "alertname": "DiskRunningFull",
       "dev": "sda2",
       "instance": "example1",
       "msgtype": "testing"
     },
     "annotations": {
        "info": "The disk sda2 is running full",
        "summary": "please check the instance example1",
        "runbook": "the following link http://test-url should be clickable"
      }
  }
]'

curl -XPOST -d"$alerts_message" http://127.0.0.1:9093/api/v1/alerts

参考文献

Clients | Prometheus
send a dummy alert to prometheus-alertmanager · GitHub
alertmanager/send_alerts.sh at master · prometheus/alertmanager · GitHub