Chapter 11 · Lesson 5 of 10
Monitoring and logs

Prometheus and node_exporter basics for a Linux server

Prometheus pulls metrics over HTTP from exporters such as node_exporter on port 9100, stores them as time series and lets you query them with PromQL.

By the end you will

Run node_exporter, scrape it from Prometheus, validate the config and write your first PromQL queries.

Time
30 min
Level
Intermediate
Check
4 questions
You need
  • An Ubuntu or Debian server with sudo
  • Basic familiarity with YAML and systemd

How Prometheus and Grafana work together

Prometheus pulls and stores metrics from exporters, and Grafana turns PromQL queries into dashboards.
  1. Prometheus pulls the /metrics page from node_exporter on a fixed interval.
  2. node_exporter answers with current CPU, memory, disk and network numbers as text.
  3. Grafana sends PromQL queries to Prometheus for each panel on a dashboard.
  4. You see the stored time series as graphs, and Prometheus keeps the history.
Static diagram
  1. Prometheusscrapenode_exporter
  2. node_exportermetricsPrometheus
  3. GrafanaPromQLPrometheus
  4. GrafanadashboardYou

Prometheus is a metrics database that pulls numbers from small HTTP endpoints called exporters. On a Linux server, node_exporter listens on port 9100 and publishes CPU, memory, disk and network figures. You tell Prometheus where to scrape in prometheus.yml, validate it with promtool check config, confirm the target is up, and then query it with PromQL.

How it works#

Prometheus uses a pull model. Every scrape interval it sends an HTTP request to each target’s /metrics page, parses the text and stores the values with a timestamp. Because Prometheus initiates the request, a dead target is obvious: the scrape fails and the built-in up metric for it becomes 0.

Metrics have names and labels, for example node_filesystem_avail_bytes{mountpoint="/"}. Counters such as node_cpu_seconds_total only increase, so you apply rate() to get a per-second value. Gauges such as available bytes go up and down, so you read them directly. The picture in the diagram above shows the flow: exporter, Prometheus, then Grafana for graphs, which is the next lessons’ topic. For which numbers matter first, see what to monitor first.

Step 1: Run node_exporter#

On the server to monitor, install the packaged exporter:

bash
sudo apt update
sudo apt install prometheus-node-exporter
systemctl status prometheus-node-exporter
curl -s http://127.0.0.1:9100/metrics | head

The Debian and Ubuntu package runs it as a systemd service. The last command prints plain text lines starting with # HELP and # TYPE. Alternatively you can download a release binary from the Prometheus project and write your own unit; the package is simpler. See run a service with systemd if you go that route.

By default it listens on all interfaces. If Prometheus runs on the same host, restrict it to localhost by setting the listen address in /etc/default/prometheus-node-exporter:

output
ARGS="--web.listen-address=127.0.0.1:9100"

Then sudo systemctl restart prometheus-node-exporter. If Prometheus is on another machine, bind to a private address and allow only that machine in the firewall.

Step 2: Install Prometheus and configure a scrape#

bash
sudo apt install prometheus

Edit /etc/prometheus/prometheus.yml to a minimal config:

yaml
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ["127.0.0.1:9090"]

  - job_name: node
    static_configs:
      - targets: ["127.0.0.1:9100"]

Each job_name groups targets, and static_configs lists addresses. For more servers, add their private addresses to the node targets list. The packaged install may already include a similar file, so edit rather than replace blindly, and keep a copy of the original.

Step 3: Validate and reload#

bash
promtool check config /etc/prometheus/prometheus.yml
sudo systemctl restart prometheus

promtool prints SUCCESS if the file parses. Always validate before restarting, because a syntax error can leave Prometheus down. A configured reload without a restart is possible when the web lifecycle option is on, but a restart is fine on a small box.

Step 4: Check the targets page#

Open http://127.0.0.1:9090/targets, through an SSH tunnel if the server is remote:

bash
ssh -L 9090:127.0.0.1:9090 deploy@203.0.113.10

