# 屏蔽告警通知

## 抑制规则

抑制是指当警报发出后，停止重复发送由此警报引发其他错误的警报的机制。比如说，当主机Down掉后会导致该主机上的其他服务也Down掉；那么当AlertManager发送了一个“主机Down掉”的告警通知后，再接收到该主机上其他服务Down的告警，应该不再为这些告警发送通知。

在Alertmanager配置文件中，使用`inhibit_rules`定义一组告警的抑制规则：

```
inhibit_rules:
  [ - <inhibit_rule> ... ]
```

每一条抑制规则的具体配置如下：

```
target_match:
  [ <labelname>: <labelvalue>, ... ]
target_match_re:
  [ <labelname>: <regex>, ... ]

source_match:
  [ <labelname>: <labelvalue>, ... ]
source_match_re:
  [ <labelname>: <regex>, ... ]

[ equal: '[' <labelname>, ... ']' ]
```

比如定义如下的抑制规则：

```
- source_match:
    alertname: NodeDown
  target_match_reg:
    severity: ~"middle|low"
  equal:
    - node
```

如果接收到以下的“主机Down”告警，并且为该告警发送了一个通知

```
{alertname="NodeDown",node="x.x.x.x",...} time annotation
```

那么alertmanager就会创建一条抑制规则实例：

```
{node="x.x.x.x",serverity=~"middle|low"}
```

当接收到以下一条告警，该告警的标签匹配上了上面的抑制规则实例，那么该告警就会被抑制

```
{alertname="MysqlDown",node="x.x.x.x",serverity="middle",...} time annotation
```

**疑问：如果主机**`x.x.x.x`**一段时间后又起来了，那么alertmanager如何对其抑制的告警恢复发送呢？**

## 临时静默

除了基于抑制机制可以控制告警通知的行为以外，用户或者管理员还可以直接通过Alertmanager的UI临时屏蔽特定的告警通知。通过定义标签的匹配规则(字符串或者正则表达式)，如果新的告警通知满足静默规则的设置，则停止向receiver发送通知。

进入Alertmanager UI，点击"New Silence"显示如下内容，用户可以通过该UI定义新的静默规则的开始时间以及持续时间，通过Matchers部分可以设置多条匹配规则(字符串匹配或者正则匹配)。填写当前静默规则的创建者以及创建原因后，点击"Create"按钮即可：

![](/files/-MA1FfEldyp1im5Aht1Q)

静默规则创建成功后，Alertmanager会开始加载该规则并且设置状态为Pending，不过很快规则就会变成Active状态，我们可以通过页面查看：

![](/files/-MA1FfEmciG3hYERZj3q)

上面的规则设置了两小时的持久时间，过了两个小时后，该规则就会变成Expired状态。

接着，我们拉搞主机的CPU使用率，触发告警，然后我们在prometheus里看到告警已经firing了

![](/files/-MA1FfEnRImQ_rMghlAp)

但是我们在alertmanager的alert页面却看不到有alert

![](/files/-MA1FfEou4qbn1bEQcWs)

我们点击进入这个静默规则的详情里面，可以看到它“影响”到的告警

![](/files/-MA1FfEp1F06BCGImUvi)

如果我们想让这条静默规则失效，手动点击“Expire”即可：

![](/files/-MA1FfEqG7IUElXphB3U)


---

# 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/ping-bi-gao-jing-tong-zhi.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.
