Search code examples
python-3.xgrafanagraphitegraphite-carbon

Get data into Graphite/Carbon using Python3


I've got a Grafana docker image running with Graphite/Carbon. Getting data using CLI works, example:

echo "local.random.diceroll $(((RANDOM%6)+1)) `date +%s`" | nc localhost 2003;

The following Python 2 code also works:

sock = socket.socket()
sock.connect((CARBON_SERVER, CARBON_PORT))
sock.sendall(message)
sock.close()

message is a string containing key value timestamp and this works, the data can be found. So the Grafana docker image is accepting data.

I wanted to get this working in Python 3, but the sendall function requires bytes as parameter. The code change is:

sock = socket.socket()
sock.connect((CARBON_SERVER, CARBON_PORT))
sock.sendall(str.encode(message))
sock.close()

Now the data isn't inserted and I can't figure out why. I tried this on a remote machine (same network) and on the local server. I also tried several packages (graphiti, graphiteudp), but they all seem to fail to insert the data. They also don't show any error message.

The simple example for graphiteudp doesn't work either on the Github page

Got an idea what I'm doing wrong?


Solution

  • You can add \n to the message you send. I have tried it with Python 3, and that works.