Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter log messages #11

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions sagemcom_f3896_client/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
DownstreamProfileMessage,
RebootMessage,
UpstreamProfileMessage,
is_login_message,
)
from sagemcom_f3896_client.models import (
EventLogItem,
Expand All @@ -42,13 +43,20 @@ class Exporter:
app: web.Application
port: int

include_login_messages: bool = False

modem_downstreams: List[ModemDownstreamChannelResult] = []
modem_upstreams: List[ModemUpstreamChannelResult] = []

profile_messages: ProfileMessageStore
previous_logs: Set[EventLogItem] = set()

def __init__(self, client: SagemcomModemSessionClient, port: int):
def __init__(
self,
client: SagemcomModemSessionClient,
port: int,
include_login_messages: bool = False,
):
self.client = client
self.app = web.Application()
self.port = port
Expand Down Expand Up @@ -374,6 +382,11 @@ async def __log_based_metrics(self, registry: CollectorRegistry) -> None:
)

log_lines = await self.client.modem_event_log()
log_lines = [
line
for line in log_lines
if self.include_login_messages or not is_login_message(line)
]
for line in log_lines:
metric_log_by_priority.labels(priority=line.priority).inc()

Expand Down Expand Up @@ -451,7 +464,11 @@ async def __log_based_metrics(self, registry: CollectorRegistry) -> None:

async def index(self, _: web.Request) -> str:
"""Serve an index page."""
logs = await self.client.modem_event_log()
logs = [
line
for line in await self.client.modem_event_log()
if self.include_login_messages or not is_login_message(line)
]

return web.Response(
text=templates.index_template(logs),
Expand All @@ -467,24 +484,37 @@ async def index(self, _: web.Request) -> str:
default=os.environ.get("MODEM_URL", "http://192.168.100.1"),
help="URL to modem - default from MODEM_URL",
)
@click.option("--include-login-messages", is_flag=True, default=False)
@click.option("-p", "--port", default=8080, help="Port to listen on")
@click.option(
"--password",
default=os.environ.get("MODEM_PASSWORD", ""),
help="Password - default from MODEM_PASSWORD",
)
def main(verbose, port: int, password: str, base_url: str):
asyncio.run(async_main(verbose, port, password, base_url))
def main(
verbose, port: int, password: str, base_url: str, include_login_messages: bool
):
asyncio.run(
async_main(
verbose,
port,
password,
base_url,
include_login_messages=include_login_messages,
)
)


async def async_main(verbose, port: int, password: str, base_url: str):
async def async_main(
verbose, port: int, password: str, base_url: str, include_login_messages: bool
):
if verbose > 0:
import logging

logging.basicConfig(level=logging.DEBUG)

async with SagemcomModemClient(base_url, password) as client:
exporter = Exporter(client, port)
exporter = Exporter(client, port, include_login_messages=include_login_messages)
await exporter.run()


Expand Down
4 changes: 4 additions & 0 deletions sagemcom_f3896_client/log_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class RebootMessage:
REBOOT_RE = re.compile(r"^Cable Modem Reboot because of - (?P<message>.*)$")


def is_login_message(item) -> bool:
return "GUI Login Status - Login Success from LAN interface" in item.message


def parse_message(message: str) -> Optional[ParsedMessage]:
"""Parse a message in the modem log"""
match = CM_STATUS_OFDM_RE.match(message)
Expand Down