Skip to content

Commit

Permalink
[jmxfetch] Allow adding custom jars to JMXFetch's classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
olivielpeau committed Oct 23, 2015
1 parent b4eae22 commit fa5b6a2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
3 changes: 3 additions & 0 deletions conf.d/jmx.yaml.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
init_config:
# custom_jar_paths: # Optional, allows specifying custom jars that will be added to the classpath of the agent's JVM
# - /path/to/custom/jarfile.jar
# - /path/to/another/custom/jarfile2.jar

instances:
# - host: localhost
Expand Down
32 changes: 22 additions & 10 deletions jmxfetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ def configure(self, checks_list=None, clean_status_file=True):
if clean_status_file:
JMXFiles.clean_status_file()

self.jmx_checks, self.invalid_checks, self.java_bin_path, self.java_options, self.tools_jar_path = \
self.jmx_checks, self.invalid_checks, self.java_bin_path, self.java_options, \
self.tools_jar_path, self.custom_jar_paths = \
self.get_configuration(self.confd_path, checks_list=checks_list)

def should_run(self):
Expand Down Expand Up @@ -143,7 +144,7 @@ def run(self, command=None, checks_list=None, reporter=None, redirect_std_stream

if len(self.jmx_checks) > 0:
return self._start(self.java_bin_path, self.java_options, self.jmx_checks,
command, reporter, self.tools_jar_path, redirect_std_streams)
command, reporter, self.tools_jar_path, self.custom_jar_paths, redirect_std_streams)
else:
# We're exiting purposefully, so exit with zero (supervisor's expected
# code). HACK: Sleep a little bit so supervisor thinks we've started cleanly
Expand Down Expand Up @@ -184,6 +185,7 @@ def get_configuration(cls, confd_path, checks_list=None):
java_bin_path = None
java_options = None
tools_jar_path = None
custom_jar_paths = []
invalid_checks = {}

for conf in glob.glob(os.path.join(confd_path, '*.yaml')):
Expand All @@ -202,7 +204,7 @@ def get_configuration(cls, confd_path, checks_list=None):
continue

try:
is_jmx, check_java_bin_path, check_java_options, check_tools_jar_path = \
is_jmx, check_java_bin_path, check_java_options, check_tools_jar_path, check_custom_jar_paths = \
cls._is_jmx_check(check_config, check_name, checks_list)
if is_jmx:
jmx_checks.append(filename)
Expand All @@ -212,15 +214,16 @@ def get_configuration(cls, confd_path, checks_list=None):
java_options = check_java_options
if tools_jar_path is None and check_tools_jar_path is not None:
tools_jar_path = check_tools_jar_path
custom_jar_paths.extend(check_custom_jar_paths)
except InvalidJMXConfiguration, e:
log.error("%s check does not have a valid JMX configuration: %s" % (check_name, e))
# Make sure check_name is a string - Fix issues with Windows
check_name = check_name.encode('ascii', 'ignore')
invalid_checks[check_name] = str(e)

return (jmx_checks, invalid_checks, java_bin_path, java_options, tools_jar_path)
return (jmx_checks, invalid_checks, java_bin_path, java_options, tools_jar_path, custom_jar_paths)

def _start(self, path_to_java, java_run_opts, jmx_checks, command, reporter, tools_jar_path, redirect_std_streams):
def _start(self, path_to_java, java_run_opts, jmx_checks, command, reporter, tools_jar_path, custom_jar_paths, redirect_std_streams):
statsd_port = self.agentConfig.get('dogstatsd_port', "8125")
if reporter is None:
reporter = "statsd:%s" % str(statsd_port)
Expand All @@ -232,10 +235,11 @@ def _start(self, path_to_java, java_run_opts, jmx_checks, command, reporter, too
path_to_jmxfetch = self._get_path_to_jmxfetch()
path_to_status_file = JMXFiles.get_status_file_path()

if tools_jar_path is None:
classpath = path_to_jmxfetch
else:
classpath = r"%s:%s" % (tools_jar_path, path_to_jmxfetch)
classpath = path_to_jmxfetch
if tools_jar_path is not None:
classpath = r"%s:%s" % (tools_jar_path, classpath)
if custom_jar_paths:
classpath = r"%s:%s" % (':'.join(custom_jar_paths), classpath)

subprocess_args = [
path_to_java, # Path to the java bin
Expand Down Expand Up @@ -319,6 +323,7 @@ def _is_jmx_check(check_config, check_name, checks_list):
is_jmx = False
is_attach_api = False
tools_jar_path = init_config.get("tools_jar_path")
custom_jar_paths = init_config.get("custom_jar_paths")

if init_config is None:
init_config = {}
Expand Down Expand Up @@ -407,7 +412,14 @@ def _is_jmx_check(check_config, check_name, checks_list):
else:
tools_jar_path = None

return is_jmx, java_bin_path, java_options, tools_jar_path
if custom_jar_paths:
if isinstance(custom_jar_paths, basestring):
custom_jar_paths = [custom_jar_paths]
for custom_jar_path in custom_jar_paths:
if not os.path.isfile(custom_jar_path):
raise InvalidJMXConfiguration("Unable to find custom jar at %s" % custom_jar_path)

return is_jmx, java_bin_path, java_options, tools_jar_path, custom_jar_paths

def _get_path_to_jmxfetch(self):
if not Platform.is_windows():
Expand Down

0 comments on commit fa5b6a2

Please sign in to comment.