Ravindra BagaleCourses & study guides मराठी

Chapter 13: Monitoring Basics

DevOps

13.5 Node Exporter — expose host metrics

Why: the problem

A Flipkart Big Billion Days sale starts at midnight. At 12:05 a.m. a web server slows down. The on-call engineer opens Grafana but the graph for that server is empty. Why? Node Exporter on that server was never started after the last reboot. No exporter, no numbers, no graph. Node Exporter is small, but everything depends on it. You must know how to install it the right way so it starts after every reboot, how to read its raw page, which metrics matter, and how to add your own custom numbers through the textfile collector, for example "last backup time". That is what this lesson covers.

What we will build

You will run Node Exporter in three ways: as a Docker container, as a systemd service from the Ubuntu package, and with the textfile collector to publish your own number (the time of the last successful backup). You will also learn to read the raw /metrics page and pick the ten metrics that matter most.

What: every term in plain English

  • /metrics page — A plain text web page with one metric per line: name, labels, value.
  • HELP and TYPE lines — Comments above each metric that explain it and say if it is a counter or gauge.
  • Collector — A part of Node Exporter that reads one area, like cpu, meminfo, filesystem.
  • Textfile collector — Reads .prom files from a folder, so your scripts can publish custom metrics.
  • systemd service — A program Linux starts at boot and restarts if it dies.
  • Label — A key=value tag on a metric, like mountpoint="/".
  • Cardinality — How many different label combinations a metric has. Too many is slow.
  • Unix timestamp — Seconds since 1 Jan 1970. Prometheus uses it for times.
Install the package Check it starts at boot Read the raw page Find the important ones Turn on the textfile folder
Node Exporter: host to /metrics to Prometheus

How: step by step

  1. Install the package

    sudo apt-get install -y prometheus-node-exporter
    

    What it does: Installs the binary and a systemd unit.

    Output

    Created symlink ... prometheus-node-exporter.service
    
  2. Check it starts at boot

    systemctl is-enabled prometheus-node-exporter
    

    What it does: enabled means it comes back after reboot.

    Output

    enabled
    
  3. Read the raw page

    curl -s localhost:9100/metrics | head -12
    

    What it does: HELP, TYPE and value lines.

    Output

    # HELP go_gc_duration_seconds A summary of ...
    # TYPE go_gc_duration_seconds summary
    
  4. Find the important ones

    curl -s localhost:9100/metrics | grep -E "^node_(load1|memory_MemAvailable_bytes|filesystem_avail_bytes)"
    

    What it does: Load, free memory and free disk.

    Output

    node_load1 0.18
    node_memory_MemAvailable_bytes 2.56e+09
    
  5. Turn on the textfile folder

    See Example 3
    

    What it does: Lets scripts add custom metrics.

    Output

    (no output)
    

Example 1: Node Exporter as a Docker container on any Linux host

Stack. Docker, prom/node-exporter

Goal. Node Exporter as a Docker container on any Linux host.

Commands

sudo docker run -d --name node-exporter --restart unless-stopped \
  --net host --pid host \
  -v /:/host:ro,rslave \
  prom/node-exporter:v1.8.2 --path.rootfs=/host
sudo docker logs node-exporter 2>&1 | tail -3
curl -s localhost:9100/metrics | grep -c "^node_"

What each line does

  • --restart unless-stopped — Docker starts it again after a reboot or crash.
  • --net host — Uses the host network, so it sees real interfaces and listens on 9100 directly.
  • --pid host — Sees the host processes.
  • -v /:/host:ro,rslave — Mounts the whole host disk read only, so disk numbers are real.
  • --path.rootfs=/host — Tells the exporter where the host root is.

Green output

ts=2026-10-25T16:20:01Z level=INFO msg="Listening on" address=[::]:9100
ts=2026-10-25T16:20:01Z level=INFO msg="TLS is disabled."
1187

Expected result. The exporter is up on port 9100 and shows about 1,200 node metrics.

Example 2: Read the ten metrics that matter on the raw page

Stack. Node Exporter, Bash

Goal. Read the ten metrics that matter on the raw page.

top10.sh

#!/usr/bin/env bash
M=$(curl -s localhost:9100/metrics)
for name in node_load1 node_cpu_seconds_total node_memory_MemTotal_bytes \
  node_memory_MemAvailable_bytes node_filesystem_avail_bytes \
  node_filesystem_size_bytes node_network_receive_bytes_total \
  node_network_transmit_bytes_total node_disk_io_time_seconds_total \
  node_boot_time_seconds; do
  echo "== $name"
  echo "$M" | grep "^$name" | grep -v -E "tmpfs|lo\"|loop" | head -2
done

