diff --git a/docker/postgres/init.sql b/docker/postgres/init.sql index 61a4408..92321fb 100644 --- a/docker/postgres/init.sql +++ b/docker/postgres/init.sql @@ -25,5 +25,6 @@ CREATE TABLE IF NOT EXISTS client_details short_name VARCHAR, long_name VARCHAR, hardware_model VARCHAR, - role VARCHAR + role VARCHAR, + mqtt_status VARCHAR default 'none' ); diff --git a/exporter/processors.py b/exporter/processors.py index 0b566c8..a1ead5a 100644 --- a/exporter/processors.py +++ b/exporter/processors.py @@ -461,9 +461,9 @@ class TraceRouteAppProcessor(Processor): traceroute.ParseFromString(payload) if traceroute.route: route = traceroute.route - self.metrics.route_discovery_counter.labels( + self.metrics.route_discovery_gauge.labels( **client_details.to_dict() - ).inc(len(route)) + ).set(len(route)) @ProcessorRegistry.register_processor(PortNum.NEIGHBORINFO_APP) diff --git a/exporter/registry.py b/exporter/registry.py index 4d2519f..bf43091 100644 --- a/exporter/registry.py +++ b/exporter/registry.py @@ -324,7 +324,7 @@ class _Metrics: ) def _init_route_discovery_metrics(self): - self.route_discovery_counter = Counter( + self.route_discovery_gauge = Gauge( 'route_length', 'Number of nodes in the route', self._get_common_labels(), diff --git a/main.py b/main.py index 2236d7b..f4c1690 100644 --- a/main.py +++ b/main.py @@ -35,11 +35,23 @@ def handle_connect(client, userdata, flags, reason_code, properties): client.subscribe(os.getenv('MQTT_TOPIC', 'msh/israel/#')) +def update_node_status(node_number, status): + with connection_pool.connection() as conn: + with conn.cursor() as cur: + cur.execute("INSERT INTO client_details (node_id, mqtt_status) VALUES (%s, %s)" + "ON CONFLICT(node_id)" + "DO UPDATE SET mqtt_status = %s", (node_number, status, status)) + conn.commit() + + def handle_message(client, userdata, message): current_timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') print(f"Received message on topic '{message.topic}' at {current_timestamp}") if '/stat/' in message.topic: - print(f"Filtered out message from topic containing '/stat/': {message.topic}") + user_id = message.topic.split('/')[-1] # Hexadecimal user ID + if user_id[0] == '!': + node_number = str(int(user_id[1:], 16)) + update_node_status(node_number, message.payload.decode('utf-8')) return envelope = ServiceEnvelope() diff --git a/requirements.txt b/requirements.txt index 4ce87a1..bfe29f9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,5 @@ unishox2-py3~=1.0.0 cryptography~=42.0.8 psycopg~=3.1.19 psycopg_pool~=3.2.2 -meshtastic~=2.3.13 \ No newline at end of file +meshtastic~=2.3.13 +psycopg-binary~=3.1.20 \ No newline at end of file