From 4d96462469d5ac28ba0095d683b52161c2f13f72 Mon Sep 17 00:00:00 2001 From: aviau Date: Wed, 22 Apr 2015 15:02:24 -0400 Subject: [PATCH] Added childs attribute to LiveHost Change-Id: I462dc319d0701b07e11707f0731b02e761638a06 --- surveil/api/datamodel/status/live_host.py | 4 + .../api/handlers/status/live_host_handler.py | 9 +- surveil/cmd/init.py | 1 + .../api/controllers/v2/status/test_hosts.py | 98 +++++++++++++++---- 4 files changed, 92 insertions(+), 20 deletions(-) diff --git a/surveil/api/datamodel/status/live_host.py b/surveil/api/datamodel/status/live_host.py index c6f55d4..b2f19ee 100644 --- a/surveil/api/datamodel/status/live_host.py +++ b/surveil/api/datamodel/status/live_host.py @@ -25,6 +25,9 @@ class LiveHost(types.Base): address = wsme.wsattr(wtypes.text, mandatory=False) """The address of the host""" + childs = wsme.wsattr([wtypes.text], mandatory=False) + """The childs of the host""" + description = wsme.wsattr(wtypes.text, mandatory=False) """The description of the host""" @@ -48,6 +51,7 @@ class LiveHost(types.Base): return cls( host_name='CoolHost', address="127.0.0.1", + childs=['surveil.com'], description='Very Nice Host', state=0, acknowledged=1, diff --git a/surveil/api/handlers/status/live_host_handler.py b/surveil/api/handlers/status/live_host_handler.py index 44d2a50..3190daa 100644 --- a/surveil/api/handlers/status/live_host_handler.py +++ b/surveil/api/handlers/status/live_host_handler.py @@ -13,6 +13,7 @@ # under the License. from __future__ import print_function +import json from surveil.api.datamodel.status import live_host from surveil.api.handlers import handler @@ -25,7 +26,9 @@ class HostHandler(handler.Handler): def get_all(self, live_query=None): """Return all live hosts.""" cli = self.request.influxdb_client - query = "SELECT * from HOST_STATE GROUP BY host_name, address LIMIT 1" + query = ("SELECT * from HOST_STATE " + "GROUP BY host_name, address, childs " + "LIMIT 1") response = cli.query(query) host_dicts = [] @@ -34,9 +37,13 @@ class HostHandler(handler.Handler): 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": first_entry['acknowledged'], "last_check": int(first_entry['last_check']), diff --git a/surveil/cmd/init.py b/surveil/cmd/init.py index d1b998a..6acdfd7 100644 --- a/surveil/cmd/init.py +++ b/surveil/cmd/init.py @@ -85,6 +85,7 @@ def main(): "_OS_PASSWORD": "blo", "_OS_TENANT": "blu", "_KS_SERVICES": "bly", + "parents": "localhost" } ) diff --git a/surveil/tests/api/controllers/v2/status/test_hosts.py b/surveil/tests/api/controllers/v2/status/test_hosts.py index 9c68c2c..8125287 100644 --- a/surveil/tests/api/controllers/v2/status/test_hosts.py +++ b/surveil/tests/api/controllers/v2/status/test_hosts.py @@ -23,23 +23,80 @@ class TestStatusHosts(functionalTest.FunctionalTest): def setUp(self): super(TestStatusHosts, self).setUp() - self.influxdb_response = ( - '{"results":[{"series":[{"name":"HOST_STATE","tags":{"host_name":' - '"localhost","address":"127.0.0.1"},"columns":["time","last_check' - '","last_state_change","output","state","state_type", "acknowledg' - 'ed"],"values":[["2015-04-19T01:09:24Z",1.429405764e+09,1.4294057' - '65316929e+09,"OK - localhost: rta 0.033ms, lost 0%",0,"HARD",0]]' - '},{"name":"HOST_STATE","tags":{"host_name":"test_keystone","addr' - 'ess":"127.0.0.1"},"columns":["time","last_check","last_state_cha' - 'nge","output","state","state_type", "acknowledged"],"values":[["' - '2015-04-19T01:09:23Z",1.429405763e+09,1.429405765317144e+09,"OK ' - '- 127.0.0.1: rta 0.032ms, lost 0%",0,"HARD",0]]},{"name":"HOST_S' - 'TATE","tags":{"host_name":"ws-arbiter","address":"127.0.0.1"},"c' - 'olumns":["time","last_check","last_state_change","output","state' - '","state_type","acknowledged"],"values":[["2015-04-19T01:09:24Z"' - ',1.429405764e+09,1.429405765317063e+09,"OK - localhost: rta 0.03' - '0ms, lost 0%",0,"HARD",0]]}]}]}' - ) + self.influxdb_response = json.dumps({ + "results": [ + { + "series": [ + {"name": "HOST_STATE", + "tags": {"host_name": "localhost", + "address": "127.0.0.1", + "childs": '[]'}, + "columns": [ + "time", + "last_check", + "last_state_change", + "output", + "state", + "state_type", + "acknowledged" + ], + "values":[ + ["2015-04-19T01:09:24Z", + 1.429405764e+09, + 1.429405765316929e+09, + "OK - localhost: rta 0.033ms, lost 0%", + 0, + "HARD", + 0] + ]}, + {"name": "HOST_STATE", + "tags": {"host_name": "test_keystone", + "address": "127.0.0.1", + "childs": '[]'}, + "columns": [ + "time", + "last_check", + "last_state_change", + "output", + "state", + "state_type", + "acknowledged" + ], + "values":[ + ["2015-04-19T01:09:23Z", + 1.429405763e+09, + 1.429405765317144e+09, + "OK - 127.0.0.1: rta 0.032ms, lost 0%", + 0, + "HARD", + 0] + ]}, + {"name": "HOST_STATE", + "tags": {"host_name": "ws-arbiter", + "address": "127.0.0.1", + "childs": '["test_keystone"]'}, + "columns": [ + "time", + "last_check", + "last_state_change", + "output", + "state", + "state_type", + "acknowledged" + ], + "values":[ + ["2015-04-19T01:09:24Z", + 1.429405764e+09, + 1.429405765317063e+09, + "OK - localhost: rta 0.030ms, lost 0%", + 0, + "HARD", + 0] + ]} + ] + } + ] + }) @httpretty.activate def test_get_all_hosts(self): @@ -52,6 +109,7 @@ class TestStatusHosts(functionalTest.FunctionalTest): expected = [ {"description": "localhost", "address": "127.0.0.1", + "childs": [], "last_state_change": 1429405765, "plugin_output": "OK - localhost: rta 0.033ms, lost 0%", "last_check": 1429405764, @@ -60,6 +118,7 @@ class TestStatusHosts(functionalTest.FunctionalTest): "host_name": "localhost"}, {"description": "test_keystone", "address": "127.0.0.1", + "childs": [], "last_state_change": 1429405765, "plugin_output": "OK - 127.0.0.1: rta 0.032ms, lost 0%", "last_check": 1429405763, @@ -68,6 +127,7 @@ class TestStatusHosts(functionalTest.FunctionalTest): "host_name": "test_keystone"}, {"description": "ws-arbiter", "address": "127.0.0.1", + "childs": ['test_keystone'], "last_state_change": 1429405765, "plugin_output": "OK - localhost: rta 0.030ms, lost 0%", "last_check": 1429405764, @@ -75,7 +135,7 @@ class TestStatusHosts(functionalTest.FunctionalTest): "acknowledged": 0, "host_name": "ws-arbiter"}] - self.assertEqual(json.loads(response.body), expected) + self.assertItemsEqual(json.loads(response.body), expected) @httpretty.activate def test_query_hosts(self): @@ -97,4 +157,4 @@ class TestStatusHosts(functionalTest.FunctionalTest): expected = [{"host_name": "ws-arbiter", "last_check": 1429405764}] - self.assertEqual(json.loads(response.body), expected) + self.assertItemsEqual(json.loads(response.body), expected)