source: genesis/tools/syntax-checker.py@ 14376

Last change on this file since 14376 was 14374, checked in by rick, 6 years ago

Update gformat to FreeBSD new default python3

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 3.0 KB
RevLine 
[14374]1#!/usr/bin/env python3
[8268]2# vim:ts=2:et:sw=2:ai
3#
4# Build topological network graph
5# Rick van der Zwet <info@rickvanderzwet.nl>
6import gformat
7import sys
[14025]8import ipaddress
[8268]9
[12574]10from collections import defaultdict
11
[8268]12__version__ = '$Id: syntax-checker.py 14374 2019-06-11 10:41:16Z rick $'
13
[14374]14allowed_multi_use = list(map(lambda x: ipaddress.ip_network(x, strict=True), [
15 '192.168.0.0/22',
16 '192.168.0.0/16',
17 '192.168.0.0/24',
18 '192.168.1.0/24',
19 '192.168.178.0/24',
20 ]))
[8268]21
[14025]22
[8268]23def check_double_ip():
[12574]24 pool = defaultdict(list)
[8268]25 try:
[10376]26 for host in gformat.get_hostlist():
[14374]27 print("## Processing host %-25s: " % host, end='')
[10904]28 datadump = gformat.get_yaml(host,add_version_info=False)
[14374]29 masterip_addr = ipaddress.IPv4Interface(datadump['masterip'])
[14025]30 masterip_is_used = False
31
[13987]32 # Check syntax of defined variables
33 _ = gformat.generate_wleiden_yaml(datadump)
[10904]34 try:
[12574]35 # Process interfaces
[10904]36 iface_keys = [elem for elem in datadump.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
37 for iface_key in iface_keys:
[14025]38 # Extra (descriptive entries) are ignored
39 if '_extra' in iface_key:
[13618]40 continue
41
[14025]42 # Process actual and virtual IP (avoiding clashes with nanostation IP)
43 for entry in ['ip', 'ns_ip']:
44 if entry in datadump[iface_key]:
[14374]45 addr = ipaddress.IPv4Interface(datadump[iface_key][entry])
[14025]46 if masterip_addr in addr.network:
47 masterip_is_used = True
48 pool[addr.network].append((host, iface_key, entry, addr))
[8268]49
[12574]50
51 # Add masterip to the list if IP has not been defined at interface
[14025]52 if not masterip_is_used:
53 pool[masterip_addr.network].append((host, 'masterip', '', masterip_addr))
[12574]54
[14374]55 print("OK")
56 except (KeyError, ValueError) as e:
57 print("[ERROR] in '%s' interface '%s' (%s)" % (host, iface_key, e))
[10904]58 raise
59 except Exception as e:
[10820]60 raise
[8268]61 sys.exit(1)
62
[14025]63 errors = 0
64 keys = sorted(pool.keys(),reverse=True)
[8268]65
[14025]66 for i,network in enumerate(keys):
67 if not network in allowed_multi_use:
68 for network2 in keys[i+1:]:
69 if not network2 in allowed_multi_use and network2.overlaps(network):
70 errors += 1
[14374]71 print("[ERROR#%i] network %s overlaps with %s:" % (errors, network, network2))
[14025]72 for (host, key, entry, addr) in sorted(pool[network] + pool[network2]):
[14374]73 print(" - %-20s - %-20s - %-5s - %s" % (host, key, entry, addr))
[14025]74
75 leden = sorted(pool[network])
76 for i,lid in enumerate(leden):
77 for lid2 in leden[i+1:]:
78 if lid[3] == lid2[3]:
79 errors += 1
[14374]80 print("[ERROR#%i] Multiple usages of IP %s:" % (errors, lid[3]))
81 print(" - %-20s - %-20s - %-5s" % (lid[0], lid[1], lid[2]))
82 print(" - %-20s - %-20s - %-5s" % (lid2[0], lid2[1], lid2[2]))
[14025]83
84 if errors > 0:
[14374]85 print("# %i Errors found" % errors)
[8268]86 return 1
87 else:
[14374]88 print("# No multiple usages of IPs found")
[8268]89 return 0
90
91
92if __name__ == "__main__":
93 sys.exit(check_double_ip())
94
Note: See TracBrowser for help on using the repository browser.