Sub topic 16
Routing alerts with Alertmanager
This chapter assumes you can write alerting rules in Prometheus (the previous chapter), edit YAML, and manage systemd services. It covers what Alertmanager does with alerts after Prometheus fires them: grouping, routing, inhibition, silencing, and the timers that control notification frequency.
What Alertmanager is for
Prometheus evaluates alerting rules, but it does not send emails or chat messages. It pushes firing alerts to Alertmanager, a separate service that listens on port 9093. Alertmanager then decides who is told, when, and how often.
Separating the two keeps rule evaluation simple. Prometheus only answers "is this expression true?". Alertmanager handles the problems that follow:
- Duplicates. Prometheus re-sends each firing alert on every evaluation cycle. Alertmanager collapses these into one.
- Floods. If a rack loses power, fifty alerts fire at once. Alertmanager can send one notification that lists all fifty.
- Wrong audience. Disk warnings may go to a chat channel, while a dead server should page someone.
- Noise during maintenance. You can mute alerts temporarily without editing rules.
Connecting Prometheus to Alertmanager
The alerting block in prometheus.yml (shown in the scrape configuration chapter) already points at localhost:9093. Run Alertmanager as a systemd service, as you did for node_exporter, with its own unprivileged user and --config.file=/etc/alertmanager/alertmanager.yml. Check the connection in the Prometheus web interface under Status, then Alertmanagers.
The route tree
The route block in alertmanager.yml is a tree. Every alert enters at the root and follows the first child route whose matchers fit. If no child matches, the root's receiver handles it. Children inherit the root's settings unless they override them.
route:
receiver: team
group_by: ['alertname', 'instance']
group_wait: 30s
group_interval: 5m
repeat_interval: 4h
routes:
- matchers:
- severity="critical"
receiver: oncall
repeat_interval: 1h
receivers:
- name: team
webhook_configs:
- url: http://localhost:5001/alerts
- name: oncall
webhook_configs:
- url: http://localhost:5002/alerts
inhibit_rules:
- source_matchers:
- severity="critical"
target_matchers:
- severity="warning"
equal: ['instance']With the rules from the previous chapter, InstanceDown carries severity: critical and goes to oncall. DiskSpaceLow carries severity: warning, matches no child, and goes to team.
The matchers list uses the same operators as PromQL label matchers: =, !=, =~, and !~. Quote the value. Older guides use match and match_re, which are deprecated.
Grouping and the three timers
group_by lists the labels that define a group. All alerts sharing those label values are delivered in one notification. Grouping by alertname and instance gives one message per alert type per server. Three timers then control delivery:
| Setting | Meaning | Effect of a larger value |
|---|---|---|
| `group_wait` | Delay before the first notification for a new group | More alerts are bundled into the first message, but the first message arrives later |
| `group_interval` | Wait before notifying about new alerts added to a group that was already notified | Fewer follow-up messages |
| `repeat_interval` | Wait before re-sending a notification for alerts that are still firing | Less repeated noise, but a longer gap before a reminder |
Total delay from a fault to a message is the rule's for duration, plus up to one evaluation interval, plus group_wait. With for: 5m and group_wait: 30s, expect roughly five and a half minutes.
A group_by: ['...'] value disables grouping entirely, so each alert is sent separately. Avoid it except in very small setups.
Inhibition
An inhibition rule suppresses some alerts while others are firing. In the example above, a firing critical alert silences warnings that share the same instance label. When a server is down, you do not also want a disk warning from it.
The equal list is essential. Without it, any critical alert anywhere would suppress every warning everywhere.
One caveat: this rule is broad. A critical InstanceDown hides a warning from that instance, which is usually what you want. A critical alert about one service would also hide an unrelated warning on the same host. If that matters, add a more specific label to the matchers, such as alertname="InstanceDown" in source_matchers.
Silences
A silence mutes alerts matching a set of matchers for a fixed time. It is stored in Alertmanager, not in the config file, so it needs no reload. You create one in the web interface or with amtool, the command-line client that ships with Alertmanager:
amtool --alertmanager.url=http://localhost:9093 silence add \
alertname=DiskSpaceLow instance=server1.example.com:9100 \
--duration=2h --author=admin --comment="Resizing volume"Silenced alerts still fire in Prometheus. Alertmanager only withholds the notification. Always give a duration and a comment so others know why it exists.
Validating and testing
Check the file before reloading. A mistake in the config can leave Alertmanager running the old configuration, or fail to start after a restart.
amtool check-config /etc/alertmanager/alertmanager.yml
amtool config routes test --config.file=/etc/alertmanager/alertmanager.yml severity=criticalThe second command prints the receiver that an alert with the given labels would reach. Here it should print oncall. Reload with systemctl reload alertmanager if your unit defines ExecReload, or send SIGHUP to the process. Alertmanager also accepts POST /-/reload.
To see what a receiver actually gets, run this small listener. It prints each notification as formatted JSON:
import json
import sys
from http.server import BaseHTTPRequestHandler, HTTPServer
class Handler(BaseHTTPRequestHandler):
def do_POST(self):
length = int(self.headers.get("Content-Length", 0))
payload = json.loads(self.rfile.read(length))
print(json.dumps(payload, indent=2), flush=True)
self.send_response(200)
self.end_headers()
port = int(sys.argv[1]) if len(sys.argv) > 1 else 5001
HTTPServer(("127.0.0.1", port), Handler).serve_forever()Run it as python3 listener.py 5001. The payload contains status (firing or resolved), groupLabels, and an alerts array with each alert's labels and annotations. By default, webhook_configs has send_resolved: true, so you also get a message when the problem clears.
Common errors
- No notification arrives. Check the Alerts page in Prometheus first. An alert in
pendingstate has not yet satisfiedfor. If it isfiring, check the Alertmanager web interface at port 9093. If the alert is missing there, thealertingblock or network path is wrong. - Alert reaches the wrong receiver. Routing stops at the first matching child unless that child sets
continue: true. Reorder routes from most specific to least specific. - Labels do not match. Matchers apply to the labels on the alert, not to the rule's annotations. Labels from the series, such as
instance, are carried onto the alert unless the rule'slabelsblock overrides them. - Constant repeats.
repeat_intervalis too short, or alerts flap becauseforis too small.
Exercise
A critical InstanceDown alert and a warning DiskSpaceLow alert both fire for server1.example.com:9100. Using the configuration in this chapter, which notifications are sent, and to which receivers? What changes if you remove equal: ['instance'] from the inhibition rule?
Show answer
Only the InstanceDown notification is sent, to oncall, because the critical alert inhibits the warning that has the same instance label. The DiskSpaceLow notification is withheld. If you remove equal, the critical alert would inhibit every warning on every server, not only those from server1, so disk warnings elsewhere would also be suppressed.