From 48d4fca08ba87e49f4dd55052d827d581e339e18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=BCllhorst?= Date: Wed, 23 Dec 2015 17:35:55 +0100 Subject: [PATCH] Implemented export of statistic files and first version of statistics file --- domain_selector.py | 21 +++++++++-- node_hierarchy.py | 1 - track_statistics.py | 90 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 4 deletions(-) create mode 100755 track_statistics.py diff --git a/domain_selector.py b/domain_selector.py index 9f995da..2925cad 100644 --- a/domain_selector.py +++ b/domain_selector.py @@ -14,11 +14,13 @@ class DomainSelector: self.graph = Graph(self.nodesData, self.graphData) if self.targets == None: self.writeConfigFiles(self.graph.nodes_list,"all") + self.writeDumpFile(self.graph.nodes_list,"all") else: nodes = {} for k,v in self.targets.iteritems(): nodes = self.graph.getNodeCloudsIn(v) self.writeConfigFiles(nodes,k) + self.writeDumpFile(nodes,k) nodes = {} def __getFile__(self, nodesFile): @@ -34,12 +36,12 @@ class DomainSelector: resource.close() return data - def writeConfigFiles(self,nodes_level, name): - maxDepth = self.maxDepth(nodes_level) + def writeConfigFiles(self, nodes, name): + maxDepth = self.maxDepth(nodes) for i in range(0,maxDepth): content = 'geo $switch {\n\tdefault\t0;' f = open(self.dataPath+'/'+name+'_node_level'+str(i),'w') - for node in nodes_level.itervalues(): + for node in nodes.itervalues(): if node.stepsToVpn == i: if node.ipv6 and node.hostname: content += '\n\t'+node.ipv6+'\t1;\t #'+node.hostname @@ -49,6 +51,19 @@ class DomainSelector: f.write(content.encode('utf8')) f.close() + def writeDumpFile(self, nodes, name): + content = {} + for node in nodes.itervalues(): + if node.ipv6 and node.hostname: + content[node.nodeid] = { + 'nodeid' : node.nodeid, + 'ipv6' : node.ipv6, + 'hostname' : node.hostname, + 'level' : node.stepsToVpn, + } + with open(self.dataPath+'/'+name+'_node_statistics.json', 'w') as outfile: + json.dump(content, outfile) + def maxDepth(self, nodes): maxDepth = 0 for v in nodes.itervalues(): diff --git a/node_hierarchy.py b/node_hierarchy.py index a4b9d27..7660ce4 100755 --- a/node_hierarchy.py +++ b/node_hierarchy.py @@ -1,7 +1,6 @@ #!/usr/bin/python # -*- coding: utf-8 - #Imports: -import json from domain_selector import DomainSelector targets = { diff --git a/track_statistics.py b/track_statistics.py new file mode 100755 index 0000000..33617c7 --- /dev/null +++ b/track_statistics.py @@ -0,0 +1,90 @@ +#!/usr/bin/python +# -*- coding: utf-8 - +#Imports: +import json +import operator + +class TrackStatistics: + def __init__(self,nodesFile,statisticsFiles, printStatus = False): + self.printStatus = printStatus + self.nodesData = self.__getFile__(nodesFile) + self.statisticsData = [] + for entry in statisticsFiles: + self.statisticsData.append({'data' : self.__getFile__(entry['data']), 'name' : entry['name']}) + #print self.statisticsData + for domain in self.statisticsData: + self.printDomainStatisticsPerLevel(domain['data'], domain['name']) + + def printDomainStatisticsPerLevel(self,data, name = "not set"): + #firmwareVersion = {} + print '-'*50 + print 'Printing statistics for domain:', name + for level in range(0,self.maxDepth(data)): + firmwareVersion = {} + for nodeid, node in data.iteritems(): + if level == node['level']: + fwver = self.nodesData['nodes'][nodeid]['nodeinfo']['software']['firmware']['release'] + if fwver in firmwareVersion: + firmwareVersion[fwver] += 1 + else: + firmwareVersion[fwver] = 1 + print '\tLevel:',level + for k,v in sorted(firmwareVersion.items(), key=operator.itemgetter(1), reverse = True): + print '\t\t '+k+':\t'+str(v) + print '-'*50 + + + + + def maxDepth(self, nodes): + maxDepth = 0 + for v in nodes.itervalues(): + if v['level'] > maxDepth: + maxDepth = v['level'] + return maxDepth+1 + + def __getFile__(self, nodesFile): + if nodesFile.startswith('https://') or nodesFile.startswith('http://'): + if self.printStatus: + print "Download node.json from URL: " + nodesFile + resource = urllib.urlopen(nodesFile) + else: + if self.printStatus: + print "Open node.json file: " + nodesFile + resource = open(nodesFile) + data = json.loads(resource.read()) + resource.close() + return data + +data = [ + { + 'data' : '../domaenensplit_webserver_config/muenster_sued_node_statistics.json', + 'name' : 'Münster Süd' + }, + { + 'data' : '../domaenensplit_webserver_config/muenster_stadt_node_statistics.json', + 'name' : 'Münster Stadt' + }, + { + 'data' : '../domaenensplit_webserver_config/kreis_coesfeld_node_statistics.json', + 'name' : 'Kreis Coesfeld' + }, + { + 'data' : '../domaenensplit_webserver_config/kreis_warendorf_node_statistics.json', + 'name' : 'Kreis Warendorf' + }, + { + 'data' : '../domaenensplit_webserver_config/kreis_steinfurt_ost_node_statistics.json', + 'name' : 'Kreis Steinfurt Ost' + }, + { + 'data' : '../domaenensplit_webserver_config/kreis_steinfurt_west_node_statistics.json', + 'name' : 'Kreis Steinfurt West' + }, + { + 'data' : '../domaenensplit_webserver_config/kreis_borken_node_statistics.json', + 'name' : 'Kreis Borken' + }, +] + +stat = TrackStatistics('nodes.json', data, printStatus = True) \ No newline at end of file