Skip to content

Commit

Permalink
[Config] handle .yaml.default files
Browse files Browse the repository at this point in the history
review fix from #2273
  • Loading branch information
tmichelet committed Mar 24, 2016
1 parent eacc1d2 commit d142f69
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
13 changes: 11 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,10 +771,17 @@ def _all_configs_paths(osname, agentConfig):
try:
confd_path = get_confd_path(osname)
all_configs = glob.glob(os.path.join(confd_path, '*.yaml'))
all_default_configs = glob.glob(os.path.join(confd_path, '*.yaml.default'))
except PathNotFound, e:
log.error("No conf.d folder found at '%s' or in the directory where the Agent is currently deployed.\n" % e.args[0])
sys.exit(3)

if all_default_configs:
current_configs = set([_conf_path_to_check_name(conf) for conf in all_configs])
for default_config in all_default_configs:
if not _conf_path_to_check_name(default_config) in current_configs:
all_configs.append(default_config)

# Compatibility code for the Nagios checks if it's still configured
# in datadog.conf
# FIXME: 6.x, should be removed
Expand All @@ -783,9 +790,11 @@ def _all_configs_paths(osname, agentConfig):
if any([nagios_key in agentConfig for nagios_key in NAGIOS_OLD_CONF_KEYS]):
all_configs.append('deprecated/nagios')


return all_configs

def _conf_path_to_check_name(conf_path):
return conf_path.rsplit('/', 1)[-1].split('.yaml')[0]

def _checks_places(agentConfig, osname):
""" Return methods to generated paths to inspect for a check provided it's name
"""
Expand Down Expand Up @@ -900,7 +909,7 @@ def load_check_directory(agentConfig, hostname):

for config_path in all_configs_paths:
# '/etc/dd-agent/checks.d/my_check.py' -> 'my_check'
check_name = config_path.rsplit('.', 1)[0].rsplit('/', 1)[-1]
check_name = _conf_path_to_check_name(config_path)

conf_is_valid, check_config, invalid_check = _validate_config(config_path, check_name, agentConfig)
init_failed_checks.update(invalid_check)
Expand Down
5 changes: 5 additions & 0 deletions tests/core/fixtures/checks/valid_conf_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
init_config:

instances:
- host: localhost
- host: localh0st
21 changes: 21 additions & 0 deletions tests/core/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,27 @@ def testConfigDeprecatedNagiosConfig(self, *args):
self.assertEquals(1, len(checks['initialized_checks']))
self.assertEquals('valid_check_1', checks['initialized_checks'][0].check(None))

def testConfigDefault(self, *args):
copyfile('%s/valid_conf.yaml' % FIXTURE_PATH,
'%s/test_check.yaml.default' % TEMP_ETC_CONF_DIR)
copyfile('%s/valid_check_1.py' % FIXTURE_PATH,
'%s/test_check.py' % TEMP_ETC_CHECKS_DIR)
checks = load_check_directory({"additional_checksd": TEMP_ETC_CHECKS_DIR}, "foo")
self.assertEquals(1, len(checks['initialized_checks']))

def testConfigCustomOverDefault(self, *args):
copyfile('%s/valid_conf.yaml' % FIXTURE_PATH,
'%s/test_check.yaml.default' % TEMP_ETC_CONF_DIR)
# a 2nd valid conf file, slightly different so that we can test which one has been picked up
# (with 2 instances for instance)
copyfile('%s/valid_conf_2.yaml' % FIXTURE_PATH,
'%s/test_check.yaml' % TEMP_ETC_CONF_DIR)
copyfile('%s/valid_check_1.py' % FIXTURE_PATH,
'%s/test_check.py' % TEMP_ETC_CHECKS_DIR)
checks = load_check_directory({"additional_checksd": TEMP_ETC_CHECKS_DIR}, "foo")
self.assertEquals(1, len(checks['initialized_checks']))
self.assertEquals(2, checks['initialized_checks'][0].instance_count()) # check that we picked the right conf

def tearDown(self):
for _dir in self.TEMP_DIRS:
rmtree(_dir)

0 comments on commit d142f69

Please sign in to comment.