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 13987 2017-09-22 18:48:45Z 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 | # Check syntax of defined variables
|
---|
22 | _ = gformat.generate_wleiden_yaml(datadump)
|
---|
23 | try:
|
---|
24 | # Process interfaces
|
---|
25 | iface_keys = [elem for elem in datadump.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
|
---|
26 | for iface_key in iface_keys:
|
---|
27 | # Virtual interfaces bridge interfaces do not have IP addreses
|
---|
28 | if not datadump[iface_key].has_key('ip'):
|
---|
29 | continue
|
---|
30 |
|
---|
31 | l = datadump[iface_key]['ip']
|
---|
32 | addr, mask = l.split('/')
|
---|
33 |
|
---|
34 | pool[addr].append((host, iface_key))
|
---|
35 |
|
---|
36 | iface_key = 'masterip'
|
---|
37 | addr = datadump['masterip']
|
---|
38 | # Add masterip to the list if IP has not been defined at interface
|
---|
39 | if not host in [x[0] for x in pool[addr]]:
|
---|
40 | pool[addr].append((host, 'masterip'))
|
---|
41 |
|
---|
42 | print "OK"
|
---|
43 | except (KeyError, ValueError), e:
|
---|
44 | print "[ERROR] in '%s' interface '%s' (%s)" % (host, iface_key, e)
|
---|
45 | raise
|
---|
46 | except Exception as e:
|
---|
47 | raise
|
---|
48 | sys.exit(1)
|
---|
49 |
|
---|
50 | error = False
|
---|
51 | for addr,leden in pool.iteritems():
|
---|
52 | if len(leden) > 1:
|
---|
53 | if not any(map(lambda x: addr.startswith(x), allowed_multi_use)):
|
---|
54 | print "[ERROR] Multiple usages of IP %s:" % (addr)
|
---|
55 | for host, key in leden:
|
---|
56 | print " - %s - %s" % (host, key)
|
---|
57 | error = True
|
---|
58 |
|
---|
59 | if error:
|
---|
60 | print "# Errors found"
|
---|
61 | return 1
|
---|
62 | else:
|
---|
63 | print "# No multiple usages of IPs found"
|
---|
64 | return 0
|
---|
65 |
|
---|
66 |
|
---|
67 | if __name__ == "__main__":
|
---|
68 | sys.exit(check_double_ip())
|
---|
69 |
|
---|