What each line does

  • node_load1 — 1-minute load average.
  • node_cpu_seconds_total — CPU seconds in each mode (idle, user, system, iowait). A counter.
  • node_memory_MemAvailable_bytes — Memory apps can still use.
  • node_filesystem_avail_bytes / size_bytes — Free and total disk bytes per mount.
  • node_network_*_bytes_total — Bytes in and out. Counters, so use rate().
  • node_disk_io_time_seconds_total — Time the disk was busy. rate() of it near 1 means 100% busy.
  • node_boot_time_seconds — When the server last booted. Helps spot surprise reboots.
  • grep -v -E "tmpfs|..." — Hides noisy temporary filesystems and the loopback interface.

Green output

== node_load1
node_load1 0.18
== node_cpu_seconds_total
node_cpu_seconds_total{cpu="0",mode="idle"} 251034.12
node_cpu_seconds_total{cpu="0",mode="iowait"} 210.4
== node_memory_MemAvailable_bytes
node_memory_MemAvailable_bytes 2.56e+09
== node_filesystem_avail_bytes
node_filesystem_avail_bytes{device="/dev/xvda1",fstype="ext4",mountpoint="/"} 6.4e+09
== node_boot_time_seconds
node_boot_time_seconds 1.7611e+09

Expected result. You can now read the raw page like a doctor reads a report. These ten metrics power most host dashboards.

Example 3: Publish your own metric: last backup time, with the textfile collector

Stack. Node Exporter textfile collector, MySQL, cron

Goal. Publish your own metric: last backup time, with the textfile collector.

Turn on the folder (Ubuntu package)

sudo mkdir -p /var/lib/node_exporter/textfile
echo 'ARGS="--collector.textfile.directory=/var/lib/node_exporter/textfile"' \
  | sudo tee /etc/default/prometheus-node-exporter
sudo systemctl restart prometheus-node-exporter

backup.sh

#!/usr/bin/env bash
set -e
DIR=/var/lib/node_exporter/textfile
mysqldump --all-databases | gzip > /backup/db-$(date +%F).sql.gz
# write to a temp file first, then move, so Prometheus never reads half a file
echo "backup_last_success_timestamp_seconds $(date +%s)" > $DIR/backup.prom.tmp
mv $DIR/backup.prom.tmp $DIR/backup.prom

PromQL: hours since last backup

(time() - backup_last_success_timestamp_seconds) / 3600

What each line does

  • --collector.textfile.directory=... — Node Exporter reads every .prom file in this folder.
  • set -e — Stop the script on the first error, so a failed dump never writes a success time.
  • date +%s — Current time as a Unix timestamp.
  • mv ... backup.prom — Moving is atomic: the file is complete or not there.
  • time() - ... — Seconds since the last good backup. Divide by 3600 for hours.

Green output

$ curl -s localhost:9100/metrics | grep backup_
backup_last_success_timestamp_seconds 1.7614068e+09
Prometheus query result:
{instance="10.0.1.12:9100", job="node"}  2.1

Expected result. The last backup was 2.1 hours ago. Later you can alert if this goes above 26 hours. HDFC and many banks watch backups exactly this way.

Verify: did it work?

  1. Is it running and enabled?

    systemctl is-active prometheus-node-exporter && systemctl is-enabled prometheus-node-exporter
    

    What it does: Both must say yes.

    Output

    active
    enabled
    
  2. Does it survive a reboot?

    sudo reboot ; then after login: curl -s localhost:9100/metrics | grep -c "^node_"
    

    What it does: Count must be above 0.

    Output

    1187
    

When it is red 1

listen tcp :9100: bind: address already in use

Cause. Two exporters run: the package and the Docker one.

Fix, step by step

  1. See who uses it: sudo ss -ltnp | grep 9100
  2. Keep one. Stop the other: sudo systemctl disable --now prometheus-node-exporter or docker rm -f node-exporter.
  3. Check again with curl.

When it is red 2

Disk numbers show the container disk, not the host

Cause. The container started without the host root mount.

Fix, step by step

  1. Add -v /:/host:ro,rslave and --path.rootfs=/host.
  2. Recreate the container.
  3. Compare with df -h on the host.

When it is red 3

textfile: failed to read textfile collector directory

Cause. The folder is missing or the service user cannot read it.

Fix, step by step

  1. sudo mkdir -p /var/lib/node_exporter/textfile
  2. Make the folder owned by the exporter user: sudo chown -R prometheus /var/lib/node_exporter
  3. Restart the service.

Cleanup

  1. Remove the Docker exporter

    sudo docker rm -f node-exporter
    

    What it does: Stops and removes the container.

    Output

    node-exporter
    
  2. Remove the package

    sudo apt-get remove -y prometheus-node-exporter
    

    What it does: Uninstalls the service.

    Output

    Removing prometheus-node-exporter ...
    

Ravindra Bagale's Tip

If the exporter is down, the graph is empty. After reboot, check it still starts: systemctl is-enabled.

Practice tasks

Easy Count metric families

Why: Knowing the size of the page helps capacity planning.

What to do: Count the TYPE lines on the page.

How:
  1. curl -s localhost:9100/metrics | grep -c "^# TYPE"

Check: You get a number around 300.

Medium Disable a noisy collector

Why: Some collectors make many useless series.

What to do: Start the exporter with --no-collector.wifi and --no-collector.infiniband.

