logparse / parsers / load_parsers.pyon commit bugfixing in config & add quiet mode (1542324)
   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-journald", "httpd", "mem", "postfix", "smbd", "sshd-journald", "sudo", "sysinfo", "temperature", "zfs"]
  18deprecated_parsers = ["sshd", "cron"]
  19
  20import logging
  21logger = logging.getLogger(__name__)
  22
  23class Parser():
  24    def __init__(self, name, path=None, info=None):
  25        self.name = str(name)
  26        self.path = Path(path) if path else None
  27        self.info = dict(info) if info else None
  28
  29def findall():
  30    logger.debug("Searching for parsers in {0}".format(parser_dir))
  31    path.append(os.path.abspath(parser_dir))
  32    parsers = []
  33    parser_candidates = os.listdir(parser_dir)
  34    for parser_name in parser_candidates:
  35        location = os.path.join(parser_dir, parser_name)
  36        if not os.path.isdir(location) or not main_module + '.py' in os.listdir(location):
  37            logger.warning("Rejecting parser {0} due to invalid structure".format(location))
  38            continue
  39        info = imp.find_module(main_module, [location])
  40        parser_obj = Parser(parser_name, location, info)
  41        parsers.append(parser_obj)
  42        logger.debug("Added parser {0}".format(parser_obj.name))
  43    return parsers
  44
  45def search(name):
  46    logger.debug("Searching for parser {0}".format(name))
  47    if name in default_parsers:
  48        logger.debug("Found parser {0} in default modules".format(name))
  49        return Parser('.'.join(__name__.split('.')[:-1] + [name]))
  50    elif name in deprecated_parsers:
  51        logger.debug("Found parser {0} in deprecated modules".format(name))
  52        return Parser('.'.join(__name__.split('.')[:-1] + [name]))
  53    else:
  54        return None
  55
  56def load(parser):
  57    logger.debug("Loading parser {0} from {1}".format(parser.name, parser.path if parser.path != None else "defaults"))
  58    return importlib.import_module(parser.name)