diff --git a/surveilclient/tests/v2_0/status/test_metrics.py b/surveilclient/tests/v2_0/status/test_metrics.py index f88aa9a..96b1259 100644 --- a/surveilclient/tests/v2_0/status/test_metrics.py +++ b/surveilclient/tests/v2_0/status/test_metrics.py @@ -20,7 +20,7 @@ from surveilclient.tests.v2_0 import clienttest class TestMetrics(clienttest.ClientTest): @httpretty.activate - def test_list(self): + def test_list_metrics(self): httpretty.register_uri( httpretty.POST, "http://localhost:8080/v2/status/" "hosts/localhost/metrics/load1", @@ -91,3 +91,18 @@ class TestMetrics(clienttest.ClientTest): metrics, {"min": "2", "warning": "15", "value": "3"} ) + + @httpretty.activate + def test_list_metrics_name(self): + httpretty.register_uri( + httpretty.GET, "http://localhost:8080/v2/status/hosts/localhost" + "/metrics", + body='[{"metric_name": "rta"},{"metric_name": "load5"}]' + ) + + metrics = self.client.status.hosts.metrics.list('localhost') + + self.assertEqual( + metrics, + [{"metric_name": "rta"}, {"metric_name": "load5"}] + ) \ No newline at end of file diff --git a/surveilclient/v2_0/shell.py b/surveilclient/v2_0/shell.py index f9bc472..5b5f70c 100644 --- a/surveilclient/v2_0/shell.py +++ b/surveilclient/v2_0/shell.py @@ -380,28 +380,38 @@ def do_status_metrics_list(sc, args): 'service_description'] arg = _dict_from_args(args, arg_names) - metrics = sc.status.hosts.metrics.get(**arg) - if args.json: - print(utils.json_formatter(metrics)) + if arg.get('metric_name', None) is None: + metrics = sc.status.hosts.metrics.list(**arg) + if args.json: + print(utils.json_formatter(metrics)) + else: + cols = ['metric_name'] + formatters = { + 'metric_name': lambda x: x.get('metric_name', '') + } + utils.print_list(metrics, cols, formatters=formatters) else: - cols = [ - 'min', - 'max', - 'warning', - 'critical', - 'value', - 'unit' - ] - - formatters = { - 'min': lambda x: x.get('min', ''), - 'max': lambda x: x.get('max', ''), - 'warning': lambda x: x.get('warning', ''), - 'critical': lambda x: x.get('critical', ''), - 'value': lambda x: x.get('value', ''), - 'unit': lambda x: x.get('unit', ''), - } - utils.print_list(metrics, cols, formatters=formatters) + metrics = sc.status.hosts.metrics.get(**arg) + if args.json: + print(utils.json_formatter(metrics)) + else: + cols = [ + 'min', + 'max', + 'warning', + 'critical', + 'value', + 'unit' + ] + formatters = { + 'min': lambda x: x.get('min', ''), + 'max': lambda x: x.get('max', ''), + 'warning': lambda x: x.get('warning', ''), + 'critical': lambda x: x.get('critical', ''), + 'value': lambda x: x.get('value', ''), + 'unit': lambda x: x.get('unit', ''), + } + utils.print_list(metrics, cols, formatters=formatters) @cliutils.arg("--host_name", help="Name of the host") diff --git a/surveilclient/v2_0/status/metrics/metrics.py b/surveilclient/v2_0/status/metrics/metrics.py index a227aa0..8ed548f 100644 --- a/surveilclient/v2_0/status/metrics/metrics.py +++ b/surveilclient/v2_0/status/metrics/metrics.py @@ -20,8 +20,8 @@ class MetricsManager(surveil_manager.SurveilManager): def get(self, host_name, metric_name, service_description=None, time_begin=None, time_end=None): + """Get a list of metrics.""" if time_begin is not None and time_end is not None: - """Get a list of metrics.""" time_delta = {'begin': time_begin, 'end': time_end} if service_description is not None: @@ -47,4 +47,16 @@ class MetricsManager(surveil_manager.SurveilManager): resp, body = self.http_client.json_request(url, 'GET') return body else: - return {} \ No newline at end of file + return {} + + def list(self, host_name, service_description=None): + """Get a list of metrics name.""" + if service_description is None: + url = MetricsManager.base_url + "/" + host_name + "/metrics" + else: + url = (MetricsManager.base_url + "/" + host_name + "/services/" + + service_description + "/metrics") + + resp, body = self.http_client.json_request( + url, 'GET') + return body \ No newline at end of file