Live API: Get specific host by host_name

Change-Id: Ie881e153df36e598800d8c36e3617e32a43706ce
This commit is contained in:
aviau 2015-04-23 15:38:59 -04:00
parent 0809e0d8fb
commit 2705b93b8d
3 changed files with 85 additions and 23 deletions
surveil
api
controllers/v2/status
handlers/status
tests/api/controllers/v2/status

@ -55,15 +55,14 @@ class HostController(rest.RestController):
def __init__(self, host_name):
pecan.request.context['host_name'] = host_name
self._id = host_name
self.host_name = host_name
@pecan.expose()
@wsme_pecan.wsexpose(live_host.LiveHost)
def get(self):
"""Returns a specific host."""
output = '{"host_name": "myhostname", "alias": %s}' % self._id
return output
handler = live_host_handler.HostHandler(pecan.request)
host = handler.get(self.host_name)
return host
class ConfigController(rest.RestController):

@ -23,6 +23,19 @@ from surveil.api.handlers.status import liveQuery_filter as query_filter
class HostHandler(handler.Handler):
"""Fulfills a request on the live hosts."""
def get(self, host_name):
"""Return a host."""
cli = self.request.influxdb_client
query = ("SELECT * from HOST_STATE "
"WHERE host_name='%s' "
"GROUP BY * LIMIT 1") % host_name
response = cli.query(query)
host = live_host.LiveHost(
**self._host_dict_from_influx_item(response.items()[0])
)
return host
def get_all(self, live_query=None):
"""Return all live hosts."""
cli = self.request.influxdb_client
@ -34,23 +47,7 @@ class HostHandler(handler.Handler):
host_dicts = []
for item in response.items():
first_entry = next(item[1])
host_dict = {
# TAGS
"host_name": item[0][1]['host_name'],
"address": item[0][1]['address'],
"description": item[0][1]['host_name'],
"childs": json.loads(item[0][1]['childs']),
# Values
"state": first_entry['state'],
"acknowledged": int(first_entry['acknowledged']),
"last_check": int(first_entry['last_check']),
"last_state_change": int(first_entry['last_state_change']),
"plugin_output": first_entry['output']
}
host_dict = self._host_dict_from_influx_item(item)
host_dicts.append(host_dict)
if live_query:
@ -65,3 +62,26 @@ class HostHandler(handler.Handler):
hosts.append(host)
return hosts
def _host_dict_from_influx_item(self, item):
points = item[1]
first_point = next(points)
tags = item[0][1]
host_dict = {
# TAGS
"host_name": tags['host_name'],
"address": tags['address'],
"description": tags['host_name'],
"childs": json.loads(tags['childs']),
# Values
"state": first_point['state'],
"acknowledged": int(first_point['acknowledged']),
"last_check": int(first_point['last_check']),
"last_state_change": int(first_point['last_state_change']),
"plugin_output": first_point['output']
}
return host_dict

@ -158,3 +158,46 @@ class TestStatusHosts(functionalTest.FunctionalTest):
expected = [{"host_name": "ws-arbiter", "last_check": 1429405764}]
self.assertItemsEqual(json.loads(response.body), expected)
@httpretty.activate
def test_get_specific_host(self):
influx_response = json.dumps(
{"results": [
{"series": [
{"name": "HOST_STATE",
"tags": {"address": "localhost",
"childs": "[\"test_keystone\"]",
"host_name": "localhost"},
"columns": ["time",
"acknowledged",
"last_check",
"last_state_change",
"output",
"state",
"state_type"],
"values":[["2015-04-23T18:03:11Z",
0,
1.429812191e+09,
1.429812192166997e+09,
"OK - localhost: rta 0.044ms, lost 0%",
0,
"HARD"]]}]}]}
)
httpretty.register_uri(httpretty.GET,
"http://influxdb:8086/query",
body=influx_response)
response = self.app.get("/v2/status/hosts/localhost")
expected = {"childs": ["test_keystone"],
"description": "localhost",
"last_state_change": 1429812192,
"acknowledged": 0,
"plugin_output": "OK - localhost: rta 0.044ms, lost 0%",
"last_check": 1429812191,
"state": 0,
"host_name": "localhost",
"address": "localhost"}
self.assertItemsEqual(json.loads(response.body), expected)