python.d: merge user/stock plugin configuration files (#11217)
Ilya Mashchenko committed
Jun 8, 2021 at 12:28 UTC
becf72608043dc5a5a2d771a3bc5e2d5250d1e21
1 file changed
+23
-20
collectors/python.d.plugin/python.d.plugin.in
+23
-20
@@ -500,27 +500,31 @@ class Plugin:
500
self.saver = None
501
self.runs = 0
502
503
- def load_config(self):
504
- paths = [
505
- DIRS.plugin_user_config,
506
- DIRS.plugin_stock_config,
507
- ]
508
- self.log.debug("looking for '{0}' in {1}".format(self.config_name, paths))
509
- abs_path = multi_path_find(self.config_name, *paths)
510
- if not abs_path:
511
- self.log.warning("'{0}' was not found, using defaults".format(self.config_name))
512
- return True
513
-
514
- self.log.debug("loading '{0}'".format(abs_path))
503
+ def load_config_file(self, filepath, expected):
504
+ self.log.debug("looking for '{0}'".format(filepath))
505
+ if not os.path.isfile(filepath):
506
+ log = self.log.info if not expected else self.log.error
507
+ log("'{0}' was not found".format(filepath))
508
+ return dict()
509
try:
516
- config = load_config(abs_path)
510
+ config = load_config(filepath)
511
except Exception as error:
518
- self.log.error("error on loading '{0}' : {1}".format(abs_path, repr(error)))
519
- return False
512
+ self.log.error("error on loading '{0}' : {1}".format(filepath, repr(error)))
513
+ return dict()
514
+ self.log.debug("'{0}' is loaded".format(filepath))
515
+ return config
516
521
- self.log.debug("'{0}' is loaded".format(abs_path))
522
- self.config.update(config)
523
- return True
517
+ def load_config(self):
518
+ user_config = self.load_config_file(
519
+ filepath=os.path.join(DIRS.plugin_user_config, self.config_name),
520
+ expected=False,
521
+ )
522
+ stock_config = self.load_config_file(
523
+ filepath=os.path.join(DIRS.plugin_stock_config, self.config_name),
524
+ expected=True,
525
+ )
526
+ self.config.update(stock_config)
527
+ self.config.update(user_config)
528
529
def load_job_statuses(self):
530
self.log.debug("looking for '{0}' in {1}".format(self.jobs_status_dump_name, DIRS.var_lib))
@@ -593,8 +597,7 @@ class Plugin:
597
return jobs
598
599
def setup(self):
596
- if not self.load_config():
597
- return False
600
+ self.load_config()
601
602
if not self.config['enabled']:
603
self.log.info('disabled in the configuration file')