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

Last change on this file since 13792 was 12486, checked in by rick, 11 years ago

Allow link specific features

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 4.5 KB
RevLine 
[8240]1#!/usr/bin/env python
[8268]2# vim:ts=2:et:sw=2:ai
3#
[12486]4#
[8268]5# Build topological network graph
[12486]6#
[8268]7# Rick van der Zwet <info@rickvanderzwet.nl>
[12486]8#
[9809]9import gformat
10import glob
11import os
[8240]12import re
[9809]13import subprocess
[8240]14import sys
15import tempfile
[9989]16import yaml
[8240]17
[9809]18OUTFILE = os.path.join(os.getcwd(),'network.png')
[12486]19LINK_SPECIFIC = False
[8240]20
[10038]21try:
22 store = yaml.load(open('store.yaml','r'))
23except IOError:
24 store = {'uptime' : {}, 'snmp' : {}, 'traffic' : {}}
25 pass
[9989]26
[11753]27def pass_filter(host):
28 for t in sys.argv:
29 if t in host:
30 return True
31 return False
32
33nodes = {}
34
[8240]35def make_graph():
[8268]36 poel = {}
37 link_type = {}
[9989]38 link_data = {}
[8268]39 try:
[8296]40 for host in gformat.get_hostlist():
[12486]41 # Filter should be proper argument
[11753]42 if not pass_filter(host):
43 continue
[8268]44 print "## Processing host", host
45 datadump = gformat.get_yaml(host)
[11753]46 nodename = datadump['nodename']
[8268]47 iface_keys = [elem for elem in datadump.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
[11753]48 nodes[nodename] = datadump
[8268]49 for iface_key in iface_keys:
50 l = datadump[iface_key]['ip']
51 addr, mask = l.split('/')
[8240]52
[8281]53 # Not parsing of these folks please
[8321]54 if not gformat.valid_addr(addr):
[8281]55 continue
56
[8268]57 addr = gformat.parseaddr(addr)
58 mask = int(mask)
59 addr = addr & ~((1 << (32 - mask)) - 1)
[11753]60 nk = nodename + ':' + datadump[iface_key]['autogen_ifname']
[8268]61 if poel.has_key(addr):
[11753]62 poel[addr] += [nk]
[8268]63 else:
[11753]64 poel[addr] = [nk]
[9989]65 # Assume all ns_ip to be 11a for a moment
66 if datadump[iface_key].has_key('ns_ip'):
[8268]67 link_type[addr] = '11a'
68 else:
69 link_type[addr] = datadump[iface_key]['type']
[9989]70
71 link_data[addr] = 1
[10890]72 iface = datadump[iface_key]['autogen_ifname']
[9989]73 INTERVAL = 60 * 10
74 if store['uptime'].has_key(nodename) and store['snmp'].has_key(nodename) and store['traffic'].has_key(nodename):
75 if store['traffic'][nodename].has_key(iface):
76 (b_in, b_out) = store['traffic'][nodename][iface]
77 uptime = store['uptime'][nodename]
78 t_kb = float(b_in + b_out) / 1024
79 print "# INFO: Found %s kB in %s seconds" % (t_kb, INTERVAL)
80 retval = ((t_kb) / uptime) * INTERVAL
81 link_data[addr] = retval
82
[12479]83 print "### ... %s %s [%s] is of type %s" % (iface, gformat.showaddr(addr), iface_key, link_type[addr])
[8268]84 except (KeyError, ValueError), e:
85 print "[FOUT] in '%s' interface '%s'" % (host,iface_key)
86 print e
87 sys.exit(1)
88
[9989]89 f = open('/tmp/test.dot','w')
[8268]90 sys.stderr.write("# Building temponary graph file\n")
[11753]91 print >> f, "digraph WirelessLeidenNetwork {"
[8268]92 print >> f ,"""
[11753]93graph [ fontsize = 10,
[8240]94 overlap = scalexy,
95 splines = true,
[11753]96 rankdir = LR,
[8240]97 ]
[11753]98node [ fontsize = 10, ]
99edge [ fontsize = 10, ]
[8240]100"""
[12479]101 for node in sorted(nodes.keys()):
102 datadump = nodes[node]
103 print "# Processing node:", node
[11753]104 lines = [node]
105 for ik in datadump['autogen_iface_keys']:
[12479]106 print "# .. ", ik
[11753]107 if 'alias' in ik or 'wlan' in datadump[ik]['autogen_ifname']:
108 continue
109 if datadump[ik].has_key('ns_mac'):
110 lines.append('<%(autogen_ifname)s> %(bridge_type)s at %(autogen_ifname)s - %(ns_mac)s\\n%(ssid)s\\n%(ns_ip)s' % datadump[ik])
111 else:
112 lines.append('<%(autogen_ifname)s> %(autogen_ifname)s' % datadump[ik])
113 print >> f,"""
114"%s" [
115shape = "record"
116label = "%s"
117];
118""" % (node, "|".join(lines))
119
120
[8268]121 for addr,leden in poel.iteritems():
122 if link_type[addr] == '11a':
[9989]123 color = 'green'
[8268]124 weight = 4
125 elif link_type[addr] == 'eth':
126 color = 'blue'
127 weight = 8
128 else:
129 color = 'black'
130 weight = 1
[9989]131 width = max(1,link_data[addr])
[8268]132 leden = sorted(set(leden))
133 for index,lid in enumerate(leden[:-1]):
134 for buur in leden[index + 1:]:
[12486]135 if LINK_SPECIFIC:
136 print >> f,' %s -> %s [color="%s", weight="%s", style="setlinewidth(%s)"]' % (lid, buur, color, weight, width)
137 else:
138 print >> f,' %s -> %s [color="%s", weight="%s", style="setlinewidth(%s)"]' % (lid.split(':')[0], buur.split(':')[0], color, weight, width)
[8268]139 print >> f, "}"
[9989]140 f.flush()
[8268]141 sys.stderr.write("# Plotting temponary graph file using graphviz\n")
142 retval = subprocess.call(["neato","-Tpng",f.name, "-o", OUTFILE])
143 if retval != 0:
144 sys.stderr.write("# FAILED [%i]\n" % retval)
145 subprocess.call(["cat",f.name])
146 exit(1)
147 sys.stderr.write("# COMPLETED find your output in %s\n" % OUTFILE)
[8240]148
149
150if __name__ == "__main__":
[8268]151 make_graph()
[8240]152
Note: See TracBrowser for help on using the repository browser.