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

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

Fix non-existing store.yaml

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