diff --git a/brian_polling_manager/configuration.py b/brian_polling_manager/configuration.py index e7c273d54762b1d074e805af20ea4721cbbf8a74..92a3ef1bd0928c67ba5d4012f866400e5b370476 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 2912e49c264260f26c14ff4b09b2cb31283e106a..07fc91ad274c4265359c3fea66e0340644507107 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 5d118036f2002699866d11427d5036795f802df2..72043739cc1823b5e4496ce17a43f6c7def912db 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 cb31270288382a802917b699eb398ffb80a58715..ab64d50a13330bbd5137e02c3c43fd42e96cbc19 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 a2e99401dcc03df611c1d555c74562ffb496191b..529e19ac6ed0491320d3e91ab9b91f625794148f 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(