Closed stegmannb closed 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")
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:
Despite the log saying so, no actual messages are sent to the broker.
Here is the log from mosquitto: