通过 API 快速创建 AlertManager silence

简介: 通过 API 快速创建 AlertManager silence


概述

通常我们要 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"
    }
}
JSON

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
}
JSON

具体实现

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"
      }
}'
BASH

📝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"]
PYTHON

移除 silence 的脚本:

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

EOF


相关文章
|
1天前
|
Prometheus 监控 Kubernetes
Prometheus Operator 与 kube-prometheus 之二 - 如何监控 1.23+ kubeadm 集群
Prometheus Operator 与 kube-prometheus 之二 - 如何监控 1.23+ kubeadm 集群
|
1天前
|
监控 JavaScript 前端开发
Grafana 系列 - 统一展示 -6-Zabbix 仪表板
Grafana 系列 - 统一展示 -6-Zabbix 仪表板
|
1天前
|
JSON Prometheus Cloud Native
Grafana 系列 -Loki- 基于日志实现告警
Grafana 系列 -Loki- 基于日志实现告警
|
1天前
|
Kubernetes 安全 Linux
Cilium 系列 -1-Cilium 特色 功能及适用场景
Cilium 系列 -1-Cilium 特色 功能及适用场景
|
1天前
|
Kubernetes 网络协议 Linux
Cilium 系列 -2-Cilium 快速安装
Cilium 系列 -2-Cilium 快速安装
|
11月前
|
JSON NoSQL 应用服务中间件
podman REST API 的images操作
podman REST API 的images操作
127 0
|
运维 Kubernetes 中间件
开发 k8s 管理平台 - k8sailor 04. 使用 gin 创建第一个 API 接口
开发 k8s 管理平台 - k8sailor 04. 使用 gin 创建第一个 API 接口
213 0
开发 k8s 管理平台 - k8sailor 04. 使用 gin 创建第一个 API 接口
|
XML JSON API
API参考—实例管理—DeleteDBInstance
调用DeleteDBInstance接口释放实例。
|
XML JSON API
API参考—实例管理—DescribeDBInstances
调用DescribeDBInstances接口查看实例列表详情。
151 0
|
JSON API 开发工具
API参考—实例管理—CreateDBInstance
API参考—实例管理—CreateDBInstance
125 0