Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve current metric value from graphite

Tags:

graphite

Suppose I have a metric named a.b.c.count. I am trying to write a python script which reads the latest value of the metric a.b.c.count in graphite.

I went through the docs and figured out that we can use curl to retrieve metrics from graphite using functions http://graphite.readthedocs.org/en/0.9.13-pre1/functions.html.

But still unable to figure out how to achieve the same.

like image 966
user3351750 Avatar asked Sep 10 '15 06:09

user3351750


1 Answers

I haven't seen a way to ask Graphite for a single value, but you can ask for a summary of values over a configurable period, and take the last one. (This is just for minimizing the data returned, you could as easily pull out the last value from any series in a given timeframe.) Example render parameters:

target=summarize(a.b.c.count,'1hour','last')&from=-1h&format=json

The JSON returned will look like this:

[{"target": "summarize(a.b.c.count, \"1hour\", \"last\")", 
  "datapoints": [[5.1333330000000004, 1442160000], 
                 [5.5499989999999997, 1442163600]]}]

Here is a Python snippet to retrieve and parse this, using the 'requests' HTTP library

import requests
r = requests.get("http://graphite.yourdomain.com/render/?" +
                 "target=summarize(a.b.c.count,'1hour','last')&from=-1h&format=json")
print r.json()[0][u'datapoints'][-1][0]
like image 104
Jonathan Ross Avatar answered Oct 11 '22 23:10

Jonathan Ross