Each target shows UP or DOWN with the last error. A target in DOWN with connection refused means nothing listens at that address. Port 9090 and 9100 expose detailed data without authentication, so never publish them to the internet. See publish container ports safely for the equivalent rule with containers.

Step 5: First PromQL queries#

In the expression browser at /graph, try:

output
up
rate(node_cpu_seconds_total{mode="idle"}[5m])
node_filesystem_avail_bytes{mountpoint="/"}
node_memory_MemAvailable_bytes

up lists every target with 1 or 0. The rate(...) query gives idle CPU seconds per second for each core, so a value near 1 means that core is idle. Disk space on / is in bytes, and memory available is the number that matters for memory pressure, since free memory alone ignores reclaimable cache. Divide to get percentages, which the next lesson uses in Grafana dashboards for CPU, memory and disk.

Check it worked#

  • curl -s http://127.0.0.1:9100/metrics | grep node_load1 prints a value.
  • promtool check config ends with SUCCESS.
  • The targets page lists both jobs as UP.
  • The query up returns 1 for each job.
  • From another machine, curl --max-time 5 http://203.0.113.10:9100/metrics fails, which is what you want.

Edge cases and gotchas#

  • Public exposure. Prometheus and exporters have no login by default. Bind to localhost or a private address and use the firewall. See set up a firewall with ufw.
  • Counters need rate. Graphing a raw counter shows a line that only rises. Wrap it in rate() or increase().
  • Retention fills the disk. Prometheus keeps data on local disk for a limited period. Watch its data directory and set retention with its startup flags.
  • Scrape interval versus rate window. The window in rate(x[5m]) should cover several scrapes. A window shorter than two scrape intervals returns nothing.
  • Containers hide the host. node_exporter inside a container sees the container’s view unless you configure host mounts. Run it on the host for host metrics.
  • Clock skew. Wrong server time makes graphs look gappy. Keep time sync enabled.

Troubleshooting#

SymptomLikely causeFix
Target shows connection refusedExporter not running or wrong portsystemctl status prometheus-node-exporter and check the address
Target shows context deadline exceededNetwork blocked or exporter slowTest with curl --max-time 5 from the Prometheus host
promtool: command not foundTool not on PATHUse the full path or install the prometheus package
yaml: unmarshal errorsIndentation or a wrong keyFix the line reported by promtool check config
Query returns No dataMetric name wrong or no scrape yetAutocomplete the name and wait one interval
Prometheus will not startInvalid config or permissionsjournalctl -u prometheus -n 50
Graph has gapsTarget down or scrape too slowCheck up over time
bind: address already in use on 9100Another exporter runningSee fix port already in use

Quick reference#

bash
sudo apt install prometheus prometheus-node-exporter
promtool check config /etc/prometheus/prometheus.yml
sudo systemctl restart prometheus
curl -s http://127.0.0.1:9100/metrics | head
output
up
rate(node_cpu_seconds_total{mode="idle"}[5m])
node_filesystem_avail_bytes{mountpoint="/"}

Next#

Make the numbers visible in Grafana dashboards for CPU, memory and disk.

Check what you learned4 questions
1In the pull model, who starts the connection that moves metrics?
2Which command validates prometheus.yml before reloading?
3What does rate(node_cpu_seconds_total[5m]) calculate?
4Why must port 9100 stay off the public internet?

Frequently asked questions

What does it mean that Prometheus uses a pull model?

Prometheus fetches metrics from each target on a schedule, by requesting an HTTP page such as /metrics. Targets do not push. A target that stops answering shows up as down.

What port does node_exporter use?

Port 9100 by default. Prometheus itself uses 9090. Neither should be reachable from the public internet.

How do I check my Prometheus config for errors?

Run promtool check config /etc/prometheus/prometheus.yml. It parses the file and reports problems before you reload Prometheus.

What does up mean in PromQL?

up is a metric Prometheus records for every target. It is 1 when the last scrape worked and 0 when it failed, so up == 0 finds broken targets.

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