feat: Refactor log-alert application into modular structure

- Introduced alerters package with base Alerter class and implementations for LogAlerter and GotifyAlerter.
- Created fetchers package with abstract LogFetcher class and implementations for FileLogFetcher, LokiLogFetcher, and ParseableLogFetcher.
- Added filters package with base Filter class and implementations for RegexpFilter and GeolocationFilter.
- Implemented rules package with base AlertRule class and SimpleAlertRule for alerting logic.
- Enhanced configuration handling with utilities for loading and validating JSON configuration.
- Updated logging setup for better logging management.
- Modified main application logic to utilize the new modular structure, improving maintainability and readability.
- Updated config.json and config.schema.json to reflect changes in alerting and fetching configurations.
- Added python-dateutil to requirements for date parsing functionality.
This commit is contained in:
2026-08-30 23:22:38 +02:00
parent b7f1b97434
commit a5337c3d29
23 changed files with 627 additions and 261 deletions
+7
View File
@@ -0,0 +1,7 @@
"""Alerters package for log-alert."""
from .base import Alerter
from .log import LogAlerter
from .gotify import GotifyAlerter
__all__ = ["Alerter", "LogAlerter", "GotifyAlerter"]
+17
View File
@@ -0,0 +1,17 @@
"""Abstract base class for alerters."""
from abc import ABC, abstractmethod
class Alerter(ABC):
"""Abstract base class for alerting implementations."""
@abstractmethod
def send_alert(self, title: str, message: str) -> None:
"""Send an alert.
Args:
title: Alert title
message: Alert message
"""
pass
+30
View File
@@ -0,0 +1,30 @@
"""Gotify alerter implementation."""
import logging
import requests
from typing import Dict, Any
from .base import Alerter
logger = logging.getLogger("log-alert")
class GotifyAlerter(Alerter):
"""Concrete implementation for Gotify alert manager."""
def __init__(self, config: Dict[str, Any]):
self.url = config["url"]
self.token = config.get("token")
def send_alert(self, title: str, message: str) -> None:
"""Send an alert to Gotify."""
payload = {
"title": title,
"message": message,
"priority": 5
}
try:
response = requests.post(f"{self.url}?token={self.token}", json=payload)
response.raise_for_status()
except requests.exceptions.RequestException as e:
logger.error(f"Error sending alert to Gotify: {e}")
+20
View File
@@ -0,0 +1,20 @@
"""Standard output alerter implementation."""
import logging
import requests
from typing import Dict, Any
from .base import Alerter
logger = logging.getLogger("log-alert")
class LogAlerter(Alerter):
"""Concrete implementation for log alert manager."""
def __init__(self, config: Dict[str, Any]):
pass
def send_alert(self, title: str, message: str) -> None:
"""Send an alert to the log."""
logger.info(f"[ALERT] {title} / {message}")