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
read-only mounts of /proc, /sys and cgroups
- Node ExporterHost metrics, with no memory of its own
- cAdvisorPer-container CPU, memory, network and disk
scraped every 15 seconds
- PrometheusStores the history and evaluates the 10 alert rules
queries one way, alerts the other
- GrafanaDashboard and data source provisioned from files
- AlertmanagerGroups alerts and decides who to tell
everything listens on 127.0.0.1 only
- Your browserThe dashboard, on the machine itself
- ntfyA push notification on your 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.
Lint. The shell scripts and the config files are checked before anything runs.
Install. The one-command installer runs from scratch and waits until the stack is really working.
Uninstall. The uninstaller runs with --remove-data, deleting metrics, Grafana's database and the generated .env.
Check for leftovers. The run fails if a container, volume or file was left behind.
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
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.
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.
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.
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.
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.
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.
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
- It monitors the one machine it is installed on. There is no multi-host setup.
- Host networking means the services are not isolated from each other.
- cAdvisor reads the Docker socket, so it is trusted the way any tool with that socket is. Mounting the socket read-only does not limit what can be asked of it.
- Phone alerts go through the public ntfy service, and the topic name is the only secret protecting them.
More
- Five containers behind one Nginx proxy, tested on every pushCase study
- All projectsPortfolio