Monitoring setup with docker-compose - Part 3: Alertmanager
Tags: docker, monitoring, prometheus
Published: 2021-10-27 11:47:00


## In short: What is Alertmanager?
The [Alertmanager](https://prometheus.io/docs/alerting/latest/alertmanager/) sends alerts to various channels like Slack or E-Mail.
Recall from [part one](https://dashdashverbose.medium.com/monitoring-setup-with-docker-compose-part-1-prometheus-3d2c9089ee82) that Prometheus creates an alert if something violates a rule.

You can use the Alertmanager to silence and group alerts as well.

## Configuration

You can get all source code from [GitHub](https://github.com/ablx/monitoring_stack). Check out the tag `part-2-grafana` if you want to follow along.

### docker-compose.yml

First of all, add the Alertmanager and a volume to `docker-compose.yml`:

```yml

  alertmanager:
    image: prom/alertmanager:v0.23.0
    restart: unless-stopped
    ports:
      - "9093:9093"
    volumes:
      - "./alertmanager:/config"
      - alertmanager-data:/data
    command: --config.file=/config/alertmanager.yml --log.level=debug


volumes:

  alertmanager-data:

```

Alertmanager will persist silence configurations to the volume.

### alertmanager/alertmanager.yml

This configuration contains information about which channels to send to. For simplicity, we use E-Mail. Refer to [the Alertmanager docs](https://prometheus.io/docs/alerting/latest/configuration/#configuration-file) to learn about other channels.


Create a folder `alertmanager` and add a file `alertmanager.yml` to it:
```yaml
route:
  receiver: 'mail'
  repeat_interval: 4h
  group_by: [ alertname ]


receivers:
  - name: 'mail'
    email_configs:
      - smarthost: 'smtp.gmail.com:465'
        auth_username: 'your_mail@gmail.com'
        auth_password: ""
        from: 'your_mail@gmail.com'
        to: 'some_mail@gmail.com'
```

The `route` section configures which alerts will be sent. In our case, we sent all alerts. You could add more routes and filter, for example, on alert tags (see the [example](https://prometheus.io/docs/alerting/latest/configuration/#example) in the docs.).

`receivers` configures our target channels. Note how `route` refers to the receiver `mail` on line two.


### Connect to prometheus
Finally, we need to tell Prometheus about the Alertmanager.

Open `prometheus/prometheus.yml` and add the following:

```yml
alerting:
  alertmanagers:
    - scheme: http
      static_configs:
        - targets: [ 'alertmanager:9093' ]

```

## Testing

Run `docker-compose up`. Open [http://localhost:9093]() in your browser to see the Alertmanager UI.

![Alertmanager after start](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1w8e9kneiev3escmfa2.png)

After a couple of minutes, the test alert fires. You can check this in [your Prometheus instance](http://localhost:9000/alerts).

![Prometheus with active alert](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nmayhplesq4glmsyjnx6.png)

Now, you can see the alert in the [http://localhost:9093](Alertmanager) as well:

![Alertmanager with alert](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o5fj09wi5uzch714l8ff.png)


Check your inbox for the notification:
![Alert mail](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9kno20xhfq8ox8hrb00v.png)


## Final note

This series shows how to set up a basic Prometheus stack. Keep in mind that this is not production-ready! But it is enough to get your hands dirty.


**If this article was helpful for your, please consider to buy me a coffee :-)**
[![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/Y8Y1EKYJI)