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

AttributeError: 'Properties' object has no attribute 'AssignedClientIdentifier' #77

Closed rduivenvoorde closed 5 months ago

rduivenvoorde commented 5 months ago

Hi Thanks for you package!

Testing a vanilla pip install in /tmp I get:

ERROR:mqtt-exporter:Caught exception in on_connect: 'Properties' object has no attribute 'AssignedClientIdentifier'
Traceback (most recent call last):
  File "/tmp/mqtt-exporter/exporter.py", line 6, in <module>
    main()
  File "/tmp/mqtt-exporter/mqtt_exporter/main.py", line 460, in main
    client.loop_forever()
  File "/tmp/venv/lib/python3.11/site-packages/paho/mqtt/client.py", line 2291, in loop_forever
    rc = self._loop(timeout)
         ^^^^^^^^^^^^^^^^^^^
  File "/tmp/venv/lib/python3.11/site-packages/paho/mqtt/client.py", line 1680, in _loop
    rc = self.loop_read()
         ^^^^^^^^^^^^^^^^
  File "/tmp/venv/lib/python3.11/site-packages/paho/mqtt/client.py", line 2094, in loop_read
    rc = self._packet_read()
         ^^^^^^^^^^^^^^^^^^^
  File "/tmp/venv/lib/python3.11/site-packages/paho/mqtt/client.py", line 3137, in _packet_read
    rc = self._packet_handle()
         ^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/venv/lib/python3.11/site-packages/paho/mqtt/client.py", line 3809, in _packet_handle
    return self._handle_connack()
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/venv/lib/python3.11/site-packages/paho/mqtt/client.py", line 3929, in _handle_connack
    on_connect(
  File "/tmp/mqtt-exporter/mqtt_exporter/main.py", line 62, in subscribe
    user_data["client_id"] = properties.AssignedClientIdentifier
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Properties' object has no attribute 'AssignedClientIdentifier'

Environment:

$ pip freeze
paho-mqtt==2.0.0
prometheus_client==0.20.0

$ uname -a
Linux 6.6.15-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.6.15-2 (2024-02-04) x86_64 GNU/Linux

python --version
Python 3.11.8

Ah, digging into it a little, it appears that we really need a client-id nowadays. So either set a default client id in settings.py (e.g. mqtt-exporter), or change the start instruction to:

MQTT_CLIENT_ID=mqtt-exporter MQTT_ADDRESS=192.168.0.1 python exporter.py
kpetremann commented 5 months ago

Hi @rduivenvoorde,

I'll check this! I don't have the issue on my setup, I'll check why!!

Could you please share your entire config of mqtt-exporter (without the passwords of course).

kpetremann commented 5 months ago

also, which MQTT server are you using? Because it is supposed to return a Client ID:

    user_data = {"client_id": settings.MQTT_CLIENT_ID}
    if not settings.MQTT_CLIENT_ID:
        user_data["client_id"] = properties.AssignedClientIdentifier
rduivenvoorde commented 5 months ago

I just blindly run the python example (after git clone) as described here: https://github.com/kpetremann/mqtt-exporter?tab=readme-ov-file#using-python

Not sure where the 'properties' is filled?

Running my own home server mqtt:

$ apt show mosquitto Package: mosquitto Version: 2.0.11-1.2+deb12u1

kpetremann commented 5 months ago

Thanks. I can reproduce the issue as well.

Looks like when MQTT_V5_PROTOCOL=True, the Client ID is provided by the server and retrieved using properties.AssignedClientIdentifier.

When it is set to False, the property AssignedClientIdentifier is not returned by the server. I'll check with the documentation to see if it is defined in a different property or if it is a feature of MQTTv5 (in that case, I'll put a default value to the Client ID).

kpetremann commented 5 months ago

@rduivenvoorde It should be better with this branch: https://github.com/kpetremann/mqtt-exporter/tree/fix_mqtt_client_id

kpetremann commented 5 months ago

hello @rduivenvoorde,

The fix has been merged and released. Please confirm it solves the issue on your side.

kpetremann commented 5 months ago

closing the issue. Don't hesitate to re-open it if the fix is not enough.