Created
July 9, 2015 16:40
-
-
Save pidpawel/7870b4f28572f3e3eea7 to your computer and use it in GitHub Desktop.
Supersimple proxy for graphite allowing to inject data without timestamp.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
from twisted.internet.protocol import Factory | |
from twisted.protocols.basic import LineReceiver | |
from twisted.internet import reactor | |
import socket | |
import time | |
class UplinkConnection: | |
def __init__(self, host, port): | |
self.host = host | |
self.port = port | |
self._socket = None | |
self._reconnect() | |
def _reconnect(self): | |
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self._socket.connect((self.host, self.port)) | |
def send(self, path, value, timestamp): | |
try: | |
send_string = "{path} {value} {timestamp}\n".format(path=path, value=value, timestamp=timestamp) | |
print(send_string) | |
self._socket.sendall(send_string) | |
except socket.error: | |
self._reconnect() | |
class Chat(LineReceiver): | |
def connectionMade(self): | |
print("New connection from {}".format(self)) | |
def lineReceived(self, line): | |
global uplink | |
try: | |
path, value = line.split(' ', 2) | |
timestamp = int(time.time()) | |
uplink.send(path, value, timestamp) | |
except ValueError: | |
pass | |
class ChatFactory(Factory): | |
def buildProtocol(self, addr): | |
return Chat() | |
if __name__ == "__main__": | |
uplink = UplinkConnection("127.0.0.1", 2003) | |
reactor.listenTCP(2005, ChatFactory()) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment