Add bgp-summ.py
This commit is contained in:
commit
9857129b6f
1 changed files with 241 additions and 0 deletions
241
bgp-summ.py
Normal file
241
bgp-summ.py
Normal file
|
|
@ -0,0 +1,241 @@
|
||||||
|
import json
|
||||||
|
import subprocess
|
||||||
|
from jinja2 import Template
|
||||||
|
from flask import Flask, render_template_string, jsonify
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
def run_curl_command():
|
||||||
|
curl_command = [
|
||||||
|
"curl", "-k", "--location", "--request", "POST", "https://ip:port/show",
|
||||||
|
"--form", "data={\"op\": \"show\", \"path\": [\"bgp\", \"summ\"]}",
|
||||||
|
"--form", "key=key"
|
||||||
|
]
|
||||||
|
response = subprocess.check_output(curl_command, text=True)
|
||||||
|
return json.loads(response)
|
||||||
|
|
||||||
|
def parse_bgp_data(data):
|
||||||
|
ipv4_section = ""
|
||||||
|
ipv6_section = ""
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
if "IPv6 Unicast Summary" in raw_data:
|
||||||
|
ipv6_section = raw_data.split("IPv6 Unicast Summary (VRF default):")[1].strip()
|
||||||
|
|
||||||
|
def process_peers(peer_data):
|
||||||
|
peers = []
|
||||||
|
for line in peer_data.split("\n"):
|
||||||
|
if line.strip():
|
||||||
|
peer_info = line.split()
|
||||||
|
if len(peer_info) >= 12:
|
||||||
|
peers.append({
|
||||||
|
"neighbor": peer_info[0],
|
||||||
|
"version": peer_info[1],
|
||||||
|
"as_number": peer_info[2],
|
||||||
|
"msg_received": peer_info[3],
|
||||||
|
"msg_sent": peer_info[4],
|
||||||
|
"table_version": peer_info[5],
|
||||||
|
"in_queue": peer_info[6],
|
||||||
|
"out_queue": peer_info[7],
|
||||||
|
"up_down": peer_info[8],
|
||||||
|
"state_pfx_rcd": peer_info[9],
|
||||||
|
"prefix_sent": peer_info[10],
|
||||||
|
"description": " ".join(peer_info[11:])
|
||||||
|
})
|
||||||
|
return peers
|
||||||
|
|
||||||
|
ipv4_peers = process_peers(ipv4_section)
|
||||||
|
ipv6_peers = process_peers(ipv6_section)
|
||||||
|
|
||||||
|
return ipv4_peers, ipv6_peers
|
||||||
|
|
||||||
|
def generate_html_table(ipv4_peers, ipv6_peers):
|
||||||
|
html_template = """
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>BGP Peer Summary</title>
|
||||||
|
<!-- Materialize CSS -->
|
||||||
|
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet">
|
||||||
|
<style>
|
||||||
|
.container {
|
||||||
|
margin-top: 30px;
|
||||||
|
}
|
||||||
|
table {
|
||||||
|
border-collapse: collapse;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
th, td {
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
padding: 8px;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
th {
|
||||||
|
background-color: #f4f4f4;
|
||||||
|
}
|
||||||
|
footer {
|
||||||
|
height: 50px;
|
||||||
|
background-color: #f4f4f4;
|
||||||
|
margin-top: 30px;
|
||||||
|
}
|
||||||
|
input[type="text"] {
|
||||||
|
margin-bottom: 20px;
|
||||||
|
padding: 10px;
|
||||||
|
width: 100%;
|
||||||
|
max-width: 400px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<h1 class="center-align">Core1.Doet.pixelHosting.nl BGP summary</h1>
|
||||||
|
|
||||||
|
<h4>IPv4 Unicast Summary</h4>
|
||||||
|
<input type="text" id="ipv4Search" placeholder="Search in IPv4 table..." onkeyup="filterTable('ipv4Search', 'ipv4Table')">
|
||||||
|
<table class="striped" id="ipv4Table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Neighbor</th>
|
||||||
|
<th>Version (V)</th>
|
||||||
|
<th>AS Number</th>
|
||||||
|
<th>Messages Received</th>
|
||||||
|
<th>Messages Sent</th>
|
||||||
|
<th>Table Version</th>
|
||||||
|
<th>Inbound Queue (InQ)</th>
|
||||||
|
<th>Outbound Queue (OutQ)</th>
|
||||||
|
<th>Up/Down</th>
|
||||||
|
<th>State/PfxRcd</th>
|
||||||
|
<th>Prefix Sent (PfxSnt)</th>
|
||||||
|
<th>Description</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for peer in ipv4_peers %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ peer.neighbor }}</td>
|
||||||
|
<td>{{ peer.version }}</td>
|
||||||
|
<td>{{ peer.as_number }}</td>
|
||||||
|
<td>{{ peer.msg_received }}</td>
|
||||||
|
<td>{{ peer.msg_sent }}</td>
|
||||||
|
<td>{{ peer.table_version }}</td>
|
||||||
|
<td>{{ peer.in_queue }}</td>
|
||||||
|
<td>{{ peer.out_queue }}</td>
|
||||||
|
<td>{{ peer.up_down }}</td>
|
||||||
|
<td>{{ peer.state_pfx_rcd }}</td>
|
||||||
|
<td>{{ peer.prefix_sent }}</td>
|
||||||
|
<td>{{ peer.description }}</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<h4>IPv6 Unicast Summary</h4>
|
||||||
|
<input type="text" id="ipv6Search" placeholder="Search in IPv6 table..." onkeyup="filterTable('ipv6Search', 'ipv6Table')">
|
||||||
|
<table class="striped" id="ipv6Table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Neighbor</th>
|
||||||
|
<th>Version (V)</th>
|
||||||
|
<th>AS Number</th>
|
||||||
|
<th>Messages Received</th>
|
||||||
|
<th>Messages Sent</th>
|
||||||
|
<th>Table Version</th>
|
||||||
|
<th>Inbound Queue (InQ)</th>
|
||||||
|
<th>Outbound Queue (OutQ)</th>
|
||||||
|
<th>Up/Down</th>
|
||||||
|
<th>State/PfxRcd</th>
|
||||||
|
<th>Prefix Sent (PfxSnt)</th>
|
||||||
|
<th>Description</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for peer in ipv6_peers %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ peer.neighbor }}</td>
|
||||||
|
<td>{{ peer.version }}</td>
|
||||||
|
<td>{{ peer.as_number }}</td>
|
||||||
|
<td>{{ peer.msg_received }}</td>
|
||||||
|
<td>{{ peer.msg_sent }}</td>
|
||||||
|
<td>{{ peer.table_version }}</td>
|
||||||
|
<td>{{ peer.in_queue }}</td>
|
||||||
|
<td>{{ peer.out_queue }}</td>
|
||||||
|
<td>{{ peer.up_down }}</td>
|
||||||
|
<td>{{ peer.state_pfx_rcd }}</td>
|
||||||
|
<td>{{ peer.prefix_sent }}</td>
|
||||||
|
<td>{{ peer.description }}</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Footer -->
|
||||||
|
<footer></footer>
|
||||||
|
|
||||||
|
<!-- Materialize JS -->
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
|
||||||
|
<script>
|
||||||
|
function filterTable(searchInputId, tableId) {
|
||||||
|
var input, filter, table, tr, td, i, j, txtValue;
|
||||||
|
input = document.getElementById(searchInputId);
|
||||||
|
filter = input.value.toUpperCase();
|
||||||
|
table = document.getElementById(tableId);
|
||||||
|
tr = table.getElementsByTagName("tr");
|
||||||
|
|
||||||
|
for (i = 1; i < tr.length; i++) {
|
||||||
|
tr[i].style.display = "none";
|
||||||
|
td = tr[i].getElementsByTagName("td");
|
||||||
|
for (j = 0; j < td.length; j++) {
|
||||||
|
if (td[j]) {
|
||||||
|
txtValue = td[j].textContent || td[j].innerText;
|
||||||
|
if (txtValue.toUpperCase().indexOf(filter) > -1) {
|
||||||
|
tr[i].style.display = "";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
"""
|
||||||
|
|
||||||
|
template = Template(html_template)
|
||||||
|
html_output = template.render(ipv4_peers=ipv4_peers, 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)
|
||||||
|
|
||||||
|
html_output = generate_html_table(ipv4_peers, ipv6_peers)
|
||||||
|
|
||||||
|
return render_template_string(html_output)
|
||||||
|
|
||||||
|
@app.route('/json')
|
||||||
|
def bgp_peer_summary_json():
|
||||||
|
data = run_curl_command()
|
||||||
|
|
||||||
|
ipv4_peers, ipv6_peers = parse_bgp_data(data)
|
||||||
|
|
||||||
|
json_data = {
|
||||||
|
"ipv4_peers": ipv4_peers,
|
||||||
|
"ipv6_peers": ipv6_peers
|
||||||
|
}
|
||||||
|
|
||||||
|
return jsonify(json_data)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(host='0.0.0.0', port=5000, debug=True)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue