ansible/roles/ipv6_edge_router/templates/quagga.py.j2
shirenn ecfd7037c2
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing
[quagga] Patch the previous script (squash)
2021-02-04 17:32:58 +01:00

56 lines
2.4 KiB
Django/Jinja
Executable file

#!/usr/bin/python3
import re
import subprocess
def parse_bgp_summary(summary):
header = 'Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd\n'
res = {'routes': 0, 'uptime': 0}
try:
index = summary.index(header) + len(header)
except ValueError:
return res
summary = summary[index:]
match = re.match(r'(?P<neighbor>[^\s\n])+\n?\s+(?P<version>4)\s+(?P<as>\d{1,5})\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+(?P<uptime>[^\s]+)\s+(?P<routes>\d+)\n', summary)
if match is None:
return res
res['routes'] = int(match.group('routes'))
uptime = match.group('uptime')
if re.fullmatch(r'[0-9]+:[0-9]+:[0-9]+', uptime) is not None:
uptime = uptime.split(':')
uptime = int(uptime[0])*3600 + int(uptime[1])*60 + int(uptime[2])
else:
uptime_match = re.fullmatch(r'([0-9]+)d([0-9]+)h([0-9]+)m', uptime)
if uptime_match is not None:
uptime = int(uptime_match.group(1))*86400 + int(uptime_match.group(2))*3600 + int(uptime_match.group(3))*60
else:
uptime_match = re.fullmatch(r'([0-9]+)w([0-9]+)d([0-9]+)h', uptime)
if uptime_match is not None:
uptime = int(uptime_match.group(1))*604800 + int(uptime_match.group(2))*86400 + int(uptime_match.group(3))*3600
else:
uptime_match = re.fullmatch(r'([0-9]+)y([0-9]+)w([0-9]+)d', uptime)
if uptime_match is not None:
uptime = int(uptime_match.group(1))*220752000 + int(uptime_match.group(2))*604800 + int(uptime_match.group(3))*86400
else:
uptime = 0
res['uptime'] = uptime
return res
def prompt(version='ipv4'):
bgp_unicast_summary = subprocess.run(['/usr/bin/vtysh', '-c', f'show bgp {version} unicast summary'], capture_output=True).stdout.decode('utf-8')
bgp_routes_uptime = parse_bgp_summary(bgp_unicast_summary)
routes = bgp_routes_uptime['routes']
uptime = bgp_routes_uptime['uptime']
print(f'# HELP quagga_bgp_{version}_routes Number of {version} routes added by quagga')
print(f'# TYPE quagga_bgp_{version}_routes gauge')
print(f'quagga_bgp_{version}_routes {routes}')
print(f'# HELP quagga_bgp_{version}_uptime {version} BGP connection uptime')
print(f'# TYPE quagga_bgp_{version}_uptime gauge')
print(f'quagga_bgp_{version}_uptime {uptime}')
if __name__ == '__main__':
prompt('ipv4')
prompt('ipv6')