Add support for live query in metrics
Change-Id: Icc6de815e4db6ad10dc179d2e30eb487b568cc65
This commit is contained in:
parent
61c59b730f
commit
7c16307c3a
|
@ -20,7 +20,7 @@ from surveilclient.tests.v2_0 import clienttest
|
|||
class TestMetrics(clienttest.ClientTest):
|
||||
|
||||
@httpretty.activate
|
||||
def test_list_metrics(self):
|
||||
def test_list_host_metrics(self):
|
||||
httpretty.register_uri(
|
||||
httpretty.POST, "http://localhost:5311/v2/status/"
|
||||
"hosts/localhost/metrics/load1",
|
||||
|
@ -28,11 +28,12 @@ class TestMetrics(clienttest.ClientTest):
|
|||
'{"min": "5", "warning": "200", "value": "150"}]'
|
||||
)
|
||||
|
||||
metrics = self.client.status.hosts.metrics.get('localhost', 'load1',
|
||||
time_begin='2015-05-22'
|
||||
'T13:38:08Z',
|
||||
time_end='2015-05-'
|
||||
'22T13:38:08Z')
|
||||
metrics = self.client.status.hosts.metrics.list(
|
||||
'localhost',
|
||||
'load1',
|
||||
start_time='2015-05-22T13:38:08Z',
|
||||
end_time='2015-05-22T13:38:08Z'
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
metrics,
|
||||
|
@ -41,7 +42,7 @@ class TestMetrics(clienttest.ClientTest):
|
|||
)
|
||||
|
||||
@httpretty.activate
|
||||
def test_list_service(self):
|
||||
def test_list_host_metrics_service(self):
|
||||
httpretty.register_uri(
|
||||
httpretty.POST, "http://localhost:5311/v2/status/hosts/localhost"
|
||||
"/services/load/metrics/load1",
|
||||
|
@ -49,11 +50,14 @@ class TestMetrics(clienttest.ClientTest):
|
|||
'{"min": "5", "warning": "200", "value": "150"}]'
|
||||
)
|
||||
|
||||
metrics = self.client.status.hosts.metrics.get('localhost', 'load1',
|
||||
'load',
|
||||
'2015-05-22T13:38:08Z',
|
||||
'2015-05-22T13:38:08Z'
|
||||
)
|
||||
live_query = ('{"time_interval": { "start_time": '
|
||||
'"2015-05-22T13:38:08Z",'
|
||||
'"end_time": "2015-05-22T13:38:08Z"}}')
|
||||
|
||||
metrics = self.client.status.hosts.metrics.list('localhost', 'load1',
|
||||
'load',
|
||||
live_query=live_query
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
metrics,
|
||||
|
@ -62,7 +66,7 @@ class TestMetrics(clienttest.ClientTest):
|
|||
)
|
||||
|
||||
@httpretty.activate
|
||||
def test_show(self):
|
||||
def test_show_host_metrics(self):
|
||||
httpretty.register_uri(
|
||||
httpretty.GET, "http://localhost:5311/v2/status/hosts/localhost"
|
||||
"/metrics/load1",
|
||||
|
@ -77,11 +81,11 @@ class TestMetrics(clienttest.ClientTest):
|
|||
)
|
||||
|
||||
@httpretty.activate
|
||||
def test_show_service(self):
|
||||
def test_show_host_service_metrics(self):
|
||||
httpretty.register_uri(
|
||||
httpretty.GET, "http://localhost:5311/v2/status/hosts/localhost"
|
||||
"/services/load/metrics/load1",
|
||||
body='{"min": "2", "warning": "15", "value": "3"}'
|
||||
body='{"value": "3"}'
|
||||
)
|
||||
|
||||
metrics = self.client.status.hosts.metrics.get('localhost', 'load1',
|
||||
|
@ -89,20 +93,22 @@ class TestMetrics(clienttest.ClientTest):
|
|||
|
||||
self.assertEqual(
|
||||
metrics,
|
||||
{"min": "2", "warning": "15", "value": "3"}
|
||||
{"value": "3"}
|
||||
)
|
||||
|
||||
@httpretty.activate
|
||||
def test_list_metrics_name(self):
|
||||
def test_show_host_service(self):
|
||||
httpretty.register_uri(
|
||||
httpretty.GET, "http://localhost:5311/v2/status/hosts/localhost"
|
||||
"/metrics",
|
||||
body='[{"metric_name": "rta"},{"metric_name": "load5"}]'
|
||||
"/services/load/metrics",
|
||||
body='[{"metric_name": "load1"},{"metric_name": "load5"}]'
|
||||
)
|
||||
|
||||
metrics = self.client.status.hosts.metrics.list('localhost')
|
||||
metrics = self.client.status.hosts.metrics.get(
|
||||
'localhost',
|
||||
service_description='load')
|
||||
|
||||
self.assertEqual(
|
||||
metrics,
|
||||
[{"metric_name": "rta"}, {"metric_name": "load5"}]
|
||||
)
|
||||
[{"metric_name": "load1"}, {"metric_name": "load5"}]
|
||||
)
|
||||
|
|
|
@ -367,50 +367,29 @@ def do_status_service_list(sc, args):
|
|||
|
||||
@cliutils.arg("--host_name", help="Name of the host")
|
||||
@cliutils.arg("--metric_name", help="Name of the metric")
|
||||
@cliutils.arg("--time_begin", help="begin of the metric")
|
||||
@cliutils.arg("--time_end", help="end of the metric")
|
||||
@cliutils.arg("--start_time", help="begin of the metric")
|
||||
@cliutils.arg("--end_time", help="end of the metric")
|
||||
@cliutils.arg("--service_description", help="Service description")
|
||||
@cliutils.arg("--live_query", help="Live query")
|
||||
def do_status_metrics_list(sc, args):
|
||||
"""List all status metrics."""
|
||||
arg_names = ['host_name',
|
||||
'metric_name',
|
||||
'time_begin',
|
||||
'time_end',
|
||||
'service_description']
|
||||
'start_time',
|
||||
'end_time',
|
||||
'service_description',
|
||||
'live_query',
|
||||
]
|
||||
arg = _dict_from_args(args, arg_names)
|
||||
|
||||
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)
|
||||
metrics = sc.status.hosts.metrics.list(**arg)
|
||||
if args.json:
|
||||
print(utils.json_formatter(metrics))
|
||||
else:
|
||||
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)
|
||||
cols = utils.get_columns(metrics, [])
|
||||
formatters = reduce(_create_format, cols, {})
|
||||
|
||||
utils.print_list(metrics, cols, formatters=formatters)
|
||||
|
||||
|
||||
@cliutils.arg("--host_name", help="Name of the host")
|
||||
|
@ -420,23 +399,23 @@ def do_status_metrics_show(sc, args):
|
|||
"""Give the last status metrics."""
|
||||
arg_names = ['host_name',
|
||||
'metric_name',
|
||||
'service_description']
|
||||
'service_description',
|
||||
]
|
||||
arg = _dict_from_args(args, arg_names)
|
||||
|
||||
metric = sc.status.hosts.metrics.get(**arg)
|
||||
metrics = sc.status.hosts.metrics.get(**arg)
|
||||
if args.json:
|
||||
print(utils.json_formatter(metric))
|
||||
print(utils.json_formatter(metrics))
|
||||
else:
|
||||
metricProperties = [
|
||||
'min',
|
||||
'max',
|
||||
'warning',
|
||||
'critical',
|
||||
'value',
|
||||
'unit'
|
||||
]
|
||||
if isinstance(metrics, dict):
|
||||
metrics = [metrics]
|
||||
|
||||
utils.print_item(metric, metricProperties)
|
||||
cols = utils.get_columns(metrics,
|
||||
['metric_name',
|
||||
])
|
||||
formatters = reduce(_create_format, cols, {})
|
||||
|
||||
utils.print_list(metrics, cols, formatters=formatters)
|
||||
|
||||
|
||||
@cliutils.arg("--host_name", help="Name of the host")
|
||||
|
@ -499,9 +478,11 @@ def do_status_events_list(sc, args):
|
|||
'service_description',
|
||||
'event_type'])
|
||||
|
||||
def create_format(init, col):
|
||||
init[col] = lambda x: x.get(col, '')
|
||||
return init
|
||||
formatters = reduce(create_format, cols, {})
|
||||
formatters = reduce(_create_format, cols, {})
|
||||
|
||||
utils.print_list(events, cols, formatters=formatters)
|
||||
|
||||
|
||||
def _create_format(init, col):
|
||||
init[col] = lambda x: x.get(col, '')
|
||||
return init
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
import json
|
||||
|
||||
from surveilclient.common import surveil_manager
|
||||
|
||||
|
@ -18,45 +19,47 @@ from surveilclient.common import surveil_manager
|
|||
class MetricsManager(surveil_manager.SurveilManager):
|
||||
base_url = '/status/hosts'
|
||||
|
||||
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:
|
||||
time_delta = {'begin': time_begin,
|
||||
'end': time_end}
|
||||
if service_description is not None:
|
||||
url = (MetricsManager.base_url + "/" + host_name + "/services/"
|
||||
+ service_description + "/metrics/" + metric_name)
|
||||
else:
|
||||
url = (MetricsManager.base_url + "/" + host_name + "/metrics/"
|
||||
+ metric_name)
|
||||
|
||||
resp, body = self.http_client.json_request(
|
||||
url, 'POST', body=time_delta
|
||||
)
|
||||
return body
|
||||
elif time_begin is None and time_end is None:
|
||||
|
||||
if service_description is not None:
|
||||
url = (MetricsManager.base_url + "/" + host_name + "/services/"
|
||||
+ service_description + "/metrics/" + metric_name)
|
||||
else:
|
||||
url = (MetricsManager.base_url + "/" + host_name + "/metrics/"
|
||||
+ metric_name)
|
||||
|
||||
resp, body = self.http_client.json_request(url, 'GET')
|
||||
return body
|
||||
else:
|
||||
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")
|
||||
def get(self, host_name, metric_name=None, service_description=None):
|
||||
"""Get a list of last metrics."""
|
||||
|
||||
resp, body = self.http_client.json_request(
|
||||
url, 'GET')
|
||||
return body
|
||||
self._create_url(host_name, service_description, metric_name),
|
||||
'GET'
|
||||
)
|
||||
|
||||
return body
|
||||
|
||||
def list(self, host_name, metric_name, service_description=None,
|
||||
live_query=None, start_time=None, end_time=None):
|
||||
"""Get a list of metrics name."""
|
||||
|
||||
if live_query is None:
|
||||
live_query = {}
|
||||
else:
|
||||
live_query = json.loads(live_query)
|
||||
|
||||
if start_time and end_time:
|
||||
live_query['time_interval'] = {
|
||||
"start_time": start_time,
|
||||
"end_time": end_time
|
||||
}
|
||||
|
||||
resp, body = self.http_client.json_request(
|
||||
self._create_url(host_name, service_description, metric_name),
|
||||
'POST', body=live_query)
|
||||
|
||||
return body
|
||||
|
||||
def _create_url(self, host_name, service_description=None,
|
||||
metric_name=None):
|
||||
url = self.base_url + '/' + host_name
|
||||
|
||||
if service_description:
|
||||
url += '/services/' + service_description
|
||||
|
||||
url += '/metrics'
|
||||
|
||||
if metric_name:
|
||||
url += '/' + metric_name
|
||||
|
||||
return url
|
||||
|
|
Loading…
Reference in New Issue