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

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

Store file in the current calling directory...

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 2.5 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
13
14OUTFILE = os.path.join(os.getcwd(),'network.png')
15
16def make_graph():
17 poel = {}
18 link_type = {}
19 try:
20 for host in gformat.get_hostlist():
21 print "## Processing host", host
22 datadump = gformat.get_yaml(host)
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 l = datadump[iface_key]['ip']
26 addr, mask = l.split('/')
27
28 # Not parsing of these folks please
29 if not gformat.valid_addr(addr):
30 continue
31
32 addr = gformat.parseaddr(addr)
33 mask = int(mask)
34 addr = addr & ~((1 << (32 - mask)) - 1)
35 if poel.has_key(addr):
36 poel[addr] += [host]
37 else:
38 poel[addr] = [host]
39 # Assume all eth2wifibridge to be 11a for a moment
40 if datadump[iface_key].has_key('eth2wifibridge'):
41 link_type[addr] = '11a'
42 else:
43 link_type[addr] = datadump[iface_key]['type']
44 print "### %s [%s] is of type %s" % (gformat.showaddr(addr), iface_key, link_type[addr])
45 except (KeyError, ValueError), e:
46 print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
47 print e
48 sys.exit(1)
49
50 f = tempfile.NamedTemporaryFile(bufsize=0)
51 sys.stderr.write("# Building temponary graph file\n")
52 print >> f, "Graph WirelessLeidenNetwork {"
53 print >> f ,"""
54graph [ fontsize = 36,
55 overlap = scalexy,
56 splines = true,
57 ]
58"""
59 for addr,leden in poel.iteritems():
60 if link_type[addr] == '11a':
61 color = 'red'
62 weight = 4
63 elif link_type[addr] == 'eth':
64 color = 'blue'
65 weight = 8
66 else:
67 color = 'black'
68 weight = 1
69 leden = sorted(set(leden))
70 for index,lid in enumerate(leden[:-1]):
71 for buur in leden[index + 1:]:
72 print >> f,' %s -- %s [label="%s", color="%s", weight="%s"]' % (lid, buur, gformat.showaddr(addr), color, weight)
73 print >> f, "}"
74 sys.stderr.write("# Plotting temponary graph file using graphviz\n")
75 retval = subprocess.call(["neato","-Tpng",f.name, "-o", OUTFILE])
76 if retval != 0:
77 sys.stderr.write("# FAILED [%i]\n" % retval)
78 subprocess.call(["cat",f.name])
79 exit(1)
80 sys.stderr.write("# COMPLETED find your output in %s\n" % OUTFILE)
81
82
83if __name__ == "__main__":
84 make_graph()
85
Note: See TracBrowser for help on using the repository browser.