Skip to content

Prometheus alert rules and Alertmanager ​

Alerting in this stack has two halves. Prometheus decides when something is wrong by evaluating rules against its data. Alertmanager decides who hears about it: it groups alerts, repeats them on a schedule and sends them to a receiver.

Both halves are configured from files in the repository, and both run as services in the same compose file.

The path of an alert ​

  1. Prometheus loads every file matching /etc/prometheus/rules/*.yml. The compose file mounts ./prometheus/rules there.
  2. The node_alerts group is evaluated every 30 seconds.
  3. A rule whose expression stays true for its for duration starts firing.
  4. Prometheus sends firing alerts to alertmanager:9093, set under alerting in prometheus.yml.
  5. Alertmanager groups them and passes them to the default or critical receiver.

You can watch the first three steps at http://localhost:9090/alerts and the last two at http://localhost:9093.

The eight shipped rules ​

All rules live in prometheus/rules/node.yml.

AlertFires whenForSeverity
InstanceDownup == 0 for any scrape target1mcritical
HighCpuUsageCPU usage above 90%5mwarning
HighMemoryUsageMemory usage above 90%5mwarning
CriticalMemoryUsageMemory usage above 95%2mcritical
DiskSpaceLowRoot filesystem above 85% full5mwarning
DiskSpaceCriticalRoot filesystem above 95% full2mcritical
HighNetworkErrorsReceive plus transmit errors above 10 per second5mwarning
SystemLoad15-minute load average above 2x the CPU count5mwarning

InstanceDown covers all three scrape jobs, so it also tells you when cAdvisor or Prometheus itself stops answering. The other seven read Node Exporter metrics.

A rule looks like this:

yaml
- alert: DiskSpaceLow
  expr: 100 - ((node_filesystem_avail_bytes{mountpoint="/",fstype!="tmpfs"} / node_filesystem_size_bytes{mountpoint="/",fstype!="tmpfs"}) * 100) > 85
  for: 5m
  labels:
    severity: warning
  annotations:
    summary: "Low disk space on {{ $labels.instance }}"
    description: "Disk usage is {{ printf \"%.1f\" $value }}% on {{ $labels.instance }} ({{ $labels.mountpoint }})."

The summary uses the instance label, which is why it pays to rename the placeholder in prometheus.yml.

Add or change a rule ​

Edit node.yml or add a new .yml file next to it. Check it, then reload Prometheus:

bash
docker run --rm \
  -v "$PWD/prometheus.yml:/etc/prometheus/prometheus.yml:ro" \
  -v "$PWD/prometheus/rules:/etc/prometheus/rules:ro" \
  --entrypoint promtool prom/prometheus:v3.16.0 \
  check config /etc/prometheus/prometheus.yml

curl -X POST localhost:9090/-/reload

promtool check config also validates every rule file, so one command covers both.

How Alertmanager routes ​

alertmanager/alertmanager.yml defines one route with one child:

yaml
route:
  group_by: ['alertname', 'instance']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 12h
  receiver: 'default'

  routes:
    - match:
        severity: critical
      receiver: 'critical'
      repeat_interval: 1h
SettingValueEffect
group_byalertname, instanceOne notification per alert per server
group_wait30sWait before the first notification of a new group
group_interval5mWait before sending news about a group that already notified
repeat_interval12h, 1h for criticalHow often a still-firing alert is sent again
resolve_timeout5mWhen an alert that stopped arriving counts as resolved

There is also one inhibit rule. A firing critical alert mutes a warning that has the same alertname and the same instance. The shipped warning and critical pairs use different alert names, for example DiskSpaceLow and DiskSpaceCritical, so as written both of them notify. If you want the critical alert to silence its warning, give the pair one name with two severities, or change equal to ['instance'].

Add a receiver ​

Nothing is delivered out of the box

Both receivers are defined without any integration. The email and Slack blocks are commented out. Alerts reach Alertmanager and show in its UI, but no message is sent until you configure a receiver.

For email, uncomment the SMTP lines under global and the email_configs block of the receiver you want:

yaml
global:
  resolve_timeout: 5m
  smtp_smarthost: 'smtp.example.com:587'
  smtp_from: 'alertmanager@example.com'
  smtp_auth_username: 'alertmanager@example.com'
  smtp_auth_password: 'your-password'

receivers:
  - name: 'critical'
    email_configs:
      - to: 'oncall@example.com'
        send_resolved: true

The file also carries a commented webhook_configs block with a Slack URL. webhook_configs posts Alertmanager's own JSON format to any HTTP endpoint you run. A Slack incoming webhook expects Slack's message format, which is what Alertmanager's slack_configs integration sends:

yaml
receivers:
  - name: 'default'
    slack_configs:
      - api_url: 'https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK'
        channel: '#alerts'
        send_resolved: true

Validate the file and restart the service to load it:

bash
docker run --rm -v "$PWD/alertmanager:/cfg:ro" \
  --entrypoint amtool prom/alertmanager:v0.35.0 \
  check-config /cfg/alertmanager.yml

docker compose restart alertmanager

alertmanager.yml holds credentials once you fill it in. Keep your edited copy out of public forks.

MIT licensed. An independent project, not affiliated with Grafana Labs, the Prometheus project or Docker.