source: genesis/tools/make_network_kml.py@ 10379

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

Link status colour by status.

Reported-by: henk

  • Property svn:executable set to *
File size: 4.9 KB
RevLine 
[10037]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
[10375]16try:
17 store = yaml.load(open('store.yaml','r'))
18except IOError:
19 store = None
[10037]20
21
22HEADER = '''<?xml version="1.0" encoding="UTF-8"?>
23<kml xmlns="http://www.opengis.net/kml/2.2">
24<Document>
[10379]25 <Style id="linkDown">
26 <LineStyle>
27 <color>990000ff</color>
28 <width>2</width>
29 </LineStyle>
30 </Style>
31 <Style id="linkUp">
32 <LineStyle>
33 <color>9900ff00</color>
34 <width>3</width>
35 </LineStyle>
36 </Style>
37 <Style id="linkPlanned">
38 <LineStyle>
39 <color>99ff0000</color>
40 <width>1</width>
41 </LineStyle>
42 </Style>
43
[10037]44'''
45
46POINT = '''
47 <Placemark>
48 <name>%(nodename)s</name>
49 <description>%(nodename)s</description>
50 <Point>
[10375]51 <coordinates>%(longitude)s,%(latitude)s,0</coordinates>
[10037]52 </Point>
53 </Placemark>
54'''
55
56LINE = '''
57 <Placemark>
[10379]58 <styleUrl>#%(styleId)s</styleUrl>
[10037]59 <LineString>
60 <coordinates>
[10375]61 %(longitudeA)s,%(latitudeA)s,0
62 %(longitudeB)s,%(latitudeB)s,0
[10037]63 </coordinates>
64 </LineString>
65 </Placemark>
66'''
67
68FOOTER = '''
69</Document>
70</kml>
71'''
[10379]72
[10377]73def make_graph(debug=False):
[10037]74 poel = {}
75 link_type = {}
76 link_data = {}
[10379]77 link_status = {}
[10037]78 hosts = {}
[10377]79
[10037]80 try:
81 for host in gformat.get_hostlist():
[10377]82 if debug: print "## Processing host", host
[10037]83 datadump = gformat.get_yaml(host)
84 nodename = datadump['nodename']
85 hosts[nodename] = datadump
86
[10379]87 for iface_key in datadump['autogen_iface_keys']:
[10037]88 l = datadump[iface_key]['ip']
89 addr, mask = l.split('/')
90
91 # Not parsing of these folks please
92 if not gformat.valid_addr(addr):
93 continue
94
95 addr = gformat.parseaddr(addr)
96 mask = int(mask)
97 addr = addr & ~((1 << (32 - mask)) - 1)
98 if poel.has_key(addr):
99 poel[addr] += [nodename]
[10379]100 if link_status[addr] == 'linkUp':
101 if datadump['status'] == 'planned':
102 link_status[addr] = 'linkPlanned'
103 elif datadump[iface_key]['status'] == 'planned':
104 link_status[addr] = 'linkPlanned'
105 elif datadump[iface_key]['status'] == 'down':
106 link_status[addr] = 'linkDown'
[10037]107 else:
108 poel[addr] = [nodename]
109 # Assume all ns_ip to be 11a for a moment
110 if datadump[iface_key].has_key('ns_ip'):
111 link_type[addr] = '11a'
112 else:
113 link_type[addr] = datadump[iface_key]['type']
114
[10379]115 if datadump['status'] == 'planned':
116 link_status[addr] = 'linkPlanned'
117 elif datadump[iface_key]['status'] == 'planned':
118 link_status[addr] = 'linkPlanned'
119 elif datadump[iface_key]['status'] == 'down':
120 link_status[addr] = 'linkDown'
121 else:
122 link_status[addr] = 'linkUp'
123
[10037]124 link_data[addr] = 1
125 iface = datadump[iface_key]['interface']
126 nodename = datadump['nodename']
127 INTERVAL = 60 * 10
[10375]128 if store and store['uptime'].has_key(nodename) and store['snmp'].has_key(nodename) and store['traffic'].has_key(nodename):
129 if store and store['traffic'][nodename].has_key(iface):
[10037]130 (b_in, b_out) = store['traffic'][nodename][iface]
131 uptime = store['uptime'][nodename]
132 t_kb = float(b_in + b_out) / 1024
[10377]133 if debug: print "# INFO: Found %s kB in %s seconds" % (t_kb, INTERVAL)
[10037]134 retval = ((t_kb) / uptime) * INTERVAL
135 link_data[addr] = retval
136
[10377]137 if debug: print "### %s [%s] is of type %s" % (gformat.showaddr(addr), iface_key, link_type[addr])
[10037]138 except (KeyError, ValueError), e:
[10377]139 if debug: print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
140 if debug: print e
[10037]141 sys.exit(1)
142
[10377]143 output = ''
144 if debug: print "# Building KML file"
145 output += HEADER
[10037]146 for nodename, datadump in hosts.iteritems():
[10377]147 output += POINT % datadump
[10037]148
149 for addr,leden in poel.iteritems():
150 if link_type[addr] == '11a':
151 color = 'green'
152 weight = 4
153 elif link_type[addr] == 'eth':
154 color = 'blue'
155 weight = 8
156 else:
157 color = 'black'
158 weight = 1
159 width = max(1,link_data[addr])
160 leden = sorted(set(leden))
161 for index,lid in enumerate(leden[:-1]):
162 for buur in leden[index + 1:]:
163 keys = {}
[10379]164 keys['styleId'] = link_status[addr]
[10037]165 for key, value in hosts[buur].iteritems():
166 keys[key + 'A'] = value
167 for key, value in hosts[lid].iteritems():
168 keys[key + 'B'] = value
[10377]169 output += LINE % keys
170 output += FOOTER
171 return output
[10037]172
173if __name__ == "__main__":
[10377]174 OUTFILE = os.path.join(os.getcwd(),'network.kml')
175 kml_data = make_graph(debug=True)
176 f = open(OUTFILE,'w')
177 f.write(kml_data)
178 f.close()
179 print "# COMPLETED find your output in %s\n" % OUTFILE
[10037]180
Note: See TracBrowser for help on using the repository browser.