Alert Manager

Prometheus decides when an alert fires; Alertmanager decides what happens: who gets notified, how, and how often. It also groups related alerts into one notification, and can silence alerts during maintenance.

Create a directory for its configuration:

mkdir -p ~/volumes/alertmanager

Add a configuration file. Alertmanager can send notifications by email, Slack, PagerDuty, webhooks and more. Here it sends email, so use the same SMTP settings as the application’s contact form:

~/volumes/alertmanager/alertmanager.ymlyaml
global:
  smtp_smarthost: 'smtp.example.com:587'
  smtp_from: 'Alertmanager <alerts@exampleforyou.net>'
  smtp_auth_username: 'USERNAME'
  smtp_auth_password: 'PASSWORD'
  smtp_require_tls: true

route:
  group_by: ['alertname']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 3h
  receiver: admins

receivers:
  - name: 'admins'
    email_configs:
      - to: 'admin@exampleforyou.net'
  • route sends every alert to the admins receiver. group_by puts alerts with the same name into one email, group_wait waits 30 seconds to collect related alerts before the first email, and repeat_interval resends every three hours while an alert is still firing.
  • receivers lists who gets notified, and how.

Check the configuration with amtool, which is in the image:

podman run --rm --entrypoint amtool -v ~/volumes/alertmanager:/etc/alertmanager:Z \
  docker.io/prom/alertmanager:v0.35.1 check-config /etc/alertmanager/alertmanager.yml

Create a volume for its data (it remembers silences and which notifications it has sent):

podman volume create alertmanager-vol

And run it:

podman run -d --name alertmanager --network monitoring-network -p 9093:9093 \
  -v ~/volumes/alertmanager:/etc/alertmanager:Z \
  -v alertmanager-vol:/alertmanager \
  docker.io/prom/alertmanager:v0.35.1

Alertmanager has a web interface for viewing alerts and creating silences at http://localhost:9093.

Now tell Prometheus where to send alerts, in prometheus.yml, using the container’s name:

~/volumes/prometheus/prometheus.ymlyaml
alerting:
  alertmanagers:
    - static_configs:
        - targets: ['alertmanager:9093']
podman restart prometheus

All being well, any alert that fires in Prometheus now appears in Alertmanager, and if the SMTP settings are right, you’ll receive an email.

Here’s the complete prometheus.yml at this point:

~/volumes/prometheus/prometheus.ymlyaml
global:
  scrape_interval: 15s
  evaluation_interval: 15s
  external_labels:
    environment: dev

rule_files:
  - /etc/prometheus/rules/*.yml

alerting:
  alertmanagers:
    - static_configs:
        - targets: ['alertmanager:9093']

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['prometheus:9090']
  - job_name: 'node_exporter'
    static_configs:
      - targets: ['host.containers.internal:9100']
  - job_name: 'fastapi'
    static_configs:
      - targets: ['host.containers.internal:8000']