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