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

pep8

parent 1d7e641b
No related branches found
No related tags found
No related merge requests found
...@@ -31,6 +31,7 @@ def cursor(cnx): ...@@ -31,6 +31,7 @@ def cursor(cnx):
if csr: if csr:
csr.close() csr.close()
def _db_test(db, router): def _db_test(db, router):
database_logger = logging.getLogger(DATABASE_LOGGER_NAME) database_logger = logging.getLogger(DATABASE_LOGGER_NAME)
with cursor(db) as crs: with cursor(db) as crs:
......
...@@ -55,7 +55,10 @@ def _load_routers(config_file): ...@@ -55,7 +55,10 @@ def _load_routers(config_file):
:return: :return:
""" """
for line in config_file: for line in config_file:
m = re.match(r'^([a-z\d]+\.[a-z\d]{3,4}\.[a-z\d]{2}\.(geant|eumedconnect)\d*\.net)\s*=([^,]+)\s*,(.*)\s*$', line) m = re.match(
r'^([a-z\d]+\.[a-z\d]{3,4}\.[a-z\d]{2}'
r'\.(geant|eumedconnect)\d*\.net)\s*=([^,]+)\s*,(.*)\s*$',
line)
if not m: if not m:
logging.warning("malformed config file line: '%s'" % line.strip()) logging.warning("malformed config file line: '%s'" % line.strip())
continue continue
...@@ -66,7 +69,6 @@ def _load_routers(config_file): ...@@ -66,7 +69,6 @@ def _load_routers(config_file):
} }
def load(f): def load(f):
""" """
loads, validates and returns configuration parameters loads, validates and returns configuration parameters
...@@ -81,4 +83,3 @@ def load(f): ...@@ -81,4 +83,3 @@ def load(f):
with open(config["routers_community.conf"]) as f: with open(config["routers_community.conf"]) as f:
config["routers"] = list(_load_routers(f)) config["routers"] = list(_load_routers(f))
return config return config
...@@ -22,7 +22,9 @@ def neighbors( ...@@ -22,7 +22,9 @@ def neighbors(
for bgp in prot.get("bgp", []): for bgp in prot.get("bgp", []):
for g in bgp.get("group", []): for g in bgp.get("group", []):
if group_expression and not \ if group_expression and not \
re.match(group_expression, g["name"]["data"]): re.match(
group_expression,
g["name"]["data"]):
continue continue
for n in g["neighbor"]: for n in g["neighbor"]:
yield n yield n
...@@ -44,9 +46,14 @@ def interfaces(parsed_json_output): ...@@ -44,9 +46,14 @@ def interfaces(parsed_json_output):
@contextlib.contextmanager @contextlib.contextmanager
def ssh_connection(hostname, ssh_params): def ssh_connection(hostname, ssh_params):
# TODO: remove this relative path logic!!
import os import os
key_filename = os.path.join(os.path.dirname(__file__), ssh_params["private-key"]) key_filename = os.path.join(
known_hosts = os.path.join(os.path.dirname(__file__), ssh_params["known-hosts"]) os.path.dirname(__file__),
ssh_params["private-key"])
known_hosts = os.path.join(
os.path.dirname(__file__),
ssh_params["known-hosts"])
k = paramiko.DSSKey.from_private_key_file(key_filename) k = paramiko.DSSKey.from_private_key_file(key_filename)
with paramiko.SSHClient() as ssh: with paramiko.SSHClient() as ssh:
...@@ -109,13 +116,15 @@ def shell_commands(): ...@@ -109,13 +116,15 @@ def shell_commands():
return {} return {}
yield { yield {
"command": 'show configuration routing-instances IAS protocols bgp | display json', "command": ('show configuration routing-instances'
' IAS protocols bgp | display json'),
"key": "bgp", "key": "bgp",
"parser": _parse_bgp_output "parser": _parse_bgp_output
} }
yield { yield {
"command": 'show configuration logical-systems VRR protocols bgp | display json', "command": ('show configuration logical-systems '
'VRR protocols bgp | display json'),
"key": "vrr", "key": "vrr",
"parser": lambda txt: _loads(txt) if txt else {} "parser": lambda txt: _loads(txt) if txt else {}
} }
......
...@@ -52,10 +52,6 @@ def get_router_details(router, params, q): ...@@ -52,10 +52,6 @@ def get_router_details(router, params, q):
command_output = commands_proc_queue.get() command_output = commands_proc_queue.get()
assert len(command_output) == len(commands) assert len(command_output) == len(commands)
# for c, o in zip(commands, command_output):
# with open("/tmp/%s-%s.output" % (router["hostname"], c["key"]), "w") as f:
# f.write(o)
result = {} result = {}
for c, o in zip(commands, command_output): for c, o in zip(commands, command_output):
if c["key"]: if c["key"]:
...@@ -87,10 +83,12 @@ def load_network_details(params): ...@@ -87,10 +83,12 @@ def load_network_details(params):
result = {} result = {}
for p in processes: for p in processes:
threading_logger.debug("waiting for get_router_details result: %r" % p["router"]) threading_logger.debug(
"waiting for get_router_details result: %r" % p["router"])
result[p["router"]["hostname"]] = p["queue"].get() result[p["router"]["hostname"]] = p["queue"].get()
p["process"].join() p["process"].join()
threading_logger.debug("got result and joined get_router_details proc: %r" % p["router"]) threading_logger.debug(
"got result and joined get_router_details proc: %r" % p["router"])
return result return result
...@@ -102,7 +100,7 @@ def _validate_config(ctx, param, value): ...@@ -102,7 +100,7 @@ def _validate_config(ctx, param, value):
@click.command() @click.command()
@click.option( @click.option(
"--params", "--params",
# required=True, # required=True,
type=click.File(), type=click.File(),
help="Configuration filename", help="Configuration filename",
default=open("config.json"), default=open("config.json"),
...@@ -122,4 +120,3 @@ if __name__ == "__main__": ...@@ -122,4 +120,3 @@ if __name__ == "__main__":
logging.getLogger(constants.JUNIPER_LOGGER_NAME).setLevel(logging.DEBUG) logging.getLogger(constants.JUNIPER_LOGGER_NAME).setLevel(logging.DEBUG)
logging.getLogger(constants.DATABASE_LOGGER_NAME).setLevel(logging.DEBUG) logging.getLogger(constants.DATABASE_LOGGER_NAME).setLevel(logging.DEBUG)
cli() cli()
...@@ -3,7 +3,8 @@ import re ...@@ -3,7 +3,8 @@ import re
from pysnmp.hlapi import nextCmd, SnmpEngine, CommunityData, \ from pysnmp.hlapi import nextCmd, SnmpEngine, CommunityData, \
UdpTransportTarget, ContextData, ObjectType, ObjectIdentity UdpTransportTarget, ContextData, ObjectType, ObjectIdentity
from pysnmp.smi import builder, view, compiler, rfc1902 from pysnmp.smi import builder, compiler
# from pysnmp.smi import view, rfc1902
from inventory_provider.constants import SNMP_LOGGER_NAME from inventory_provider.constants import SNMP_LOGGER_NAME
...@@ -32,10 +33,15 @@ def walk(agent_hostname, community, base_oid): ...@@ -32,10 +33,15 @@ def walk(agent_hostname, community, base_oid):
snmp_logger = logging.getLogger(SNMP_LOGGER_NAME) snmp_logger = logging.getLogger(SNMP_LOGGER_NAME)
mibBuilder = builder.MibBuilder() mibBuilder = builder.MibBuilder()
mibViewController = view.MibViewController(mibBuilder) # mibViewController = view.MibViewController(mibBuilder)
compiler.addMibCompiler(mibBuilder, sources=['http://mibs.snmplabs.com/asn1/@mib@']) compiler.addMibCompiler(
mibBuilder,
sources=['http://mibs.snmplabs.com/asn1/@mib@'])
# Pre-load MIB modules we expect to work with # Pre-load MIB modules we expect to work with
mibBuilder.loadModules('SNMPv2-MIB', 'SNMP-COMMUNITY-MIB', 'RFC1213-MIB') mibBuilder.loadModules(
'SNMPv2-MIB',
'SNMP-COMMUNITY-MIB',
'RFC1213-MIB')
snmp_logger.debug("walking %s: %s" % (agent_hostname, base_oid)) snmp_logger.debug("walking %s: %s" % (agent_hostname, base_oid))
...@@ -76,7 +82,6 @@ def get_router_interfaces(router, config): ...@@ -76,7 +82,6 @@ def get_router_interfaces(router, config):
assert m, "sanity failure parsing oid: " + v4IfcName["oid"] assert m, "sanity failure parsing oid: " + v4IfcName["oid"]
v4IfcNames[m.group(1)] = v4IfcName["value"] v4IfcNames[m.group(1)] = v4IfcName["value"]
interfaces = []
for v4Address, v4Mask, v4InterfaceOID in zip( for v4Address, v4Mask, v4InterfaceOID in zip(
details["v4Address"], details["v4Address"],
details["v4Mask"], details["v4Mask"],
......
...@@ -28,12 +28,18 @@ for c in juniper.shell_commands(): ...@@ -28,12 +28,18 @@ for c in juniper.shell_commands():
SHELL_COMMANDS.append({ SHELL_COMMANDS.append({
"key": "bgpv4", "key": "bgpv4",
"command": r'show configuration routing-instances IAS protocols bgp | display set | match neighbor | match description | match "GEANT-IX | GEANT-IX-"' "command": (r'show configuration routing-instances IAS '
r'protocols bgp | display set | match neighbor'
r' | match description'
r' | match "GEANT-IX | GEANT-IX-"')
}) })
SHELL_COMMANDS.append({ SHELL_COMMANDS.append({
"key": "bgpv6", "key": "bgpv6",
"command": r'show configuration routing-instances IAS protocols bgp | display set | match neighbor | match description | match "GEANT-IXv6 | GEANT-IXv6-"' "command": (r'show configuration routing-instances IAS'
r' protocols bgp | display set | match neighbor'
r' | match description'
r' | match "GEANT-IXv6 | GEANT-IXv6-"')
}) })
...@@ -109,4 +115,4 @@ def main(): ...@@ -109,4 +115,4 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
main() main()
\ No newline at end of file
...@@ -77,7 +77,6 @@ CACHE_SCHEMA = { ...@@ -77,7 +77,6 @@ CACHE_SCHEMA = {
} }
@pytest.fixture @pytest.fixture
def cached_test_data(): def cached_test_data():
with open("router-info.json") as f: with open("router-info.json") as f:
...@@ -98,7 +97,7 @@ def _parsed_old_style_output_data(s): ...@@ -98,7 +97,7 @@ def _parsed_old_style_output_data(s):
r'neighbor\s+([a-f\d\.:]+)\s+' r'neighbor\s+([a-f\d\.:]+)\s+'
r'description\s+"?([^"]+)"?\s*$'), l) r'description\s+"?([^"]+)"?\s*$'), l)
assert m assert m
yield { yield {
"routing-instances": m.group(1), "routing-instances": m.group(1),
"protocols": m.group(2), "protocols": m.group(2),
...@@ -130,7 +129,7 @@ def test_ipv4_neighbors(router_output): ...@@ -130,7 +129,7 @@ def test_ipv4_neighbors(router_output):
parsers = dict([(c["key"], c["parser"]) for c in juniper.shell_commands()]) parsers = dict([(c["key"], c["parser"]) for c in juniper.shell_commands()])
neighbors = parsers["bgp"]( neighbors = parsers["bgp"](
router_output["bgp"], router_output["bgp"],
group_expression = r'^GEANT-IX[\s-].*$') group_expression=r'^GEANT-IX[\s-].*$')
assert len(neighbors) == len(old_v4_data) assert len(neighbors) == len(old_v4_data)
for n in neighbors: for n in neighbors:
...@@ -149,7 +148,7 @@ def test_ipv6_neighbors(router_output): ...@@ -149,7 +148,7 @@ def test_ipv6_neighbors(router_output):
parsers = dict([(c["key"], c["parser"]) for c in juniper.shell_commands()]) parsers = dict([(c["key"], c["parser"]) for c in juniper.shell_commands()])
neighbors = parsers["bgp"]( neighbors = parsers["bgp"](
router_output["bgp"], router_output["bgp"],
group_expression = r'^GEANT-IXv6[\s-].*$') group_expression=r'^GEANT-IXv6[\s-].*$')
assert len(neighbors) == len(old_v6_data) assert len(neighbors) == len(old_v6_data)
for n in neighbors: for n in neighbors:
...@@ -171,4 +170,3 @@ def test_juniper_shell_output_parsing(router_output): ...@@ -171,4 +170,3 @@ def test_juniper_shell_output_parsing(router_output):
if c["key"] is None: if c["key"] is None:
continue continue
c["parser"](router_output[c["key"]]) c["parser"](router_output[c["key"]])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment