rework bgpmap parsing so it is bird1 and bird2 compatible

This commit is contained in:
Simon Marsh 2019-05-15 12:29:20 +01:00
parent 0de6a8cfaa
commit 1967bebab7
Signed by: burble
GPG Key ID: 7B9FE8780CFB6593

65
lg.py
View File

@ -600,20 +600,47 @@ def build_as_tree_from_raw_bird_ouput(host, proto, text):
for line in text: for line in text:
line = line.strip() line = line.strip()
expr = re.search(r'(.*)unicast\s+\[(\w+)\s+', line) # check for bird2 style line containing protocol name
if expr: # matches: ... unicast_[(protocol)_ ...
if expr.group(1).strip(): b2_unicast_line = re.search(r'(.*)unicast\s+\[(\w+)\s+', line)
net_dest = expr.group(1).strip() if b2_unicast_line:
peer_protocol_name = expr.group(2).strip() # save the net_dest and protocol name for later
if b2_unicast_line.group(1).strip():
net_dest = b2_unicast_line.group(1).strip()
peer_protocol_name = b2_unicast_line.group(2).strip()
expr2 = re.search(r'via\s+([0-9a-fA-F:\.]+)', line) peer_match = False
if expr2:
# check line for bird1 style line
# matches: ___via_(next hop ip addr)_on_(iface)_[(protocol)_ ...
b1_peer_line = re.search(r'(.*)via\s+([0-9a-fA-F:\.]+)\s+on.*\[(\w+)\s+', line)
if b1_peer_line:
# save the net_dest, peer and protocol name for later
if b1_peer_line.group(1).strip():
net_dest = b1_peer_line.group(1).strip()
peer_ip = b1_peer_line.group(2).strip()
peer_protocol_name = b1_peer_line.group(3).strip()
# flag that a match was found
peer_match = True
else:
# if this wasn't a bird1 style peer line, then check for a bird2
# style line instead. Doing the check in the else clause prevents
# falsely matching bird1 lines
# matches: _via_(next hop address)
b2_peer_line = re.search(r'via\s+([0-9a-fA-F:\.]+)', line)
if b2_peer_line:
peer_ip = b2_peer_line.group(1).strip()
# flag that a match was found
peer_match = True
if peer_match:
# common code for when either a bird1 or bird2 peer line was found
if path: if path:
path.append(net_dest) path.append(net_dest)
paths.append(path) paths.append(path)
path = None path = None
peer_ip = expr2.group(1).strip()
# Check if via line is a internal route # Check if via line is a internal route
for rt_host, rt_ips in app.config["ROUTER_IP"].items(): for rt_host, rt_ips in app.config["ROUTER_IP"].items():
# Special case for internal routing # Special case for internal routing
@ -621,24 +648,24 @@ def build_as_tree_from_raw_bird_ouput(host, proto, text):
paths.append([peer_protocol_name, rt_host]) paths.append([peer_protocol_name, rt_host])
path = None path = None
break break
else: else:
# ugly hack for good printing path = [ peer_protocol_name ]
path = [ peer_protocol_name ]
# path = ["%s\r%s" % (peer_protocol_name, get_as_name(get_as_number_from_protocol_name(host, proto, peer_protocol_name)))]
expr3 = re.search(r'(.*)unreachable\s+\[(\w+)\s+', line) # check for unreachable routes (common for bird1 & 2)
if expr3: # matches: ...unreachable_[(protocol)_
unreachable_line = re.search(r'(.*)unreachable\s+\[(\w+)\s+', line)
if unreachable_line:
if path: if path:
path.append(net_dest) path.append(net_dest)
paths.append(path) paths.append(path)
path = None path = None
if expr3.group(1).strip(): if unreachable_line.group(1).strip():
net_dest = expr3.group(1).strip() net_dest = unreachable_line.group(1).strip()
expr4 = re.search(r'^dev', line) # check for on-link routes
#handle on-link routes onlink_line = re.search(r'^dev', line)
if expr4: if onlink_line:
paths.append([peer_protocol_name, net_dest]) paths.append([peer_protocol_name, net_dest])
path = None path = None