从石墨中检索当前指标值

Retrieve current metric value from graphite

假设我有一个名为 a.b.c.count 的指标。我正在尝试编写一个 python 脚本,该脚本读取石墨中指标 a.b.c.count 的最新值。

我浏览了文档,发现我们可以使用 curl 来使用函数 http://graphite.readthedocs.org/en/0.9.13-pre1/functions.html 从 Graphite 检索指标。

但仍然无法弄清楚如何实现相同的目标。

我还没有找到向 Graphite 请求单个值的方法,但您可以请求在可配置的时间段内汇总值,然后取最后一个。 (这只是为了最小化返回的数据,您可以轻松地从给定时间范围内的任何系列中提取最后一个值。)渲染参数示例:

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

返回的 JSON 将如下所示:

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

这是一个 Python 片段来检索和解析它,使用 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]