0dc291ad4db3800af2b35c385d99dce77315d9f6
1#
2# load_parsers.py
3#
4# Search for and load files which parse logs for particular services
5#
6
7import imp
8import importlib
9import os
10import glob
11from pathlib import Path
12from sys import path
13from typing import NamedTuple
14
15parser_dir = "/usr/share/logparse/"
16main_module = "__init__"
17default_parsers = ["cron", "httpd", "mem", "postfix", "smbd", "sshd", "sudo", "sysinfo", "temperature", "zfs"]
18
19import logging
20logger = logging.getLogger(__name__)
21
22class Parser():
23 def __init__(self, name, path=None, info=None):
24 self.name = str(name)
25 self.path = Path(path) if path else None
26 self.info = dict(info) if info else None
27
28def findall():
29 logger.debug("Searching for parsers in {0}".format(parser_dir))
30 path.append(os.path.abspath(parser_dir))
31 parsers = []
32 parser_candidates = os.listdir(parser_dir)
33 for parser_name in parser_candidates:
34 location = os.path.join(parser_dir, parser_name)
35 if not os.path.isdir(location) or not main_module + '.py' in os.listdir(location):
36 logger.warning("Rejecting parser {0} due to invalid structure".format(location))
37 continue
38 info = imp.find_module(main_module, [location])
39 parser_obj = Parser(parser_name, location, info)
40 parsers.append(parser_obj)
41 logger.debug("Added parser {0}".format(parser_obj.name))
42 return parsers
43
44def search(name):
45 logger.debug("Searching for parser {0}".format(name))
46 if name in default_parsers:
47 logger.debug("Found parser {0} in default modules".format(name))
48 return Parser('.'.join(__name__.split('.')[:-1] + [name]))
49 else:
50 return None
51
52def load(parser):
53 logger.debug("Loading parser {0} from {1}".format(parser.name, parser.path if parser.path != None else "defaults"))
54 return importlib.import_module(parser.name)