Sub topic 15
Writing Alerting Rules
Prerequisites and the alerting model
This chapter assumes that Prometheus is running, node_exporter is being scraped, and you can query metrics with PromQL. You should also understand labels and recording rules.
An alerting rule evaluates a PromQL expression at a regular interval. When the expression returns one or more series, Prometheus creates an alert for each returned series. The alert carries labels that identify its meaning and annotations that provide human-readable details.
An alert normally moves through these states:
| State | Meaning |
|---|---|
| Inactive | The expression is not currently true. |
| Pending | The expression is true, but its `for` duration has not elapsed. |
| Firing | The expression has remained true for the required duration. |
The for field prevents short-lived spikes from creating notifications. Prometheus evaluates rules according to its configured evaluation_interval.
flowchart TD
A[Evaluate PromQL expression] --> B{Does it return a series?}
B -->|No| C[Inactive]
B -->|Yes| D{Has for duration elapsed?}
D -->|No| E[Pending]
D -->|Yes| F[Firing]
E -->|Expression becomes false| C
F -->|Expression becomes false| CWrite a practical alert rule
Create a rule file such as /etc/prometheus/rules/node-alerts.yml. The following rules detect an unreachable node exporter and a filesystem with little free space.
groups:
- name: node-alerts
rules:
- alert: InstanceDown
expr: up{job="node"} == 0
for: 5m
labels:
severity: critical
annotations:
summary: "{{ $labels.instance }} is down"
description: "Prometheus cannot scrape node_exporter on {{ $labels.instance }}."
- alert: DiskSpaceLow
expr: node_filesystem_avail_bytes{fstype!~"tmpfs|overlay"} / node_filesystem_size_bytes{fstype!~"tmpfs|overlay"} < 0.10
for: 10m
labels:
severity: warning
annotations:
summary: "Less than 10% disk space free on {{ $labels.instance }}"
description: "{{ $labels.mountpoint }} has less than 10% free space."The alert field gives the alert a stable name. The expr field is the PromQL condition. Labels are machine-readable values used for grouping and routing. Annotations are explanatory text shown in a notification.
The up metric is created for every scrape target. It is 1 when the last scrape succeeded and 0 when it failed. A failed scrape can result from a stopped exporter, a firewall, an incorrect address, or a TLS or authentication problem.
The disk expression divides available bytes by total filesystem bytes. The filesystem matcher excludes virtual filesystems that often produce irrelevant alerts. Add more matchers if temporary mounts should not be monitored.
Validate and load rules
Check the file before reloading Prometheus:
promtool check rules /etc/prometheus/rules/node-alerts.ymlAdd the rule path under rule_files in prometheus.yml, then reload Prometheus using the method configured for your installation. In the Prometheus web interface, open Alerts to inspect inactive, pending, and firing alerts.
Common errors include invalid YAML indentation, a misspelled metric name, a selector that matches no series, and a threshold with the wrong unit. Test the expression in the Prometheus expression browser before adding for, labels, and annotations.
Exercise
Why can a disk alert remain pending even after its expression becomes true?
Show answer
The alert remains pending until its configured for duration elapses. If the expression becomes false first, Prometheus returns it to inactive.