Lightweight DIY Linux Server Monitoring – Part 2: Integrating with Home Assistant via MQTT Discovery
In my previous post, Lightweight DIY Linux Server Monitoring , I shared a simple, dependency-free Bash script designed to monitor Linux environments in a lightweight way and push metrics like disk usage, memory, CPU load, and upgradable "
apt" packages to an external MQTT broker.While that setup worked great for a standalone MQTT setup with Node-RED I wanted to take it a step further. In this follow-up, we will upgrade the script to seamlessly integrate with Home Assistant(HA) using MQTT and the Auto-Discovery function.
Instead of manually setting up sensors in Home Assistant, the script automatically creates its own device and sensors in Home Assistant the first time it runs and each day if something gone wrong !
What’s New?
Home Assistant MQTT Auto-Discovery: The script automatically publishes Home Assistant configuration payloads (with the
Home Assistant MQTT Auto-Discovery: The script automatically publishes Home Assistant configuration payloads (with the
retain flag) so your server pops up under "Settings > Devices & Services > MQTT" all by itself.Smart Configuration Throttle: To avoid flooding the MQTT broker, the script only sends the heavy configuration payloads once a day (or automatically after a reboot when /tmp is cleared).
Unified Dashboard: Your Linux servers and Linux VM:s can now feed data straight into Home Assistant alongside all your other smart home devices.
Install
Install HA MQTT with info from this page.
Install HA MQTT with info from this page.
You must register a user to send messages to the Brocker. This could be a new one or reuse and old one.
In HA "Settings => Devices & Services => + Add integration => MQTT => Install"
and check In HA "Settings => Devices & Services" that its configured.
In HA "Settings => Apps" you now will see the "Mosquitto broker" up and running
Here is the complete script ready to be dropped onto your Linux servers or Proxmox nodes.
Please update the following in the script:
- BROKER="192.nn.nn.nn"
- MQTT_USER="XXX"
- MQTT_PASS="XXX"
Different Linux distributions have some differences but as "root" at the source server
apt install mosquitto-clients
apt install sudo
visudo
and at the end add
your_user ALL=(ALL) NOPASSWD: /usr/bin/apt update
this will make that the user executing the script, "your_user", don't have to use any password for the "apt update" command.
In the home catalog for the executing user save the script on your source Linux machine.
Execute the script with
bash -x serverCheck_HA.sh
Check the result and if OK ....
Watch the Magic: Once the script runs, check Home Assistant under "Settings > Devices & Services > MQTT". Your source server will automatically appear with all five sensors ready to be added to your dashboards !
To run the script, for example, every 5th minute the easiest way is to simply set up a cronjob with "crontab -e" and add the row with the path to the script "*/5 * * * * bash /home/pi/serverCheck_HA.sh".

Comments
Post a Comment
Feel free to leave a comment ! ... but due to a lot of spam comments I have to moderate them. Will reply ASAP !