Sub topic 10
Using rate and aggregation for CPU, memory, and disk
Assume Prometheus is scraping node_exporter, and you know basic PromQL syntax and metric labels. The examples use the instance label to identify each server.
The working model
A Prometheus time series is a stream of timestamped values. A counter increases over time, such as CPU seconds used. A gauge represents a current value, such as available memory.
rate() converts a counter into an average per-second change over a time range. Aggregation combines several series into a useful result. For example, a server usually has one CPU series for each CPU core, so you can average those series by instance.
CPU utilisation with `rate`
node_cpu_seconds_total is a counter. It records how many seconds each CPU spent in a mode such as idle, user, or system.
The idle rate is the fraction of time the CPUs were idle during the last five minutes. Subtracting it from one gives utilisation:
100 * (1 - avg by (instance) (
rate(node_cpu_seconds_total{mode="idle"}[5m])
))The [5m] range gives rate() enough samples to smooth short spikes. avg by (instance) removes the cpu label while retaining one result per server.
Memory utilisation with gauges
Memory metrics are gauges, so do not apply rate() to them. Use the current available and total values:
100 * (
1 - node_memory_MemAvailable_bytes
/ node_memory_MemTotal_bytes
)The result is a percentage. MemAvailable estimates memory that applications can use without swapping, so it is usually more useful than simply subtracting a field named free.
Disk utilisation and filesystem selection
To calculate the fraction of space available on each filesystem:
node_filesystem_avail_bytes{fstype!~"tmpfs|overlay"}
/
node_filesystem_size_bytes{fstype!~"tmpfs|overlay"}The !~ matcher excludes filesystem types matching the regular expression. Virtual filesystems can otherwise create noisy panels or misleading alerts. To show used space as a percentage, subtract the result from one and multiply by 100.
| Goal | Metric operation | Important labels |
|---|---|---|
| CPU percentage per server | `rate()` followed by `avg by (instance)` | `instance`, `cpu`, `mode` |
| Memory percentage | Divide gauges | `instance` |
| Disk free fraction | Divide gauges after filtering | `instance`, `mountpoint`, `device` |
Common errors
- Applying
rate()to a gauge produces a meaningless result. - Averaging disk percentages across mountpoints hides a full filesystem. Keep
mountpointin the result. - Omitting
{mode="idle"}from the CPU query mixes all CPU modes and can produce an incorrect value. - Using a short range such as
[30s]can make results unstable when scrapes are infrequent. - Comparing vectors with mismatched labels can return no data. Check the labels on both sides when a division is empty.
Exercise
Why does the CPU query apply rate before averaging by instance?
Show answer
Each CPU core exposes a cumulative counter. Rate converts each counter into a recent utilisation fraction, and averaging then combines the per-core fractions into one value for the server.