kpetremann / mqtt-exporter

Simple generic MQTT Prometheus exporter for IoT working out of the box
https://hub.docker.com/r/kpetrem/mqtt-exporter
MIT License
103 stars 29 forks source link

I can not connect #72

Closed byjosez closed 6 months ago

byjosez commented 6 months ago

Hello, it is not an error that I am reporting but, I cannot get mqtt-exporter to extract the metrics from my mqtt server hosted on a raspberry that is reachable locally, I do not see errors in the log, I execute through the console

mosquitto_sub -h 192.168.105.112 -p 1883 -v -t '#'

Ouput

solar_assistant/inverter_1/charger_source_priority/state Solar and utility simultaneously
solar_assistant/inverter_1/max_charge_current/state 60
solar_assistant/inverter_1/battery_absorption_charge_voltage/state 28.2
solar_assistant/inverter_1/to_grid_battery_voltage/state 23.0
solar_assistant/inverter_1/serial_number/state 96112307100039
solar_assistant/inverter_1/solar_power_priority/state Battery/Load/Utility
solar_assistant/inverter_1/battery_float_charge_voltage/state 27.0
solar_assistant/inverter_1/max_grid_charge_current/state 30
solar_assistant/inverter_1/output_source_priority/state Solar/Utility/Battery
solar_assistant/inverter_1/shutdown_battery_voltage/state 21.0
solar_assistant/inverter_1/back_to_battery_voltage/state 27.0

But when I try to do it through mqtt-exporter I can't get anything. I hope you can help me.

I will attach the configuration of how it is being done.

# Docker compose

mqtt-exporter:
    container_name: mqtt-exporter
    image: kpetrem/mqtt-exporter
    environment:
      - MQTT_ADDRESS=192.168.105.112
    ports:
      - 9001:9000
    restart: unless-stopped

# Prometheus config

- job_name: mqtt-exporter
    static_configs:
      - targets: ["mqtt-exporter:9001"]
# Log container 

mqtt-exporter-1             | INFO:mqtt-exporter:subscribing to "#"
mqtt-exporter-1             | INFO:mqtt-exporter:creating prometheus metric: PromMetricId(name='sensor_state', labels=())

image

Change the default port since I use portainer and it publishes on port 9000, now I get this:

# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 187.0
python_gc_objects_collected_total{generation="1"} 42.0
python_gc_objects_collected_total{generation="2"} 0.0
# HELP python_gc_objects_uncollectable_total Uncollectable objects found during GC
# TYPE python_gc_objects_uncollectable_total counter
python_gc_objects_uncollectable_total{generation="0"} 0.0
python_gc_objects_uncollectable_total{generation="1"} 0.0
python_gc_objects_uncollectable_total{generation="2"} 0.0
# HELP python_gc_collections_total Number of times this generation was collected
# TYPE python_gc_collections_total counter
python_gc_collections_total{generation="0"} 44.0
python_gc_collections_total{generation="1"} 4.0
python_gc_collections_total{generation="2"} 0.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="3",minor="10",patchlevel="13",version="3.10.13"} 1.0
# HELP process_virtual_memory_bytes Virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 2.748416e+07
# HELP process_resident_memory_bytes Resident memory size in bytes.
# TYPE process_resident_memory_bytes gauge
process_resident_memory_bytes 1.9951616e+07
# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1.70810707506e+09
# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
# TYPE process_cpu_seconds_total counter
process_cpu_seconds_total 0.54
# HELP process_open_fds Number of open file descriptors.
# TYPE process_open_fds gauge
process_open_fds 8.0
# HELP process_max_fds Maximum number of open file descriptors.
# TYPE process_max_fds gauge
process_max_fds 1.048576e+06
# HELP mqtt_message_total Counter of received messages
# TYPE mqtt_message_total counter
mqtt_message_total{topic="solar_assistant_inverter_1"} 62.0
mqtt_message_total{topic="homeassistant_sensor_solar_assistant_set_response_message_config"} 1.0
mqtt_message_total{topic="solar_assistant_total"} 6.0
# HELP mqtt_message_created Counter of received messages
# TYPE mqtt_message_created gauge
mqtt_message_created{topic="solar_assistant_inverter_1"} 1.7081070759833767e+09
mqtt_message_created{topic="homeassistant_sensor_solar_assistant_set_response_message_config"} 1.7081070759885392e+09
mqtt_message_created{topic="solar_assistant_total"} 1.7081070766748183e+09
# HELP mqtt_state metric generated from MQTT message.
# TYPE mqtt_state gauge
mqtt_state{topic="solar_assistant_inverter_1"} 0.0
mqtt_state{topic="solar_assistant_total"} 100.0
kpetremann commented 6 months ago

Hi @byjosez,

At first look, I think you are using the wrong port. If your Prometheus instance is also in a container, then mqtt-exporter is receiving the request from internal Docker network bridge, so you should use the port 9000 on which mqtt-exporter is actually listening on.

The port 9001 being for requests from outside your Docker environment, like doing a curl from the host or your computer.

I hope it helps.

byjosez commented 6 months ago

Hi @byjosez,

At first look, I think you are using the wrong port. If your Prometheus instance is also in a container, then mqtt-exporter is receiving the request from internal Docker network bridge, so you should use the port 9000 on which mqtt-exporter is actually listening on.

The port 9001 being for requests from outside your Docker environment, like doing a curl from the host or your computer.

I hope it helps.

Yes, I changed the port to 9000 and I get those metrics that I published last in the post, is that really what I should export? I don't see the data that I published when I run

mosquitto_sub -h 192.168.105.112 -p 1883 -v -t '#'

kpetremann commented 6 months ago

You don't have metrics because it is not following the format used by mqtt-exporter by default. I don't know about solar assistant, but it seems the metrics are very similar to ESPhome.

Maybe try to enable ESPhome compatibility: https://github.com/kpetremann/mqtt-exporter?tab=readme-ov-file#esphome

You would just need to add the following environment variable: ESPHOME_TOPIC_PREFIXES="solar_assistant"

byjosez commented 6 months ago

You don't have metrics because it is not following the format used by mqtt-exporter by default. I don't know about solar assistant, but it seems the metrics are very similar to ESPhome.

Maybe try to enable ESPhome compatibility: https://github.com/kpetremann/mqtt-exporter?tab=readme-ov-file#esphome

You would just need to add the following environment variable: ESPHOME_TOPIC_PREFIXES="solar_assistant"

It doesn't work, I had to use node-red to then export it to influxdb and show it in grafana, thank you very much for your time bro.