python.d.plugin: disabled_by_default feature added
lgz committed
Jun 22, 2018 at 17:55 UTC
571d85d26deeb7c943fd25c43d91f3232d0571e0
1 file changed
+37
-15
plugins.d/python.d.plugin
+37
-15
@@ -65,6 +65,14 @@ def multi_job_check(config):
65
return next((True for key in config if isinstance(config[key], dict)), False)
66
67
68
+class RawModule:
69
+ def __init__(self, name, path, conf_path, explicitly_enabled=True):
70
+ self.name = name
71
+ self.path = path
72
+ self.conf_path = conf_path
73
+ self.explicitly_enabled = explicitly_enabled
74
+
75
+
76
class Job(object):
77
def __init__(self, initialized_job, job_id):
78
"""
@@ -224,43 +232,57 @@ class Plugin(object):
232
conf_path = ''.join([CHARTS_PY_CONFIG_DIR, mod_name, '.conf'])
233
234
if DEBUG:
227
- yield mod, mod_name, mod_path, conf_path
235
+ yield RawModule(
236
+ name=mod_name,
237
+ path=mod_path,
238
+ conf_path=conf_path,
239
+ )
240
else:
229
- if all([self.config.get('default_run', True),
230
- self.config.get(mod_name, True)]):
231
- yield mod, mod_name, mod_path, conf_path
232
-
233
- elif all([not self.config.get('default_run'),
234
- self.config.get(mod_name)]):
235
- yield mod, mod_name, mod_path, conf_path
241
+ if any(
242
+ [
243
+ self.config.get('default_run', True) and self.config.get(mod_name, True),
244
+ (not self.config.get('default_run')) and self.config.get(mod_name),
245
+ ]
246
+ ):
247
+ yield RawModule(
248
+ name=mod_name,
249
+ path=mod_path,
250
+ conf_path=conf_path,
251
+ explicitly_enabled=self.config.get(mod_name),
252
+ )
253
254
def load_and_initialize_modules(self):
238
- for mod, mod_name, mod_path, conf_path in self.enabled_modules():
255
+ for mod in self.enabled_modules():
256
257
# Load module from file ------------------------------------------------------------
241
- loaded_module, error = self.loader.load_module_from_file(mod_name, mod_path)
258
+ loaded_module, error = self.loader.load_module_from_file(mod.name, mod.path)
259
log = Logger.error if error else Logger.debug
260
log("module load source: '{module_name}' => [{status}]".format(status='FAILED' if error else 'OK',
244
- module_name=mod_name))
261
+ module_name=mod.name))
262
if error:
263
Logger.error("load source error : {0}".format(error))
264
continue
265
266
# Load module config from file ------------------------------------------------------
250
- loaded_config, error = self.loader.load_config_from_file(conf_path)
267
+ loaded_config, error = self.loader.load_config_from_file(mod.conf_path)
268
log = Logger.error if error else Logger.debug
269
log("module load config: '{module_name}' => [{status}]".format(status='FAILED' if error else 'OK',
253
- module_name=mod_name))
270
+ module_name=mod.name))
271
if error:
272
Logger.error('load config error : {0}'.format(error))
273
257
- # Service instance initialization ---------------------------------------------------
274
+ # Skip disabled modules
275
+ if getattr(loaded_module, 'disabled_by_default', False) and not mod.explicitly_enabled:
276
+ Logger.info("module '{0}' disabled by default".format(loaded_module.__name__))
277
+ continue
278
+
279
+ # Module initialization ---------------------------------------------------
280
+
281
initialized_module = Module(service=loaded_module, config=loaded_config)
282
Logger.debug("module status: '{module_name}' => [{status}] "
283
"(jobs: {jobs_number})".format(status='OK' if initialized_module else 'FAILED',
284
module_name=initialized_module.name,
285
jobs_number=len(initialized_module)))
263
-
286
if initialized_module:
287
self.modules[initialized_module.name] = initialized_module
288