Sub topic 2
Configuring Prometheus to scrape targets
This chapter assumes you have Prometheus and nodeexporter installed, that nodeexporter listens on port 9100, and that you can edit YAML files and use systemd.
Configuring Prometheus to scrape targets
Prometheus pulls metrics from each target on a schedule. You list the targets in prometheus.yml:
global:
scrape_interval: 15s
scrape_configs:
- job_name: node
static_configs:
- targets: ['server1.example.com:9100', 'server2.example.com:9100']Each job_name groups targets that expose the same kind of metrics. Reload Prometheus after you edit the file, for example with sudo systemctl restart prometheus.
Verifying targets and fixing failed scrapes
Open http://localhost:9090/targets to see each target's state. The up metric also reports this: 1 means the last scrape succeeded and 0 means it failed.
- Confirm the host and port are reachable with
curl http://server1.example.com:9100/metrics. - Check that the firewall allows port 9100 from the Prometheus server.
- Read the error column on the Targets page for timeouts or HTTP status codes.
Selecting series with PromQL
A time series is one metric name plus a set of labels. A selector picks series by name and label:
up{job="node"}
node_memory_MemAvailable_bytes{instance="server1.example.com:9100"}The = operator matches exactly, and !~ excludes values matching a regular expression.
Using rate and aggregation for CPU, memory, and disk
node_cpu_seconds_total is a counter, so rate() converts it into a per-second change over a window. avg by (instance) combines the CPU cores of each server.
# CPU utilisation per server, in percent
100 * (1 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])))
# Memory in use, in percent
100 * (1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)
# Disk space in use, in percent, ignoring virtual filesystems
100 * (1 - node_filesystem_avail_bytes{fstype!~"tmpfs|overlay"} / node_filesystem_size_bytes{fstype!~"tmpfs|overlay"})Adding Prometheus as a Grafana data source
Grafana reads from Prometheus through a data source. You can add it in the web interface, or provision it from a file at /etc/grafana/provisioning/datasources/prometheus.yml:
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://localhost:9090
isDefault: trueRestart Grafana to load the file.
Exercise
Exercise
A target shows up = 0 on the Targets page. Which two checks do you run first?
Show answer
Run curl http://<host>:9100/metrics from the Prometheus server to test reachability and the endpoint, then check firewall rules for port 9100.