Skip to content

Commit e1694a2

Browse files
authored
[READY] Add tags for wasgeht and switch filtering (#1163)
2 parents 6ad3a4d + 575706d commit e1694a2

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

facts/inventory.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ def populateswitches(switchesfile):
324324
"num": elems[1],
325325
"ipv6": elems[3],
326326
"ipv6ptr": ip6toptr(elems[3]),
327+
"switchtype": elems[4],
328+
"hierarchy": elems[5],
329+
"model": elems[7],
327330
"fqdn": name.lower() + ".scale.lan",
328331
"aliases": roomalias(name),
329332
}
@@ -381,6 +384,9 @@ def populateaps(aps_file, apuse_file):
381384
"configver": str(row["config_ver"]),
382385
"fqdn": name + ".scale.lan",
383386
"aliases": [row["serial"].lower()],
387+
"map_id": str(row["map_id"]),
388+
"map_x": str(row["map_x"]),
389+
"map_y": str(row["map_y"]),
384390
}
385391
)
386392
return aps
@@ -467,11 +473,10 @@ def populateservers(serversfile, vlans):
467473
continue
468474

469475
vlan = ""
470-
building = ""
476+
building = _building_from_vlans(vlans, ipv6=ipv6)
471477
for vln in vlans:
472478
if ipv6.find(vln["ipv6prefix"]) == 0:
473479
vlan = vln["name"]
474-
building = vln["building"]
475480

