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

Last change on this file since 12449 was 11753, checked in by rick, 12 years ago

Make pretty NS connection graphs.

Example: ../tools/make-network-graph.py Watertoren Kempers RecPark Kaag2 Herman HMKerk

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 4.2 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 pass_filter(host):
24 for t in sys.argv:
25 if t in host:
26 return True
27 return False
28
29nodes = {}
30
31def make_graph():
32 poel = {}
33 link_type = {}
34 link_data = {}
35 try:
36 for host in gformat.get_hostlist():
37 if not pass_filter(host):
38 continue
39 print "## Processing host", host
40 datadump = gformat.get_yaml(host)
41 nodename = datadump['nodename']
42 iface_keys = [elem for elem in datadump.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
43 nodes[nodename] = datadump
44 for iface_key in iface_keys:
45 l = datadump[iface_key]['ip']
46 addr, mask = l.split('/')
47
48 # Not parsing of these folks please
49 if not gformat.valid_addr(addr):
50 continue
51
52 addr = gformat.parseaddr(addr)
53 mask = int(mask)
54 addr = addr & ~((1 << (32 - mask)) - 1)
55 nk = nodename + ':' + datadump[iface_key]['autogen_ifname']
56 if poel.has_key(addr):
57 poel[addr] += [nk]
58 else:
59 poel[addr] = [nk]
60 # Assume all ns_ip to be 11a for a moment
61 if datadump[iface_key].has_key('ns_ip'):
62 link_type[addr] = '11a'
63 else:
64 link_type[addr] = datadump[iface_key]['type']
65
66 link_data[addr] = 1
67 iface = datadump[iface_key]['autogen_ifname']
68 print nodename, iface
69 INTERVAL = 60 * 10
70 if store['uptime'].has_key(nodename) and store['snmp'].has_key(nodename) and store['traffic'].has_key(nodename):
71 if store['traffic'][nodename].has_key(iface):
72 (b_in, b_out) = store['traffic'][nodename][iface]
73 uptime = store['uptime'][nodename]
74 t_kb = float(b_in + b_out) / 1024
75 print "# INFO: Found %s kB in %s seconds" % (t_kb, INTERVAL)
76 retval = ((t_kb) / uptime) * INTERVAL
77 link_data[addr] = retval
78
79 print "### %s [%s] is of type %s" % (gformat.showaddr(addr), iface_key, link_type[addr])
80 except (KeyError, ValueError), e:
81 print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
82 print e
83 sys.exit(1)
84
85 f = open('/tmp/test.dot','w')
86 sys.stderr.write("# Building temponary graph file\n")
87 print >> f, "digraph WirelessLeidenNetwork {"
88 print >> f ,"""
89graph [ fontsize = 10,
90 overlap = scalexy,
91 splines = true,
92 rankdir = LR,
93 ]
94node [ fontsize = 10, ]
95edge [ fontsize = 10, ]
96"""
97 for node, datadump in nodes.iteritems():
98 lines = [node]
99 for ik in datadump['autogen_iface_keys']:
100 if 'alias' in ik or 'wlan' in datadump[ik]['autogen_ifname']:
101 continue
102 if datadump[ik].has_key('ns_mac'):
103 lines.append('<%(autogen_ifname)s> %(bridge_type)s at %(autogen_ifname)s - %(ns_mac)s\\n%(ssid)s\\n%(ns_ip)s' % datadump[ik])
104 else:
105 lines.append('<%(autogen_ifname)s> %(autogen_ifname)s' % datadump[ik])
106 print >> f,"""
107"%s" [
108shape = "record"
109label = "%s"
110];
111""" % (node, "|".join(lines))
112
113
114 for addr,leden in poel.iteritems():
115 if link_type[addr] == '11a':
116 color = 'green'
117 weight = 4
118 elif link_type[addr] == 'eth':
119 color = 'blue'
120 weight = 8
121 else:
122 color = 'black'
123 weight = 1
124 width = max(1,link_data[addr])
125 leden = sorted(set(leden))
126 for index,lid in enumerate(leden[:-1]):
127 for buur in leden[index + 1:]:
128 print >> f,' %s -> %s [color="%s", weight="%s", style="setlinewidth(%s)"]' % (lid, buur, color, weight, width)
129 print >> f, "}"
130 f.flush()
131 sys.stderr.write("# Plotting temponary graph file using graphviz\n")
132 retval = subprocess.call(["neato","-Tpng",f.name, "-o", OUTFILE])
133 if retval != 0:
134 sys.stderr.write("# FAILED [%i]\n" % retval)
135 subprocess.call(["cat",f.name])
136 exit(1)
137 sys.stderr.write("# COMPLETED find your output in %s\n" % OUTFILE)
138
139
140if __name__ == "__main__":
141 make_graph()
142
Note: See TracBrowser for help on using the repository browser.