Skip to content

Commit

Permalink
[service_discovery][jmx] some cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
truthbk committed Nov 18, 2016
1 parent 21ac6e1 commit 25c5199
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ embedded/*
dump.rdb
tests/core/fixtures/flare/dd*
.python-version
.ropeproject
.bundle
tags
46 changes: 29 additions & 17 deletions agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
get_system_stats,
load_check_directory,
load_check,
generate_jmx_configs
generate_jmx_configs,
_is_affirmative
)
from daemon import AgentSupervisor, Daemon
from emitter import http_emitter
Expand Down Expand Up @@ -94,15 +95,8 @@ def __init__(self, pidfile, autorestart, start_event=True, in_developer_mode=Fal
self.reload_configs_flag = False
self.sd_backend = None
self.supervisor_proxy = None
self.sd_pipe = None

if Platform.is_windows():
pipe_name = SD_PIPE_WIN_PATH.format(pipename=SD_PIPE_NAME)
else:
pipe_name = os.path.join(SD_PIPE_UNIX_PATH, SD_PIPE_NAME)

if not os.path.exists(pipe_name):
os.mkfifo(pipe_name)
self.sd_pipe = os.open(pipe_name, os.O_RDWR) # RW to avoid blocking (will only W)

def _handle_sigterm(self, signum, frame):
"""Handles SIGTERM and SIGINT, which gracefully stops the agent."""
Expand All @@ -128,6 +122,7 @@ def reload_configs(self, checks_to_reload=set()):
Can also reload only an explicit set of checks."""
log.info("Attempting a configuration reload...")
hostname = get_hostname(self._agentConfig)
jmx_sd_configs = None

# if no check was given, reload them all
if not checks_to_reload:
Expand All @@ -137,14 +132,16 @@ def reload_configs(self, checks_to_reload=set()):
check.stop()

self._checksd = load_check_directory(self._agentConfig, hostname)
jmx_sd_configs = generate_jmx_configs(self._agentConfig, hostname)
if self._jmx_service_discovery_enabled:
jmx_sd_configs = generate_jmx_configs(self._agentConfig, hostname)
else:
new_checksd = copy(self._checksd)

jmx_checks = [check for check in checks_to_reload if check in JMX_CHECKS]
py_checks = set(checks_to_reload) - set(jmx_checks)
self.refresh_specific_checks(hostname, new_checksd, py_checks)
jmx_sd_configs = generate_jmx_configs(self._agentConfig, hostname, jmx_checks)
if self._jmx_service_discovery_enabled:
jmx_sd_configs = generate_jmx_configs(self._agentConfig, hostname, jmx_checks)

# once the reload is done, replace existing checks with the new ones
self._checksd = new_checksd
Expand Down Expand Up @@ -248,16 +245,27 @@ def run(self, config=None):
if self._agentConfig.get('service_discovery'):
self.sd_backend = get_sd_backend(self._agentConfig)

# Initialize Supervisor proxy (unix specific)
self.supervisor_proxy = self._get_supervisor_socket(self._agentConfig)
if _is_affirmative(self._agentConfig.get('sd_jmx_enable')):
if Platform.is_windows():
pipe_name = SD_PIPE_WIN_PATH.format(pipename=SD_PIPE_NAME)
else:
pipe_name = os.path.join(SD_PIPE_UNIX_PATH, SD_PIPE_NAME)

if not os.path.exists(pipe_name):
os.mkfifo(pipe_name)
self.sd_pipe = os.open(pipe_name, os.O_RDWR) # RW to avoid blocking (will only W)

# Initialize Supervisor proxy
self.supervisor_proxy = self._get_supervisor_socket(self._agentConfig)

# Load the checks.d checks
self._checksd = load_check_directory(self._agentConfig, hostname)

# Load JMX configs if available
jmx_sd_configs = generate_jmx_configs(self._agentConfig, hostname)
if jmx_sd_configs:
self._submit_jmx_service_discovery(jmx_sd_configs)
if self._jmx_service_discovery_enabled:
jmx_sd_configs = generate_jmx_configs(self._agentConfig, hostname)
if jmx_sd_configs:
self._submit_jmx_service_discovery(jmx_sd_configs)

# Initialize the Collector
self.collector = Collector(self._agentConfig, emitters, systemStats, hostname)
Expand Down Expand Up @@ -395,9 +403,13 @@ def _get_supervisor_socket(self, agentConfig):

return supervisor_proxy

@property
def _jmx_service_discovery_enabled(self):
return self.sd_pipe is not None

def _submit_jmx_service_discovery(self, jmx_sd_configs):

if not jmx_sd_configs:
if not jmx_sd_configs or not self.sd_pipe:
return

if self.supervisor_proxy is not None:
Expand Down
5 changes: 3 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# (C) Datadog, Inc. 2010-2016 all rights reserved
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)

Expand Down Expand Up @@ -1127,8 +1128,8 @@ def generate_jmx_configs(agentConfig, hostname, checknames=None):
# generated["{}_{}".format(check_name, idx)] = yaml
generated["{}_{}".format(check_name, 0)] = yaml
log.debug("YAML generated: %s", yaml)
except Exception as e:
log.exception("Unable to generate YAML config for %s: %s", check_name, e)
except Exception:
log.exception("Unable to generate YAML config for %s", check_name)

return generated

Expand Down
2 changes: 1 addition & 1 deletion datadog.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ gce_updated_hostname: yes
# and modify its value.
# sd_template_dir: /datadog/check_configs
#
# JMX Service Disocvery
# Enable JMX checks for service disocvery
# sd_jmx_enable: no
#
# ========================================================================== #
Expand Down
12 changes: 0 additions & 12 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,6 @@ def config_to_yaml(config):

return yaml_output

def dump_yaml(path, config):
'''
Dump config dict to YAML file in path
'''
try:
yaml = config_to_yaml(config)
except Exception as e:
log.exception("Unable to convert config to YAML: %s", e)
else:
with open(path, 'w+') as f:
f.write(yaml)


class Watchdog(object):
"""
Expand Down

0 comments on commit 25c5199

Please sign in to comment.