jamiebegin / metrics2mqtt

Publish cross-platorm system performance metrics to a MQTT broker.
MIT License
63 stars 9 forks source link

The log does not show an error on authentication failure #4

Closed stegmannb closed 4 years ago

stegmannb commented 4 years ago

When I try to connect to a broker using username and password authentication and I supply wrong credentials the log does not show any error.

When I try to connect like this: /usr/bin/python3 /usr/local/bin/metrics2mqtt --broker broker.local --username metrics2mqtt --password not_my_password --cpu=60 --du=/ --interval 60 --net=eth0,15 -vvvvv

The log output shows:

2020-08-05 20:58:14,377 - metrics2mqtt - INFO - Connected to MQTT broker. 2020-08-05 20:58:14,379 - metrics2mqtt - DEBUG - Publishing '{'name': 'node05 CPU', 'unique_id': 'node05_cpu', 'qos': 1, 'icon': 'mdi:chip', 'unit_of_measurement': '%', 'availability_topic': 'homeassistant/sensor/node05/cpu/availability', 'json_attributes_topic': 'homeassistant/sensor/node05/cpu/attributes', 'state_topic': 'homeassistant/sensor/node05/cpu/state'}' to topic 'homeassistant/sensor/node05/cpu/config'. 2020-08-05 20:58:14,380 - metrics2mqtt - INFO - Publishing 'online' to topic 'homeassistant/sensor/node05/cpu/availability'. 2020-08-05 20:58:14,380 - metrics2mqtt - DEBUG - Publishing '{'name': 'node05 Disk Usage (/ Volume)', 'unique_id': 'node05_disk_usage', 'qos': 1, 'icon': 'mdi:harddisk', 'unit_of_measurement': '%', 'availability_topic': 'homeassistant/sensor/node05/disk_usage/availability', 'json_attributes_topic': 'homeassistant/sensor/node05/disk_usage/attributes', 'state_topic': 'homeassistant/sensor/node05/disk_usage/state'}' to topic 'homeassistant/sensor/node05/disk_usage__/config'. 2020-08-05 20:58:14,381 - metrics2mqtt - INFO - Publishing 'online' to topic 'homeassistant/sensor/node05/disk_usage__/availability'. 2020-08-05 20:58:14,381 - metrics2mqtt - DEBUG - Publishing '{'name': 'node05 Network (eth0)', 'unique_id': 'node05_net_eth0', 'qos': 1, 'icon': 'mdi:server-network', 'unit_of_measurement': 'kb/s', 'availability_topic': 'homeassistant/sensor/node05/net_eth0/availability', 'json_attributes_topic': 'homeassistant/sensor/node05/net_eth0/attributes', 'state_topic': 'homeassistant/sensor/node05/net_eth0/state'}' to topic 'homeassistant/sensor/node05/net_eth0/config'. 2020-08-05 20:58:14,382 - metrics2mqtt - INFO - Publishing 'online' to topic 'homeassistant/sensor/node05/net_eth0/availability'.

Despite the log saying so, no actual messages are sent to the broker.

Here is the log from mosquitto:

1596657017: New connection from 192.168.1.15 on port 1883. 1596657017: Sending CONNACK to 192.168.1.15 (0, 5) 1596657017: Socket error on client , disconnecting.

stegmannb commented 4 years ago

I think self.client.connect(self.broker_host) does not throw an exception when the authentication fails. You need the on_connect callback in order to find out if the connection succeeded.

Something like:

    def connect(self):
        self.client = mqtt.Client(self.system_name + '_psutilmqtt')
        try:
            if self.username or self.password:
                self.client.username_pw_set(self.username, self.password)
            self.client.on_connect = self.on_connect
            self.client.connect(self.broker_host)
            self.client.loop_start()
        except Exception as e:
            logger.error("Error while trying to connect to MQTT broker.")
            logger.error(str(e))
            raise

    def on_connect(self, client, userdata, flags, rc):
        if rc == 0:
            logger.info("Connected to MQTT broker.")
            // start monitor
        elif rc == 1:
            logger.info("Connection refused – incorrect protocol version")
        elif rc == 2:
            logger.info("Connection refused – invalid client identifier")
        elif rc == 3:
            logger.info("Connection refused – server unavailable")
        elif rc == 4:
            logger.info("Connection refused – bad username or password")
        elif rc == 5:
            logger.info("Connection refused – not authorised")
        else:
            logger.info("Connection refused")