summaryrefslogtreecommitdiffstats
path: root/retiolum/scripts/adv_graphgen/tinc_stats/Availability.py
diff options
context:
space:
mode:
authorroot <root@pigstarter.retiolum>2013-06-24 17:24:49 +0200
committerroot <root@pigstarter.retiolum>2013-06-24 17:24:49 +0200
commitfe1f4dec340c121e43d0fca6d1139db694b2c48b (patch)
treef8c91b480fef621ef960ee11d2fd9cafb59cfb41 /retiolum/scripts/adv_graphgen/tinc_stats/Availability.py
parentd5751b4a5132522b38c611e6bfd79537e927db50 (diff)
refactor tinc_stats into a single python module
Diffstat (limited to 'retiolum/scripts/adv_graphgen/tinc_stats/Availability.py')
-rwxr-xr-xretiolum/scripts/adv_graphgen/tinc_stats/Availability.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/retiolum/scripts/adv_graphgen/tinc_stats/Availability.py b/retiolum/scripts/adv_graphgen/tinc_stats/Availability.py
new file mode 100755
index 00000000..a1ef13f1
--- /dev/null
+++ b/retiolum/scripts/adv_graphgen/tinc_stats/Availability.py
@@ -0,0 +1,58 @@
+#!/usr/bin/python
+# -*- coding: utf8 -*-
+
+import sys,json
+""" TODO: Refactoring needed to pull the edges out of the node structures again,
+it should be easier to handle both structures"""
+DUMP_FILE = "/krebs/db/availability"
+
+def get_all_nodes():
+ import os
+ return os.listdir("/etc/tinc/retiolum/hosts")
+
+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,e:
+ pass
+ 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()