source: genesis/tools/make-network-kml.py@ 10376

Last change on this file since 10376 was 10375, checked in by rick, 13 years ago

long v.s. lat v.s. order = weird....

  • Property svn:executable set to *
File size: 3.7 KB
Line 
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>
6import gformat
7import glob
8import os
9import re
10import subprocess
11import sys
12import tempfile
13import yaml
14
15OUTFILE = os.path.join(os.getcwd(),'network.kml')
16
17try:
18 store = yaml.load(open('store.yaml','r'))
19except IOError:
20 store = None
21
22
23HEADER = '''<?xml version="1.0" encoding="UTF-8"?>
24<kml xmlns="http://www.opengis.net/kml/2.2">
25<Document>
26'''
27
28POINT = '''
29 <Placemark>
30 <name>%(nodename)s</name>
31 <description>%(nodename)s</description>
32 <Point>
33 <coordinates>%(longitude)s,%(latitude)s,0</coordinates>
34 </Point>
35 </Placemark>
36'''
37
38LINE = '''
39 <Placemark>
40 <LineString>
41 <coordinates>
42 %(longitudeA)s,%(latitudeA)s,0
43 %(longitudeB)s,%(latitudeB)s,0
44 </coordinates>
45 </LineString>
46 </Placemark>
47'''
48
49FOOTER = '''
50</Document>
51</kml>
52'''
53def make_graph():
54 poel = {}
55 link_type = {}
56 link_data = {}
57 hosts = {}
58 try:
59 for host in gformat.get_hostlist():
60 print "## Processing host", host
61 datadump = gformat.get_yaml(host)
62 nodename = datadump['nodename']
63 hosts[nodename] = datadump
64
65 iface_keys = [elem for elem in datadump.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
66 for iface_key in iface_keys:
67 l = datadump[iface_key]['ip']
68 addr, mask = l.split('/')
69
70 # Not parsing of these folks please
71 if not gformat.valid_addr(addr):
72 continue
73
74 addr = gformat.parseaddr(addr)
75 mask = int(mask)
76 addr = addr & ~((1 << (32 - mask)) - 1)
77 if poel.has_key(addr):
78 poel[addr] += [nodename]
79 else:
80 poel[addr] = [nodename]
81 # Assume all ns_ip to be 11a for a moment
82 if datadump[iface_key].has_key('ns_ip'):
83 link_type[addr] = '11a'
84 else:
85 link_type[addr] = datadump[iface_key]['type']
86
87 link_data[addr] = 1
88 iface = datadump[iface_key]['interface']
89 nodename = datadump['nodename']
90 INTERVAL = 60 * 10
91 if store and store['uptime'].has_key(nodename) and store['snmp'].has_key(nodename) and store['traffic'].has_key(nodename):
92 if store and store['traffic'][nodename].has_key(iface):
93 (b_in, b_out) = store['traffic'][nodename][iface]
94 uptime = store['uptime'][nodename]
95 t_kb = float(b_in + b_out) / 1024
96 print "# INFO: Found %s kB in %s seconds" % (t_kb, INTERVAL)
97 retval = ((t_kb) / uptime) * INTERVAL
98 link_data[addr] = retval
99
100 print "### %s [%s] is of type %s" % (gformat.showaddr(addr), iface_key, link_type[addr])
101 except (KeyError, ValueError), e:
102 print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
103 print e
104 sys.exit(1)
105
106 f = open(OUTFILE,'w')
107 sys.stderr.write("# Building KML file\n")
108 print >> f, HEADER
109 for nodename, datadump in hosts.iteritems():
110 print >> f, POINT % datadump
111
112 for addr,leden in poel.iteritems():
113 if link_type[addr] == '11a':
114 color = 'green'
115 weight = 4
116 elif link_type[addr] == 'eth':
117 color = 'blue'
118 weight = 8
119 else:
120 color = 'black'
121 weight = 1
122 width = max(1,link_data[addr])
123 leden = sorted(set(leden))
124 for index,lid in enumerate(leden[:-1]):
125 for buur in leden[index + 1:]:
126 keys = {}
127 for key, value in hosts[buur].iteritems():
128 keys[key + 'A'] = value
129 for key, value in hosts[lid].iteritems():
130 keys[key + 'B'] = value
131 print >> f, LINE % keys
132 print >> f, FOOTER
133 f.flush()
134 sys.stderr.write("# COMPLETED find your output in %s\n" % OUTFILE)
135
136if __name__ == "__main__":
137 make_graph()
138
Note: See TracBrowser for help on using the repository browser.