From df2d881b4da4dbf2a1ab3b6c507213cc26ca8796 Mon Sep 17 00:00:00 2001 From: Ties de Kock Date: Mon, 11 Dec 2023 20:22:37 +0100 Subject: [PATCH] Log modem log items to console Log the modem log items to console. This prevents log lines from being lost when the modem resets. --- CHANGELOG.md | 5 +++++ sagemcom_f3896_client/exporter.py | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8054237..91d3e48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,3 +4,8 @@ index page. * Build with Python 3.12 * Update pre-commit settings + * Log new modem log items with `f3896.eventlog` tag: +``` +INFO:f3896.eventlog:2023-12-09T19:46:45+00:00 [notice]: US profile assignment change. US Chan ID: 27; Previous Profile: 10 13; New Profile: 11 13.;CM-MAC=44:05:de:ad:be:ef;CMTS-MAC=00:01:de:ad:be:ef;CM-QOS=1.1;CM-VER=3.1; +INFO:f3896.eventlog:2023-12-09T18:36:26+00:00 [notice]: US profile assignment change. US Chan ID: 27; Previous Profile: 9 13; New Profile: 10 13.;CM-MAC=44:05:de:ad:be:ef;CMTS-MAC=00:01:de:ad:be:ef;CM-QOS=1.1;CM-VER=3.1; +``` diff --git a/sagemcom_f3896_client/exporter.py b/sagemcom_f3896_client/exporter.py index 339fbaf..8f7e33d 100644 --- a/sagemcom_f3896_client/exporter.py +++ b/sagemcom_f3896_client/exporter.py @@ -1,7 +1,7 @@ import asyncio import logging import os -from typing import List +from typing import List, Set import aiohttp import click @@ -18,12 +18,14 @@ UpstreamProfileMessage, ) from sagemcom_f3896_client.models import ( + EventLogItem, ModemDownstreamChannelResult, ModemUpstreamChannelResult, ) from sagemcom_f3896_client.profile_messages import ProfileMessageStore LOG = logging.getLogger(__name__) +MODEM_LOG = logging.getLogger("modem.eventlog") MODEM_METRICS_DURATION = Summary( "modem_metrics_processing_seconds", "Time spent processing modem metrics" @@ -44,6 +46,7 @@ class Exporter: modem_upstreams: List[ModemUpstreamChannelResult] = [] profile_messages: ProfileMessageStore + previous_logs: Set[EventLogItem] = set() def __init__(self, client: SagemcomModemSessionClient, port: int): self.client = client @@ -373,6 +376,16 @@ async def __log_based_metrics(self, registry: CollectorRegistry) -> None: log_lines = await self.client.modem_event_log() for line in log_lines: metric_log_by_priority.labels(priority=line.priority).inc() + + # print the new log message + current_messages = set(log_lines) + new_messages = current_messages - self.previous_logs + for msg in sorted(new_messages, reverse=True): + MODEM_LOG.info( + "%s [%s]: %s", msg.time.isoformat(), msg.priority, msg.message + ) + self.previous_logs = current_messages + # parse the log lines log_messages = [line.parse() for line in reversed(log_lines)]