Skip to content

Instantly share code, notes, and snippets.

@hackprime
Last active November 27, 2015 04:24
Show Gist options
  • Save hackprime/3a2f5f37dd691e78c260 to your computer and use it in GitHub Desktop.
Save hackprime/3a2f5f37dd691e78c260 to your computer and use it in GitHub Desktop.
How to get your data into graphite.
import pickle
import struct
import time
import socket
CARBON_HOST = '192.168.99.100'
CARBON_PLAIN_DATA_PORT = 2003
CARBON_PICKLED_DATA_PORT = 2004
def send(host, port, message):
sock = socket.socket()
sock.connect((CARBON_HOST, port))
sock.sendall(message)
sock.close()
def prepare_pickled(data):
pickled = pickle.dumps(data, protocol=2)
header = struct.pack("!L", len(pickled))
return header + pickled
key = 'graphite.your.key.here'
value1 = 66
value2 = 42
timestamp1 = int(time.time())
timestamp2 = timestamp1 + 60
data_as_string = "%s %s %d\n" % (key, value1, timestamp1)
data = [(key, (timestamp1, value1)), (key, (timestamp2, value2))]
send(CARBON_HOST, CARBON_PLAIN_DATA_PORT, data_as_string)
send(CARBON_HOST, CARBON_PICKLED_DATA_PORT, prepare_pickled(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment