Chapter 11 · Lesson 8 of 10
Monitoring and logs

Disk, memory and CPU alerts that matter (Prometheus rules)

The alerts worth having on a server are disk predicted to fill, low filesystem space, low available memory, OOM kills, sustained CPU and load, heavy swap and a node that stopped reporting, each with thresholds you tune.

By the end you will

Add a small set of Prometheus alert rules for disk, memory, CPU and node health, and tune each threshold to your server.

Time
25 min
Level
Intermediate
Check
4 questions
You need
  • Prometheus scraping node_exporter
  • Alert rules loaded as in the alerting lesson

Seven alerts cover most small servers: a disk predicted to fill, low filesystem free percent, low available memory, OOM kills, sustained high CPU with load, heavy swap use and a node that stopped reporting. The rules below are starting points. Every number depends on your hardware and workload, so tune each one after you have watched your own graphs for a while.

How it works#

Each rule is a PromQL expression plus a for duration, as explained in alerting without alert fatigue. The metrics come from node_exporter, set up in Prometheus and node exporter basics. Most of these are saturation signals, from what to monitor first, and they fail by running out of something. A fixed threshold is a guess about how much headroom you need, so treat the values below as defaults to change.

Step 1: Disk predicted to fill#

yaml
- alert: DiskWillFillIn24h
  expr: |
    predict_linear(node_filesystem_avail_bytes{mountpoint="/",fstype!~"tmpfs|overlay"}[6h], 24 * 3600) < 0
  for: 30m
  labels:
    severity: ticket
  annotations:
    summary: "{{ $labels.instance }} root disk predicted to fill within 24h"

predict_linear fits a line to the last six hours and projects it 24 hours ahead. If the projection is below zero, the disk will fill at the current rate. It handles the fast filler, such as runaway logs, that a plain percentage notices too late. Short windows react quickly but flap, long windows are steady but slow. When it fires, follow fix a full disk on a Linux server.

Step 2: Filesystem free percent#

yaml
- alert: DiskFreeLow
  expr: |
    node_filesystem_avail_bytes{mountpoint="/",fstype!~"tmpfs|overlay"}
      / node_filesystem_size_bytes{mountpoint="/",fstype!~"tmpfs|overlay"} < 0.10
  for: 15m
  labels:
    severity: page

This is the safety net. Ten percent is a conventional starting point. On a very large disk you can go lower, and on a small one you may want a higher figure, because the real question is how many gigabytes it takes to do your next deploy. Also watch inodes with node_filesystem_files_free if you store many small files.

Step 3: Memory available#

yaml
- alert: MemoryAvailableLow
  expr: node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes < 0.10
  for: 10m
  labels:
    severity: page

Use MemAvailable, which includes reclaimable cache, not MemFree. A server with 5 percent MemFree and 60 percent MemAvailable is healthy. Ten percent available for ten minutes means real pressure. See check disk, memory and CPU usage for the command-line view.

Step 4: OOM kills#

yaml
- alert: OomKillDetected
  expr: increase(node_vmstat_oom_kill[10m]) > 0
  labels:
    severity: page
  annotations:
    summary: "The kernel killed a process for memory on {{ $labels.instance }}"

The counter node_vmstat_oom_kill increases on every kill, so any increase means something died. It is available when node_exporter’s vmstat collector is on, which is the default on recent versions, but confirm it appears in your /metrics. You can also confirm from logs with journalctl -k | grep -i "out of memory". The fix path is fix out-of-memory kills. Containers can be killed by their own memory limit without the host running short, so check container logs too.

Step 5: Sustained CPU and load#

yaml
- alert: CpuBusyHigh
  expr: |
    100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 90
  for: 20m
  labels:
    severity: ticket

- alert: LoadHigh
  expr: node_load5 / count by (instance) (node_cpu_seconds_total{mode="idle"}) > 2
  for: 20m
  labels:
    severity: ticket

CPU at 100 percent for a minute is normal work. Twenty minutes means a runaway job or too little capacity. Load average divided by the number of cores shows how many tasks queue per core, and the right ceiling depends on the workload, so watch your normal range first. Use ticket severity: high CPU alone rarely means users are hurting.

