portfolio lone-faerie

A selection of projects made by lone-faerie

MQTTop

by lone-faerie

Provide system metrics over MQTT

Github Download


MQTTop is a bridge to provide top-like system metrics over MQTT. This was mainly designed for use with Home Assistant and supports MQTT Discovery, and has currently been tested on Linux and with an NVIDIA GPU.

Inspired by btop and linux2mqtt


I wanted to add system metrics into my Home Assistant instance for a remote device, but none of the existing solutions exactly met my needs so I built this program. It is able to provide metrics for CPU usage, memory usage, disk usage, directory size, network connection, battery level, and NVIDIA GPU usage. MQTTop makes use of procfs and sysfs to retrieve most of the metrics, as well as inotify for directories and the NVML library for NVIDIA GPU metrics. The MQTT connection is handled by the paho client library with added support for Home Assistant discovery.

MQTTop is written in Go with a focus on being as lightweight as possible. Metrics are implemented as an interface, allowing for easy extensibility, and each metric runs in a separate goroutine. They are coordinated through an MQTT bridge, reporting new metrics through channels. This allows for metrics to be published as soon as they update, without needing to interrupt any other metrics. The metrics that are able trigger with callbacks, such as directory metrics, and the rest use polling. By rate limiting the callbacks and only publishing when there are changes, this helps to avoid flooding the network. The program can be run from the command line or through Docker, with builds both with and without GPU support so reliance on proprietary libraries is optional. Configuration is done through YAML files, command line arguments, and/or environment variables, allowing for the most flexibility.

tags: go - mqtt - linux - docker - home-assistant