diff --git a/test/data/check-snmp-agent-configs.py b/test/data/check-snmp-agent-configs.py index 77304c0636a22ce30db3eca9558ed41dea5e1a43..b080cb60ceda9b313d3847168be029778c1a8289 100644 --- a/test/data/check-snmp-agent-configs.py +++ b/test/data/check-snmp-agent-configs.py @@ -1,20 +1,30 @@ +import ipaddress + from inventory_provider.tasks.common import get_current_redis from inventory_provider import config from lxml import etree import socket +COMMUNITY_STRING = '0pBiFbD' COLLECTORS = [ - 'prod-noc-alarms01.geant.org', - 'prod-noc-alarms02.geant.org', - 'prod-noc-alarms03.geant.org', - 'uat-noc-alarms01.geant.org', - 'uat-noc-alarms02.geant.org', - 'uat-noc-alarms03.geant.org', - 'test-noc-alarms01.geant.org', - 'test-noc-alarms02.geant.org', - 'test-noc-alarms03.geant.org' + 'prod-noc-alarms01.geant.org', + 'prod-noc-alarms02.geant.org', + 'prod-noc-alarms03.geant.org', + 'uat-noc-alarms01.geant.org', + 'uat-noc-alarms02.geant.org', + 'uat-noc-alarms03.geant.org', + 'test-noc-alarms01.geant.org', + 'test-noc-alarms02.geant.org', + 'test-noc-alarms03.geant.org' ] +# COLLECTORS = [ +# 'prod-poller-admin.geant.org', +# 'uat-poller-admin.geant.org', +# 'test-poller-admin.geant.org', +# 'test-poller-sensu-agent01.geant.org', +# 'test-poller-sensu-agent02.geant.org' +# ] def v4_address(hostname): for a in socket.getaddrinfo(hostname, None): @@ -38,6 +48,15 @@ def agent_addresses(netconf_string): return doc.xpath('//configuration/snmp/trap-group/targets/name/text()') +def client_addresses(netconf_string, community): + doc = etree.fromstring(netconf_string) + subnets = doc.xpath( + f'//configuration/snmp/community[name/text()="{community}"]' + '/clients/name/text()') + for s in subnets: + yield ipaddress.ip_network(s, strict=False) + + columns = [''] + COLLECTORS print(','.join(columns)) columns = [''] + [ADDRESS[c] for c in COLLECTORS] @@ -48,6 +67,11 @@ def _c(b): return '+' if b else 'MISSING' +def _in_any_subnet(hostname, subnets): + address = ipaddress.ip_address(ADDRESS[hostname]) + return any([address in s for s in subnets]) + + r = get_current_redis(params) for k in r.scan_iter('netconf:*'): k = k.decode('utf-8') @@ -56,7 +80,13 @@ for k in r.scan_iter('netconf:*'): columns = [router] data = r.get(k).decode('utf-8') - agents = agent_addresses(data) + with open(f'{router}.xml', 'w') as f: + f.write(data) + agents = agent_addresses(data) columns += [_c(ADDRESS[c] in agents) for c in COLLECTORS] + + # acl_subnets = list(client_addresses(data, community=COMMUNITY_STRING)) + # columns += [_c(_in_any_subnet(c, acl_subnets)) for c in COLLECTORS] + print(','.join(columns))