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