Telegraf MQTT Connection

Hello, I am new to InfluxDB and I am working with Telegraf, InfluxDB, and Grafana for an IoT project where I basically read time-series data from a sensor and have to store said data in a database and then display it on a dashboard.

I am sending the data from the sensors over MQTT and subscribing to the topic via telegraf. The problem I am facing, or I suppose this is the problem, is that telegraf for an obscure reason decided to not connect to the broker (that I have in the same network as all the other containers, using HiveMQ as a broker), I suspect that Docker is not giving access to telegraf but I’m really at a point where I just have no idea where the problem is after 1-2 weeks of searching. I tried all the tutorials and guides I could find all the posts related and nothing worked.

So please if anybody could help me I would be very thankful.

PS: Files that I’m using:

1- My docker-compose.yml:


version: "3.8"

services:
  hivemq:
    image: "hivemq/hivemq4"
    container_name: mqttbroker
    environment:
      - HIVEMQ_DISABLE_AUTH_PLUGIN=true
      - no_proxy=127.0.0.1,localhost,mqttbroker,joynrbackend,monitor-app, \
        clustered-app-node-1,clustered-app-node-2,backpressure-provider-small, \
        backpressure-provider-large,backpressure-monitor-app,test-driver
    ports:
      - 8080:8080
      - 1883:1883

  influxdb:
    image: influxdb:1.8
    container_name: influxdb
    restart: always
    hostname: influxdb
    environment:
      INFLUX_DB: $INFLUX_DB # nom de la base de données créée à l'initialisation d'InfluxDB
      INFLUXDB_USER: $USER # nom de l'utilisateur pour gérer cette base de données
      INFLUXDB_USER_PASSWORD: $PASSWORD # mot de passe de l'utilisateur pour gérer cette base de données
    volumes:
      - ./influxdb:/var/lib/influxdb # volume pour stocker la base de données InfluxDB

  telegraf:
    image: telegraf
    depends_on:
      - influxdb # indique que le service influxdb est nécessaire
    container_name: telegraf
    restart: always
    links:
      - influxdb:influxdb
    tty: true
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # nécessaire pour remonter les données du démon Docker
      - ./telegraf/telegraf.conf:/etc/telegraf/telegraf.conf # fichier de configuration de Telegraf

  grafana:
    image: grafana/grafana
    depends_on:
      - influxdb # indique que le service influxdb est nécessaire
    container_name: grafana
    restart: always
    ports:
      - 3000:3000 # port pour accéder à l'interface web de Grafana
    links:
      - influxdb:influxdb
    environment:
      GF_INSTALL_PLUGINS: "grafana-clock-panel,\
        grafana-influxdb-08-datasource,\
        grafana-kairosdb-datasource,\
        grafana-piechart-panel,\
        grafana-simple-json-datasource,\
        grafana-worldmap-panel"
      GF_SECURITY_ADMIN_USER: $GF_SECURITY_ADMIN_USER # nom de l'utilisateur créé par défaut pour accéder à Grafana
      GF_SECURITY_ADMIN_PASSWORD: $GF_SECURITY_ADMIN_PASSWORD # mot de passe de l'utilisateur créé par défaut pour accéder à Grafana
    volumes:
      - ./grafana:/var/lib/grafana

2- The telegraf.conf file:


[agent]
  interval = "10s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = ""
  hostname = "telegraf"
  omit_hostname = false

[[outputs.influxdb]]
  urls = ["http://influxdb:8086"]
  database = "telegraf"
  skip_database_creation = false
  username = "telegraf_user"
  password = "telegraf_password"

[[inputs.mqtt_consumer]]
  servers = ["tcp://localhost:1883"]
  topics = [
    "sensors/data"
  ]
  data_format = "json"

What is the error message?


I supect this is maybe the problem:

[[inputs.mqtt_consumer]]
  servers = ["tcp://localhost:1883"]

AFAIK localhost inside a docker container is resolved to the current container itself, not the host.
Try this config:

[[inputs.mqtt_consumer]]
  servers = ["tcp://hivemq:1883"]

Hello, thanks for the answer. I did not get an error message it’s just that I was trying to send the data from a raspberry pi over MQTT but telegraf did not capture the data but when I tried monitoring the container it worked fine so the problem was specifically from the MQTT side.

I will try your solution and see if it works.

Thank you ^^

Update: It worked finally. Thank you so much