meshtastic-metrics-exporter/exporter/processors.py

42 lines
1.5 KiB
Python
Raw Normal View History

import redis
2024-06-23 12:15:31 -07:00
from meshtastic.mesh_pb2 import MeshPacket
from prometheus_client import CollectorRegistry, Counter
2024-06-23 12:15:31 -07:00
from exporter.registry import ProcessorRegistry
class MessageProcessor:
def __init__(self, registry: CollectorRegistry, redis_client: redis.Redis):
2024-06-23 12:15:31 -07:00
self.registry = registry
self.redis_client = redis_client
self.counter = Counter('mesh_packets', 'Number of mesh packets processed',
['source_id', 'source_short_name', 'source_long_name', 'portnum'],
registry=self.registry)
2024-06-23 12:15:31 -07:00
def process(self, mesh_packet: MeshPacket):
port_num = mesh_packet.decoded.portnum
payload = mesh_packet.decoded.payload
processor = ProcessorRegistry.get_processor(port_num)(self.registry, self.redis_client)
2024-06-23 12:15:31 -07:00
client_details = self._get_client_details(mesh_packet)
self.counter.labels(
source_id=client_details['id'],
source_short_name=client_details['short_name'],
source_long_name=client_details['long_name'],
portnum=port_num
).inc()
processor.process_packet(payload)
def _get_client_details(self, mesh_packet: MeshPacket):
from_id = mesh_packet['from']
details = self.redis_client.hgetall(f"node:{from_id}")
if details:
return details
return {
'id': from_id,
'short_name': 'Unknown',
'long_name': 'Unknown',
}