source: genesis/tools/make-network-graph.py@ 10032

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

Hack the data visualisation in here as well.

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 3.3 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.png')
16
17store = yaml.load(open('store.yaml','r'))
18
19def make_graph():
20 poel = {}
21 link_type = {}
22 link_data = {}
23 try:
24 for host in gformat.get_hostlist():
25 print "## Processing host", host
26 datadump = gformat.get_yaml(host)
27 iface_keys = [elem for elem in datadump.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
28 for iface_key in iface_keys:
29 l = datadump[iface_key]['ip']
30 addr, mask = l.split('/')
31
32 # Not parsing of these folks please
33 if not gformat.valid_addr(addr):
34 continue
35
36 addr = gformat.parseaddr(addr)
37 mask = int(mask)
38 addr = addr & ~((1 << (32 - mask)) - 1)
39 if poel.has_key(addr):
40 poel[addr] += [host]
41 else:
42 poel[addr] = [host]
43 # Assume all ns_ip to be 11a for a moment
44 if datadump[iface_key].has_key('ns_ip'):
45 link_type[addr] = '11a'
46 else:
47 link_type[addr] = datadump[iface_key]['type']
48
49 link_data[addr] = 1
50 iface = datadump[iface_key]['interface']
51 nodename = datadump['nodename']
52 print nodename, iface
53 INTERVAL = 60 * 10
54 if store['uptime'].has_key(nodename) and store['snmp'].has_key(nodename) and store['traffic'].has_key(nodename):
55 if store['traffic'][nodename].has_key(iface):
56 (b_in, b_out) = store['traffic'][nodename][iface]
57 uptime = store['uptime'][nodename]
58 t_kb = float(b_in + b_out) / 1024
59 print "# INFO: Found %s kB in %s seconds" % (t_kb, INTERVAL)
60 retval = ((t_kb) / uptime) * INTERVAL
61 link_data[addr] = retval
62
63 print "### %s [%s] is of type %s" % (gformat.showaddr(addr), iface_key, link_type[addr])
64 except (KeyError, ValueError), e:
65 print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
66 print e
67 sys.exit(1)
68
69 #f = tempfile.NamedTemporaryFile(bufsize=0)
70 f = open('/tmp/test.dot','w')
71 sys.stderr.write("# Building temponary graph file\n")
72 print >> f, "Graph WirelessLeidenNetwork {"
73 print >> f ,"""
74graph [ fontsize = 36,
75 overlap = scalexy,
76 splines = true,
77 ]
78"""
79 for addr,leden in poel.iteritems():
80 if link_type[addr] == '11a':
81 color = 'green'
82 weight = 4
83 elif link_type[addr] == 'eth':
84 color = 'blue'
85 weight = 8
86 else:
87 color = 'black'
88 weight = 1
89 width = max(1,link_data[addr])
90 leden = sorted(set(leden))
91 for index,lid in enumerate(leden[:-1]):
92 for buur in leden[index + 1:]:
93 print >> f,' %s -- %s [label="%s", color="%s", weight="%s", style="setlinewidth(%s)"]' % (lid, buur, gformat.showaddr(addr), color, weight, width)
94 print >> f, "}"
95 f.flush()
96 sys.stderr.write("# Plotting temponary graph file using graphviz\n")
97 retval = subprocess.call(["neato","-Tpng",f.name, "-o", OUTFILE])
98 if retval != 0:
99 sys.stderr.write("# FAILED [%i]\n" % retval)
100 subprocess.call(["cat",f.name])
101 exit(1)
102 sys.stderr.write("# COMPLETED find your output in %s\n" % OUTFILE)
103
104
105if __name__ == "__main__":
106 make_graph()
107
Note: See TracBrowser for help on using the repository browser.