Grafana Json数据源

SimpleJson方式需要在Grafana引入simple-json-datasource的插件,官网【https://github.com/grafana/simple-json-datasource】下载好插件

放到/var/lib/grafana/plugins 目录下:

 

Grafana Json数据源_第1张图片

重启Grafana服务:

service grafana-server restart

 

SimpleJson 结构图:

Grafana Json数据源_第2张图片

 

如果要支持SimpleJson,后端WebAPI需要实现4个URL:

  1. /:返回200,用于SimpleJson数据源测试连通性;
  2. /search:返回所有可选的指标;
  3. /query:返回对应指标的时间序列点;
  4. /annotations:返回注解。

 

java 后端demo:

@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public Map ReturnTest(HttpServletResponse response) {
    log.info("g....../");
 response.setHeader("Access-Control-Allow-Headers", "accept, content-type");
 response.setHeader("Access-Control-Allow-Methods", "POST");
 response.setHeader("Access-Control-Allow-Origin", "*");

 Map map = new HashMap();
 map.put("result", "200 ok");
 return map;
}

@RequestMapping(value = "/search", method = RequestMethod.POST)
@ResponseBody
public List Search(HttpServletResponse response) {
    log.info("g....../search");
 response.setHeader("Access-Control-Allow-Headers", "accept, content-type");
 response.setHeader("Access-Control-Allow-Methods", "POST");
 response.setHeader("Access-Control-Allow-Origin", "*");

 List result = new ArrayList();
 result.add("CPU");
 result.add("RAM");
 result.add("LOAD");
 result.add("SWAP");
 result.add("DISK");
 result.add("NET");
 return result;
}

@RequestMapping(value = "/query", method = RequestMethod.POST)
@ResponseBody
public List Query(@RequestBody Map params, HttpServletResponse response) {
    log.info("g....../query");
 List targetList = (List) params.get("targets");
 List> result = new ArrayList<>();
 for (Map targetMap : targetList) {
        String target = (String) targetMap.get("target");
 Map scopedVars = (Map) params.get("scopedVars");
 Map IP = (Map) scopedVars.get("IP");
 String nodeIP = (String) IP.get("text");
 if (target.equals("CPU")) {
            result.add(testService.getCpuMap(nodeIP));
 } else if (target.equals("RAM")) {
            result.add(testService.getRamMap(nodeIP));
 } else if (target.equals("LOAD")) {
            result.add(testService.getLoadMap(nodeIP));
 } else if (target.equals("SWAP")) {
            result.add(testService.getSwapMap(nodeIP));
 } else if (target.equals("DISK")) {
            result = testService.getDiskList(nodeIP);
 } else if (target.equals("NET")) {
            result = testService.getNetList(nodeIP);
 }
    }
    response.setHeader("Access-Control-Allow-Headers", "accept, content-type");
 response.setHeader("Access-Control-Allow-Methods", "POST");
 response.setHeader("Access-Control-Allow-Origin", "*");
 Collections.sort(result, (o1, o2) -> {
        String name1 = String.valueOf(o1.get("target").toString());
 String name2 = String.valueOf(o2.get("target").toString());
 return name1.compareTo(name2);
 });
 return result;
}


@RequestMapping(value = "/annotations", method = RequestMethod.POST)
@ResponseBody
public Map Annotations() {
    log.info("g....../annotations");
 Map map = new HashMap<>();
 map.put("result", "200 ok");
 return map;
}

 

Grafana 数据源配置:

 

Grafana Json数据源_第3张图片

 

配置java 接口 / 服务地址  返回200ok 

Grafana Json数据源_第4张图片

 

 

根据后端获取 /query 接口数据并展示

Grafana Json数据源_第5张图片

 

最终结果如下:

 

Grafana Json数据源_第6张图片

 

综上所述,基于SimpleJson数据源,只要配置数据源之后,按正确的方式添加API即可将数据灵活展现在Grafana中,当然SimpleJson只是一个数据源协议载体,

理论上可以对接任何类型的后台数据,只要组装成它支持的格式即可。


 

 

 

 

你可能感兴趣的:(监控,Grafana)