Connecting Telegraf to Mosquitto with output to InfluxDb
Published Updated 5 years ago
For a small project (a microcontroller with a temperature sensor) I needed a mqtt broker to process the sensors data to display it in Grafana. There’s already a TIG stack running on my server, so it was obvious to me that I will go with Mosquitto.
For some reason Mosquitto never returned any data scraped by Telegraf. At this point I was really puzzled, as all tutorials I have found online had the same configuration and a similar setup as my stack. It took me quite some time to discover that Mosquitto introduced a breaking change in version 2, as it now connects by default only to the loopback interface, which explains the issue I saw: no connections from outside of the container are successful. All tutorials I have seen on the internet are from before version 2, so there was no mention of this newly introduced behaviour.
To change this behavior a single line needs to be added to the mosquitto.conf: listener 1883 (or whatever port you’re using). After this change a connection from Telegraf is successful.
My entire mosquitto.conf looks like this after this change:
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
listener 1883
allow_anonymous true
This configuration file is mounted into the container by using a volume mount in a docker-compose file:
mosquitto:
image: eclipse-mosquitto:2.0.7
container_name: mosquitto
ports:
- 9001:9001
- 1883:1883
volumes:
- /media/enc/docker-volumes/mosquitto/conf/mosquitto.conf:/mosquitto/config/mosquitto.conf
- /media/enc/docker-volumes/mosquitto/data/:/mosquitto/data/
- /media/enc/docker-volumes/mosquitto/log/:/mosquitto/log/
Telegraf is configured to subscribe to the mqtt topics by using the mqtt_consumer of inputs and outputting the data to influxdb:
[[outputs.influxdb]]
urls = ["http://influxdb:8086"]
database = "sensors"
skip_database_creation = true
[[inputs.mqtt_consumer]]
servers = ["tcp://mosquitto:1883"]
topics = [
"bedroom/#"
]
After this simple change Mosquitto accepts external (outside of the container) connections and the Telegraf is able to subscribe to the data and send it to InfluxDb.
Comments
Comments are hosted on comments.itobey.dev. Loading them sets cookies and shares your IP address with that service.