476481
servers.append(
477482
{
@@ -947,6 +952,24 @@ def generatekeaconfig(servers, aps, vlans, outputdir):
947952
f.write(json.dumps(conf_keav6_config, indent=2))
948953

949954

955+
def _building_from_vlans(vlans, ipv6=None, ipv4=None):
956+
"""Look up building name from VLAN list by matching an IPv6 or IPv4 address."""
957+
for vlan in vlans:
958+
if ipv6:
959+
net = ipaddress.ip_network(
960+
f"{vlan['ipv6prefix']}/{vlan['ipv6bitmask']}", strict=False
961+
)
962+
if ipaddress.ip_address(ipv6) in net:
963+
return vlan["building"]
964+
if ipv4 and vlan["ipv4prefix"] != "0.0.0.0":
965+
net = ipaddress.ip_network(
966+
f"{vlan['ipv4prefix']}/{vlan['ipv4bitmask']}", strict=False
967+
)
968+
if ipaddress.ip_address(ipv4) in net:
969+
return vlan["building"]
970+
return ""
971+
972+
950973
def _prom_exclude(name):
951974
"""Return True if the name should be excluded from prom configs."""
952975
return re.match(r"^(deceased|donotuse|massflash|spare|expob5|expoc4|expoc5)", name)
@@ -1031,7 +1054,7 @@ def generatezones(switches, routers, pis, aps, servers, outputdir):
10311054
return True
10321055

10331056

1034-
def generatewasgehtconfig(switches, routers, pis, aps, servers, outputdir):
1057+
def generatewasgehtconfig(switches, routers, pis, aps, servers, vlans, outputdir):
10351058
wasgehtconfig = {}
10361059
wasgehtconfig["uplink"] = {
10371060
"checks": {
@@ -1045,11 +1068,16 @@ def generatewasgehtconfig(switches, routers, pis, aps, servers, outputdir):
10451068
}
10461069
}
10471070
for switch in switches:
1071+
if switch["hierarchy"].startswith("Z"):
1072+
continue
10481073
entry = {
10491074
"tags": {
10501075
"type": "switch",
10511076
"os": "junos",
10521077
"num": switch["num"],
1078+
"switchtype": switch["switchtype"],
1079+
"model": switch["model"],
1080+
"building": _building_from_vlans(vlans, ipv6=switch["ipv6"]),
10531081
**(
10541082
{"aliases": ", ".join(switch["aliases"])}
10551083
if switch["aliases"]
@@ -1097,6 +1125,7 @@ def generatewasgehtconfig(switches, routers, pis, aps, servers, outputdir):
10971125
"tags": {
10981126
"type": "router",
10991127
"os": "nixos",
1128+
"building": _building_from_vlans(vlans, ipv6=router["ipv6"]),
11001129
},
11011130
"checks": {
11021131
"ping": {
@@ -1105,11 +1134,14 @@ def generatewasgehtconfig(switches, routers, pis, aps, servers, outputdir):
11051134
},
11061135
}
11071136
for pi in pis:
1137+
if pi["name"] == "pi-massflash" or pi["name"].startswith("pi-reghelp"):
1138+
continue
11081139
wasgehtconfig[pi["name"]] = {
11091140
"tags": {
11101141
"type": "pi",
11111142
"os": "nixos",
11121143
"role": "registration" if pi["name"].startswith("pi-reg") else "sign",
1144+
"building": _building_from_vlans(vlans, ipv6=pi["ipv6"]),
11131145
},
11141146
"checks": {
11151147
"ping": {
@@ -1124,6 +1156,10 @@ def generatewasgehtconfig(switches, routers, pis, aps, servers, outputdir):
11241156
"os": "openwrt",
11251157
"channels": f"{ap['wifi2']} / {ap['wifi5']}",
11261158
"config": ap["configver"],
1159+
"building": _building_from_vlans(vlans, ipv4=ap["ipv4"]),
1160+
"map_id": ap["map_id"],
1161+
"map_x": ap["map_x"],
1162+
"map_y": ap["map_y"],
11271163
**({"aliases": ", ".join(ap["aliases"])} if ap["aliases"] else {}),
11281164
},
11291165
"checks": {
@@ -1256,14 +1292,14 @@ def main():
12561292
elif subcomm == "prom":
12571293
generatepromconfigs(switches, pis, aps, outputdir)
12581294
elif subcomm == "wasgeht":
1259-
generatewasgehtconfig(switches, routers, pis, aps, servers, outputdir)
1295+
generatewasgehtconfig(switches, routers, pis, aps, servers, vlans, outputdir)
12601296
elif subcomm == "allnet":
12611297
generateallnetwork(switches, routers, outputdir)
12621298
elif subcomm == "all":
12631299
generatekeaconfig(servers, aps, vlans, outputdir)
12641300
generatezones(switches, routers, pis, aps, servers, outputdir)
12651301
generatepromconfigs(switches, pis, aps, outputdir)
1266-
generatewasgehtconfig(switches, routers, pis, aps, servers, outputdir)
1302+
generatewasgehtconfig(switches, routers, pis, aps, servers, vlans, outputdir)
12671303
generateallnetwork(switches, routers, outputdir)
12681304
elif subcomm == "debug":
12691305
# overload outputdir as 2nd debug parameter

facts/test_inventory.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ def test_populateswitches():
359359
"ipv6": "2001:470:f325:103::200:16",
360360
# pylint: disable=line-too-long
361361
"ipv6ptr": "6.1.0.0.0.0.2.0.0.0.0.0.0.0.0.0.3.0.1.0.5.2.3.f.0.7.4.0.1.0.0.2.ip6.arpa",
362+
"switchtype": "Catwalk",
363+
"hierarchy": "W.1",
364+
"model": "ex4200-48p",
362365
"aliases": [],
363366
},
364367
{
@@ -368,6 +371,9 @@ def test_populateswitches():
368371
"ipv6": "2001:470:f325:503::200:17",
369372
# pylint: disable=line-too-long
370373
"ipv6ptr": "7.1.0.0.0.0.2.0.0.0.0.0.0.0.0.0.3.0.5.0.5.2.3.f.0.7.4.0.1.0.0.2.ip6.arpa",
374+
"switchtype": "cfRoom",
375+
"hierarchy": "I.9",
376+
"model": "ex4200-48p",
371377
"aliases": ["rm209", "rm210"],
372378
},
373379
],
@@ -414,6 +420,9 @@ def test_populateaps():
414420
"wifi5": "36",
415421
"configver": "0",
416422
"aliases": ["n7a-0093"],
423+
"map_id": "0",
424+
"map_x": "50",
425+
"map_y": "50",
417426
},
418427
{
419428
"name": "101-b",
@@ -425,6 +434,9 @@ def test_populateaps():
425434
"wifi5": "165",
426435
"configver": "0",
427436
"aliases": ["n8c-0002"],
437+
"map_id": "0",
438+
"map_x": "50",
439+
"map_y": "50",
428440
},
429441
],
430442
]
@@ -462,6 +474,9 @@ def test_populateservers():
462474
{
463475
"name": "cfInfra",
464476
"ipv6prefix": "2001:470:f325:503::",
477+
"ipv6bitmask": "64",
478+
"ipv4prefix": "10.128.3.0",
479+
"ipv4bitmask": "24",
465480
"building": "Conference",
466481
}
467482
]

facts/testdata/testswitchtypes

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
//Name Num MgtVL IPv6 Type // Hierarchy Switch Noiselevel
2-
Expo-Catwalk 16 103 2001:470:f325:103::200:16 Catwalk // W.1 Loud
3-
Rm209-210 17 503 2001:470:f325:503::200:17 cfRoom // I.9 Normal
1+
//Name Num MgtVL IPv6 Type Hierarchy Noiselevel Model Mgmt MAC
2+
Expo-Catwalk 16 103 2001:470:f325:103::200:16 Catwalk W.1 Loud ex4200-48p 00:26:88:7d:d3:00
3+
Rm209-210 17 503 2001:470:f325:503::200:17 cfRoom I.9 Normal ex4200-48p 00:26:88:6e:9f:80

0 commit comments

Comments
 (0)