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

Last change on this file since 10295 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
RevLine 
[8240]1#!/usr/bin/env python
[8268]2# vim:ts=2:et:sw=2:ai
3#
4# Build topological network graph
5# Rick van der Zwet <info@rickvanderzwet.nl>
[9809]6import gformat
7import glob
8import os
[8240]9import re
[9809]10import subprocess
[8240]11import sys
12import tempfile
[9989]13import yaml
[8240]14
[9809]15OUTFILE = os.path.join(os.getcwd(),'network.png')
[8240]16
[10038]17try:
18 store = yaml.load(open('store.yaml','r'))
19except IOError:
20 store = {'uptime' : {}, 'snmp' : {}, 'traffic' : {}}
21 pass
[9989]22
[8240]23def make_graph():
[8268]24 poel = {}
25 link_type = {}
[9989]26 link_data = {}
[8268]27 try:
[8296]28 for host in gformat.get_hostlist():
[8268]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('/')
[8240]35
[8281]36 # Not parsing of these folks please
[8321]37 if not gformat.valid_addr(addr):
[8281]38 continue
39
[8268]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]
[9989]47 # Assume all ns_ip to be 11a for a moment
48 if datadump[iface_key].has_key('ns_ip'):
[8268]49 link_type[addr] = '11a'
50 else:
51 link_type[addr] = datadump[iface_key]['type']
[9989]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
[8268]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
[9989]73 #f = tempfile.NamedTemporaryFile(bufsize=0)
74 f = open('/tmp/test.dot','w')
[8268]75 sys.stderr.write("# Building temponary graph file\n")
76 print >> f, "Graph WirelessLeidenNetwork {"
77 print >> f ,"""
[8240]78graph [ fontsize = 36,
79 overlap = scalexy,
80 splines = true,
81 ]
82"""
[8268]83 for addr,leden in poel.iteritems():
84 if link_type[addr] == '11a':
[9989]85 color = 'green'
[8268]86 weight = 4
87 elif link_type[addr] == 'eth':
88 color = 'blue'
89 weight = 8
90 else:
91 color = 'black'
92 weight = 1
[9989]93 width = max(1,link_data[addr])
[8268]94 leden = sorted(set(leden))
95 for index,lid in enumerate(leden[:-1]):
96 for buur in leden[index + 1:]:
[9989]97 print >> f,' %s -- %s [label="%s", color="%s", weight="%s", style="setlinewidth(%s)"]' % (lid, buur, gformat.showaddr(addr), color, weight, width)
[8268]98 print >> f, "}"
[9989]99 f.flush()
[8268]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)
[8240]107
108
109if __name__ == "__main__":
[8268]110 make_graph()
[8240]111
Note: See TracBrowser for help on using the repository browser.