From 33483e844e508a4b256ab06d9096d1e7f580643b Mon Sep 17 00:00:00 2001 From: root Date: Mon, 24 Jun 2013 18:04:25 +0200 Subject: refactor Graphite sender --- retiolum/scripts/adv_graphgen/parse_tinc_anon.py | 26 ++++-------- retiolum/scripts/adv_graphgen/parse_tinc_stats.py | 49 +++++++++------------- .../scripts/adv_graphgen/tinc_stats/Graphite.py | 16 +++++++ 3 files changed, 42 insertions(+), 49 deletions(-) create mode 100644 retiolum/scripts/adv_graphgen/tinc_stats/Graphite.py (limited to 'retiolum') diff --git a/retiolum/scripts/adv_graphgen/parse_tinc_anon.py b/retiolum/scripts/adv_graphgen/parse_tinc_anon.py index 9041fb35..05b82828 100755 --- a/retiolum/scripts/adv_graphgen/parse_tinc_anon.py +++ b/retiolum/scripts/adv_graphgen/parse_tinc_anon.py @@ -1,25 +1,15 @@ #!/usr/bin/python # -*- coding: utf8 -*- -from tinc_stats.BackwardsReader import BackwardsReader -from tinc_stats.Graph import generate_stats,delete_unused_nodes,merge_edges -import sys,json -#supernodes= [ "kaah","supernode","euer","pa_sharepoint","oxberg" ] +from tinc_stats.Graphite import GraphiteSender +from time import time + try: - import socket - from time import time import os - host = os.environ.get("GRAPHITE_HOST","localhost") - port = 2003 - g_path = os.environ.get("GRAPHITE_PATH","retiolum") + gr = GraphiteSender(os.environ.get("GRAPHITE_HOST","localhost")) begin = time() - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sys.stderr.write("connecting to %s:%d"%(host,port)) - s.connect((host,port)) except Exception as e: sys.stderr.write( "Cannot connect to graphite: " + str(e)) -""" 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 write_digraph(nodes): """ @@ -94,12 +84,10 @@ def write_node(k,v): edge += ",dir=both" edge += "]" print edge + if __name__ == "__main__": nodes = delete_unused_nodes(json.load(sys.stdin)) write_digraph(nodes) try: - end = time() - msg = '%s.graph.anon_build_time %d %d\r\n' % (g_path,((end-begin)*1000),end) - s.send(msg) - s.close() + gr.send("graph.anon_build_time",(time()-begin)*1000) except Exception as e: pass diff --git a/retiolum/scripts/adv_graphgen/parse_tinc_stats.py b/retiolum/scripts/adv_graphgen/parse_tinc_stats.py index 4383b356..c23e348d 100755 --- a/retiolum/scripts/adv_graphgen/parse_tinc_stats.py +++ b/retiolum/scripts/adv_graphgen/parse_tinc_stats.py @@ -5,25 +5,18 @@ from tinc_stats.Graph import generate_stats,delete_unused_nodes,merge_edges,get_ from tinc_stats.Supernodes import check_all_the_super from tinc_stats.Availability import get_node_availability import sys,json +from tinc_stats.Graphite import GraphiteSender +from time import time + try: - from time import time - import socket import os - host = os.environ.get("GRAPHITE_HOST","localhost") - port = 2003 - g_path = os.environ.get("GRAPHITE_PATH","retiolum") + gr = GraphiteSender(os.environ.get("GRAPHITE_HOST","localhost")) begin = time() - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sys.stderr.write("connecting to %s:%d"%(host,port)) - s.connect((host,port)) except Exception as e: - sys.stderr.write("Cannot connect to graphite: %s\n" % str(e)) + sys.stderr.write( "Cannot connect to graphite: " + str(e)) supernodes= [ ] -for supernode,addr in check_all_the_super(): - supernodes.append(supernode) -""" 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 write_digraph(nodes): @@ -149,20 +142,16 @@ def write_node(k,v): edge += "]" print edge -def decode_input(FILE): - return json.load(FILE) -nodes = decode_input(sys.stdin) -nodes = delete_unused_nodes(nodes) -try: - dump_graph(nodes) -except Exception,e: - sys.stderr.write("Cannot dump graph: %s" % str(e)) -write_digraph(nodes) - -try: - end = time() - msg = '%s.graph.detail_build_time %d %d\r\n' % (g_path,((end-begin)*1000),end) - s.send(msg) - sys.stderr.write(msg) - s.close() -except Exception as e: sys.stderr.write( str(e) + "\n") +if __name__ == "__main__": + nodes = json.load(sys.stdin) + nodes = delete_unused_nodes(nodes) + for supernode,addr in check_all_the_super(): + supernodes.append(supernode) + try: + dump_graph(nodes) + except Exception,e: + sys.stderr.write("Cannot dump graph: %s" % str(e)) + write_digraph(nodes) + try: + gr.send("graph.anon_build_time",(time()-begin)*1000) + except Exception as e: pass diff --git a/retiolum/scripts/adv_graphgen/tinc_stats/Graphite.py b/retiolum/scripts/adv_graphgen/tinc_stats/Graphite.py new file mode 100644 index 00000000..ee01a02b --- /dev/null +++ b/retiolum/scripts/adv_graphgen/tinc_stats/Graphite.py @@ -0,0 +1,16 @@ +#!/usr/bin/python + +import socket +from time import time + +class GraphiteSender: + def __init__(self,host,port=2003,prefix="retiolum"): + self.host = host + self.port = port + self.prefix = prefix + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.sock.connect((host,port)) + + def send(name,data): + from time import time + self.sock.send("%s.%s %d %d\r\n"%self.prefix,data,time()) -- cgit v1.2.3