{"id":435,"date":"2014-04-24T12:41:23","date_gmt":"2014-04-24T17:41:23","guid":{"rendered":"http:\/\/www.virtualroadside.com\/blog\/?p=435"},"modified":"2014-04-24T12:41:23","modified_gmt":"2014-04-24T17:41:23","slug":"downloading-and-uploading-dashboards-tofrom-a-graphite-server","status":"publish","type":"post","link":"http:\/\/www.virtualroadside.com\/blog\/index.php\/2014\/04\/24\/downloading-and-uploading-dashboards-tofrom-a-graphite-server\/","title":{"rendered":"Downloading and uploading dashboards to\/from a graphite server"},"content":{"rendered":"<p>I&#8217;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&#8217;s the code, use as you wish.<\/p>\n<pre>#!\/usr\/bin\/env python\r\n#\r\n# Author: Dustin Spicuzza\r\n# \r\n# Use this script to download\/upload dashboards to\/from a graphite server.\r\n#\r\n\r\nimport json\r\n\r\nimport sys\r\nimport urllib\r\nimport urllib2\r\n\r\n\r\ndownload_url = 'http:\/\/%s\/dashboard\/load\/%s'\r\nupload_url = 'http:\/\/%s\/dashboard\/save\/%s'\r\n\r\n\r\ndef download(ip, name, fname):\r\n    \r\n    uf = urllib2.urlopen('http:\/\/%s\/dashboard\/load\/%s' % (ip, name))\r\n\r\n    data = json.loads(urllib2.unquote(uf.read()))\r\n    data_str = json.dumps(data['state'], sort_keys=True, indent=4, separators=(',',': '))\r\n\r\n    if fname == None:\r\n        print data_str\r\n    else:\r\n        with open(fname, 'w') as fp:\r\n            fp.write(data_str)\r\n\r\n    return 0\r\n\r\n\r\ndef upload(ip, name, fname):\r\n\r\n    if fname is None:\r\n        data_str = sys.stdin.read()\r\n    else:\r\n        with open(fname, 'r') as fp:\r\n            data_str = fp.read()\r\n\r\n    data = json.loads(data_str)\r\n\r\n    if data['name'] != name:\r\n        print \"ERROR: dashboard name doesn't match name in state file\"\r\n        return 1\r\n\r\n    post_data = urllib.urlencode([('state', json.dumps(data))])\r\n\r\n    request = urllib2.Request(upload_url % (ip, name), post_data)\r\n    request.add_header(\"Content-type\", \"application\/x-www-form-urlencoded\")\r\n\r\n    print urllib2.urlopen(request).read()\r\n    return 0\r\n\r\n\r\ndef usage():\r\n    print \"Usage: dasher.py [upload graphite_host name filename] | [download graphite_host name [filename]]\"\r\n    exit(1)\r\n\r\nif __name__ == '__main__':\r\n\r\n    if len(sys.argv) < 4:\r\n        usage()\r\n\r\n    action = sys.argv[1]\r\n    ip = sys.argv[2]\r\n    name = sys.argv[3]\r\n    fname = None\r\n\r\n    if len(sys.argv) > 4:\r\n        fname = sys.argv[4]\r\n\r\n    if action == 'upload':\r\n        retval = upload(ip, name, fname)\r\n\r\n    elif action == 'download':\r\n        retval = download(ip, name, fname)\r\n\r\n    else:\r\n        usage()\r\n\r\n    exit(retval)\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;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&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54,45,55],"tags":[],"_links":{"self":[{"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/435"}],"collection":[{"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=435"}],"version-history":[{"count":1,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/435\/revisions"}],"predecessor-version":[{"id":436,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/435\/revisions\/436"}],"wp:attachment":[{"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=435"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}