source: genesis/tools/make_network_kml.py@ 10378

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

gformat allow creating network.kml

  • Property svn:executable set to *
File size: 3.8 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
15
16try:
17 store = yaml.load(open('store.yaml','r'))
18except IOError:
19 store = None
20
21
22HEADER = '''<?xml version="1.0" encoding="UTF-8"?>
23<kml xmlns="http://www.opengis.net/kml/2.2">
24<Document>
25'''
26
27POINT = '''
28 <Placemark>
29 <name>%(nodename)s</name>
30 <description>%(nodename)s</description>
31 <Point>
32 <coordinates>%(longitude)s,%(latitude)s,0</coordinates>
33 </Point>
34 </Placemark>
35'''
36
37LINE = '''
38 <Placemark>
39 <LineString>
40 <coordinates>
41 %(longitudeA)s,%(latitudeA)s,0
42 %(longitudeB)s,%(latitudeB)s,0
43 </coordinates>
44 </LineString>
45 </Placemark>
46'''
47
48FOOTER = '''
49</Document>
50</kml>
51'''
52def make_graph(debug=False):
53 poel = {}
54 link_type = {}
55 link_data = {}
56 hosts = {}
57
58 try:
59 for host in gformat.get_hostlist():
60 if debug: 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 if debug: print "# INFO: Found %s kB in %s seconds" % (t_kb, INTERVAL)
97 retval = ((t_kb) / uptime) * INTERVAL
98 link_data[addr] = retval
99
100 if debug: print "### %s [%s] is of type %s" % (gformat.showaddr(addr), iface_key, link_type[addr])
101 except (KeyError, ValueError), e:
102 if debug: print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
103 if debug: print e
104 sys.exit(1)
105
106 output = ''
107 if debug: print "# Building KML file"
108 output += HEADER
109 for nodename, datadump in hosts.iteritems():
110 output += 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 output += LINE % keys
132 output += FOOTER
133 return output
134
135if __name__ == "__main__":
136 OUTFILE = os.path.join(os.getcwd(),'network.kml')
137 kml_data = make_graph(debug=True)
138 f = open(OUTFILE,'w')
139 f.write(kml_data)
140 f.close()
141 print "# COMPLETED find your output in %s\n" % OUTFILE
142
Note: See TracBrowser for help on using the repository browser.