Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 121 lines (101 sloc) 3.83 KB
#!/usr/bin/env python3
import argparse
from collections import namedtuple
import csv
from pathlib import Path
import re
from typing import List
SEARCH_DIR = Path("/gnoc/rancid/current/autopop-net.internet2.edu/configs")
CONFIGS_CORE = list(SEARCH_DIR.glob("core*")) \
+ list(SEARCH_DIR.glob("agg*")) \
+ list(SEARCH_DIR.glob("rr*"))
CONFIGS_WAVE = list(SEARCH_DIR.glob("wave-sw*"))
PortInfo = namedtuple("PortInfo", "device port serial description circuit")
NonePort = PortInfo("", "", "", "", "")
def main():
args = parse_args()
# Extract 8200 information
c8200_ports = extract_c8200_ports()
c8200_leftovers = set(c8200_ports)
# Extract wave-sw information
wave_ports = extract_wave_ports()
cisco_by_port = {trim_gdt_sn(p.serial):p for p in c8200_ports}
with open(args.outfile, "w") as outfile:
writer = csv.writer(outfile)
writer.writerow(list(PortInfo._fields) + list(PortInfo._fields))
for wave_port in wave_ports:
short_sn = trim_gdt_sn(wave_port.serial)
cisco_port = cisco_by_port.get(short_sn, NonePort)
c8200_leftovers.discard(cisco_port)
writer.writerow(tuple(wave_port) + tuple(cisco_port))
for cisco_port in c8200_leftovers:
writer.writerow(NonePort + tuple(cisco_port))
# for c8200_port in c8200_ports:
# print(c8200_port)
return
GDT_SN = re.compile("^(GDT.*)[AB]$")
def trim_gdt_sn(sn: str) -> str:
m = GDT_SN.match(sn)
if m:
return m.group(1)
return sn
RE_CID = re.compile('.*(I2-[A-Z0-9a-z-]+-\d+).*')
def extract_c8200_ports(search_files:List[Path]=CONFIGS_CORE) -> List[PortInfo]:
RE_HOSTNAME = re.compile(r'^hostname (.*)$', re.MULTILINE)
RE_SN = re.compile(r'^NAME: "([^"]+)", DESCR: "[^"]+"\n+PID:.*SN: (.+)$', re.MULTILINE)
RE_DESCR = re.compile(r'^interface (.*)\n\s+description (.*)$', re.MULTILINE)
rv = []
for f in search_files:
all_text = f.read_text()
hostname = RE_HOSTNAME.findall(all_text)
if len(hostname) == 0:
continue
hostname = hostname[0]
descrs = {intf: descr for (intf,descr) in RE_DESCR.findall(all_text)}
serials = [(name, sn) for (name, sn) in RE_SN.findall(all_text) if "GigE" in name]
for (intf, sn) in serials:
d = descrs.get(intf, "")
m_cid = RE_CID.match(d)
cid = m_cid[1] if m_cid else ""
rv.append(PortInfo(hostname, intf, sn, d, cid))
return rv
def extract_wave_ports(search_files:List[Path]=CONFIGS_WAVE) -> List[PortInfo]:
rv = []
RE_HOSTNAME = re.compile(r'^system set host-name (.*)$', re.MULTILINE)
RE_SN = re.compile(
r'^[-+]+ XCVR VENDOR DATA - Xcvr ([0-9/]+)\s+[-+]+\n'
r'(?:\|.*\|\n|[-+]+\n)*'
r'\| Vendor SN\s+ \| ([^ ]+)\s+ \|.*$',
re.MULTILINE
)
RE_DESCR = re.compile(r'^port set port ([^ ]+) label "(.*)"\s*$', re.MULTILINE)
rv = []
for f in search_files:
all_text = f.read_text()
hostname = RE_HOSTNAME.findall(all_text)
if len(hostname) == 0:
continue
hostname = hostname[0]
descrs = {intf: descr for (intf,descr) in RE_DESCR.findall(all_text)}
serials = RE_SN.findall(all_text)
print(f)
print("Descriptions:", len(descrs))
print("Serials:", len(serials))
for (intf, sn) in serials:
d = descrs.get(intf, "")
m_cid = RE_CID.match(d)
cid = m_cid[1] if m_cid else ""
rv.append(PortInfo(hostname, intf, sn, d, cid))
return rv
def parse_args():
parser = argparse.ArgumentParser(
description=""
)
parser.add_argument(
'-o', '--outfile',
dest='outfile', default="correlate-bb-cids.csv",
help='Report output file'
)
return parser.parse_args()
if __name__ == "__main__":
main()