导语
在时序分析及监控展示领域,Grafana无疑是开源解决方案中的翘楚,其灵活的插件机制,支持各种漂亮的面板、丰富的数据源以及强大的应用。典型的面板有Graph、Text、Singlestat、PieChart、Table、Histogram等,支持的数据源有ES、Graphite、InfluxDB、OpenTSDB、MySQL、Druid 、Prometheus、SimpleJson等,提供的应用有Zabbix、K8s等。但是某公司在分布式集群中物理机监控的数据源为gRPC方式,即连接gRPC即可获取实时物理机监控信息,如CPU,内存,磁盘,负载等信息,这样就能使用后台转发gRPC的消息,这里使用SSM框架的java后台为grafana提供的SimpleJson数据源。
作者撰文时,网上几乎没有分享SimpleJson数据源的java后台API,为此分享给大家,初次分享,不便之处请见谅。
整体架构如下所示:
一、WebAPI
如果要支持SimpleJson,后端WebAPI需要实现4个URL:
/:返回200,用于SimpleJson数据源测试连通性;
/search:返回所有可选的指标;
/query:返回对应指标的时间序列点;
/annotations:返回注解。
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public Map ReturnTest(HttpServletResponse response){
response.setHeader("Access-Control-Allow-Headers", "accept, content-type");
response.setHeader("Access-Control-Allow-Methods", "POST");
response.setHeader("Access-Control-Allow-Origin", "*");
Map<String, Object> map = new HashMap<String, Object>();
map.put("result", "200 ok");
return map;
}
@RequestMapping(value = "/search", method = RequestMethod.POST)
@ResponseBody
public List Search(HttpServletResponse response) {
response.setHeader("Access-Control-Allow-Headers", "accept, content-type");
response.setHeader("Access-Control-Allow-Methods", "POST");
response.setHeader("Access-Control-Allow-Origin", "*");
List<String> result = new ArrayList<String>();
result.add("CPU");
result.add("RAM");
result.add("LOAD");
result.add("SWAP");
result.add("DISK");
result.add("NET");
return result;
}
``
```java
@RequestMapping(value = "/query", method = RequestMethod.POST)
@ResponseBody
public List Query(@RequestBody Map<String,Object> params, HttpServletResponse response) {
List<Map> targetList = (List) params.get("targets");
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>() ;
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(nodeMonitorService.getCpuMap(nodeIP));
}else if (target.equals("RAM")){
result.add(nodeMonitorService.getRamMap(nodeIP));
}else if (target.equals("LOAD")) {
result.add(nodeMonitorService.getLoadMap(nodeIP));
}else if (target.equals("SWAP")){
result.add(nodeMonitorService.getSwapMap(nodeIP));
}else if(target.equals("DISK")){
result = nodeMonitorService.getDiskList(nodeIP);
}else if (target.equals("NET")){
result = nodeMonitorService.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, new Comparator<Map<String, Object>>() {
public int compare(Map<String, Object> o1, Map<String, Object> 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() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("result", "200 ok");
return map;
}
注意header里面加上这3条:
response.setHeader("Access-Control-Allow-Headers", "accept, content-type");
response.setHeader("Access-Control-Allow-Methods", "POST");
response.setHeader("Access-Control-Allow-Origin", "*");
二、grafana配置
安装grafana,安装SimpleJson插件,在grafana官网,有详细说明。
启动访问,使用admin账户登录,
1、创建数据源:DataSources
type选择SimpleJson类型,URL填入后台服务的API地址,
2、创建面板dashboard
这个面板我已经分享到grafana网站了,可以前去下载:https://grafana.com/dashboards/5075
综上所述,基于SimpleJson数据源,只要配置数据源之后,按正确的方式添加API即可将数据灵活展现在Grafana中,当然SimpleJson只是一个数据源协议载体,理论上可以对接任何类型的后台数据,只要组装成它支持的格式即可。
网友评论