X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=plugins.d%2Fpython.d.plugin;h=efa62cbc5a34affbd86651147a50e08871ff7e0f;hb=HEAD;hp=44b729094ce307189f3fcecbdfc32464c37d1b60;hpb=9e06977a81b3a2204625588c74f2fff58d70356d;p=netdata.git diff --git a/plugins.d/python.d.plugin b/plugins.d/python.d.plugin index 44b72909..efa62cbc 100755 --- a/plugins.d/python.d.plugin +++ b/plugins.d/python.d.plugin @@ -73,21 +73,29 @@ try: DICT = OrderedDict msg.info('YAML output is ordered') except ImportError: - ORDERED = False - DICT = dict - msg.info('YAML output is unordered') -else: + try: + from ordereddict import OrderedDict + ORDERED = True + DICT = OrderedDict + msg.info('YAML output is ordered') + except ImportError: + ORDERED = False + DICT = dict + msg.info('YAML output is unordered') +if ORDERED: def ordered_load(stream, Loader=yaml.Loader, object_pairs_hook=OrderedDict): class OrderedLoader(Loader): pass + def construct_mapping(loader, node): - loader.flatten_mapping(node) - return object_pairs_hook(loader.construct_pairs(node)) + loader.flatten_mapping(node) + return object_pairs_hook(loader.construct_pairs(node)) OrderedLoader.add_constructor( yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, construct_mapping) return yaml.load(stream, OrderedLoader) + class PythonCharts(object): """ Main class used to control every python module. @@ -97,12 +105,16 @@ class PythonCharts(object): modules=None, modules_path='../python.d/', modules_configs='../conf.d/', - modules_disabled=None): + modules_disabled=None, + modules_enabled=None, + default_run=None): """ :param modules: list :param modules_path: str :param modules_configs: str :param modules_disabled: list + :param modules_enabled: list + :param default_run: bool """ if modules is None: @@ -115,13 +127,13 @@ class PythonCharts(object): self.configs = modules_configs # load modules - loaded_modules = self._load_modules(modules_path, modules, modules_disabled) + loaded_modules = self._load_modules(modules_path, modules, modules_disabled, modules_enabled, default_run) # load configuration files configured_modules = self._load_configs(loaded_modules) # good economy and prosperity: - self.jobs = self._create_jobs(configured_modules) # type: list + self.jobs = self._create_jobs(configured_modules) # type # enable timetable override like `python.d.plugin mysql debug 1` if DEBUG_FLAG and OVERRIDE_UPDATE_EVERY: @@ -151,7 +163,7 @@ class PythonCharts(object): msg.error("Problem loading", name, str(e)) return None - def _load_modules(self, path, modules, disabled): + def _load_modules(self, path, modules, disabled, enabled, default_run): """ Load modules from 'modules' list or dynamically every file from 'path' (only .chart.py files) :param path: str @@ -177,7 +189,10 @@ class PythonCharts(object): msg.fatal('no modules found.') else: # scan directory specified in path and load all modules from there - names = os.listdir(path) + if default_run is False: + names = [module for module in os.listdir(path) if module[:-9] in enabled] + else: + names = os.listdir(path) for mod in names: if mod.replace(MODULE_EXTENSION, "") in disabled: msg.error(mod + ": disabled module ", mod.replace(MODULE_EXTENSION, "")) @@ -363,7 +378,8 @@ class PythonCharts(object): if job.override_name is not None: new_name = job.__module__ + '_' + sub(r'\s+', '_', job.override_name) if new_name in overridden: - msg.info("DROPPED:", job.name, ", job '" + job.override_name + "' is already served by another job.") + msg.info("DROPPED:", job.name, ", job '" + job.override_name + + "' is already served by another job.") self._stop(job) i -= 1 else: @@ -475,7 +491,7 @@ def parse_cmdline(directory, *commands): elif cmd == "trace" or cmd == "all": TRACE_FLAG = True elif os.path.isfile(directory + cmd + ".chart.py") or os.path.isfile(directory + cmd): - #DEBUG_FLAG = True + # DEBUG_FLAG = True mods.append(cmd.replace(".chart.py", "")) else: try: @@ -500,7 +516,9 @@ def run(): global DEBUG_FLAG, TRACE_FLAG, BASE_CONFIG # read configuration file - disabled = [] + disabled = ['nginx_log', 'gunicorn_log'] + enabled = list() + default_run = True configfile = CONFIG_DIR + "python.d.conf" msg.PROGRAM = PROGRAM msg.info("reading configuration file:", configfile) @@ -542,12 +560,17 @@ def run(): except (KeyError, TypeError): pass + default_run = True if ('default_run' not in conf or conf.get('default_run')) else False + for k, v in conf.items(): - if k in ("update_every", "debug", "enabled"): + if k in ("update_every", "debug", "enabled", "default_run"): continue - if v is False: - disabled.append(k) - + if default_run: + if v is False: + disabled.append(k) + else: + if v is True: + enabled.append(k) # parse passed command line arguments modules = parse_cmdline(MODULES_DIR, *sys.argv) msg.DEBUG_FLAG = DEBUG_FLAG @@ -562,7 +585,7 @@ def run(): ", ONLY_MODULES=" + str(modules)) # run plugins - charts = PythonCharts(modules, MODULES_DIR, CONFIG_DIR + "python.d/", disabled) + charts = PythonCharts(modules, MODULES_DIR, CONFIG_DIR + "python.d/", disabled, enabled, default_run) charts.check() charts.create() charts.update()