| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- import csv,re,collections
- rows=[]
- with open('/tmp/fdb_hosts.tsv',encoding='utf-8') as f:
- for x in csv.DictReader(f,delimiter='\t'): rows.append(x)
- devnames={}
- for x in rows:
- devnames[x['switch_name']]=x['switch_ip']
- def is_device(name,ip):
- n=name.lower()
- if re.search(r'(core|dist|access|switch|ap-|-ap\b|surve|office-access)',n): return True
- if ip in devnames.values(): return True
- return False
- def peer_from_alias(alias):
- a=alias.strip()
- m=re.search(r'TO[-_ ]?(.+)$',a,re.I)
- return m.group(1).strip() if m else None
- def find_dev(tok):
- tok=tok.strip().strip('-')
- if not tok: return None
- tl=tok.lower()
- # exact-ish prefix match on device names
- best=None
- for nm in devnames:
- nl=nm.lower()
- if tl[:10] in nl or nl[:10] in tl:
- return nm
- # token like 3101-GRDA27-OFFICE
- key=tl.split('-')[0]
- if key and key in nl:
- if best is None: best=nm
- return best
- links=[];seenset=set()
- endpoints=[];dev_edges=[]
- for x in rows:
- alias=x['port_alias']; sw=x['switch_name']
- ptok=peer_from_alias(alias)
- if ptok:
- b=find_dev(ptok)
- if b:
- key=tuple(sorted([sw,b]))
- if key not in seenset and b!=sw:
- seenset.add(key); links.append((sw,x['port'],alias,b))
- continue
- if is_device(x['zabbix_name'],x['ip']):
- dev_edges.append(x)
- else:
- endpoints.append(x)
- print("rows=",len(rows))
- print("alias_links=",len(links))
- print("device_edges(no alias)=",len(dev_edges))
- print("endpoint_edges=",len(endpoints))
- print("\n-- 45 alias-based switch links --")
- for s,p,a,b in links[:45]:
- print(f" {s[:44]:44s}[{p:12s} {a[:22]:22s}] <-> {b[:44]}")
- # also show device_edges sample (switch mgmt seen on a port, no alias)
- print("\n-- 20 device_edges (no alias) --")
- for x in dev_edges[:20]:
- print(f" {x['zabbix_name'][:40]:40s} -> {x['switch_name'][:38]:38s} {x['port']:12s} macs={x['port_mac_count']}")
- with open('/tmp/fdb_links_alias.tsv','w',encoding='utf-8') as f:
- f.write('a_switch\ta_port\ta_alias\tb_switch\n')
- for r in links: f.write('\t'.join(r)+'\n')
- with open('/tmp/fdb_endpoints.tsv','w',encoding='utf-8') as f:
- f.write('ip\tname\tmac\tswitch_name\tswitch_ip\tport\tport_alias\tport_mac_count\n')
- for x in endpoints:
- f.write('\t'.join([x['ip'],x['zabbix_name'],x['mac'],x['switch_name'],x['switch_ip'],x['port'],x['port_alias'],x['port_mac_count']])+'\n')
- with open('/tmp/fdb_devedges.tsv','w',encoding='utf-8') as f:
- f.write('ip\tname\tmac\tswitch_name\tswitch_ip\tport\tport_alias\tport_mac_count\n')
- for x in dev_edges:
- f.write('\t'.join([x['ip'],x['zabbix_name'],x['mac'],x['switch_name'],x['switch_ip'],x['port'],x['port_alias'],x['port_mac_count']])+'\n')
|