Archive for April, 2014

Downloading and uploading dashboards to/from a graphite server

Thursday, April 24th, 2014

I’ve been using graphite and statsd lately, and over lunch quickly whipped up this script to download/upload dashboard configurations to/from a graphite server. Here’s the code, use as you wish.

#!/usr/bin/env python
# Author: Dustin Spicuzza
# Use this script to download/upload dashboards to/from a graphite server.

import json

import sys
import urllib
import urllib2

download_url = 'http://%s/dashboard/load/%s'
upload_url = 'http://%s/dashboard/save/%s'

def download(ip, name, fname):
    uf = urllib2.urlopen('http://%s/dashboard/load/%s' % (ip, name))

    data = json.loads(urllib2.unquote(
    data_str = json.dumps(data['state'], sort_keys=True, indent=4, separators=(',',': '))

    if fname == None:
        print data_str
        with open(fname, 'w') as fp:

    return 0

def upload(ip, name, fname):

    if fname is None:
        data_str =
        with open(fname, 'r') as fp:
            data_str =

    data = json.loads(data_str)

    if data['name'] != name:
        print "ERROR: dashboard name doesn't match name in state file"
        return 1

    post_data = urllib.urlencode([('state', json.dumps(data))])

    request = urllib2.Request(upload_url % (ip, name), post_data)
    request.add_header("Content-type", "application/x-www-form-urlencoded")

    print urllib2.urlopen(request).read()
    return 0

def usage():
    print "Usage: [upload graphite_host name filename] | [download graphite_host name [filename]]"

if __name__ == '__main__':

    if len(sys.argv) < 4:

    action = sys.argv[1]
    ip = sys.argv[2]
    name = sys.argv[3]
    fname = None

    if len(sys.argv) > 4:
        fname = sys.argv[4]

    if action == 'upload':
        retval = upload(ip, name, fname)

    elif action == 'download':
        retval = download(ip, name, fname)