Step 6: Swap and node down#

yaml
- alert: SwapHeavilyUsed
  expr: |
    (node_memory_SwapTotal_bytes - node_memory_SwapFree_bytes)
      / node_memory_SwapTotal_bytes > 0.5
  for: 30m
  labels:
    severity: ticket

- alert: NodeDown
  expr: up{job="node"} == 0
  for: 3m
  labels:
    severity: page

Small amounts of used swap are harmless, since the kernel parks idle pages there. Heavy use for a long time with low available memory signals thrashing. See add swap space on Ubuntu. NodeDown is the most important rule: if a host is unreachable, every other alert for it is silent. A few minutes of for avoids paging on a reboot. Add a rule for a division by zero case if SwapTotal is 0, because servers without swap return no data rather than 0.

Check it worked#

Run promtool check rules on the file, then trigger each alert safely: stop node_exporter for NodeDown, fill a scratch volume for the disk rules, or temporarily lower a threshold on a test machine. Confirm the message reaches your destination with a working runbook link, then restore the real value. Never test a memory alert by actually exhausting a production server.

Edge cases and gotchas#

  • Thresholds are starting points. A 16 GB box and a 512 MB box need different numbers. Tune from your own graphs.
  • Compare types carefully. predict_linear needs a range vector like [6h], while comparison uses an instant vector.
  • Page versus ticket. Only page for things that stop users soon: node down, disk nearly full, memory exhausted. The rest can open a ticket.
  • Maintenance and reboots. A for of a few minutes absorbs a normal reboot. Use silences for longer planned work.
  • Containers share host memory. Host alerts cannot see which container grew. Add container metrics, see container metrics with cAdvisor and docker stats.
  • Missing metrics mean silent rules. An expression with no data never fires. Alert on absent() or on up for important jobs.

Troubleshooting#

SymptomLikely causeFix
Rule never fires in a testMetric labels differ from the ruleRun the expression in the graph view and adjust labels
parse error: ... expected type range vectorMissing [6h] range on predict_linearAdd the range selector
Disk alert fires on /boot or tmpfsNo filesystem filterAdd mountpoint and fstype!~ filters
Constant DiskWillFillIn24h after a big deployShort-term growth skews the fitLengthen the window or the for time
Memory alert while free -h looks fineUsing MemFree instead of MemAvailableSwitch the metric
node_vmstat_oom_kill missingCollector disabled or old exporterCheck /metrics and the exporter flags
NodeDown fires on every rebootfor too shortRaise it a few minutes
No data for swap rulesServer has no swapRemove the rule or guard against zero

Quick reference#

output
predict_linear(node_filesystem_avail_bytes{mountpoint="/"}[6h], 24 * 3600) < 0
node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes < 0.10
increase(node_vmstat_oom_kill[10m]) > 0
up{job="node"} == 0

Next#

Add containers in container metrics with cAdvisor and docker stats.

Check what you learned4 questions
1Why does predict_linear often beat a fixed free-space percentage?
2Which metric tells you the kernel killed a process for memory?
3Why alert on sustained CPU instead of any CPU spike?
4What does up == 0 for 5m tell you?

Frequently asked questions

What is a good disk space alert threshold?

There is no universal number. A percentage such as 10 to 15 percent free is a common starting point, but a huge disk can run fine at 5 percent while a small one is in trouble at 20. Predicting time to full with predict_linear is more useful than a fixed percentage.

How do I alert when a disk will fill soon?

Use predict_linear on node_filesystem_avail_bytes over a few hours and alert when the prediction for the next day or so drops below zero, with a for duration so spikes do not fire it.

Which memory metric should an alert use?

node_memory_MemAvailable_bytes, compared to node_memory_MemTotal_bytes. MemFree ignores reclaimable cache and gives false alarms.

How do I detect OOM kills?

node_exporter has a counter, node_vmstat_oom_kill, that increases on each OOM kill. You can also search the kernel log for out of memory messages.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios