Monitoring
A working monitoring setup is a must-have for a proper home lab. In this blog article as well as the video, I explain the basics regarding a monitoring system based on Grafana, Prometheus and MQTT.
(German YouTube video)
This installation is just an example and is not production ready!
Install Grafana
I recommend referring to the official Documentation. The basic steps are shown below.
Install dependencies
sudo apt install -y apt-transport-httpssudo apt install -y software-properties-common wgetAdd GPG key
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -Add apt source
echo "deb https://packages.grafana.com/enterprise/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.listInstall Grafana
sudo apt update
sudo apt install grafana-enterpriseEnable the service
sudo systemctl enable --now grafana-serverBuild Grafana MQTT datasource from source
This step isn’t necessary any more as the MQTT datasource is now officially available"
The original build process is outlined in a GitHub Issue.
Install Go
wget -O -https://raw.githubusercontent.com/mauleyzaola/scripts/master/go/go.install.sh | shsource ~/.bashrcInstall Mage
sudo apt install gitgit clone https://github.com/magefile/magecd magego run bootstrap.goInstall Node.js
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -sudo apt install -y nodejsEnable Yarn
sudo corepack enableModify the Grafana plugin configuration.
sudo nano /etc/grafana/grafana.iniModify these variables:
plugins = /var/lib/grafana/plugins
allow_loading_unsigned_plugins = grafana-mqtt-datasourceCreate plugin directory
mkdir /var/lib/grafana/pluginsBuild MQTT datasource
cd /var/lib/grafana/pluginsgit clone https://github.com/grafana/mqtt-datasource.gitcd mqtt-datasourceyarn add @grafana/toolkityarn buildyarn installRestart Grafana
sudo systemctl restart grafana-serverInstall and configure Mosquitto
Install Mosquitto
sudo apt install mosquittoConfigure Mosquitto
Create the configuration file
sudo nano /etc/mosquitto/conf.d/mosquitto.confInsert the configuration:
listener 1883 0.0.0.0
log_dest syslog
log_dest stdout
log_dest topic
log_type error
log_type warning
log_type notice
log_type information
connection_messages true
log_timestamp true
allow_anonymous false
password_file /etc/mosquitto/pwfileCreate the password file
sudo nano /etc/mosquitto/pwfileAdd users to this file in the <username>:<password> scheme.
Hash the password file
sudo mosquitto_passwd -U /etc/mosquitto/pwfileStart the Mosquitto service
sudo systemctl enable --now mosquittoInstall and configure Prometheus
Create a service account
I recommend using an unprivileged service account for Prometheus.
sudo adduser prometheussudo su - prometheusDownload and extract Prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.36.0/prometheus-2.36.0.linux-amd64.tar.gztar -xzvf prometheus-2.36.0.linux-amd64.tar.gzmv prometheus-2.36.0.linux-amd64/ prometheus/Create the Prometheus service
The following commands have to be executed as an admin.
sudo nano /etc/systemd/system/prometheus.service[Unit]
Description=Prometheus Server
After=network-online.target
[Service]
User=prometheus
Restart=on-failure
ExecStart=/home/prometheus/prometheus/prometheus \
--config.file=/home/prometheus/prometheus/prometheus.yml \
--storage.tsdb.path=/home/prometheus/prometheus/data
[Install]
WantedBy=multi-user.targetStart the Prometheus service
sudo systemctl enable --now prometheusInstall the Node Exporter
I recommend using a service account here, too. The process of creating one is the same as for Prometheus.
Download and extract the Node Exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gztar -xzvf node_exporter-1.3.1.linux-amd64.tar.gzmv node_exporter-1.3.1.linux-amd64/ node_exporter/Create the Node Exporter service
sudo nano /etc/systemd/system/node-exporter.service[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
ExecStart=/home/prometheus/node_exporter/node_exporter
[Install]
WantedBy=default.targetStart the Node Exporter Service
sudo systemctl enable --now node node-exporterAdd the Node Exporter to Prometheus
The config file is located in the prometheus folder in the home directory of the Prometheus service account.
nano prometheus.yamlThis configuration has to be added to the job configuration section of the configuration:
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']Restart the Prometheus service
sudo systemctl restart prometheusDone!