From 05bf2c8be209e619f8c9e727fd1353198b042642 Mon Sep 17 00:00:00 2001 From: makefu Date: Wed, 9 Sep 2015 14:41:38 +0200 Subject: begin packaging of tinc_stats --- .../adv_graphgen/tinc_graphs/Availability.py | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 retiolum/scripts/adv_graphgen/tinc_graphs/Availability.py (limited to 'retiolum/scripts/adv_graphgen/tinc_graphs/Availability.py') diff --git a/retiolum/scripts/adv_graphgen/tinc_graphs/Availability.py b/retiolum/scripts/adv_graphgen/tinc_graphs/Availability.py new file mode 100755 index 00000000..42f28ef9 --- /dev/null +++ b/retiolum/scripts/adv_graphgen/tinc_graphs/Availability.py @@ -0,0 +1,60 @@ +#!/usr/bin/python +# -*- coding: utf8 -*- + +import sys,json,os +""" TODO: Refactoring needed to pull the edges out of the node structures again, +it should be easier to handle both structures""" +DUMP_FILE = os.environment.get("AVAILABILITY_FILE","tinc-availability.json") +hostpath=os.environment.get("TINC_HOSTPATH", "/etc/tinc/retiolum/hosts") + +def get_all_nodes(): + return os.listdir(hostpath) + +def generate_stats(): + """ Generates availability statistics of the network and nodes + """ + import json + jlines = [] + try: + f = open(DUMP_FILE,'r') + for line in f: + jlines.append(json.loads(line)) + f.close() + except Exception as e: + print("Unable to open and parse Availability DB: {} (override with AVAILABILITY_FILE)".format(DUMP_FILE) + sys.exit(1) + + all_nodes = {} + for k in get_all_nodes(): + all_nodes[k] = get_node_availability(k,jlines) + print (json.dumps(all_nodes)) + +def get_node_availability(name,jlines): + """ calculates the node availability by reading the generated dump file + adding together the uptime of the node and returning the time + parms: + name - node name + jlines - list of already parsed dictionaries node archive + """ + begin = last = current = 0 + uptime = 0 + for stat in jlines: + if not stat['nodes']: + continue + ts = stat['timestamp'] + if not begin: + begin = last = ts + current = ts + if stat['nodes'].get(name,{}).get('to',[]): + uptime += current - last + else: + pass + last = ts + all_the_time = last - begin + try: + return uptime/ all_the_time + except: + return 1 + +if __name__ == "__main__": + generate_stats() -- cgit v1.2.3 From 219fab970c7fe455d3dd9bc48e909d96a234046b Mon Sep 17 00:00:00 2001 From: makefu Date: Thu, 10 Sep 2015 15:48:38 +0200 Subject: adv_graphgen: finish packaging --- retiolum/scripts/adv_graphgen/tinc_graphs/Availability.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'retiolum/scripts/adv_graphgen/tinc_graphs/Availability.py') diff --git a/retiolum/scripts/adv_graphgen/tinc_graphs/Availability.py b/retiolum/scripts/adv_graphgen/tinc_graphs/Availability.py index 42f28ef9..888335a7 100755 --- a/retiolum/scripts/adv_graphgen/tinc_graphs/Availability.py +++ b/retiolum/scripts/adv_graphgen/tinc_graphs/Availability.py @@ -1,11 +1,12 @@ #!/usr/bin/python +# TODO: Rewrite this shitty piece of software ... # -*- coding: utf8 -*- import sys,json,os """ TODO: Refactoring needed to pull the edges out of the node structures again, it should be easier to handle both structures""" -DUMP_FILE = os.environment.get("AVAILABILITY_FILE","tinc-availability.json") -hostpath=os.environment.get("TINC_HOSTPATH", "/etc/tinc/retiolum/hosts") +DUMP_FILE = os.environ.get("AVAILABILITY_FILE","tinc-availability.json") +hostpath=os.environ.get("TINC_HOSTPATH", "/etc/tinc/retiolum/hosts") def get_all_nodes(): return os.listdir(hostpath) @@ -16,12 +17,12 @@ def generate_stats(): import json jlines = [] try: - f = open(DUMP_FILE,'r') + f = open(DUMP_FILE,'r+') for line in f: jlines.append(json.loads(line)) f.close() except Exception as e: - print("Unable to open and parse Availability DB: {} (override with AVAILABILITY_FILE)".format(DUMP_FILE) + print("Unable to open and parse Availability DB: {} (override with AVAILABILITY_FILE)".format(DUMP_FILE)) sys.exit(1) all_nodes = {} -- cgit v1.2.3