Skip to content
Snippets Groups Projects
Commit 0b0c766f authored by Erik Reid's avatar Erik Reid
Browse files

store & return all peerings for a remote

parent af9596c2
No related branches found
No related tags found
No related merge requests found
...@@ -384,13 +384,15 @@ def peer_info(address): ...@@ -384,13 +384,15 @@ def peer_info(address):
_location_from_service_dict(s) for s in i['services']] _location_from_service_dict(s) for s in i['services']]
snmp_info = r.get( snmp_info = r.get(
f'snmp-peerings:{address}') f'snmp-peerings:remote:{address}')
if snmp_info: if snmp_info:
snmp_info = json.loads(snmp_info.decode('utf-8')) snmp_info = json.loads(snmp_info.decode('utf-8'))
result['snmp'] = { result['snmp'] = [
'community': snmp_info['community'], {
'oid': snmp_info['oid'] 'hostname': h['hostname'],
} 'community': h['community'],
'oid': h['oid']
} for h in snmp_info]
result['locations'] = _remove_duplicates_from_list(result['locations']) result['locations'] = _remove_duplicates_from_list(result['locations'])
result = json.dumps(result) result = json.dumps(result)
......
...@@ -96,7 +96,7 @@ def snmp_refresh_peerings(self, hostname, community, logical_systems): ...@@ -96,7 +96,7 @@ def snmp_refresh_peerings(self, hostname, community, logical_systems):
logger.exception(msg) logger.exception(msg)
self.log_warning(msg) self.log_warning(msg)
r = get_current_redis(InventoryTask.config) r = get_current_redis(InventoryTask.config)
peerings = r.get(f'snmp-peerings:{hostname}:all') peerings = r.get(f'snmp-peerings:hosts:{hostname}')
if peerings is None: if peerings is None:
raise InventoryTaskError( raise InventoryTaskError(
f'snmp error with {peerings}' f'snmp error with {peerings}'
...@@ -106,16 +106,7 @@ def snmp_refresh_peerings(self, hostname, community, logical_systems): ...@@ -106,16 +106,7 @@ def snmp_refresh_peerings(self, hostname, community, logical_systems):
self.log_warning(f'using cached snmp peering data for {hostname}') self.log_warning(f'using cached snmp peering data for {hostname}')
r = get_next_redis(InventoryTask.config) r = get_next_redis(InventoryTask.config)
r.set(f'snmp-peerings:hosts:{hostname}', json.dumps(peerings))
rp = r.pipeline()
rp.set(f'snmp-peerings:{hostname}:all', json.dumps(peerings))
for session in peerings:
rp.set(
f'snmp-peerings:{hostname}:{session["remote"]}',
json.dumps(session))
rp.execute()
self.log_info(f'snmp peering info loaded from {hostname}') self.log_info(f'snmp peering info loaded from {hostname}')
...@@ -726,6 +717,7 @@ def refresh_finalizer(self, pending_task_ids_json): ...@@ -726,6 +717,7 @@ def refresh_finalizer(self, pending_task_ids_json):
_wait_for_tasks(task_ids, update_callback=self.log_info) _wait_for_tasks(task_ids, update_callback=self.log_info)
_build_subnet_db(update_callback=self.log_info) _build_subnet_db(update_callback=self.log_info)
_build_peering_db(update_callback=self.log_info)
except (jsonschema.ValidationError, except (jsonschema.ValidationError,
json.JSONDecodeError, json.JSONDecodeError,
...@@ -760,6 +752,32 @@ def _build_subnet_db(update_callback=lambda s: None): ...@@ -760,6 +752,32 @@ def _build_subnet_db(update_callback=lambda s: None):
rp.execute() rp.execute()
def _build_peering_db(update_callback=lambda s: None):
r = get_next_redis(InventoryTask.config)
update_callback('loading all network peerings')
peerings = {}
# scan with bigger batches, to mitigate network latency effects
key_prefix = 'snmp-peerings:hosts:'
for k in r.scan_iter(f'{key_prefix}*', count=1000):
key_name = k.decode('utf-8')
hostname = key_name[len(key_prefix):]
host_peerings = r.get(key_name).decode('utf-8')
host_peerings = json.loads(host_peerings)
for p in host_peerings:
p['hostname'] = hostname
peerings.setdefault(p['remote'], []).append(p)
update_callback(f'saving {len(peerings)} remote peers')
rp = r.pipeline()
for k, v in peerings.items():
rp.set(f'snmp-peerings:remote:{k}', json.dumps(v))
rp.execute()
def check_task_status(task_id, parent=None, forget=False): def check_task_status(task_id, parent=None, forget=False):
r = AsyncResult(task_id, app=app) r = AsyncResult(task_id, app=app)
assert r.id == task_id # sanity assert r.id == task_id # sanity
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment