> For the complete documentation index, see [llms.txt](https://bohans.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bohans.gitbook.io/devops/prometheus/prometheus-gao-jing-chu-li/alertmanager/an-zhuang.md).

# 安装

> Alertmanager 和 Prometheus Server 一样均采用 Golang 实现，并且没有第三方依赖。

### **1. 获取并安装软件包**

Alertmanager 最新版本的下载地址可以从 Prometheus 官方网站 <https://prometheus.io/download/> 上获取。

{% code overflow="wrap" %}

```bash
export VERSION=0.15.2
curl -LO https://github.com/prometheus/alertmanager/releases/download/v$VERSION/alertmanager-$VERSION.darwin-amd64.tar.gz
tar xvf alertmanager-$VERSION.darwin-amd64.tar.gz
```

{% endcode %}

### **2. 创建 Alertmanager 配置文件**

Alertmanager 解压后会包含一个默认的 alertmanager.yml 配置文件，内容如下所示：

```yaml
route:
  group_by: ['alertname']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 1h
  receiver: 'web.hook'
receivers:
  - name: 'web.hook'
    webhook_configs:
      - url: 'http://127.0.0.1:5001/'
inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'dev', 'instance']
```

Alertmanager 的配置主要包含两个部分：**路由（route）**&#x548C;**接收器（receivers）**。

所有的告警信息从顶级路由开始，根据标签匹配规则进入到不同的子路由，并且根据子路由设置的接收器发送告警。目前配置文件中只设置了一个顶级路由，并且定义的接收器为 web.hook。因此，所有的告警都会发送给 web.hook。

### 3. 启动 Alertmanager

Alermanager 会将数据保存到本地中，默认的存储路径为`data/`，启动 Alertmanager 之前需要创建相应的目录：

```bash
mkdir data
./alertmanager
```

### 4. 查看运行状态

Alertmanager 启动后可以通过 9093 端口访问（[http://192.168.33.10:9093](http://192.168.33.10:9093/)

<figure><img src="https://2387758490-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FYztI6SlxGY9tfxfqvcLe%2Fuploads%2FpqIE5x2atcVHel9DRBdR%2Fimage.png?alt=media&amp;token=6f9321bd-d1e0-4d68-a4e6-86b5ee306b72" alt=""><figcaption></figcaption></figure>

* Alert 菜单下可以查看 Alertmanager 接收到的告警内容。
* Silences 菜单下则可以通过 UI 创建静默规则。
* 进入 Status 菜单，可以看到当前系统的运行状态以及配置信息。

### 5. 关联 Prometheus 与 Alertmanager

编辑 Prometheus 配置文件 prometheus.yml，并添加以下内容：

```yaml
alerting:
  alertmanagers:
    - static_configs:
        - targets: ['localhost:9093']
```

> 重启 Prometheus 服务，从 <http://192.168.33.10:9090/config> 可以查看 alerting 配置是否生效。
