zabbix 获取监控指标数据api接口history.get源码分析

1、api_jsonrpc.php请求处理流程

1)header Content-Type验证,必须是[application/json-rpc,application/json, application/jsonrequest]其中一者,
不然会返回412状态码,HTTP/1.0 412 Precondition Failed

2)json参数解析,验证

3)根据参数method找相应的处理类、方法,比如history.get对应为CHistory类的get方法,调用history.get方法

4)返回结果json处理

2、获取指标数据history.get版本差异分析

1)获取指标数据常见参数为:

{
“params”: {
“history”: 0,
“hostids”: “10047”,
“time_from”: 1575422330,
“time_till”: 1575422430
},
“id”: 400978859,
“jsonrpc”: “2.0”,
“method”: “history.get”,
“auth”: “5ab76cf26626842cbae1132”
}
说明:获取10041主机的指标数据,时间范围为:[1575422330, 1575422430]

针对上述案例,zabbix api内部实现上,3.x版本和4.x版本截然不同。
4.x版本:可以使用history索引,整个调用 6秒左右
a)先根据hostids,得到itemids;
b)然后history单表查询,select * from history where itemids in() and clock<= and clock>=

3.x版本:不能使用history 索引,查询超慢,50秒左右
a)items、history连表查询,select h.* from history h, items i where …;

2)获取指标数据注意事项:

a)3.x版本,请指定itemids、时间范围;
b)4.x版本,请指定hostids或itemids、时间范围;

发布了294 篇原创文章 · 获赞 98 · 访问量 77万+

猜你喜欢

转载自blog.csdn.net/chuangxin/article/details/103438683