[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
|
---|
| 8 |
|
---|
[12574] | 9 | from collections import defaultdict
|
---|
| 10 |
|
---|
[8268] | 11 | __version__ = '$Id: syntax-checker.py 12574 2013-12-17 20:12:22Z rick $'
|
---|
| 12 |
|
---|
[12291] | 13 | allowed_multi_use = ['0.0.0.0', '192.168.1.', '192.168.178.']
|
---|
[8268] | 14 |
|
---|
| 15 | def check_double_ip():
|
---|
[12574] | 16 | pool = defaultdict(list)
|
---|
[8268] | 17 | try:
|
---|
[10376] | 18 | for host in gformat.get_hostlist():
|
---|
[12574] | 19 | print "## Processing host %-25s: " % host,
|
---|
[10904] | 20 | datadump = gformat.get_yaml(host,add_version_info=False)
|
---|
| 21 | try:
|
---|
[12574] | 22 | # Process interfaces
|
---|
[10904] | 23 | iface_keys = [elem for elem in datadump.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
|
---|
| 24 | for iface_key in iface_keys:
|
---|
| 25 | l = datadump[iface_key]['ip']
|
---|
| 26 | addr, mask = l.split('/')
|
---|
[8268] | 27 |
|
---|
[12574] | 28 | pool[addr].append((host, iface_key))
|
---|
| 29 |
|
---|
| 30 | iface_key = 'masterip'
|
---|
| 31 | addr = datadump['masterip']
|
---|
| 32 | # Add masterip to the list if IP has not been defined at interface
|
---|
| 33 | if not host in [x[0] for x in pool[addr]]:
|
---|
| 34 | pool[addr].append((host, 'masterip'))
|
---|
| 35 |
|
---|
[10904] | 36 | print "OK"
|
---|
| 37 | except (KeyError, ValueError), e:
|
---|
[12574] | 38 | print "[ERROR] in '%s' interface '%s' (%s)" % (host, iface_key, e)
|
---|
[10904] | 39 | raise
|
---|
| 40 | except Exception as e:
|
---|
[10820] | 41 | raise
|
---|
[8268] | 42 | sys.exit(1)
|
---|
| 43 |
|
---|
| 44 | error = False
|
---|
| 45 | for addr,leden in pool.iteritems():
|
---|
| 46 | if len(leden) > 1:
|
---|
[12291] | 47 | if not any(map(lambda x: addr.startswith(x), allowed_multi_use)):
|
---|
[8268] | 48 | print "[ERROR] Multiple usages of IP %s:" % (addr)
|
---|
[12574] | 49 | for host, key in leden:
|
---|
| 50 | print " - %s - %s" % (host, key)
|
---|
[8268] | 51 | error = True
|
---|
| 52 |
|
---|
| 53 | if error:
|
---|
| 54 | print "# Errors found"
|
---|
| 55 | return 1
|
---|
| 56 | else:
|
---|
| 57 | print "# No multiple usages of IPs found"
|
---|
| 58 | return 0
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | if __name__ == "__main__":
|
---|
| 62 | sys.exit(check_double_ip())
|
---|
| 63 |
|
---|