Telegraf MQTT connection network error

Hi there,

I want to connect Telegraf and Mosquitto on Docker.

I think there’s a problem with the servers setting of mqtt_consumer, but I don’t know what it is.

  1. This error is set to servers=[“tcp://mosquitto:1883”]
2021-12-14T06:21:46Z I! Starting Telegraf 1.20.4
2021-12-14T06:21:46Z I! Using config file: /etc/telegraf/telegraf.conf
2021-12-14T06:21:46Z I! Loaded inputs: mqtt_consumer
2021-12-14T06:21:46Z I! Loaded aggregators: 
2021-12-14T06:21:46Z I! Loaded processors: 
2021-12-14T06:21:46Z I! Loaded outputs: file influxdb_v2
2021-12-14T06:21:46Z I! Tags enabled: host=3140e3d0a6a8
2021-12-14T06:21:46Z I! [agent] Config: Interval:10s, Quiet:false, Hostname:"3140e3d0a6a8", Flush Interval:10s
2021-12-14T06:21:46Z E! [telegraf] Error running agent: starting input inputs.mqtt_consumer: network Error : dial tcp 172.28.0.3:1883: connect: connection refused
  1. This error is set to servers=[“tcp://docker.for.mac.localhost:1883”]
2021-12-14T07:19:26Z I! Starting Telegraf 1.20.4
2021-12-14T07:19:26Z I! Using config file: /etc/telegraf/telegraf.conf
2021-12-14T07:19:26Z I! Loaded inputs: mqtt_consumer
2021-12-14T07:19:26Z I! Loaded aggregators: 
2021-12-14T07:19:26Z I! Loaded processors: 
2021-12-14T07:19:26Z I! Loaded outputs: file influxdb_v2
2021-12-14T07:19:26Z I! Tags enabled: host=3140e3d0a6a8
2021-12-14T07:19:26Z I! [agent] Config: Interval:10s, Quiet:false, Hostname:"3140e3d0a6a8", Flush Interval:10s
2021-12-14T07:19:26Z E! [telegraf] Error running agent: starting input inputs.mqtt_consumer: network Error : EOF

Anyone will be of help to me. Thank you.

docker-compose.yml

version: "3.8"

services:
  telegraf:
    container_name: telegraf0
    image: telegraf
    restart: always
    ports: 
      - "5050:5050"
    depends_on:
      - mosquitto
      - influxdb

  mosquitto:
    container_name: mosquitto
    image: eclipse-mosquitto:latest
    restart: always
    ports:
      - "1883:1883"
      - "9001:9001"
    depends_on:
      - influxdb
    volumes:
      - mosquitto-data:/mosquitto/data
  
  influxdb:
    container_name: influx
    image: influxdb
    restart: always
    ports:
      - "8086:8086"
    volumes:
      - influxdb-data:/var/lib/influxdb
      - ./influxdb-backup:/backup

  grafana:
    container_name: grafana
    image: grafana/grafana
    links:
            - influxdb
    hostname: grafana
    restart: always
    ports:
      - "3000:3000"
    volumes:
      - grafana-data:/var/lib/grafana
    depends_on:
      - influxdb
      
volumes:
  mosquitto-data:
  influxdb-data:
  grafana-data:

telegraf.conf

[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 = "" 
  

 [[outputs.influxdb_v2]]
   urls = ["http://influx:8086"]
   token = "******************"
   organization = "admin"
   namepass = ["test_bucket"]
   bucket = "test_bucket"

[[outputs.file]]
  files = ["stdout", "/tmp/metrics.out"]

 [[inputs.mqtt_consumer]]
  #servers = ["tcp://docker.for.mac.localhost:1883"]
  servers = ["tcp://mosquitto:1883"]
  topics = [
    "#",
  ]
  qos = 0
  #connection_timeout = "30s"
  client_id = "telegraf"
  #persistent_session = false
  data_format = "influx

Just an idea:
It could be that the Mosquitto server is not yet running when Telegraf starts. That depends-on: in docker-compose.yml does not guarantee, as far as I know, that the application in the container is actually already fully functional.
Have you tried starting the containers independently?
Does the Mosquitto container run?
Can you access the Mosquitto container via an MQTT client, for example?

Thank you.
Mosquito is working well.
When I subscribed to mosquito_sub -t test and did mosquito_pub -t test -m hi,
The message was delivered in sub.
I’m using macOS m1 Big Sur. Can this be a problem?

  • Oh I got it! thank you so much :grin:
    I thought mosquitto would work, but it didn’t.
    When I run it independently, ‘Address not available’ occurs.
    I added listener 1883 and socket_domain ipv4 to mosquitto.conf and it works fine!
    thanks!

Reference [Address not available · Issue #2158 · eclipse/mosquitto · GitHub]