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 | from collections import defaultdict
|
---|
10 |
|
---|
11 | __version__ = '$Id: syntax-checker.py 12574 2013-12-17 20:12:22Z rick $'
|
---|
12 |
|
---|
13 | allowed_multi_use = ['0.0.0.0', '192.168.1.', '192.168.178.']
|
---|
14 |
|
---|
15 | def check_double_ip():
|
---|
16 | pool = defaultdict(list)
|
---|
17 | try:
|
---|
18 | for host in gformat.get_hostlist():
|
---|
19 | print "## Processing host %-25s: " % host,
|
---|
20 | datadump = gformat.get_yaml(host,add_version_info=False)
|
---|
21 | try:
|
---|
22 | # Process interfaces
|
---|
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('/')
|
---|
27 |
|
---|
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 |
|
---|
36 | print "OK"
|
---|
37 | except (KeyError, ValueError), e:
|
---|
38 | print "[ERROR] in '%s' interface '%s' (%s)" % (host, iface_key, e)
|
---|
39 | raise
|
---|
40 | except Exception as e:
|
---|
41 | raise
|
---|
42 | sys.exit(1)
|
---|
43 |
|
---|
44 | error = False
|
---|
45 | for addr,leden in pool.iteritems():
|
---|
46 | if len(leden) > 1:
|
---|
47 | if not any(map(lambda x: addr.startswith(x), allowed_multi_use)):
|
---|
48 | print "[ERROR] Multiple usages of IP %s:" % (addr)
|
---|
49 | for host, key in leden:
|
---|
50 | print " - %s - %s" % (host, key)
|
---|
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 |
|
---|