diff --git a/bgp-summ.py b/bgp-summ.py index 768cb54..d212817 100644 --- a/bgp-summ.py +++ b/bgp-summ.py @@ -17,19 +17,27 @@ def run_curl_command(): def parse_bgp_data(data): ipv4_section = "" ipv6_section = "" + ipv4_info = {} + ipv6_info = {} if "data" in data: raw_data = data["data"] if "IPv4 Unicast Summary" in raw_data: - ipv4_section = raw_data.split("IPv4 Unicast Summary (VRF default):")[1].split("IPv6 Unicast Summary (VRF default):")[0].strip() + ipv4_raw = raw_data.split("IPv4 Unicast Summary (VRF default):")[1] + ipv4_section = ipv4_raw.split("IPv6 Unicast Summary (VRF default):")[0].strip() + ipv4_info = extract_bgp_info(ipv4_raw) if "IPv6 Unicast Summary" in raw_data: - ipv6_section = raw_data.split("IPv6 Unicast Summary (VRF default):")[1].strip() + ipv6_raw = raw_data.split("IPv6 Unicast Summary (VRF default):")[1].strip() + ipv6_section = ipv6_raw + ipv6_info = extract_bgp_info(ipv6_raw) def process_peers(peer_data): peers = [] for line in peer_data.split("\n"): + if line.strip().startswith("Neighbor"): + continue if line.strip(): peer_info = line.split() if len(peer_info) >= 12: @@ -52,9 +60,31 @@ def parse_bgp_data(data): ipv4_peers = process_peers(ipv4_section) ipv6_peers = process_peers(ipv6_section) - return ipv4_peers, ipv6_peers + return ipv4_info, ipv4_peers, ipv6_info, ipv6_peers -def generate_html_table(ipv4_peers, ipv6_peers): +def extract_bgp_info(raw_data): + lines = raw_data.split("\n") + info = {} + for line in lines: + if "BGP router identifier" in line: + parts = line.split(",") + info["router_id"] = parts[0].split("identifier")[1].strip() + info["local_as"] = parts[1].split("number")[1].strip().split(" ")[0] + if "vrf-id" in parts[-1]: + info["vrf_id"] = parts[-1].split("vrf-id")[1].strip() + if "BGP table version" in line: + info["table_version"] = line.split("version")[1].strip() + if "RIB entries" in line: + parts = line.split(",") + info["rib_entries"] = parts[0].split("entries")[1].strip() + info["rib_memory"] = parts[1].split("using")[1].strip() + if "Peers" in line: + parts = line.split(",") + info["peers"] = parts[0].split("Peers")[1].strip() + info["peers_memory"] = parts[1].split("using")[1].strip() + return info + +def generate_html_table(ipv4_info, ipv4_peers, ipv6_info, ipv6_peers): html_template = """ @@ -98,6 +128,10 @@ def generate_html_table(ipv4_peers, ipv6_peers):

Core1.Doet.pixelHosting.nl BGP summary

IPv4 Unicast Summary

+

BGP Router ID: {{ ipv4_info.router_id }}, Local AS Number: {{ ipv4_info.local_as }} VRF ID: {{ ipv4_info.vrf_id }}

+

BGP Table Version: {{ ipv4_info.table_version }}

+

RIB Entries: {{ ipv4_info.rib_entries }}, using {{ ipv4_info.rib_memory }}

+

Peers: {{ ipv4_info.peers }}, using {{ ipv4_info.peers_memory }}

@@ -137,6 +171,10 @@ def generate_html_table(ipv4_peers, ipv6_peers):

IPv6 Unicast Summary

+

BGP Router ID: {{ ipv6_info.router_id }}, Local AS Number: {{ ipv6_info.local_as }} VRF ID: {{ ipv6_info.vrf_id }}

+

BGP Table Version: {{ ipv6_info.table_version }}

+

RIB Entries: {{ ipv6_info.rib_entries }}, using {{ ipv6_info.rib_memory }}

+

Peers: {{ ipv6_info.peers }}, using {{ ipv6_info.peers_memory }}

@@ -210,17 +248,16 @@ def generate_html_table(ipv4_peers, ipv6_peers): """ template = Template(html_template) - html_output = template.render(ipv4_peers=ipv4_peers, ipv6_peers=ipv6_peers) - + html_output = template.render(ipv4_info=ipv4_info, ipv4_peers=ipv4_peers, ipv6_info=ipv6_info, ipv6_peers=ipv6_peers) return html_output @app.route('/') def bgp_peer_summary(): data = run_curl_command() - ipv4_peers, ipv6_peers = parse_bgp_data(data) + ipv4_info, ipv4_peers, ipv6_info, ipv6_peers = parse_bgp_data(data) - html_output = generate_html_table(ipv4_peers, ipv6_peers) + html_output = generate_html_table(ipv4_info, ipv4_peers, ipv6_info, ipv6_peers) return render_template_string(html_output) @@ -228,10 +265,12 @@ def bgp_peer_summary(): def bgp_peer_summary_json(): data = run_curl_command() - ipv4_peers, ipv6_peers = parse_bgp_data(data) + ipv4_info, ipv4_peers, ipv6_info, ipv6_peers = parse_bgp_data(data) json_data = { + "ipv4_info": ipv4_info, "ipv4_peers": ipv4_peers, + "ipv6_info": ipv6_info, "ipv6_peers": ipv6_peers }