@app.route("/chart/a", methods=["GET"])
def multi_chart_data():
if not g.id:
abort(400, "no graph id given")
//使用id来查询tmp_graph表中对应的counters和endpoints的信息
j = TmpGraph.get(g.id)
if not j:
abort(400, "no such tmp_graph where id=%s" %g.id)
counters = j.counters
if not counters:
abort(400, "no counters of %s" %g.id)
counters = sorted(set(counters))
endpoints = j.endpoints
if not endpoints:
abort(400, "no endpoints of %s, and tags:%s" %(g.id, g.tags))
endpoints = sorted(set(endpoints))
ret = {
"units": "",
"title": "",
"series": []
}
//调用open-falcon的/graph/history的接口来获取监控数据
query_result = graph_history(endpoints, counters, g.cf, g.start, g.end)
我们看一下open-falcon的/graph/history接口是怎样实现的:
func QueryGraphDrawData(c *gin.Context) {
//省略了获取参数和部分校验工作
...
respData := []*cmodel.GraphQueryResponse{}
for _, host := range inputs.HostNames {
for _, counter := range inputs.Counters {
var step int
if inputs.Step > 0 {
step = inputs.Step
} else {
step, err = getCounterStep(host, counter)
if err != nil {
continue
}
}
data, _ := fetchData(host, counter, inputs.ConsolFun, inputs.StartTime, inputs.EndTime, step)
respData = append(respData, data)
}
}
h.JSONR(c, respData)
}
进入open-falcon的dashboard界面时,页面的左侧展示了目前纳入监控的主机的列表,默认只展示50条,可以通过主机名查找和主机关联标签查找的方法找到你关注的host列表:
涉及到的API接口: URL: http://10.202.42.2:8081/api/endpoints?q=gs-server&tags=&limit=50&page=1&_r=0.6087057948380055 方法: GET 接口:http://ip:port/api/endpoints 参数:q=gs-server tags=空
对应的Python代码
可以看到,api_endpoints这个函数从请求中提取出主机名的查找关键字,tags以及分页的信息后,调用了get_api_endpoints方法来获取endpoints(即主机)列表。
从这段代码中可以看出dashboard模块解析用户的请求后,调用了open-falcon的api接口/graph/endpoints来获取endpoints列表,dashboard没有对数据库的查询操作。
选择主机,点击刷新counter列表,可以查询出这些主机,对应的接口为 URL:http://10.202.42.2:8081/api/counters 方法:POST 参数: _r 0.4806078226590633 eids ["349","1"] limit 50 page 1 q
对应的Python代码:
dashboard模块依旧是调用了open-falcon的API接口获取监控项的列表,下面我们来看看这个接口的实现
可以看到/graph/endpoint_counter这个API返回了所有和选中的endpoint的关联的所有监控项的列表,列表中的每一项是map类型的,包含了监控项的类型,统计频率,名字,关联的主机id等信息。
点击页面右上角的查看图表的按钮,会链接到数据展示的界面
对应的接口为: URL:http://10.202.42.2:8081/chart 方法:POST 参数: _r 0.6320655494664266 counters[] […] 0 cpu.busy 1 cpu.guest 2 cpu.idle endpoints[] […] 0 gs-server-3238 1 gs-server-4171 graph_type k
对应的Python实现
可以看到这个函数调用了TmpGraph.add(endpoints, counters)的方法,并得到了返回值id_,下面我们来看看TmpGraph.add方法具体做了哪些事情
可以看到TmpGraph.add方法向open-falcon的/dashboard/tmpgraph这个api发送了请求,下面我们来看看 /dashboard/tmpgraph这个api的实现:
分别把endpoints列表和counters列表拼成字符串,然后计算出相应的md5值,插入到tmp_graph中,得到对应的数据库的id,返回给前端,然后前端再利用这个id来发送下面的请求。
URL:http://10.202.42.2:8081/chart/a?cf=AVERAGE&end=&graph_type=a&id=69&start=&sum=off 方法:GET
对应Python的接口:
我们看一下open-falcon的/graph/history接口是怎样实现的:
使用了两个for循环,针对每个endpoint的每个counter,找到监控项对应的采集频率,然后执行fetchData的操作,可以看到open-falcon的/graph/history这个API返回的是cmodel.GraphQueryResponse类型的指针的切片,接下来我们看看dashboard模块是怎样处理返回的数据的。
在Dashboard图表的界面上,我们可以看到支持endpoint(主机)视角,counter(监控项)视角和组合视角:
对应的代码逻辑: