历史数据查询

任何push到open-falcon中的数据,事后都可以通过query组件提供的API,来查询得到。

查询历史数据

查询过去一段时间内的历史数据,使用接口 HTTP POST /graph/history。该接口不能查询最新上报的两个数据点。一个python例子,如下

  1. #-*- coding:utf8 -*-
  2. import requests
  3. import time
  4. import json
  5. end = int(time.time()) # 起始时间戳
  6. start = end - 3600 # 截至时间戳 (例子中为查询过去一个小时的数据)
  7. d = {
  8. "start": start,
  9. "end": end,
  10. "cf": "AVERAGE",
  11. "endpoint_counters": [
  12. {
  13. "endpoint": "host1",
  14. "counter": "cpu.idle",
  15. },
  16. {
  17. "endpoint": "host1",
  18. "counter": "load.1min",
  19. },
  20. ],
  21. }
  22. query_api = "http://127.0.0.1:9966/graph/history"
  23. r = requests.post(query_api, data=json.dumps(d))
  24. print r.text

其中,

  1. start: 要查询的历史数据起始时间点(为UNIX时间戳形式)
  2. end: 要查询的历史数据结束时间点(为UNIX时间戳形式)
  3. cf: 指定的采样方式,可以选择的有:AVERAGE、MAX、MIN
  4. endpoint_counters: 数组,其中每个元素为 endpoint和counter组成的键值对, 其中counter是由metric/sorted(tags)构成的,没有tags的话就是metric本身。
  5. query_api: query组件的监听地址 + api

查询最新上报的数据

查询最新上报的一个数据点,使用接口HTTP POST /graph/last。一个bash的例子,如下

  1. #!/bin/bash
  2. if [ $# != 2 ];then
  3. printf "format:./last \"endpoint\" \"counter\"\n"
  4. exit 1
  5. fi
  6. # args
  7. endpoint=$1
  8. counter=$2
  9. # form request body
  10. req="[{\"endpoint\":\"$endpoint\", \"counter\":\"$counter\"}]"
  11. # request
  12. url="http://127.0.0.1:9966/graph/last"
  13. curl -s -X POST -d "$req" "$url" | python -m json.tool