# 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` 返回值中的结构一致
  },
  ...
]
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pshizhsysu.gitbook.io/prometheus/ff08-san-ff09-prometheus-gao-jing-chu-li/kuo-zhan-yue-du/alertmanagerde-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
