企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
[TOC] ## **API Version** AlertManager有两套API,v1与v2,不过两套API的内部逻辑基本是一致的,调用哪套都没有关系。v1没有相关的文档,不过我们可以找到v2的相关文档。 API-v2的swagger文件的链接为: https://github.com/prometheus/alertmanager/blob/master/api/v2/openapi.yaml 把这个文件的内容拷贝到 https://editor.swagger.io 里面,便可以查看API。下面罗列了v2版本的所有API: ``` # Alert GET /api/v2/alerts POST /api/v2/alerts # AlertGroup GET /api/v2/alerts/groups # General GET /api/v2/status # Receiver GET /api/v2/receivers # Silence GET /api/v2/silences POST /api/v2/silences GET /api/v2/silence/{silenceID} DELETE /api/v2/silence/{silenceID} ``` 其中最重要的是Alert与AlertGroup的那三个API,接下来我们详细地讲解一下 ## **`POST /api/v2/alerts`** Body参数示例如下: ``` [ { "labels": {"label": "value", ...}, "annotations": {"label": "value", ...}, "generatorURL": "string", "startsAt": "2020-01-01T00:00:00.000+08:00", # optional "endsAt": "2020-01-01T01:00:00.000+08:00" # optional }, ... ] ``` Body参数是一个数组,里面是一个个的告警。其中startsAt与endsAt是可选参数,且格式必须是上面的那种,不能是时间戳。 ## **`GET /api/v2/alerts`** Query参数如下,以下参数用来过滤告警 | 参数名 | 类型 | 默认值 | 是否必须 | 其他说明 | | --- | --- | --- | --- | --- | | active | bool | true | optional | - | | silenced | bool | true | optional | - | | inhibited | bool | true | optional | - | | unprocessed | bool | true | optional | - | | filter | array[string] | 无 | optional | - | | receiver | string| 无 | optional | - | 其返回值如下: ``` [ { "labels": {"label": "value", ...}, "annotations": {"label": "value", ...}, "generatorURL": "string", "startsAt": "2020-01-01T00:00:00.000+08:00", "endsAt": "2020-01-01T01:00:00.000+08:00", "updatedAt": "2020-01-01T01:00:00.000+08:00", "fingerprint": "string" "receivers": [{"name": "string"}, ...], "status": { "state": "active", # active, unprocessed, ... "silencedBy": ["string", ...], "inhibitedBy": ["string", ...] }, ... ] ``` ## **`GET /api/v2/alerts/groups`** Query参数如下,以下参数用来过滤告警 | 参数名 | 类型 | 默认值 | 是否必须 | 其他说明 | | --- | --- | --- | --- | --- | | active | bool | true | optional | - | | silenced | bool | true | optional | - | | inhibited | bool | true | optional | - | | unprocessed | bool | true | optional | - | | filter | array[string] | 无 | optional | - | | receiver | string| 无 | optional | - | 其返回值如下: ``` [ { "labels": {"label": "value", ...}, "receiver": {"name": "string"}, # 注意与alert的receivers的区别 "alerts": [alert1, alert2, ...] # alert的Json结构与 `GET /api/v2/alerts` 返回值中的结构一致 }, ... ] ```