为了账号安全,请及时绑定邮箱和手机立即绑定

通过 API 快速创建 AlertManager silence

标签:
Kubernetes

概述

通常我们要 silence 某个 AlertManager 的 alert 时,需要通过 UI 界面操作,如下图:

AlertManager silence

效率有点低,而且不够自动化,那么是否可以有一种办法快速创建 AlertManager silence 呢?

– 有的,通过 API.

API Payload

v1

如下:

{
    "matchers": [
        {
            "name": "alername1",
            "value": ".*",
            "isRegex": true
        }
    ],
    "startsAt": "2022-04-29T22:12:33.533Z",
    "endsAt": "2022-04-29T23:11:44.603Z",
    "createdBy": "api",
    "comment": "Silence",
    "status": {
        "state": "active"
    }
}

v2

{
    "matchers": [
        {
            "name": "service",
            "value": "rancher",
            "isRegex": false,
            "isEqual": true
        },
        {
            "name": "alertname",
            "value": "TargetDown",
            "isRegex": false,
            "isEqual": true
        }
    ],
    "startsAt": "2022-04-29T10:11:35.656Z",
    "endsAt": "2022-04-29T12:11:35.656Z",
    "createdBy": "Casey Cui",
    "comment": "配置错误导致的误报",
    "id": null
}

具体实现

curl 实现

📝 Notes:

以 v1 api 为例

如下:

curl https://alertmanager.ewhisper.cn/api/v1/silences -d '{
      "matchers": [
        {
          "name": "alername1",
          "value": ".*",
          "isRegex": true
        }
      ],
      "startsAt": "2022-04-29T22:12:33.533Z",
      "endsAt": "2022-04-29T23:11:44.603Z",
      "createdBy": "api",
      "comment": "Silence",
      "status": {
        "state": "active"
      }
}'

📝Notes:
在 K8S 集群内,地址一般为:http://alertmanager:9093

python 实现

如下:

#!/usr/bin/python3
import requests
import socket
import datetime
import time

res = requests.post("http://alertmanager:9093/api/v2/silences", json={
    "matchers": [
        {"name": "job", "value": "myjob", "isRegex": False},
        {"name": "instance", "value": "{}:1234".format(socket.gethostname()), "isRegex": False},
        ],
    "startsAt": datetime.datetime.utcfromtimestamp(time.time()).isoformat(),
    "endsAt": datetime.datetime.utcfromtimestamp(time.time() + 4*3600).isoformat(),
    "comment": "Backups on {}".format(socket.gethostname()),
    "createdBy": "My backup script",
    },
    )
res.raise_for_status()
silenceId = res.json()["silenceID"]

移除 silence 的脚本:

res = requests.delete("http://alertmanager:9093/api/v2/silence/{}".format(silenceId))
res.raise_for_status()

EOF

三人行, 必有我师; 知识共享, 天下为公. 本文由东风微鸣技术博客 EWhisper.cn 编写.

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消