From ff5b181d6c31d963efb981aa980bb49f90b0bf09 Mon Sep 17 00:00:00 2001 From: Github GPG acces Date: Fri, 15 Mar 2024 13:18:50 +0100 Subject: [PATCH 1/2] Add MQTT hostname configuration added the possibility of configuring the hostname of the MQTT topic --- docker-compose/glances.conf | 1 + glances/exports/mqtt/__init__.py | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) mode change 100644 => 100755 docker-compose/glances.conf mode change 100644 => 100755 glances/exports/mqtt/__init__.py diff --git a/docker-compose/glances.conf b/docker-compose/glances.conf old mode 100644 new mode 100755 index f07ff1c0..1aa17314 --- a/docker-compose/glances.conf +++ b/docker-compose/glances.conf @@ -595,6 +595,7 @@ queue=glances_queue # Configuration for the --export mqtt option host=localhost port=8883 +hostname=NONE tls=false user=guest password=guest diff --git a/glances/exports/mqtt/__init__.py b/glances/exports/mqtt/__init__.py old mode 100644 new mode 100755 index 9055fa6f..54d0a8f2 --- a/glances/exports/mqtt/__init__.py +++ b/glances/exports/mqtt/__init__.py @@ -38,13 +38,17 @@ class Export(GlancesExport): # Load the MQTT configuration file self.export_enable = self.load_conf( - 'mqtt', mandatories=['host', 'password'], options=['port', 'user', 'topic', 'tls', 'topic_structure'] + 'mqtt', mandatories=['host', 'password'], options=['port', 'user', 'hostname', 'topic', 'tls', 'topic_structure'] ) if not self.export_enable: exit('Missing MQTT config') # Get the current hostname - self.hostname = socket.gethostname() + self.hostname = (self.hostname or socket.gethostname()) + if self.hostname in ['NONE']: + self.hostname = socket.gethostname() + else: + self.hostname = self.hostname self.port = int(self.port) or 8883 self.topic = self.topic or 'glances' self.user = self.user or 'glances' From ad442736452f86198cb8705f98b3284cdcb8ed12 Mon Sep 17 00:00:00 2001 From: Github GPG acces Date: Fri, 15 Mar 2024 21:45:20 +0100 Subject: [PATCH 2/2] changes structure --- docker-compose/glances.conf | 1 - glances/exports/mqtt/__init__.py | 14 +++++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/docker-compose/glances.conf b/docker-compose/glances.conf index 1aa17314..f07ff1c0 100755 --- a/docker-compose/glances.conf +++ b/docker-compose/glances.conf @@ -595,7 +595,6 @@ queue=glances_queue # Configuration for the --export mqtt option host=localhost port=8883 -hostname=NONE tls=false user=guest password=guest diff --git a/glances/exports/mqtt/__init__.py b/glances/exports/mqtt/__init__.py index 54d0a8f2..8563ccdd 100755 --- a/glances/exports/mqtt/__init__.py +++ b/glances/exports/mqtt/__init__.py @@ -38,17 +38,13 @@ class Export(GlancesExport): # Load the MQTT configuration file self.export_enable = self.load_conf( - 'mqtt', mandatories=['host', 'password'], options=['port', 'user', 'hostname', 'topic', 'tls', 'topic_structure'] + 'mqtt', mandatories=['host', 'password'], options=['port', 'user', 'devicename', 'topic', 'tls', 'topic_structure'] ) if not self.export_enable: exit('Missing MQTT config') # Get the current hostname - self.hostname = (self.hostname or socket.gethostname()) - if self.hostname in ['NONE']: - self.hostname = socket.gethostname() - else: - self.hostname = self.hostname + self.devicename = self.devicename or socket.gethostname() self.port = int(self.port) or 8883 self.topic = self.topic or 'glances' self.user = self.user or 'glances' @@ -69,7 +65,7 @@ class Export(GlancesExport): if not self.export_enable: return None try: - client = paho.Client(client_id='glances_' + self.hostname, clean_session=False) + client = paho.Client(client_id='glances_' + self.devicename, clean_session=False) client.username_pw_set(username=self.user, password=self.password) if self.tls: client.tls_set(certifi.where()) @@ -93,7 +89,7 @@ class Export(GlancesExport): for sensor, value in zip(columns, points): try: sensor = [whitelisted(name) for name in sensor.split('.')] - to_export = [self.topic, self.hostname, name] + to_export = [self.topic, self.devicename, name] to_export.extend(sensor) topic = '/'.join(to_export) @@ -102,7 +98,7 @@ class Export(GlancesExport): logger.error("Can not export stats to MQTT server (%s)" % e) elif self.topic_structure == 'per-plugin': try: - topic = '/'.join([self.topic, self.hostname, name]) + topic = '/'.join([self.topic, self.devicename, name]) sensor_values = dict(zip(columns, points)) # Build the value to output