From 233b65cc1d9ac71d51438dc0502044bf086351b6 Mon Sep 17 00:00:00 2001 From: paulfantom Date: Tue, 21 Jun 2016 11:43:28 +0200 Subject: [PATCH] fix update_every, PEP8 formatting --- plugins.d/python.d.plugin | 34 +++++++++++++++++++-------------- python.d/python_modules/base.py | 7 ++++--- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/plugins.d/python.d.plugin b/plugins.d/python.d.plugin index db5afccb..ac37eb3d 100755 --- a/plugins.d/python.d.plugin +++ b/plugins.d/python.d.plugin @@ -10,10 +10,6 @@ import time MODULE_EXTENSION = ".chart.py" -BASE_CONFIG = {'update_every': 1, - 'priority': 90000, - 'retries': 10} - # ----------------------------------------------------------------------------- # logging functions @@ -21,6 +17,7 @@ BASE_CONFIG = {'update_every': 1, PROGRAM = os.path.basename(__file__).replace(".plugin", "") DEBUG_FLAG = False + def debug(*args): """ Print message on stderr. @@ -33,6 +30,7 @@ def debug(*args): sys.stderr.write("\n") sys.stderr.flush() + def error(*args): """ Print message on stderr. @@ -43,6 +41,7 @@ def error(*args): sys.stderr.write("\n") sys.stderr.flush() + def info(*args): """ Print message on stderr. @@ -53,6 +52,7 @@ def info(*args): sys.stderr.write("\n") sys.stderr.flush() + def fatal(*args): """ Print message on stderr and exit. @@ -66,14 +66,13 @@ def fatal(*args): sys.exit(1) - # ----------------------------------------------------------------------------- # globals & python modules management # setup environment # https://github.com/firehol/netdata/wiki/External-Plugins#environment-variables MODULES_DIR = os.path.abspath(os.getenv('NETDATA_PLUGINS_DIR', - os.path.dirname(__file__)) + "/../python.d") + "/" + os.path.dirname(__file__)) + "/../python.d") + "/" CONFIG_DIR = os.getenv('NETDATA_CONFIG_DIR', "/etc/netdata/") UPDATE_EVERY = os.getenv('NETDATA_UPDATE_EVERY', 1) # directories should end with '/' @@ -81,6 +80,10 @@ if CONFIG_DIR[-1] != "/": CONFIG_DIR += "/" sys.path.append(MODULES_DIR + "python_modules") +BASE_CONFIG = {'update_every': UPDATE_EVERY, + 'priority': 90000, + 'retries': 10} + try: assert sys.version_info >= (3, 1) import importlib.machinery @@ -106,12 +109,13 @@ try: except ImportError: fatal('Cannot find yaml library') + class PythonCharts(object): """ Main class used to control every python module. """ def __init__(self, - update_every=None, + update_every=None, # TODO remove this modules=None, modules_path='../python.d/', modules_configs='../conf.d/', @@ -141,9 +145,9 @@ class PythonCharts(object): # good economy and prosperity: self.jobs = self._create_jobs(configured_modules) # type: list - if DEBUG_FLAG and update_every is not None: - for job in self.jobs: - job.create_timetable(update_every) +# if DEBUG_FLAG and update_every is not None: +# for job in self.jobs: +# job.create_timetable(update_every) @staticmethod def _import_module(path, name=None): @@ -532,7 +536,7 @@ def run(): conf = read_config(configfile) if conf is not None: try: - # FIXME: is this right? exit the whole plugin? + # exit the whole plugin when 'enable: no' is set in 'python.d.conf' if str(conf['enable']) is False: fatal('disabled in configuration file.\n') except (KeyError, TypeError): @@ -552,9 +556,11 @@ def run(): disabled.append(k) # parse passed command line arguments - out = parse_cmdline(MODULES_DIR, *sys.argv) - modules = out['modules'] - info("MODULES_DIR='" + MODULES_DIR + "', CONFIG_DIR='" + CONFIG_DIR + "', UPDATE_EVERY=" + str(UPDATE_EVERY) + ", ONLY_MODULES=" + str(out['modules'])) + modules = parse_cmdline(MODULES_DIR, *sys.argv) + info("MODULES_DIR='" + MODULES_DIR + + "', CONFIG_DIR='" + CONFIG_DIR + + "', UPDATE_EVERY=" + str(UPDATE_EVERY) + + ", ONLY_MODULES=" + str(modules)) # run plugins charts = PythonCharts(update_every, modules, MODULES_DIR, CONFIG_DIR + "python.d/", disabled) diff --git a/python.d/python_modules/base.py b/python.d/python_modules/base.py index 57370006..a60afb26 100644 --- a/python.d/python_modules/base.py +++ b/python.d/python_modules/base.py @@ -36,10 +36,11 @@ class BaseService(object): 'retries':0} :param config: dict """ - self.update_every = int(config['update_every']) - self.priority = int(config['priority']) - self.retries = int(config['retries']) + self.update_every = int(config.pop('update_every')) + self.priority = int(config.pop('priority')) + self.retries = int(config.pop('retries')) self.retries_left = self.retries + self.configuration = config def create_timetable(self, freq=None): """ -- 2.39.2