Sub topic 1
Why monitor with metrics
Prerequisites
This chapter assumes you can use the Linux command line, edit configuration files, and start, stop, and inspect a systemd service with systemctl. You should also know YAML syntax, the idea of a TCP port, and basic HTTP requests. No PromQL is needed yet.
How the pieces fit together
Prometheus is a time-series database. It collects numbers on a schedule by pulling them from HTTP endpoints. Each server runs an exporter, a small program that publishes its metrics on a port. Prometheus scrapes those endpoints, stores the samples, and evaluates alerting rules. Alertmanager receives fired alerts and routes them to people or tools. Grafana reads from Prometheus and draws dashboards.
Metrics, labels, and time series
A metric is a named number. A time series is one metric with one set of label values, recorded over time. Labels split a metric into separate series:
node_cpu_seconds_total{cpu="0", mode="idle"}
node_cpu_seconds_total{cpu="0", mode="user"}These are two series. Each combination of label values creates a new series, so high-cardinality labels such as user IDs can overload Prometheus.
Counters, gauges, histograms, and summaries
- Counter: a value that only increases and resets when the process restarts. Read it with
rate(). - Gauge: a value that moves up and down, such as memory in use.
- Histogram: counts observations in buckets, so you can estimate percentiles on the server side.
- Summary: computes quantiles inside the exporter. You cannot combine summary quantiles across instances.
Exposing host metrics with node_exporter
node_exporter is the exporter that reports Linux host metrics. Create a dedicated system user so the service does not run as root:
sudo useradd --system --no-create-home --shell /usr/sbin/nologin node_exporterCopy the nodeexporter binary to `/usr/local/bin/nodeexporter`, then check the endpoint:
curl -s http://localhost:9100/metrics | head -n 5Running as systemd services
systemd starts services at boot and restarts them after failures. Save this unit as /etc/systemd/system/node_exporter.service:
[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.targetThen load and start it:
sudo systemctl daemon-reload
sudo systemctl enable --now node_exporter
systemctl status node_exporter --no-pagerExercise
Why does the unit file set User=node_exporter instead of running the binary as root?
Show answer
Running as an unprivileged user limits what the process can access if it is compromised, and the exporter only needs read access to system metrics.