Sub topic 5
Exposing Linux host metrics with node_exporter
This chapter assumes you can edit configuration files on a Linux server, manage services with systemd, read basic YAML, and use curl against an HTTP port. You should also know in general terms what CPU, memory, disk, and network usage mean. By the end you will know what node_exporter collects, how to install and check it, and how to read its output.
What node_exporter is
node_exporter is a small program written in Go that reads kernel and hardware statistics from a Linux host. It publishes them over HTTP in the Prometheus text format. It is the standard exporter for host-level metrics. An exporter is a process that translates data from some other system into metrics that Prometheus can scrape.
On Linux, node_exporter mostly reads the virtual filesystems /proc and /sys. These are interfaces the kernel provides. It does not store anything and does not push data anywhere. Each time Prometheus requests /metrics, the exporter reads the current values and returns them.
The diagram below shows the path of one scrape.
Installing the binary
The project publishes prebuilt binaries on its GitHub releases page. Many distributions also package it, for example as prometheus-node-exporter on Debian and Ubuntu. A manual install gives you control over the version and the path used by the unit file in the next chapter.
Download the release archive for your architecture, check its checksum against the published sha256sums.txt, and unpack it. Replace the version number below with the current release.
VERSION=1.8.2
cd /tmp
curl -LO https://github.com/prometheus/node_exporter/releases/download/v${VERSION}/node_exporter-${VERSION}.linux-amd64.tar.gz
curl -LO https://github.com/prometheus/node_exporter/releases/download/v${VERSION}/sha256sums.txt
sha256sum --check --ignore-missing sha256sums.txt
tar xzf node_exporter-${VERSION}.linux-amd64.tar.gz
sudo install -o root -g root -m 0755 \
node_exporter-${VERSION}.linux-amd64/node_exporter /usr/local/bin/node_exporterThen create a system user that cannot log in. The exporter needs no special privileges for most collectors, so it should not run as root.
sudo useradd --system --no-create-home --shell /usr/sbin/nologin node_exporterRunning it once by hand
Before you write a service file, run the binary in a terminal to confirm it works.
/usr/local/bin/node_exporterBy default it listens on port 9100 on all interfaces. In a second terminal, request the metrics.
curl -s http://localhost:9100/metrics | head -n 20You will see lines that begin with # HELP and # TYPE, followed by samples. The following lines are typical.
# HELP node_load1 1m load average.
# TYPE node_load1 gauge
node_load1 0.42
# HELP node_cpu_seconds_total Seconds the CPUs spent in each mode.
# TYPE node_cpu_seconds_total counter
node_cpu_seconds_total{cpu="0",mode="idle"} 183422.17
node_cpu_seconds_total{cpu="0",mode="user"} 2310.55Stop the process with Ctrl+C when you are done.
Running it under systemd
The unit below runs the exporter as the unprivileged user, restarts it after a failure, and starts it at boot. The next chapter covers service management in more detail. It is shown here so you have a working exporter to inspect.
[Unit]
Description=Prometheus Node Exporter
After=network-online.target
Wants=network-online.target
[Service]
User=node_exporter
ExecStart=/usr/local/bin/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.targetSave it as /etc/systemd/system/node_exporter.service, then load and start it.
sudo systemctl daemon-reload
sudo systemctl enable --now node_exporter
systemctl status node_exporter --no-pagerReading the output
Every metric family in the output has three parts: a # HELP line, a # TYPE line, and one or more samples. A sample is a metric name, optional labels in braces, and a value. The metric type tells you how to query it. Counters only go up (apart from resets), so you take a rate() of them. Gauges can move in either direction, so you read them directly.
The most useful families for a server dashboard are listed below.
| Metric | Type | Meaning |
|---|---|---|
| `node_cpu_seconds_total` | counter | CPU time per core and mode (`idle`, `user`, `system`, `iowait`, and others) |
| `node_memory_MemTotal_bytes` | gauge | Total physical memory |
| `node_memory_MemAvailable_bytes` | gauge | Memory available to new workloads without swapping |
| `node_filesystem_avail_bytes` | gauge | Space available to unprivileged users on a filesystem |
| `node_filesystem_size_bytes` | gauge | Total size of a filesystem |
| `node_disk_io_time_seconds_total` | counter | Time the disk spent doing I/O |
| `node_network_receive_bytes_total` | counter | Bytes received per interface |
| `node_load1`, `node_load5`, `node_load15` | gauge | Load averages |
Some details are easy to get wrong:
- CPU values are cumulative seconds, not percentages. A percentage appears only after you apply
rate()in PromQL. MemAvailableis a better measure of free memory thanMemFree. Linux uses spare memory for caches, soMemFreeis usually low on a healthy host.node_filesystem_avail_bytesexcludes blocks reserved for root, whilenode_filesystem_free_bytesincludes them. Alert onavail.- Virtual filesystems such as
tmpfsandoverlayappear alongside real disks. Queries usually filter them out withfstype!~"tmpfs|overlay".
Collectors
node_exporter is organised into collectors. Each collector covers one area, such as cpu, meminfo, filesystem, or netdev. Many are enabled by default. Others are disabled because they are expensive or rarely needed. The --help output lists all of them.
/usr/local/bin/node_exporter --help 2>&1 | grep -E 'collector\.'You turn a collector on with --collector.<name> and off with --no-collector.<name>. For example, the systemd collector is off by default. It reports the state of each unit and is useful for alerting on failed services. The textfile collector reads .prom files from a directory, which lets cron jobs and scripts publish their own metrics.
Flags go on the ExecStart line. This example enables two optional collectors and limits the listening address.
ExecStart=/usr/local/bin/node_exporter \
--collector.systemd \
--collector.textfile.directory=/var/lib/node_exporter/textfile \
--web.listen-address=127.0.0.1:9100After editing the unit file, run sudo systemctl daemon-reload and sudo systemctl restart node_exporter.
Common problems
| Symptom | Likely cause | What to check | Column |
|---|---|---|---|
| `curl` gets "connection refused" | The service is not running, or it listens on another address | `systemctl status node_exporter` and `ss -tlnp \ | grep 9100` |
| `curl` works locally but Prometheus cannot reach it | Firewall or a loopback-only listen address | Firewall rules and the `--web.listen-address` value | |
| Service fails with status 203/EXEC | Wrong path in `ExecStart`, or the binary is not executable | `ls -l /usr/local/bin/node_exporter` | |
| A metric you expect is missing | The collector is disabled, or the kernel does not provide the data | The `--help` output and the exporter startup log | |
| Filesystem metrics are missing for a mount | The mount is excluded by the default filesystem exclusions | Collector flags for the `filesystem` collector |
The startup log shows which collectors were enabled.
journalctl -u node_exporter -n 30 --no-pagerWhere this fits
The exporter only makes data available. Nothing is recorded until Prometheus scrapes it, which you configure in a later chapter. For now, the goal is a host that answers on /metrics with the families above.
Exercise
On a server running nodeexporter, `curl -s localhost:9100/metrics | grep nodecpusecondstotal shows values such as 183422.17 for mode="idle"`. Why is this not a CPU percentage, and which PromQL function turns it into one?
Show answer
The metric is a counter of cumulative seconds the CPU has spent in each mode since boot, so it only grows. It becomes a utilisation figure when you take its per-second rate with rate() over a window, for example rate(node_cpu_seconds_total{mode="idle"}[5m]), and then subtract the idle share from 1 and multiply by 100.
Exercise
You want to alert on low disk space. Should you use node_filesystem_free_bytes or node_filesystem_avail_bytes, and why? Which filesystem types should the query usually exclude?
Show answer
Use node_filesystem_avail_bytes. It counts only the space available to unprivileged users, while free_bytes also includes blocks reserved for root, which makes a nearly full disk look healthier than it is. Exclude virtual filesystems such as tmpfs and overlay, for example with fstype!~"tmpfs|overlay".
Exercise
A teammate says node_exporter should listen on 0.0.0.0:9100 so that "anyone can check it". Give one reason this is risky and one way to reduce the risk.
Show answer
The endpoint has no authentication and reveals host details such as mounts, kernel version, and network interfaces. To reduce the risk, bind it to a private address with --web.listen-address, or use a firewall rule that allows only the Prometheus server to reach port 9100.