Skip to content

Commit

Permalink
[flare] Add java version output
Browse files Browse the repository at this point in the history
- To make it easier to get JMXFetch's configuration from flare,
`get_configuration` becomes a classmethod that calls `_is_jmx_check` as
a staticmethod
- For more consistency, `check_list` is renamed to `checks_list`
  • Loading branch information
olivielpeau committed Jun 29, 2015
1 parent d6e57af commit b507c0b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
24 changes: 13 additions & 11 deletions jmxfetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,22 @@ def register_signal_handlers(self):
except ValueError:
log.exception("Unable to register signal handlers.")

def configure(self, check_list=None):
def configure(self, checks_list=None):
"""
Instantiate JMXFetch parameters, clean potential previous run leftovers.
"""
JMXFiles.clean_status_file()

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

def should_run(self):
"""
Should JMXFetch run ?
"""
return self.jmx_checks is not None and self.jmx_checks != []

def run(self, command=None, check_list=None, reporter=None, redirect_std_streams=False):
def run(self, command=None, checks_list=None, reporter=None, redirect_std_streams=False):
"""
Run JMXFetch
Expand All @@ -118,10 +118,10 @@ def run(self, command=None, check_list=None, reporter=None, redirect_std_streams
and sys.stderr.
"""

if check_list or self.jmx_checks is None:
# (Re)set/(re)configure JMXFetch parameters when `check_list` is specified or
if checks_list or self.jmx_checks is None:
# (Re)set/(re)configure JMXFetch parameters when `checks_list` is specified or
# no configuration was found
self.configure(check_list)
self.configure(checks_list)

try:
command = command or JMX_COLLECT_COMMAND
Expand All @@ -145,9 +145,10 @@ def run(self, command=None, check_list=None, reporter=None, redirect_std_streams
log.exception("Error while initiating JMXFetch")
raise

def get_configuration(self, checks_list=None):
@classmethod
def get_configuration(cls, confd_path, checks_list=None):
"""
Return a tuple (jmx_checks, invalid_checks, java_bin_path, java_options)
Return a tuple (jmx_checks, invalid_checks, java_bin_path, java_options, tools_jar_path)
jmx_checks: list of yaml files that are jmx checks
(they have the is_jmx flag enabled or they are in JMX_CHECKS)
Expand Down Expand Up @@ -176,7 +177,7 @@ def get_configuration(self, checks_list=None):
tools_jar_path = None
invalid_checks = {}

for conf in glob.glob(os.path.join(self.confd_path, '*.yaml')):
for conf in glob.glob(os.path.join(confd_path, '*.yaml')):
filename = os.path.basename(conf)
check_name = filename.split('.')[0]

Expand All @@ -193,7 +194,7 @@ def get_configuration(self, checks_list=None):

try:
is_jmx, check_java_bin_path, check_java_options, check_tools_jar_path = \
self._is_jmx_check(check_config, check_name, checks_list)
cls._is_jmx_check(check_config, check_name, checks_list)
if is_jmx:
jmx_checks.append(filename)
if java_bin_path is None and check_java_bin_path is not None:
Expand Down Expand Up @@ -301,7 +302,8 @@ def _start(self, path_to_java, java_run_opts, jmx_checks, command, reporter, too
log.exception("Couldn't launch JMXFetch")
raise

def _is_jmx_check(self, check_config, check_name, checks_list):
@staticmethod
def _is_jmx_check(check_config, check_name, checks_list):
init_config = check_config.get('init_config', {}) or {}
java_bin_path = None
java_options = None
Expand Down
19 changes: 19 additions & 0 deletions utils/flare.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
get_logging_config,
get_url_endpoint,
)
from jmxfetch import JMXFetch
from util import get_hostname
from utils.jmx_command import jmx_command
from utils.jmxfiles import JMXFiles
Expand Down Expand Up @@ -231,6 +232,16 @@ def _add_jmxinfo_tar(self):
'jmxfetch'
)

# java version
log.info(" * java -version output")
self._add_command_output_tar(
os.path.join('jmxinfo', 'java_version.log'),
# We use lambda so that JMXFetch.get_configuration is evaluated in _add_command_output_tar,
# which captures the logging output from JMXFetch
lambda: self._java_version(JMXFetch.get_configuration(get_confd_path())[2]),
'jmxfetch'
)

# Check if the file is readable (and log it)
@classmethod
def _can_read(cls, f, output=True, warn=True):
Expand Down Expand Up @@ -381,6 +392,14 @@ def _jmx_command_call(self, command):
except Exception, e:
print "Unable to call jmx command {0}: {1}".format(command, e)

# Print java version
def _java_version(self, java_bin_path):
java_bin_path = java_bin_path or 'java'
try:
self._print_output_command([java_bin_path, '-version'])
except OSError:
print 'Unable to execute java bin with command: {0}'.format(java_bin_path)

# Run a pip freeze
def _pip_freeze(self):
try:
Expand Down

0 comments on commit b507c0b

Please sign in to comment.