Giter Club home page Giter Club logo

mqtt-to-prometheus's Introduction

MQTT to Prometheus Exporter for Smart Garden

A lightweight exporter that reads sensor data from an MQTT broker and exposes it as Prometheus metrics. Designed for seamless integration with Grafana Stack Components like Loki/Promtail for monitoring and logging. Part of my upcoming Smart Garden project to automate and monitor garden systems using IoT devices.

Disclaimer

This is by far not meant or designed to be a generic mqtt-to-prometheus exporter. I aim to use this only for my smart gardening project. A reasonable exporter that could fit your needs better, if you want a somewhat generic approach: kpetremann/mqtt-exporter

Prerequisites

A selection of tools and software needed:

  • Docker
  • Docker Compose
  • Python 3.9
  • MQTT Broker (e.g., Mosquitto)
  • Prometheus
  • Grafana
  • Promtail for log scraping (optional)

Installation

You can use the docker images or build it yourself, choose for your comfort.

Using Dockerhub

Coming soon!

Build your own Images

  1. Clone the repository:

    git clone https://github.com/Mawiguk0/mqtt-to-prometheus.git
    cd mqtt-to-prometheus
  2. Build and run the Docker containers:

    docker-compose up --build

Configuration

The project uses environment variables for configuration:

  • MQTT_BROKER: Address of the MQTT broker.
  • MQTT_PORT: Port of the MQTT broker (default: 1883).
  • MQTT_TOPIC: MQTT topic to subscribe to.
  • PROMETHEUS_PORT: Port for the Prometheus metrics server (default: 8000).
  • RETRY_INTERVAL: Interval between retry attempts to connect to the MQTT broker (default: 5 seconds).

Variable coming from python-json-logger, useful for some:

  • LOG_LEVEL: Set the desired logging level DEBUG, INFO, WARNING, ERROR (default: INFO)

Usage

  1. Start the service:

    docker-compose up --build
  2. Access Prometheus metrics: Open your browser and navigate to http://localhost:8000/metrics.

  3. View logs in Grafana: Configure Grafana to read logs from Loki and explore the logs using the "Explore" feature.

Health Check

The project includes a health check script to verify the service's health. The Docker container is configured to run this script periodically.

Logging

Logs are structured using JSON format and are compatible with Grafana Loki for easy log aggregation and querying. The promtail service is used to scrape logs from Docker containers and send them to Loki.

Consult Logging Documentation for further insights.

Prometheus Metrics

The following metrics are exposed:

  • mqtt_humidity: Humidity from MQTT topic
  • mqtt_temperature: Temperature from MQTT topic
  • mqtt_adc: ADC value from MQTT topic
  • mqtt_battery: Battery level from MQTT topic
  • mqtt_rssi: RSSI from MQTT topic
  • mqtt_snr: SNR from MQTT topic
  • mqtt_pferror: Packet Frequency Error from MQTT topic
  • mqtt_packet_size: Packet Size from MQTT topic
  • mqtt_last_data_received_timestamp: Timestamp of the last data received

Docker Compose Setup

The docker-compose.yml file sets up the following services:

  • mqtt_to_prometheus: The main service that reads MQTT data and exposes Prometheus metrics.
  • prometheus: The Prometheus service for scraping and storing metrics.
  • promtail: The Promtail service for log scraping.

Troubleshooting

  • MQTT Connection Issues: Ensure the MQTT broker address and port are correctly configured.
  • Metrics Not Updating: Check the MQTT topic for correct data and ensure the Prometheus scrape interval is appropriate.
  • Log Issues: Verify that Promtail is correctly configured to scrape Docker logs.

Architecture and Decision Records

For detailed information about the architecture and decision-making process behind this project, please refer to the following documents:

Architecture Documentation
Architecture Decision Records
What is an ADR? https://adr.github.io/

Contributing

Contributions are welcome! Please fork the repository and submit pull requests.

License

This project is licensed under the MIT License. See the LICENSE file for details.

mqtt-to-prometheus's People

Contributors

mawiguk0 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.