From 2bc37d6d78bc4132ea802fb78a1e8ea3fdf8ba79 Mon Sep 17 00:00:00 2001 From: Erik Reid <erik.reid@geant.org> Date: Fri, 1 Oct 2021 13:41:35 +0200 Subject: [PATCH] bugfixes and updated tests with eumetsat data --- brian_polling_manager/configuration.py | 17 ++++++++++- brian_polling_manager/eumetsat_multicast.py | 4 +-- brian_polling_manager/inventory.py | 33 +++++++++++++++++++++ brian_polling_manager/main.py | 6 +++- test/conftest.py | 23 ++++++++++---- 5 files changed, 74 insertions(+), 9 deletions(-) diff --git a/brian_polling_manager/configuration.py b/brian_polling_manager/configuration.py index e7c273d..92a3ef1 100644 --- a/brian_polling_manager/configuration.py +++ b/brian_polling_manager/configuration.py @@ -133,6 +133,7 @@ class State(object): GWS_INDIRECT = 'gws-indirect.json' INTERFACES = 'interfaces.json' STATE = 'state.json' + EUMET_MC = 'eumetsat-multicast.json' STATE_SCHEMA = { '$schema': 'http://json-schema.org/draft-07/schema#', @@ -150,7 +151,8 @@ class State(object): 'state': os.path.join(state_dir, State.STATE), 'interfaces': os.path.join(state_dir, State.INTERFACES), 'gws-direct': os.path.join(state_dir, State.GWS_DIRECT), - 'gws-indirect': os.path.join(state_dir, State.GWS_INDIRECT) + 'gws-indirect': os.path.join(state_dir, State.GWS_INDIRECT), + 'eumetsat-multicast': os.path.join(state_dir, State.EUMET_MC) } @staticmethod @@ -231,6 +233,19 @@ class State(object): new_services, inventory.GWS_INDIRECT_SCHEMA) + @property + def eumetsat_multicast(self) -> list: + return State._load_json( + self.cache_filenames['eumetsat-multicast'], + inventory.MULTICAST_SUBSCRIPTION_LIST_SCHEMA) + + @eumetsat_multicast.setter + def eumetsat_multicast(self, new_subscriptions): + State._save_json( + self.cache_filenames['eumetsat-multicast'], + new_subscriptions, + inventory.MULTICAST_SUBSCRIPTION_LIST_SCHEMA) + def _setup_logging(filename=None): """ diff --git a/brian_polling_manager/eumetsat_multicast.py b/brian_polling_manager/eumetsat_multicast.py index 2912e49..07fc91a 100644 --- a/brian_polling_manager/eumetsat_multicast.py +++ b/brian_polling_manager/eumetsat_multicast.py @@ -33,10 +33,10 @@ class EUMETSATMulticastHostCheck(sensu.AbstractCheck): return self.hostname -def refresh(sensu_params, eumetsat_multicast_config): +def refresh(sensu_params, subscriptions): # one check per unique host - all_routers = {x['router'] for x in eumetsat_multicast_config} + all_routers = {x['router'] for x in subscriptions} required_checks = [ EUMETSATMulticastHostCheck( sensu_params['eumetsat-multicast-check'], hostname) diff --git a/brian_polling_manager/inventory.py b/brian_polling_manager/inventory.py index 5d11803..7204373 100644 --- a/brian_polling_manager/inventory.py +++ b/brian_polling_manager/inventory.py @@ -97,6 +97,26 @@ GWS_INDIRECT_SCHEMA = { } +# much less strict version of the actual schema +MULTICAST_SUBSCRIPTION_LIST_SCHEMA = { + '$schema': 'http://json-schema.org/draft-07/schema#', + + 'definitions': { + 'subscription': { + 'type': 'object', + 'properties': { + # we really only use this field + # don't depend strictly on unused data + 'router': {'type': 'string'} + }, + 'required': ['router'] + } + }, + + 'type': 'array', + 'items': {'$ref': '#/definitions/subscription'} +} + def _pick_one(haystack): if not isinstance(haystack, (list, tuple, set)): haystack = [haystack] @@ -159,6 +179,19 @@ def load_gws_indirect_services(base_urls): 'poller/gws/indirect', base_urls, GWS_INDIRECT_SCHEMA) +def load_eumetsat_multicast_subscriptions(base_urls): + """ + Load /poller/eumetsat-multicast from inventory provider + + :param base_urls: inventory provider base api url, or a list of them + :return: a list of dicts, each with a 'router' key + """ + return _load_inventory_json( + 'poller/eumetsat-multicast', + base_urls, + MULTICAST_SUBSCRIPTION_LIST_SCHEMA) + + def last_update_timestamp(base_urls) -> float: try: r = requests.get( diff --git a/brian_polling_manager/main.py b/brian_polling_manager/main.py index cb31270..ab64d50 100644 --- a/brian_polling_manager/main.py +++ b/brian_polling_manager/main.py @@ -54,7 +54,8 @@ REFRESH_RESULT_SCHEMA = { 'properties': { 'interfaces': {'$ref': '#/definitions/refresh-result'}, 'gws_direct': {'$ref': '#/definitions/refresh-result'}, - 'gws_indirect': {'$ref': '#/definitions/refresh-result'} + 'gws_indirect': {'$ref': '#/definitions/refresh-result'}, + 'eumetsat_multicast': {'$ref': '#/definitions/refresh-result'}, }, 'required': ['interfaces'], 'additionalProperties': False @@ -85,6 +86,9 @@ def refresh(config, force=False): config['inventory']) state.gws_indirect = inventory.load_gws_indirect_services( config['inventory']) + state.eumetsat_multicast \ + = inventory.load_eumetsat_multicast_subscriptions( + config['inventory']) result = { 'interfaces': interfaces.refresh(config['sensu'], state.interfaces), 'gws_direct': gws_direct.refresh(config['sensu'], state.gws_direct), diff --git a/test/conftest.py b/test/conftest.py index a2e9940..529e19a 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -36,20 +36,28 @@ def config(): 'interface-check': { 'script': '/var/lib/sensu/bin/counter2influx.sh', 'measurement': 'counters', - 'command': ('{script} {measurement} ' - '{community} {hostname} ' - '{interface} {ifIndex}'), + 'command': '{script} {measurement}' + ' {community} {hostname}' + ' {interface} {ifIndex}', }, 'gws-direct-interface-check': { 'script': '/var/lib/sensu/bin/poll-gws-direct.sh', 'measurement': 'gwsd_counters', - 'command': ('{script} {measurement} ' - '{nren} {isp} {hostname} {tag}') + 'command': '{script} {measurement}' + ' {nren} {isp} {hostname} {tag}' }, 'dscp32-service-check': { 'script': '/var/lib/sensu/bin/poll-gws-indirect.sh', 'measurement': 'dscp32_counters', 'command': '{script} {measurement} {service}' + }, + 'eumetsat-multicast-check': { + 'script': '/home/brian_checks/venv/eumetsat-multicast', + 'measurement': 'multicast', + 'command': '{script}' + ' --inventory http://localhost:18080' + ' --measurement {measurement}' + ' --hostname {hostname}' } }, 'statedir': state_dir_name, @@ -184,6 +192,11 @@ def mocked_inventory(): url=re.compile(r'.*inventory.+/poller/gws/indirect.*'), body=_load_test_data('gws-indirect.json')) + responses.add( + method=responses.GET, + url=re.compile(r'.*inventory.+/poller/eumetsat-multicast'), + body=_load_test_data('eumetsat-multicast.json')) + bogus_version = {'latch': {'timestamp': 10000 * random.random()}} # mocked api for returning all checks responses.add( -- GitLab