Python 2.x

Interesting Python Graphite Libraries

  • A Python module for sending metrics to Graphite over UDP
    https://github.com/derpston/python-graphiteudp - Charlie wrote this library, and we use it internally. It’s a good simple library for sending metrics over UDP in python.
  • Diamond - Send system metrics to Graphite
    Diamond - A really useful python daemon to send system metrics such as CPU usage, load, Disk IO etc.

Sending a metric via TCP

import socket

conn = socket.create_connection(("carbon.hostedgraphite.com", 2003))
conn.send("YOUR-API-KEY.foo 1.2\n")
conn.close()

   

Sending a metric via UDP

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto("YOUR-API-KEY.foo 1.2\n", ("carbon.hostedgraphite.com", 2003))

   

Sending a metric using HTTP POST

import urllib2, base64

url = "https://www.hostedgraphite.com/api/v1/sink"
api_key = "YOUR-API-KEY"

request = urllib2.Request(url, "foo 1.2")
request.add_header("Authorization", "Basic %s" % base64.encodestring(api_key).strip())
result = urllib2.urlopen(request)
 

Sending a metric via TCP over TLS tunnel

import socket, ssl

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sock = ssl.wrap_socket(s)
ssl_sock.connect(("carbon.hostedgraphite.com", 20030))
ssl_sock.write("YOUR-API-KEY.foo 1.2\n")