Add List and Show for metric status

Change-Id: I714e997d7ba23f43506518fe10eae0e586149e3d
This commit is contained in:
flavien.peyre 2015-05-22 14:08:34 -04:00
parent 2481fd67d7
commit 7abd715f5e
6 changed files with 213 additions and 1 deletions

View File

@ -0,0 +1,93 @@
# Copyright 2015 - Savoir-Faire Linux inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# 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 httpretty
from surveilclient.tests.v2_0 import clienttest
class TestMetrics(clienttest.ClientTest):
@httpretty.activate
def test_list(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:8080/v2/status/"
"hosts/localhost/metrics/load1",
body='[{"min": "2", "warning": "15", "value": "3"},'
'{"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')
self.assertEqual(
metrics,
[{"min": "2", "warning": "15", "value": "3"},
{"min": "5", "warning": "200", "value": "150"}]
)
@httpretty.activate
def test_list_service(self):
httpretty.register_uri(
httpretty.POST, "http://localhost:8080/v2/status/hosts/localhost"
"/services/load/metrics/load1",
body='[{"min": "2", "warning": "15", "value": "3"},'
'{"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'
)
self.assertEqual(
metrics,
[{"min": "2", "warning": "15", "value": "3"},
{"min": "5", "warning": "200", "value": "150"}]
)
@httpretty.activate
def test_show(self):
httpretty.register_uri(
httpretty.GET, "http://localhost:8080/v2/status/hosts/localhost"
"/metrics/load1",
body='{"min": "2", "warning": "15", "value": "3"}'
)
metrics = self.client.status.hosts.metrics.get('localhost', 'load1')
self.assertEqual(
metrics,
{"min": "2", "warning": "15", "value": "3"}
)
@httpretty.activate
def test_show_service(self):
httpretty.register_uri(
httpretty.GET, "http://localhost:8080/v2/status/hosts/localhost"
"/services/load/metrics/load1",
body='{"min": "2", "warning": "15", "value": "3"}'
)
metrics = self.client.status.hosts.metrics.get('localhost', 'load1',
'load')
self.assertEqual(
metrics,
{"min": "2", "warning": "15", "value": "3"}
)

View File

@ -285,6 +285,70 @@ def do_status_service_list(sc, args):
utils.print_list(services, cols, formatters=formatters)
@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("--service_description", help="Service description")
def do_status_metrics_list(sc, args):
"""List all status metrics."""
arg_names = ['host_name',
'metric_name',
'time_begin',
'time_end',
'service_description']
arg = _dict_from_args(args, arg_names)
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")
@cliutils.arg("metric_name", help="Name of the metric")
@cliutils.arg("--service_description", help="Service description")
def do_status_metrics_show(sc, args):
"""Give the last status metrics."""
arg_names = ['host_name',
'metric_name',
'service_description']
arg = _dict_from_args(args, arg_names)
metric = sc.status.hosts.metrics.get(**arg)
if args.json:
print(utils.json_formatter(metric))
else:
metricProperties = [
'min',
'max',
'warning',
'critical',
'value',
'unit'
]
utils.print_item(metric, metricProperties)
@cliutils.arg("host_name", help="Name of the host")
@cliutils.arg("--service_description", help="Service description")
@cliutils.arg("--time_stamp")

View File

@ -23,4 +23,4 @@ class StatusManager(surveil_manager.SurveilManager):
def __init__(self, http_client):
super(StatusManager, self).__init__(http_client)
self.hosts = hosts.HostsManager(self.http_client)
self.services = services.ServicesManager(self.http_client)
self.services = services.ServicesManager(self.http_client)

View File

@ -13,11 +13,16 @@
# under the License.
from surveilclient.common import surveil_manager
from surveilclient.v2_0.status.metrics import metrics
class HostsManager(surveil_manager.SurveilManager):
base_url = '/status/hosts'
def __init__(self, http_client):
super(HostsManager, self).__init__(http_client)
self.metrics = metrics.MetricsManager(self.http_client)
def list(self, live_query={'filters': '{}'}):
"""Get a list of hosts."""
resp, body = self.http_client.json_request(

View File

@ -0,0 +1,50 @@
# Copyright 2014-2015 - Savoir-Faire Linux inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
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):
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:
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 {}