Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 65Observability · Alertmanager

How to send alerts to Slack or email

Graphs do not wake anybody up. Prometheus decides that something is wrong (an alert rule), Alertmanager decides who hears about it and how — email, Slack, Teams — groups many alerts into one message, and lets you silence them during maintenance. This how-to writes one alert rule, routes critical alerts to Slack and the rest to email, sends a test alert, and fixes the "the alert fired but nobody got a message" problem.

  • 5 Lessen
  • 3 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

65.1

The quick answer

/etc/alertmanager/alertmanager.yml
global:
  smtp_smarthost: "smtp.example.com:587"
  smtp_from: "alerts@example.com"
  smtp_auth_username: "alerts@example.com"
  smtp_auth_password_file: /etc/alertmanager/smtp_password
route:
  receiver: ops-email
  group_by: ["alertname", "instance"]
  routes:
    - matchers: ['severity="critical"']
      receiver: oncall-slack
receivers:
  - name: ops-email
    email_configs:
      - to: "ops@example.com"
        send_resolved: true
  - name: oncall-slack
    slack_configs:
      - api_url_file: /etc/alertmanager/slack_url
        channel: "#oncall"
        send_resolved: true
amtool check-config /etc/alertmanager/alertmanager.yml
systemctl reload alertmanager
amtool --alertmanager.url=http://127.0.0.1:9093 alert add TestAlert severity=critical instance=web1 --annotation=summary="Test from amtool"

65.2

Step by step

  1. Write the secrets into files (not into the YAML, so the YAML can live in Git):
install -m 600 -o alertmanager /dev/null /etc/alertmanager/slack_url
install -m 600 -o alertmanager /dev/null /etc/alertmanager/smtp_password

Put the Slack incoming webhook URL (Slack app → Incoming Webhooks → Add to channel) into slack_url, and the SMTP password into smtp_password.

  1. An alert rule in Prometheus — with for: (no message for a 10-second blip) and a clear summary:
/etc/prometheus/rules/web.yml
groups:
  - name: web
    rules:
      - alert: SiteDown
        expr: probe_success{job="blackbox"} == 0
        for: 2m
        labels:
          severity: critical
        annotations:
          summary: "{{ $labels.instance }} does not answer"
promtool check rules /etc/prometheus/rules/web.yml
systemctl reload prometheus

(probe_success comes from the blackbox exporter; for servers use up == 0.)

  1. Routing (quick answer): everything goes to ops-email by default; severity="critical" goes to Slack. group_by makes one message per alert name and server, not one per alert.
  2. Test before you trust it — ask where an alert would go, then send a real test alert:
amtool config routes test --config.file=/etc/alertmanager/alertmanager.yml severity=critical alertname=SiteDown
oncall-slack
amtool --alertmanager.url=http://127.0.0.1:9093 alert add TestAlert severity=critical instance=web1 --annotation=summary="Test from amtool"

The Slack message should arrive within about 30 seconds (group_wait).

  1. Silence during maintenance — always with an end time and a reason:
amtool --alertmanager.url=http://127.0.0.1:9093 silence add instance=web1 --duration=2h --comment="Patch window OPS-311"
  1. Good alerts: alert on what users feel (site down, errors, slow) and on what will break soon (disk in 4 hours) — not on every CPU spike. Every critical alert needs a short runbook link in its annotations.

65.3

Check that it worked

The test alert arrives in Slack (critical) and a warning test arrives by email; when the test alert expires you get the resolved message too. In Prometheus → Alerts your rules are listed (inactive = green).

65.4

Common problems

ProblemCauseFix
The rule fires, no message arrivesPrometheus does not know Alertmanager, or the route sends it elsewhere.Check alerting: in prometheus.yml; amtool config routes test ….
Slack: invalid_token / 404Wrong or deleted webhook URL.Make a new incoming webhook; write it to slack_url.
Email: 535 authentication failed / TLS errorsSMTP login or port.Port 587 + smtp_require_tls; check user and password file.
Too many messages (alert storm)No grouping, rules without for:, warnings for everything.group_by, for:, inhibit rules; delete noisy rules.
Same alert every few minutesrepeat_interval too short.4h for warnings, 1h for critical.
No "resolved" messagesend_resolved not set.Add send_resolved: true to the receiver.

65.5

Go further

1Why does an alert rule need for: 2m?

So a short blip does not page anybody; the condition must be true for 2 minutes first.

2How do you check where an alert would be sent, without sending it?

Alle commando's in dit hoofdstuk