-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmesh-collectd.py
More file actions
498 lines (416 loc) · 16.7 KB
/
mesh-collectd.py
File metadata and controls
498 lines (416 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import ssl
import json
import time
import os
import sqlite3
import threading
from queue import Queue
from datetime import datetime
from paho.mqtt.client import Client
def load_config(config_path="config.json"):
try:
with open(config_path, "r") as config_file:
return json.load(config_file)
except Exception as e:
print(f"Error loading config file: {e}")
exit(1)
def log_message(msg_type, node_id, details):
"""
Standardized logging function that formats all messages consistently.
Args:
msg_type (str): Type of message (MESSAGE, NODEINFO, NEIGHBORS, POSITION, TRACEROUTE)
node_id: ID of the node
details (dict): Additional details to log
"""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
base_info = f"[{timestamp}] [{msg_type}] Node {node_id}"
if msg_type == "MESSAGE":
print(
f"{base_info} → {details['receiver']} | "
f"Type: {details['type']} | RSSI: {details['rssi']} | "
f"SNR: {details['snr']} | Physical Sender: {details['physical_sender']}"
)
elif msg_type == "NODEINFO":
print(
f"{base_info} | "
f"Long: {details['longname']} | Short: {details['shortname']} | "
f"HW: {details['hardware']} | Role: {details['role']}"
)
elif msg_type == "NEIGHBORS":
print(f"{base_info} | Neighbor count: {details['count']}")
for neighbor in details["neighbors"]:
print(f" → Neighbor {neighbor['node_id']} | SNR: {neighbor.get('snr', 'N/A')}")
elif msg_type == "POSITION":
print(f"{base_info} | " f"Lat: {details['latitude']:.5f} | Lon: {details['longitude']:.5f}")
elif msg_type == "TRACEROUTE":
print(f"{base_info} | Route length: {len(details['route'])}")
for i in range(len(details["route"]) - 1):
print(f" {details['route'][i]} → {details['route'][i+1]}")
def init_db(db_path="mqtt_messages.db"):
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Drop and recreate nodes table with explicit PRIMARY KEY constraint
# cursor.execute("DROP TABLE IF EXISTS nodes")
cursor.execute(
"""CREATE TABLE IF NOT EXISTS nodes (
id INTEGER NOT NULL PRIMARY KEY, -- This ensures uniqueness
longname TEXT,
shortname TEXT,
hardware INTEGER,
role INTEGER,
last_seen INTEGER,
latitude REAL,
longitude REAL
)"""
)
cursor.execute(
"""CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
topic TEXT,
sender INTEGER,
receiver INTEGER,
physical_sender INTEGER,
timestamp INTEGER,
rssi REAL,
snr REAL,
type TEXT
)"""
)
cursor.execute(
"""CREATE TABLE IF NOT EXISTS neighbors (
id INTEGER PRIMARY KEY AUTOINCREMENT,
node_id INTEGER,
neighbor_id INTEGER,
snr REAL,
timestamp INTEGER,
FOREIGN KEY (node_id) REFERENCES nodes (id),
FOREIGN KEY (neighbor_id) REFERENCES nodes (id)
)"""
)
cursor.execute(
"""CREATE TABLE IF NOT EXISTS traceroutes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
from_node TEXT,
to_node TEXT,
timestamp INTEGER
)"""
)
# Add new nodes_count table
cursor.execute(
"""CREATE TABLE IF NOT EXISTS nodes_count (
id INTEGER PRIMARY KEY AUTOINCREMENT,
node_id INTEGER,
timestamp INTEGER,
count_30min INTEGER,
count_60min INTEGER,
count_120min INTEGER
)"""
)
conn.commit()
return conn
def save_nodes_count_to_db(conn, node_id, timestamp, counts):
cursor = conn.cursor()
try:
cursor.execute(
"""INSERT INTO nodes_count
(node_id, timestamp, count_30min, count_60min, count_120min)
VALUES (?, ?, ?, ?, ?)""",
(node_id, timestamp, counts.get("30min", 0), counts.get("60min", 0), counts.get("120min", 0)),
)
conn.commit()
except Exception as e:
print(f"Error saving node counts: {e}")
conn.rollback()
def save_nodeinfo_to_db(conn, node_id, longname=None, shortname=None, hardware=None, role=None, timestamp=None, latitude=None, longitude=None):
cursor = conn.cursor()
try:
# First check if the node exists
cursor.execute("SELECT longname, shortname, hardware, role, latitude, longitude FROM nodes WHERE id = ?", (node_id,))
existing = cursor.fetchone()
if existing:
# Update existing node, keeping old values when new ones aren't provided
update_values = [
longname if longname is not None else existing[0],
shortname if shortname is not None else existing[1],
hardware if hardware is not None else existing[2],
role if role is not None else existing[3],
timestamp, # Always update timestamp if provided
latitude if latitude is not None else existing[4],
longitude if longitude is not None else existing[5],
node_id
]
cursor.execute(
"""UPDATE nodes
SET longname = ?, shortname = ?, hardware = ?, role = ?,
last_seen = ?, latitude = ?, longitude = ?
WHERE id = ?""",
tuple(update_values)
)
else:
# Insert new node
cursor.execute(
"""INSERT INTO nodes
(id, longname, shortname, hardware, role, last_seen, latitude, longitude)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
(node_id, longname, shortname, hardware, role, timestamp, latitude, longitude)
)
conn.commit()
print(f"Successfully {'updated' if existing else 'inserted'} node {node_id}")
except Exception as e:
print(f"Error saving node info for node {node_id}: {e}")
conn.rollback()
def save_traceroute_to_db(conn, route, timestamp):
cursor = conn.cursor()
try:
# Process each consecutive pair in the route
for i in range(len(route) - 1):
from_node = sanitize_string(route[i])
to_node = sanitize_string(route[i + 1])
if "Unknown" not in [from_node, to_node]:
cursor.execute(
"""INSERT INTO traceroutes (from_node, to_node, timestamp)
VALUES (?, ?, ?)""",
(from_node, to_node, timestamp),
)
conn.commit()
except Exception as e:
print(f"Error saving traceroute info: {e}")
def save_message_to_db(conn, topic, sender, receiver, physical_sender, timestamp, rssi, snr, type):
cursor = conn.cursor()
cursor.execute(
"INSERT INTO messages (topic, sender, receiver, physical_sender, timestamp, rssi, snr, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
(topic, sender, receiver, physical_sender, timestamp, rssi, snr, type),
)
conn.commit()
def save_neighbors_to_db(conn, node_id, neighbors, timestamp):
cursor = conn.cursor()
try:
# First, delete old neighbor entries for this node
cursor.execute("DELETE FROM neighbors WHERE node_id = ? AND timestamp = ?", (node_id, timestamp))
# Insert new neighbor relationships
for neighbor in neighbors:
cursor.execute(
"""INSERT INTO neighbors (node_id, neighbor_id, snr, timestamp)
VALUES (?, ?, ?, ?)""",
(node_id, neighbor["node_id"], neighbor["snr"], timestamp),
)
conn.commit()
except Exception as e:
print(f"Error saving neighbor info: {e}")
def db_worker(queue, db_path):
conn = sqlite3.connect(db_path)
while True:
item = queue.get()
if item is None:
break # Stop signal
try:
if item[0] == "message":
save_message_to_db(conn, *item[1:])
elif item[0] == "nodeinfo":
save_nodeinfo_to_db(conn, *item[1:])
elif item[0] == "neighbors":
save_neighbors_to_db(conn, *item[1:])
elif item[0] == "position":
save_nodeinfo_to_db(conn, *item[1:])
elif item[0] == "traceroute":
save_traceroute_to_db(conn, *item[1:])
elif item[0] == "nodes_count":
save_nodes_count_to_db(conn, *item[1:])
except Exception as e:
print(f"Database error: {e}")
conn.close()
def hex_to_int(hex_id):
"""Convert hex node ID to integer, removing the leading '!' if present"""
try:
if hex_id.startswith("!"):
hex_id = hex_id[1:]
return int(hex_id, 16)
except (ValueError, AttributeError):
print(f"Failed to convert hex ID: {hex_id}")
return None
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
client.subscribe(config["MQTT_TOPIC"])
else:
print(f"Failed to connect, return code {rc}")
def on_disconnect(client, userdata, rc):
print("Disconnected from MQTT Broker. Attempting to reconnect...")
while True:
try:
client.reconnect()
print("Reconnected to MQTT Broker!")
break
except Exception:
time.sleep(5)
def sanitize_string(input_str):
"""
Sanitize and clean a string by:
1. Converting to ASCII (removing non-ASCII characters)
2. Stripping leading and trailing whitespaces
3. Replacing multiple whitespaces with a single space
"""
# if not isinstance(input_str, str):
# return input_str
input_str = str(input_str)
# Convert to ASCII, strip, and replace multiple spaces
return " ".join(input_str.encode("ascii", "ignore").decode("ascii").split())
def on_message(client, userdata, msg):
try:
topic = msg.topic
try:
payload = json.loads(msg.payload.decode("utf-8"))
except:
return
node_id = payload.get("from")
receiver = payload.get("to")
timestamp = payload.get("timestamp")
rssi = payload.get("rssi")
snr = payload.get("snr")
msg_type = payload.get("type")
physical_sender = hex_to_int(payload.get("sender"))
# Log base message information
log_message("MESSAGE", node_id, {"receiver": receiver, "type": msg_type, "rssi": rssi, "snr": snr, "physical_sender": physical_sender})
userdata.put(("message", topic, node_id, receiver, physical_sender, timestamp, rssi, snr, msg_type))
# Handle nodes count messages
if "nodes_count" in topic:
try:
node_id = int(topic.split("/")[-1], 16) # Convert hex node ID to int
payload = json.loads(msg.payload.decode("utf-8"))
timestamp = int(time.time())
print(
f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] [NODES_COUNT] "
f"Node {node_id} | 30min: {payload.get('30min', 0)} | "
f"60min: {payload.get('60min', 0)} | 120min: {payload.get('120min', 0)}"
)
userdata.put(("nodes_count", node_id, timestamp, payload))
return
except Exception as e:
print(f"Error processing nodes count message: {e}")
return
# Rest of the existing message handling...
try:
payload = json.loads(msg.payload.decode("utf-8"))
except:
return
if msg_type == "nodeinfo" and "payload" in payload:
node_payload = payload["payload"]
if all(k in node_payload for k in ["id", "longname", "shortname", "hardware", "role"]):
log_message(
"NODEINFO",
node_id,
{
"longname": sanitize_string(node_payload["longname"]),
"shortname": sanitize_string(node_payload["shortname"]),
"hardware": node_payload["hardware"],
"role": node_payload["role"],
},
)
userdata.put(
(
"nodeinfo",
node_id,
sanitize_string(node_payload["longname"]),
sanitize_string(node_payload["shortname"]),
node_payload["hardware"],
node_payload["role"],
timestamp,
)
)
elif msg_type == "neighborinfo" and "payload" in payload:
neighbor_payload = payload["payload"]
if "node_id" in neighbor_payload and "neighbors" in neighbor_payload:
neighbors = neighbor_payload["neighbors"]
log_message("NEIGHBORS", node_id, {"count": len(neighbors), "neighbors": neighbors})
userdata.put(("neighbors", node_id, neighbors, timestamp))
elif msg_type == "traceroute" and "payload" in payload:
route_payload = payload["payload"]
if "route" in route_payload:
route = route_payload["route"]
current_time = int(time.time())
log_message("TRACEROUTE", node_id, {"route": route})
userdata.put(("traceroute", route, current_time))
elif msg_type == "position" and "payload" in payload:
pos_payload = payload["payload"]
if all(k in pos_payload for k in ["latitude_i", "longitude_i"]):
latitude = float(int(pos_payload["latitude_i"]) * 1e-7)
longitude = float(int(pos_payload["longitude_i"]) * 1e-7)
log_message("POSITION", node_id, {"latitude": latitude, "longitude": longitude})
# Update only location information for the node
userdata.put(
(
"position",
node_id, # node_id
None, # longname (no update)
None, # shortname (no update)
None, # hardware (no update)
None, # role (no update)
timestamp, # last_seen
latitude, # latitude
longitude, # longitude
)
)
except Exception as e:
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] [ERROR] Error processing message: {e}")
def cleanup_duplicate_nodes(conn):
cursor = conn.cursor()
try:
# Find and remove duplicates, keeping the most recent entry
cursor.execute(
"""
DELETE FROM nodes
WHERE rowid NOT IN (
SELECT MAX(rowid)
FROM nodes
GROUP BY id
)
"""
)
conn.commit()
print("Cleaned up duplicate node entries")
except Exception as e:
print(f"Error cleaning up duplicates: {e}")
conn.rollback()
# Load configuration
config = load_config()
# Initialize database
db_path = "mqtt_messages.db"
db_conn = init_db(db_path)
cleanup_duplicate_nodes(db_conn)
# Create a thread-safe queue for database operations
message_queue = Queue()
# Start the database worker thread
db_thread = threading.Thread(target=db_worker, args=(message_queue, db_path), daemon=True)
db_thread.start()
# Create MQTT client
client = Client(client_id=config.get("CLIENT_ID"))
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message
# Pass the queue to the MQTT client as userdata
client.user_data_set(message_queue)
# Set username and password if provided
if config.get("MQTT_USERNAME") and config.get("MQTT_PASSWORD"):
client.username_pw_set(config["MQTT_USERNAME"], config["MQTT_PASSWORD"])
# Enable SSL if specified
if config.get("USE_SSL"):
client.tls_set(cert_reqs=ssl.CERT_NONE)
client.tls_insecure_set(True)
# Connect to the MQTT broker
client.connect(config["MQTT_BROKER"], config["MQTT_PORT"], 60)
# Start MQTT loop
client.loop_start()
# Keep the script running
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Disconnecting...")
finally:
# Stop the MQTT client
client.loop_stop()
client.disconnect()
# Stop the database worker
message_queue.put(None) # Signal the worker to exit
db_thread.join()
print("Disconnected and database closed.")