WORK IN PROGRESS
Prometheus is a very nice open-source monitoring system for recording real-time metrics (and providing real-time alerts) in a time-series database for a variety of purposes.
Here we're going to setup Prometheus on a server to monitor a wealth of statistics (such as CPU/memory/disk usage, disk IOps, network traffic, TCP connections , timesync drift, etc.) as well as monitor several endpoints (which could be web applications or services like bitwarden, rocketchat, gitlab, etc.).
We'll also setup a simple dashboard with Grafana, another (great) open-source visualisation and analytics platform that integrates fantastically with (and out-of-the-box) with Prometheus. This will provide a nice dashboard that we'll use to quickly see the status of the endpoints we're monitoring.
An example can be seen at https://mon.jaytaala.com.
Guide
We'll cover the following steps:
Installing and configuring Prometheus (with Docker)
I'm assuming you are running an up-to-date Linux distro and you have Docker installed (if not, you'll need to install Docker).
We only need to run a single command (although it's not a short one) to install and configure our Prometheus setup. To make this a bit easier, I provide my setup files in a git repo you can clone:
https://gitlab.jaytaala.com/docker/prometheus.git
git clone https://gitlab.jaytaala.com/docker/prometheus
The repo contains only two files (which are the only two we need):
- run-container.sh : shell script that will run our docker command to install / configure prometheus.
- prometheus.yml : single configuration file for prometheus;
See the Explanation of run-container.sh section for an explanation and several notes for these files.
Once you've cloned the repo, simply make the script executable (if it isn't already) and run the script with:
chmod +x prometheus.sh ./prometheus.sh
Docker will then download the latest official Prometheus Docker image, create and configure the container and then run said container.
If things worked as they should you should be able to visit the basic Prometheus web interface at http:\\<INTERNAL-SERVER-IP>:10090.
Replace <INTERNAL-SERVER-IP> with the actual internal IP address of your server, e.g. 192.168.1.x, 10.0.0.x (or whatever it is).
That's it! You will now have Prometheus running on port 10090.
Note: port 10090 is not the default Prometheus port (which is 9090) - I have another service using that port (which I didn't want to change). See the Explanation of run-container.sh section for notes on changing the port.
(Optional Read) Explanation of run-container.sh
This script simply creates (and/or runs) our Prometheus container. So what's going on here?
(Optional Read) Explanation of prometheus.yml
This file is the main Prometheus.yml scrape configuration file. It defines things like how often Prometheus should scrape (query) data and from where (and how) it should scrape this data.
Updating Prometheus with Docker
Updating with Docker is straight-forward. We just need to pull the (latest) image and then re-run our run-container.sh
script.
docker pull prom/prometheus ./run-container.sh
Installing and configuring node_exporter (to monitor server stats)
node_exporter is a prometheus exporter which monitors hardware and 'nix OS metrics. It runs on a port exposed to prometheus and prometheus can then query it and get a (large) ranger of metrics for whatever machine is running node_exporter.
By default node_exporter enables a large number of "collectors" (modules which collect certain information from the machine). See here for a list of collectors enabled by default (and what info they collect).
node_exporter can be run from a docker container, but it's not recommended since it should be run directly on the host hardware to collect stats.
Installing node_exporter can be done by downloading a recent version version, untar'ing and executing. We're going to be doing an extra step here to manage node_exporter with systemd (so it starts on server boot etc.).
We start with downloading. You can find a link for the latest version at https://prometheus.io/download/#node_exporter. At the time of this writing the latest stable version for linux-amd64 was node_exporter-0.18.1.linux-amd64.tar.gz.
We'll download, untar, and then move it to /opt/node_exporter
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz tar -xf node_exporter-0.18.1.linux-amd64.tar.gz sudo mv node_exporter-0.18.1.linux-amd64 /opt/node_exporter
With node_exporter installed let's make it easier to manage by creating a systemd service. Create a file in /etc/systemd/system/ with your favourite text editor (I'm using vim here):
sudo vim /etc/systemd/system/node_exporter.service
and paste the following:
[Unit] Description=Node Exporter Wants=network-online.target After=network-online.target [Service] Type=simple ExecStart=/opt/node_exporter --web.listen-address=:10091 [Install] WantedBy=multi-user.target
I'm using a non-default port here (10091) so change it to whatever port you prefer (or have free).
Finally let's enable it (to start on boot) and start the service
sudo systemctl daemon-reload sudo systemctl enable --now node_exporter
Installing and configuring blackbox (to monitor endpoints)
Installing and configuring Grafana
References
- https://prometheus.io/
- https://github.com/prometheus/node_exporter
- https://prometheus.io/download/#node_exporter
- https://grafana.com/
Related articles