f2821106c6456d8a03d112adab1725f5624ec6e5
1#
2# load_parsers.py
3#
4# Search for and load files which parse logs for particular services
5#
6
7import imp
8import os
9import glob
10
11parser_dir = "/usr/share/logparse/"
12main_module = "__init__.py"
13
14import logging
15logger = logging.getLogger(__name__)
16
17def search():
18 logger.debug("Searching for parsers in {0}".format(parser_dir))
19 parsers = []
20 parser_candidates = glob.glob(os.path.join(os.path.dirname(parser_dir), "*"))
21 logger.debug("Found parser candidates {0}".format(str(parser_candidates)))
22 for p in parser_candidates:
23 location = os.path.join(parser_dir, p)
24 if not os.path.isdir(parser_dir) or not main_module + '.py' in os.listdir(location):
25 continue
26 info = imp.find_module(main_module, [location])
27 parsers.append({"name": p, "info": info})
28 return parsers
29
30def load(parser):
31 logger.debug("Loading {0}".format(parser["name"]))
32 return imp.load_module(parser["name"], *parser["info"])
33