Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 63Observability · Prometheus

How to monitor a Linux server with Prometheus

You want CPU, memory, disk and network of every server in one place — with history, so you can see "when did the disk start to fill?". The standard open-source way: node_exporter on each server publishes the numbers, Prometheus collects them every 15 seconds. This how-to adds a new server to an existing Prometheus, checks the target is up, and gives the PromQL queries you need every day.

  • 5 Lessons
  • 8 Commands

$ run as a normal user# run as root (or with sudo)

63.1

The quick answer

On the new server (node_exporter installed and running on port 9100):

curl -s http://127.0.0.1:9100/metrics | grep -m3 '^node_load'
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.9.10/32" port port="9100" protocol="tcp" accept' && firewall-cmd --reload

On the Prometheus server: add "web3:9100" to the node job, then:

promtool check config /etc/prometheus/prometheus.yml
systemctl reload prometheus

10.0.9.10 = the Prometheus server — only it may read port 9100.

63.2

Step by step

  1. Install node_exporter on the server (as a systemd service — see the full guide) and check it answers locally (quick answer).
  2. Open port 9100 only for Prometheus — the metrics show a lot about the server. UFW:
ufw allow from 10.0.9.10 to any port 9100 proto tcp
  1. Add the target to Prometheus, with labels you will filter on later:
/etc/prometheus/prometheus.yml (part)
  - job_name: node
    static_configs:
      - targets: ["web1:9100", "web2:9100", "db1:9100", "web3:9100"]
        labels:
          env: prod

Many servers: use file_sd_configs (a target list file that Ansible writes) or cloud service discovery (ec2_sd_configs, azure_sd_configs).

  1. Check and reload, then look at Status → Targets (web3 must be UP):
promtool check config /etc/prometheus/prometheus.yml
systemctl reload prometheus
curl -s 'http://127.0.0.1:9090/api/v1/query?query=up{job="node"}' | jq -r '.data.result[] | .metric.instance + " " + .value[1]'
web1:9100 1
web3:9100 1
  1. The queries you need every day (paste them into the Prometheus or Grafana query box):
QuestionPromQL
CPU used %100 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100
Memory used %100 * (1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)
Disk free %100 * node_filesystem_avail_bytes{fstype!~"tmpfs|overlay"} / node_filesystem_size_bytes
Load per corenode_load5 / count by (instance) (node_cpu_seconds_total{mode="idle"})
Network in (bits/s)rate(node_network_receive_bytes_total{device!="lo"}[5m]) * 8
Disk full in 4 hours?predict_linear(node_filesystem_avail_bytes[6h], 4*3600) < 0
  1. Add alerts for down, disk and CPU (the rules file in the full guide), and a dashboard in Grafana.

63.3

Check that it worked

The target is UP, the queries return a value for the new server, and in an hour you can see a graph with real history.

63.4

Common problems

ProblemCauseFix
Target DOWN: context deadline exceededFirewall or security group blocks 9100.Open 9100 for the Prometheus IP only; test with curl from the Prometheus server.
Target DOWN: connection refusednode_exporter is not running, or listens on another address.systemctl status node_exporter; check --web.listen-address.
no such hostPrometheus can not resolve the name.Use the IP, or fix DNS / /etc/hosts on the Prometheus server.
Disk query shows strange mountstmpfs, overlay, snap loops.Filter fstype and mountpoint labels.
Reload did nothingConfig error — Prometheus keeps the old config.promtool check config; read journalctl -u prometheus.
Many servers, the list is hard to keepStatic targets by hand.file_sd_configs written by Ansible, or cloud service discovery.

63.5

Go further

1Why should port 9100 be open only for the Prometheus server?

The metrics show a lot about the server (versions, disks, network); only the collector needs them.

2Which PromQL function warns before a disk is full?

predict_linear(...) on node_filesystem_avail_bytes.

All commands in this chapter