Add dimension for influxdb-relay

The node name of influxdb and influxdb-relay can be defined in
the configuration file of influxdb-relay.
This change enables to specify the node name by "detection_args"
option with "influxdb_node" or "influxdb_relay_node".

Change-Id: I5f394893798c5c3bab87bbf7065d5c5f3776514d
This commit is contained in:
Koji Nakazono 2017-05-17 13:07:43 +09:00
parent d2387eaa46
commit 706c0a7872
5 changed files with 97 additions and 27 deletions

View File

@ -32,6 +32,7 @@ class InfluxDB(detection.Plugin):
'bind_address': '127.0.0.1',
'bind_port': 8086
}
INFLUXDB_NODE_ARG_NAME = 'influxdb_node'
def _detect(self):
"""Run detection, set self.available True if the service is detected.
@ -80,10 +81,15 @@ class InfluxDB(detection.Plugin):
return config
def _monitor_process(self):
dimensions = {}
if self.args and self.args.get(self.INFLUXDB_NODE_ARG_NAME):
dimensions.update({self.INFLUXDB_NODE_ARG_NAME: self.args.get(self.INFLUXDB_NODE_ARG_NAME)})
return detection.watch_process([self.PROC_NAME],
service='influxdb',
component='influxdb',
exact_match=False)
exact_match=False,
dimensions=dimensions)
def _monitor_ping_endpoint(self):
config = agent_config.Plugins()
@ -93,13 +99,19 @@ class InfluxDB(detection.Plugin):
host, port = self._explode_bind_address(http_conf)
listening = utils.find_addrs_listening_on_port(port)
dimensions = {'service': 'influxdb', 'component': 'influxdb'}
if self.args and self.args.get(self.INFLUXDB_NODE_ARG_NAME):
dimensions.update(
{self.INFLUXDB_NODE_ARG_NAME: self.args.get(self.INFLUXDB_NODE_ARG_NAME)})
if listening:
config['http_check'] = {
'init_config': None,
'instances': [
{
'name': 'influxdb',
'url': 'http://%s:%d/ping' % (host, port)
'url': 'http://%s:%d/ping' % (host, port),
'dimensions': dimensions
}
]
}

View File

@ -39,6 +39,7 @@ class InfluxDBRelay(detection.Plugin):
'bind_address': '127.0.0.1',
'bind_port': 9096
}
RELAY_NODE_ARG_NAME = 'influxdb_relay_node'
def _detect(self):
"""Run detection, set self.available True if the service is detected.
@ -84,10 +85,16 @@ class InfluxDBRelay(detection.Plugin):
def _monitor_process(self):
LOG.info("\tMonitoring the influxdb-relay process")
dimensions = {}
if self.args and self.args.get(self.RELAY_NODE_ARG_NAME):
dimensions.update({self.RELAY_NODE_ARG_NAME: self.args.get(self.RELAY_NODE_ARG_NAME)})
return detection.watch_process([self.PROC_NAME],
service='influxdb',
component='influxdb-relay',
exact_match=False)
exact_match=False,
dimensions=dimensions)
def _monitor_endpoint(self):
config = agent_config.Plugins()
@ -100,9 +107,17 @@ class InfluxDBRelay(detection.Plugin):
listening = utils.find_addrs_listening_on_port(port)
if listening:
LOG.info("\tMonitoring the influxdb-relay ping endpoint")
dimensions = {'service': 'influxdb',
'component': 'influxdb-relay'}
if self.args and self.args.get(self.RELAY_NODE_ARG_NAME):
dimensions.update(
{self.RELAY_NODE_ARG_NAME: self.args.get(self.RELAY_NODE_ARG_NAME)})
instance = {
'name': 'influxdb-relay',
'url': 'http://%s:%d/ping' % (host, port)
'url': 'http://%s:%d/ping' % (host, port),
'dimensions': dimensions
}
config['http_check'] = {

View File

@ -90,7 +90,7 @@ def find_addr_listening_on_port_over_tcp(port):
def watch_process(search_strings, service=None, component=None,
exact_match=True, detailed=True, process_name=None):
exact_match=True, detailed=True, process_name=None, dimensions=None):
"""Takes a list of process search strings and returns a Plugins object with the config set.
This was built as a helper as many plugins setup process watching
"""
@ -103,7 +103,7 @@ def watch_process(search_strings, service=None, component=None,
'exact_match': exact_match,
'search_string': search_strings}
dimensions = _get_dimensions(service, component)
dimensions = _get_dimensions(service, component, dimensions)
if len(dimensions) > 0:
parameters['dimensions'] = dimensions
@ -112,7 +112,8 @@ def watch_process(search_strings, service=None, component=None,
return config
def watch_process_by_username(username, process_name, service=None, component=None, detailed=True):
def watch_process_by_username(username, process_name, service=None,
component=None, detailed=True, dimensions=None):
"""Takes a user and returns a Plugins object with the config set for a process check by user.
This was built as a helper as many plugins setup process watching.
"""
@ -122,7 +123,7 @@ def watch_process_by_username(username, process_name, service=None, component=No
'detailed': detailed,
'username': username}
dimensions = _get_dimensions(service, component)
dimensions = _get_dimensions(service, component, dimensions)
if len(dimensions) > 0:
parameters['dimensions'] = dimensions
@ -132,7 +133,7 @@ def watch_process_by_username(username, process_name, service=None, component=No
def watch_file_size(directory_name, file_names, file_recursive=False,
service=None, component=None):
service=None, component=None, dimensions=None):
"""Takes a directory, a list of files, recursive flag and returns a
Plugins object with the config set.
"""
@ -141,7 +142,7 @@ def watch_file_size(directory_name, file_names, file_recursive=False,
'file_names': file_names,
'recursive': file_recursive}
dimensions = _get_dimensions(service, component)
dimensions = _get_dimensions(service, component, dimensions)
if len(dimensions) > 0:
parameters['dimensions'] = dimensions
@ -150,13 +151,13 @@ def watch_file_size(directory_name, file_names, file_recursive=False,
return config
def watch_directory(directory_name, service=None, component=None):
def watch_directory(directory_name, service=None, component=None, dimensions=None):
"""Takes a directory name and returns a Plugins object with the config set.
"""
config = agent_config.Plugins()
parameters = {'directory': directory_name}
dimensions = _get_dimensions(service, component)
dimensions = _get_dimensions(service, component, dimensions)
if len(dimensions) > 0:
parameters['dimensions'] = dimensions
@ -165,8 +166,8 @@ def watch_directory(directory_name, service=None, component=None):
return config
def service_api_check(name, url, pattern,
use_keystone=True, service=None, component=None):
def service_api_check(name, url, pattern, use_keystone=True,
service=None, component=None, dimensions=None):
"""Setup a service api to be watched by the http_check plugin.
"""
config = agent_config.Plugins()
@ -176,7 +177,7 @@ def service_api_check(name, url, pattern,
'timeout': 10,
'use_keystone': use_keystone}
dimensions = _get_dimensions(service, component)
dimensions = _get_dimensions(service, component, dimensions)
if len(dimensions) > 0:
parameters['dimensions'] = dimensions
@ -185,8 +186,9 @@ def service_api_check(name, url, pattern,
return config
def _get_dimensions(service, component):
dimensions = {}
def _get_dimensions(service, component, dimensions=None):
if dimensions is None:
dimensions = {}
# If service parameter is set in the plugin config, add the service dimension which
# will override the service in the agent config
if service:

View File

@ -151,6 +151,13 @@ class TestInfluxDBDetection(base.BaseTestCase):
falop.return_value = False
self._build_config(http_enabled=False)
@mock.patch('monasca_setup.detection.plugins.influxdb.'
'utils.find_addrs_listening_on_port')
def test_should_build_config_db_listens_with_influxdb_node_key(self, falop):
falop.return_value = True
self._ir.args = {'influxdb_node': 'test-key'}
self._build_config(process_up=True)
def _build_config(self, http_enabled=True, process_up=True):
"""Verify built configuration
@ -183,18 +190,35 @@ class TestInfluxDBDetection(base.BaseTestCase):
raise 'Untested monitored item %s' % key
def _verify_http_conf(self, built_config, e_host_port):
dimensions = {
'component': 'influxdb',
'service': 'influxdb'
}
if self._ir.args and self._ir.args.get(self._ir.INFLUXDB_NODE_ARG_NAME):
dimensions.update(
{self._ir.INFLUXDB_NODE_ARG_NAME: self._ir.args.get(self._ir.INFLUXDB_NODE_ARG_NAME)})
expected_http = {
'init_config': None,
'instances': [
{
'name': 'influxdb',
'url': 'http://%s:%d/ping' % e_host_port
'url': 'http://%s:%d/ping' % e_host_port,
'dimensions': dimensions
}
]
}
self.assertDictEqual(expected_http, built_config)
def _verify_process_conf(self, actual_config):
dimensions = {
'component': 'influxdb',
'service': 'influxdb'
}
if self._ir.args and self._ir.args.get(self._ir.INFLUXDB_NODE_ARG_NAME):
dimensions.update(
{self._ir.INFLUXDB_NODE_ARG_NAME: self._ir.args.get(self._ir.INFLUXDB_NODE_ARG_NAME)})
expected_process = {
'init_config': None,
'instances': [
@ -203,10 +227,7 @@ class TestInfluxDBDetection(base.BaseTestCase):
'search_string': ['influxd'],
'exact_match': False,
'name': 'influxd',
'dimensions': {
'component': 'influxdb',
'service': 'influxdb'
}
'dimensions': dimensions
}
]
}

View File

@ -146,6 +146,12 @@ class TestInfluxDBRelayDetection(base.BaseTestCase):
falop.return_value = False
self._build_config(process_up=False)
@mock.patch('monasca_setup.detection.plugins.influxdb_relay.'
'utils.find_addrs_listening_on_port')
def test_should_build_config_relay_listens_with_node_name(self, falop):
falop.return_value = True
self._ir.args = {'influxdb_relay_node': 'relay-node-name'}
self._build_config(process_up=True)
def _build_config(self, process_up=True):
"""Verify built configuration
@ -175,18 +181,35 @@ class TestInfluxDBRelayDetection(base.BaseTestCase):
raise 'Untested monitored item %s' % key
def _verify_http_conf(self, built_config, e_host_port):
dimensions = {
'component': 'influxdb-relay',
'service': 'influxdb'
}
if self._ir.args and self._ir.args.get(self._ir.RELAY_NODE_ARG_NAME):
dimensions.update(
{self._ir.RELAY_NODE_ARG_NAME: self._ir.args.get(self._ir.RELAY_NODE_ARG_NAME)})
expected_http = {
'init_config': None,
'instances': [
{
'name': 'influxdb-relay',
'url': 'http://%s:%d/ping' % e_host_port
'url': 'http://%s:%d/ping' % e_host_port,
'dimensions': dimensions
}
]
}
self.assertDictEqual(expected_http, built_config)
def _verify_process_conf(self, actual_config):
dimensions = {
'component': 'influxdb-relay',
'service': 'influxdb'
}
if self._ir.args and self._ir.args.get(self._ir.RELAY_NODE_ARG_NAME):
dimensions.update(
{self._ir.RELAY_NODE_ARG_NAME: self._ir.args.get(self._ir.RELAY_NODE_ARG_NAME)})
expected_process = {
'init_config': None,
'instances': [
@ -195,10 +218,7 @@ class TestInfluxDBRelayDetection(base.BaseTestCase):
'search_string': ['influxdb-relay'],
'exact_match': False,
'name': 'influxdb-relay',
'dimensions': {
'component': 'influxdb-relay',
'service': 'influxdb'
}
'dimensions': dimensions
}
]
}