[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 10904 2012-05-18 21:48:21Z rick $'
|
---|
| 10 |
|
---|
| 11 | allowed_multi_use = ['0.0.0.0', '192.168.1.100']
|
---|
| 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:
|
---|
| 41 | if not addr in allowed_multi_use:
|
---|
| 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 |
|
---|