Monitoring a Linux host with Prometheus and Grafana

A monitoring stack for one Linux machine and every container on it, built from the same tools used on production servers. It installs with one command, is provisioned entirely from files, and sends alerts to a phone.

Source on GitHub (opens in a new tab)

Stack
Prometheus, Grafana, Alertmanager, Node Exporter, cAdvisor
Runs on
Docker Compose, any Linux host
Alerting
10 rules, sent to a phone through ntfy
Retention
30 days, capped at 5 GB
Release
v0.4.0, with tagged updates and rollback

How it fits together

    • Linux hostCPU, memory, disks, network, sensors, battery
    • Docker containersEvery container on the machine
    • Node ExporterHost metrics, with no memory of its own
    • cAdvisorPer-container CPU, memory, network and disk
    • PrometheusStores the history and evaluates the 10 alert rules
    • GrafanaDashboard and data source provisioned from files
    • AlertmanagerGroups alerts and decides who to tell
    • Your browserThe dashboard, on the machine itself
    • ntfyA push notification on your phone
Metrics flow down: the two exporters read the kernel, Prometheus scrapes them and keeps the history, Grafana draws it, and Alertmanager sends alerts to a phone.

What CI proves on every push

The installer is the product, so CI does what a new user would do, on a clean machine, and checks the machine is clean again afterwards.

  1. Lint. The shell scripts and the config files are checked before anything runs.

  2. Install. The one-command installer runs from scratch and waits until the stack is really working.

  3. Uninstall. The uninstaller runs with --remove-data, deleting metrics, Grafana's database and the generated .env.

  4. Check for leftovers. The run fails if a container, volume or file was left behind.

  5. Dependabot. A new Prometheus, Grafana or exporter version arrives as a pull request and goes through the same four steps before it is merged.

Decisions, and why

  1. Everything is set up from files. Grafana's data source and dashboard come from provisioning files in the repository. Anything clicked together in the UI lives only in Grafana's database, so it would not survive a reinstall and could not be shared.

  2. Every service uses host networking. A containerised Node Exporter reports the container's own virtual network card, not the machine's real one. Host networking fixes that, and putting Prometheus and Grafana there too turns every connection into plain localhost. The cost is no network isolation between the services, which is acceptable on a single machine.

  3. Private by default. Every service listens on 127.0.0.1. Grafana is opened to the home network only if you choose that during install, and nothing else is ever reachable from another device.

  4. History is capped so it cannot fill the disk. Prometheus keeps 30 days or 5 GB, whichever comes first. A monitoring tool that takes the machine down by filling its disk has failed at its one job.

  5. Image versions are pinned. No :latest tag anywhere, so a clone next year gets the same software and the dashboard still works. Dependabot proposes each update and CI tests it.

  6. cAdvisor is trimmed to what is used. By default it exports about 1000 series for a dozen containers. It collects containers only, and Prometheus keeps just the metrics the dashboard and alerts use, about 120.

  7. Secrets never reach Git. The installer generates the Grafana password and the private ntfy topic on the machine. Both live in ignored files, and the committed .env.example never holds a real value.

What it does not do

More