How:
  1. Add them to ARGS.
  2. Restart and count lines again.

Check: Fewer lines, same useful data.

Hard Cron health metric

Why: Silent cron jobs fail for months unnoticed.

What to do: Make every cron job write a success timestamp like the backup example.

How:
  1. One .prom file per job.
  2. A PromQL query per job for hours since success.

Check: You see the age of every job in one table.

Easy Say Node Exporter in two lines

Why: If you can say it simply, you understand it. Juniors and interviewers both want the short version first.

What to do: Write two short lines about Node Exporter for a fresher who knows only Linux, Git and Docker basics.

How:
  1. Write the problem it solves.
  2. Write the one command, query or config line you use for it.
  3. Read it aloud to a friend.

Check: A fresher understands it without asking a question.

Easy Keep a throwaway monitoring lab

Why: Practice needs a lab you can break. If it gets messy, you delete the containers and start again in one minute.

What to do: Run Node Exporter in Docker and read its metrics page.

How:
  1. docker run -d --name ne -p 9100:9100 prom/node-exporter
  2. curl -s localhost:9100/metrics | grep node_load1
  3. docker rm -f ne

Check: You see a line that starts with node_load1 and a number.

Medium Do it on a second stack

Why: Monitoring ideas must work on Prometheus and on CloudWatch. Only the tool changes, not the thinking.

What to do: Repeat this lesson on a different stack than the one you used first.

How:
  1. If you used Prometheus, try the CloudWatch console or AWS CLI.
  2. Keep the same question: is it up, is it fast, is it failing, is it full?
  3. Compare the two graphs side by side.

Check: Both tools answer the same question with a number and a graph.

Medium Break it on purpose

Why: You must know the red error by face. During an outage there is no time to read slowly.

What to do: Cause one error from the "When it is red" part of this lesson and fix it.

How:
  1. Follow the error section.
  2. Copy the exact message into your notes.
  3. Write the fix in one line.

Check: You fixed it using only the steps, without searching the web.

Medium Write a one-page runbook

Why: At 2 a.m. nobody remembers PromQL. A short page saves the on-call person.

What to do: Write a one-page runbook for this lesson.

How:
  1. Title, when the alert fires, the graph to open, the exact commands, how to check, how to undo.

Check: A teammate can follow it alone, without calling you.

Hard Explain it like an interview

Why: Interviewers want real stories, not definitions.

What to do: Record a 60 second answer about this lesson.

How:
  1. Say the problem, the metric you watched, the number you saw and the fix.

Check: Your answer has a real metric name and a real number.

Hard Draw the flow top to bottom

Why: A drawing shows where a number is born and where it ends up.

What to do: Draw this lesson as boxes from top to bottom.

How:
  1. Start at the server or app, go through the exporter, Prometheus or CloudWatch, then Grafana, then the alert on your phone.

Check: Each box matches a real command or file in this lesson.

Homework: try at home

Try at home

At home, publish a custom metric for "number of logged-in users" with the textfile collector. Use who | wc -l in a script run by cron every minute, and query it in Prometheus.

Recap

  • Node Exporter shows about 1,000 host metrics on port 9100.
  • Install it as a systemd service or a Docker container with host mounts.
  • Ten metrics cover most needs: load, CPU, memory, disk, network, boot time.
  • The textfile collector publishes your own numbers.
  • Make sure it starts after every reboot.

FAQ

Do I need Node Exporter in Kubernetes?

Yes, as a DaemonSet, one pod on each node. kube-prometheus-stack does it for you.

Does Node Exporter use much CPU?

Very little, usually under 1% and about 20 MB RAM.

Can it monitor processes?

Only totals. For per-process use process-exporter.

Is it safe to open 9100 publicly?

No. It shows details about your server. Allow only Prometheus.

What about Windows?

windows_exporter is the Windows version.

Interview questions

  1. What is Node Exporter?

    A Prometheus exporter that exposes Linux host metrics like CPU, memory, disk and network on port 9100.

  2. How do you expose a custom metric from a shell script?

    Use the textfile collector: write name value lines to a .prom file in the configured folder, atomically with mv.

  3. How do you run it in Docker correctly?

    With --net host, --pid host and the host root mounted read only plus --path.rootfs.

  4. How would you monitor backup jobs?

    Write a last-success timestamp metric and alert when time() minus it is too large.

  5. What is high cardinality?

    A metric with too many label values, like user id as a label. It makes Prometheus slow and uses lots of memory.

Quick quiz

Q1. Which collector lets you add custom numbers?

The textfile collector.

Q2. Why write to a .tmp file and then mv?

So Prometheus never reads a half-written file.

Q3. Which metric shows free memory for apps?

node_memory_MemAvailable_bytes.

Q4. Why use --pid host?

So the exporter sees the host processes.

Q5. How do you check the service starts at boot?

systemctl is-enabled prometheus-node-exporter.

Did you understand? Let us try it once more.

Ravi Sir's Cards for this lesson

मराठी cards