Extract current metric value from graphite

Suppose I have a metric named abccount . I am trying to write a python script that reads the last value of the abccount metric in graphite.

I looked through the documents and realized that we can use curl to extract metrics from graphite using the functions http://graphite.readthedocs.org/en/0.9.13-pre1/functions.html .

But still it is not possible to understand how to achieve the same.

+5
source share
1 answer

I have not seen a way to ask Graphite for a single value, but you can request a summary of the values ​​over a custom period and take the latter. (This is just to minimize the returned data, you can easily pull the last value from any series for a certain period of time.) Examples of visualization parameters:

 target=summarize(abccount,'1hour','last')&from=-1h&format=json 

The returned JSON will look like this:

 [{"target": "summarize(abccount, \"1hour\", \"last\")", "datapoints": [[5.1333330000000004, 1442160000], [5.5499989999999997, 1442163600]]}] 

Here is a Python snippet to extract and parse this using the HTTP request library

 import requests r = requests.get("http://graphite.yourdomain.com/render/?" + "target=summarize(abccount,'1hour','last')&from=-1h&format=json") print r.json()[0][u'datapoints'][-1][0] 
+4
source

All Articles