Files
multiarch-docker-images/log-alert/fetchers/parseable.py
T
thomas a5337c3d29 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.
2026-08-30 23:22:38 +02:00

65 lines
2.5 KiB
Python

"""Parseable log fetcher implementation."""
import datetime
import logging
from time import time
import requests
from typing import Dict, Any, List
from .base import LogFetcher
logger = logging.getLogger("log-alert")
class ParseableLogFetcher(LogFetcher):
"""Concrete implementation for fetching logs from Parseable."""
def __init__(self, config: Dict[str, Any]):
self.url = config["url"]
self.dataset = config["dataset"]
self.user = config.get("user")
self.password = config.get("password")
self.last_fetched_time = int(time()) # Initialize with current time
def fetch_logs(self, filters: Dict[str, Any]) -> List[Dict[str, Any]]:
"""Fetch logs from Parseable without time range."""
old_time = self.last_fetched_time
self.last_fetched_time = int(time())
return self.fetch_logs_time_range(filters, old_time, self.last_fetched_time)
def fetch_logs_time_range(self, filters: Dict[str, Any], start_time: int, end_time: int) -> List[Dict[str, Any]]:
"""Fetch logs from Parseable within the specified time range."""
query = 'SELECT * FROM \''
query += f'{self.dataset}\' WHERE '
labelNum = 0
for label in filters.get("labels", {}):
if labelNum > 0:
query += ' AND '
query += f'{label}=\'{filters["labels"][label]}\''
labelNum += 1
if "text" in filters:
if labelNum > 0:
query += ' AND '
query += f'log LIKE \'%{filters["text"]}%\''
logger.debug(f"Executing Parseable query: {query}")
payload = {
"query": query,
"startTime": datetime.datetime.fromtimestamp(start_time).strftime("%Y-%m-%dT%H:%M:%SZ"),
"endTime": datetime.datetime.fromtimestamp(end_time).strftime("%Y-%m-%dT%H:%M:%SZ")
}
try:
response = requests.post(f"{self.url}/api/v1/query", json=payload, auth=(self.user, self.password), headers = { "Content-Type": "application/json" })
response.raise_for_status()
data = response.json()
logs = []
for item in data:
logs.append({
"timestamp": item.get("p_timestamp"),
"log": item.get("log"),
"labels": {k: v for k, v in item.items() if k not in ["p_timestamp", "log"]}
})
return logs
except requests.exceptions.RequestException as e:
logger.error(f"Error fetching logs from Parseable: {e}")
return []