> For the complete documentation index, see [llms.txt](https://pshizhsysu.gitbook.io/prometheus/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pshizhsysu.gitbook.io/prometheus/ff08-san-ff09-prometheus-gao-jing-chu-li/kuo-zhan-yue-du/alertmanagerde-api.md).

# AlertManager的API

AlertManager接收Prometheus的告警，并为告警发送通知，起着衔接的作用。要了解Prometheus与AlertManager的交互流程，了解AlertManager的API很有必要。

## 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 | -    |
| filter    | array\[string] | 无    | optional | -    |
| receiver  | string         | 无    | optional | -    |

返回值示例如下：

```
[
  { 
    "labels": {"label": "value", ...},
    "receiver": {"name": "string"},  # 注意与alert的receivers的区别
    "alerts": [alert1, alert2, ...]  # alert的Json结构与 `GET /api/v2/alerts` 返回值中的结构一致
  },
  ...
]
```
