#!/usr/bin/env python3 """Считает топ ошибок и их частоту по часам.""" from __future__ import annotations import argparse import json import re import sys from collections import Counter from datetime import datetime from pathlib import Path DEFAULT_PATTERN = r"(?i)\b(error|failed|failure|critical|fatal|exception|panic)\b" ISO_TIME = re.compile(r"(?P\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2})") SYSLOG_TIME = re.compile(r"(?P[A-Z][a-z]{2}\s+\d{1,2}\s+\d{2}:\d{2}:\d{2})") NOISE = ( (re.compile(r"\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b", re.IGNORECASE), ""), (re.compile(r"\b(?:\d{1,3}\.){3}\d{1,3}\b"), ""), (re.compile(r"\b0x[0-9a-f]+\b", re.IGNORECASE), ""), (re.compile(r"\b\d+\b"), ""), ) def normalize(line: str) -> str: text = line for pattern, replacement in NOISE: text = pattern.sub(replacement, text) text = re.sub(r"\s+", " ", text).strip() return text[:220] def hour_bucket(line: str) -> str: match = ISO_TIME.search(line) if match: stamp = match.group("stamp") try: return datetime.fromisoformat(stamp).strftime("%Y-%m-%d %H") except ValueError: return stamp[:13].replace("T", " ") match = SYSLOG_TIME.search(line) if match: stamp = match.group("stamp") try: parsed = datetime.strptime(stamp, "%b %d %H:%M:%S").replace(year=datetime.now().year) return parsed.strftime("%Y-%m-%d %H") except ValueError: return stamp[:6] return "unknown" def main() -> int: parser = argparse.ArgumentParser(description="Топ ошибок в логе") parser.add_argument("--file", required=True, type=Path) parser.add_argument("--pattern", default=DEFAULT_PATTERN) parser.add_argument("--top", type=int, default=10) parser.add_argument("--json", action="store_true") args = parser.parse_args() try: matcher = re.compile(args.pattern) except re.error as error: print(f"ОШИБКА в регулярном выражении: {error}", file=sys.stderr) return 2 try: lines = args.file.read_text(encoding="utf-8", errors="replace").splitlines() except OSError as error: print(f"ОШИБКА: {error}", file=sys.stderr) return 2 messages = Counter() hours = Counter() matched = 0 for line in lines: if not matcher.search(line): continue matched += 1 messages[normalize(line)] += 1 hours[hour_bucket(line)] += 1 payload = { "file": str(args.file), "lines": len(lines), "matched": matched, "top_errors": messages.most_common(args.top), "by_hour": sorted(hours.items()), } if args.json: print(json.dumps(payload, ensure_ascii=False, indent=2)) else: print(f"Файл: {args.file}, строк: {len(lines)}, совпадений: {matched}") print("Топ ошибок:") for message, count in payload["top_errors"]: print(f" {count:6d} {message}") print("По часам:") for hour, count in payload["by_hour"]: print(f" {hour} {count}") return 0 if __name__ == "__main__": raise SystemExit(main())