Skip to content
GitHub no longer supports this web browser. Learn more about the browsers we support.
MQTT Exporter analog to the Prometheus PushGateway
Go Makefile Dockerfile
Branch: master
Clone or download
hikhvar Merge pull request #2 from pulsar256/feature/auth-and-fixes
MQTT auth, fixed error handling, less strict json parsing
Latest commit 776bc49 Feb 3, 2020
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
cmd
pkg less strict json payload type validation Feb 3, 2020
systemd Initial commit Mar 18, 2018
.dockerignore Initial commit Mar 18, 2018
.gitignore Initial commit Mar 18, 2018
.travis.yml Added binary deployment Dec 30, 2018
Dockerfile
Gopkg.lock Updated Gopkg.lock to support digest. Dec 30, 2018
Gopkg.toml
LICENSE Initial commit Mar 18, 2018
Makefile Improved Makefile rebustness and add support for multiple ARM versions Dec 30, 2018
Readme.md
config.yaml.dist auth config for mqtt, fixed error handling Feb 3, 2020

Readme.md

MQTT2Prometheus

This exporter is an analog to the Prometheus Pushgateway. Clients can push metrics via MQTT to an MQTT Broker. This exporter subscribes to the broker and publish the received messages as prometheus metrics. I wrote this exporter to publish metrics from small embedded sensors based on the NodeMCU to prometheus. The used arduino scetch can be found in the dht22tomqtt repository.

Assumptions about Messages and Topics

This exporter makes some assumptions about the message format and MQTT topics. This exporter assumes that each client publish the metrics into a dedicated topic. The last level topic becomes the sensor label in prometheus. This exporter assume that the message are JSON objects with only float fields. The golang type for the messages is:

type MQTTPayload map[string]float64

For example the message

{"temperature":23.20,"humidity":51.60,"heat_index":22.92}

published to the MQTT topic devices/me/livingroom becomes the following prometheus metrics:

temperature{sensor="livingroom"} 23.2
heat_index{sensor="livingroom"} 22.92
humidity{sensor="livingroom"} 51.6

Build

To build the exporter run:

make build

Docker

Use Public Image

To start the public available image run:

docker run -it -v "$(pwd)/config.yaml:/config.yaml"  -p 8002:8002 hikhvar/mqtt2prometheus:latest 

Build The Image locally

To build a docker container with the mqtt2prometheus exporter included run:

make container

To run the container with a given config file:

docker run -it -v "$(pwd)/config.yaml:/config.yaml"  -p 8002:8002 mqtt2prometheus:latest 

Configuration

The exporter can be configured via command line and config file.

Commandline

Available command line flags:

Usage of ./mqtt2prometheus.linux_amd64:
  -config string
        config file (default "config.yaml")
  -listen-address string
        listen address for HTTP server used to expose metrics
  -listen-port string
        HTTP port used to expose metrics (default "8002")

Config file

The config file can look like this:

# Settings for the MQTT Client. Currently only these three are supported
mqtt:
  # The MQTT broker to connect to
  server: tcp://127.0.0.1:1883
  # The Topic path to subscripe to. Actually this will become `$topic_path/+`
  topic_path: v1/devices/me
  # The MQTT QoS level
  qos: 0
cache:
  # Timeout. Each received metric will be presented for this time if no update is send via MQTT
  timeout: 2min
# This is a list of valid metrics. Only metrics listed here will be exported
metrics:
    # The name of the metric in prometheus
  - prom_name: temperature
    # The name of the metric in a MQTT JSON message
    mqtt_name: temperature
    # The prometheus help text for this metric
    help: DHT22 temperature reading
    # The prometheus type for this metric. Valid values are: "gauge" and "counter"
    type: gauge
    # A map of string to string for constant labels. This labels will be attached to every prometheus metric
    const_labels:
      sensor_type: dht22
    # The name of the metric in prometheus
  - prom_name: humidity
    # The name of the metric in a MQTT JSON message
    mqtt_name: humidity
    # The prometheus help text for this metric
    help: DHT22 humidity reading
    # The prometheus type for this metric. Valid values are: "gauge" and "counter"
    type: gauge
    # A map of string to string for constant labels. This labels will be attached to every prometheus metric
    const_labels:
      sensor_type: dht22
    # The name of the metric in prometheus
  - prom_name: heat_index
    # The name of the metric in a MQTT JSON message
    mqtt_name: heat_index
    # The prometheus help text for this metric
    help: DHT22 heatIndex calculation
    # The prometheus type for this metric. Valid values are: "gauge" and "counter"
    type: gauge
    # A map of string to string for constant labels. This labels will be attached to every prometheus metric
    const_labels:
      sensor_type: dht22%       
You can’t perform that action at this time.