Skip to content

Commit

Permalink
git - Merge pull request #6 from DinoTools/new_filepatterns
Browse files Browse the repository at this point in the history
Add new filename patterns
  • Loading branch information
phibos authored Dec 6, 2024
2 parents ef4e844 + 77a45ef commit 1b26d97
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
## RouterOS Log Exporter

/config.yaml
/logs/

## MKDocs

Expand Down
2 changes: 1 addition & 1 deletion config.dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ outputs:
queue_size: 100
file_config:
dir: "./logs"
filename: "{hostname}.json"
filename: "{{hostname}}-{{timestamp:%Y-%m-%d}}.json"

devices:
- hostname: 192.168.0.1
Expand Down
52 changes: 43 additions & 9 deletions routeros_log_exporter/output/file.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# SPDX-FileCopyrightText: PhiBo DinoTools (2024)
# SPDX-License-Identifier: GPL-3.0-or-later

from datetime import datetime
import logging
import os.path
from pprint import pformat
import queue
import re
from typing import Any, Dict, List, TextIO
from datetime import datetime


from . import Output
from .format import Format
Expand All @@ -18,10 +21,35 @@
class FileOutput(Output):
name = "file"

def __init__(self, filename_pattern: str, output_format: Format):
def __init__(self, filename_template: str, output_format: Format):
super().__init__()

self._filename_pattern: str = filename_pattern
self._filename_template: str = filename_template
self._filename_patterns: Dict[str, Dict[str, Any]] = {}
for m in re.finditer(r"{{(?P<type>[^}:]+)(:(?P<params>[^}]+))?}}", self._filename_template):
pattern_type = m.group("type")
pattern = m.group(0)
pattern_config = {
"type": pattern_type,
}
if pattern_type == "timestamp":
pattern_config["format"] = m.group("params")
elif pattern_type in ("hostname",):
# Patterns without params
pass
else:
logger.warning(
f"Unknown pattern '{pattern_type}' "
f"found in filename template: {self._filename_template}"
)
continue

self._filename_patterns[pattern] = pattern_config
logger.debug(
f"Found pattern '{pattern}' in filename template '{filename_template}'"
f" with config {pformat(pattern_config)}"
)

self._fp_cache: Dict[str, List] = {}
self._clean_fp_lastrun = datetime.now()

Expand All @@ -44,7 +72,7 @@ def from_config(cls, config: Dict[str, Any]) -> Output:
if not isinstance(raw_log_dir, str):
raise ConfigError("log_dir not set or not a string")
log_dir = raw_log_dir
filename_pattern = os.path.join(log_dir, filename)
filename_template = os.path.join(log_dir, filename)

format_name = config.get("format")
if not isinstance(format_name, str):
Expand All @@ -55,7 +83,7 @@ def from_config(cls, config: Dict[str, Any]) -> Output:
raise ConfigError(f"Unable to find format with name '{format_name}'")

return cls(
filename_pattern=filename_pattern,
filename_template=filename_template,
output_format=format_cls(config=config.get("format_config", {}))
)

Expand Down Expand Up @@ -86,11 +114,17 @@ def _clean_fp(self):
logger.info("File cache cleaning finished")

def write(self, message: LogMessage):
metadata = {
"hostname": message.fetcher.hostname,
}
filename = self._filename_template
for pattern, pattern_config in self._filename_patterns.items():
if pattern_config["type"] == "hostname":
data = message.fetcher.hostname
elif pattern_config["type"] == "timestamp":
data = message.timestamp.strftime(pattern_config["format"])
else:
continue

filename = filename.replace(pattern, data)

filename = self._filename_pattern.format(**metadata)
fp = self._get_fp(filename)
fp.write(self.output_format.process(message.as_dict))

Expand Down

0 comments on commit 1b26d97

Please sign in to comment.