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