def initialize(config): """Do various initilization things. :param config: a :class:`plumd.config.conf` instance :type config: plumd.config.conf """ # set the configured timezone, from: # http://stackoverflow.com/questions/6321160/python-logging-how-to-set-time-to-gmt tz = config.get('tz') if tz: try: os.environ['TZ'] = tz time.tzset() except Exception as e: err = "unable to set timezone: {0} : {1}\n" sys.stderr.write(err.format(tz, e)) sys.exit(1) # add hostname metadata if config.get('meta.hostname'): meta = config.get('meta') # meta is a list of single item {'key': 'value'} dicts for entry in meta: for key, val in entry.items(): if key == 'hostname': err = "meta.hostname: hostname in meta already: {0} : {1}\n" sys.stderr.write(err.format(val, config.path)) sys.exit(1) meta.append({'hostname': plumd.util.get_hostname()}) config.set_conf('meta', meta) return config
def get_config(): """Returns a conf object - either from the default file or cmd line arg. :rtype: plumd.config.conf """ descr = """plumd: measures system and service metrics.""" # get cli options - currently just the path to this instances config file argp = argparse.ArgumentParser(description=descr) argp.add_argument('--config', metavar='config', type=str, nargs='?', default=plumd.DEFAULT_CONFIG_FILE, help='main configuration file in yaml format') args = argp.parse_args() # get a simple configuration helper object config = None try: config = plumd.config.Conf(args.config).defaults(plumd.DEFAULT_CONFIG) except plumd.ConfigError as e: sys.stderr.write("configuration failed to load: {0}\n".format(e)) sys.exit(1) # ensure log level is known llevel = config.get('log.level') if llevel not in plumd.LOG_LEVELS: llevels = " ".join(list(plumd.LOG_LEVELS.keys())) sys.stderr.write("unkonwn log level: {0}\n".format(llevel)) sys.stderr.write("valid log levels are: {0}\n".format(llevels)) sys.exit(1) # all done, return our config return config
def get_plugins(config, log): """Returns a plugin loader object with plugins already started. :param config: a :class:`plumd.config.conf` instance :type config: plumd.config.conf :param log: a structlog logging instance :type log: structlog :rtype: plumd.load.PluginLoader""" # load reader and writer plugins and start log.info("loading plugins") psys = None # several things can go wrong loading plugins try: psys = plumd.PluginLoader(log, config) except plumd.PluginLoadError as e: log.critical("plugin load failed: {0}".format(e)) sys.exit(1) except plumd.ConfigError as e: log.critical("invalid plugin config: {0}".format(e)) sys.exit(1) except plumd.DuplicatePlugin as e: log.critical("duplicate plugin: {0}".format(e)) sys.exit(1) else: log.info("plugins loaded") time.sleep(random.randrange(int(config.get('delay.startup')))) psys.start() log.debug("main: plugins: {0}".format(psys)) # all done, return the PluginLoader object return psys
def get_log(config): """Returns a configured logger. :param config: a :class:`plumd.config.conf` instance :type config: plumd.config.conf :param src: all log entries generated are tagged with src=<src> :type src: str :rtype: structlog.logger """ # create logger handler = logging.StreamHandler(sys.stdout) formatter = logging.Formatter(config.get('log.format')) handler.setFormatter(formatter) stdlog = logging.getLogger() stdlog.addHandler(handler) stdlog.setLevel(plumd.LOG_LEVELS[config.get('log.level')]) return stdlog