Revert "prometheus监控"

pull/65/head
fengshuonan 4 years ago
parent 2f31a40722
commit 15cd3273be

@ -192,10 +192,6 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>

@ -1,42 +0,0 @@
package cn.stylefeng.guns.config.web;
import cn.stylefeng.roses.kernel.monitor.api.PrometheusApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
/**
* prometheus
*
* @author chenli
* @date 2021/3/3 17:14
*/
@Configuration
@Slf4j
public class PrometheusConfiguration {
@Value("${prometheus.enabled}")
private boolean prometheusEnabled;
@Resource
private PrometheusApi prometheusApi;
/***
* prometheus
*
* @author chenli
* @date 2021/3/3 17:14
*/
@Bean
public void configPrometheusMenu(){
if(prometheusEnabled){
prometheusApi.displayPrometheusMenu();
} else {
prometheusApi.closePrometheusMenu();
}
}
}

@ -1,26 +1,12 @@
package cn.stylefeng.guns.modular.system.monitor;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.core.consts.ProjectConstants;
import cn.stylefeng.roses.kernel.monitor.api.PrometheusApi;
import cn.stylefeng.roses.kernel.monitor.api.pojo.prometheus.PromResultInfo;
import cn.stylefeng.roses.kernel.monitor.prometheus.service.PrometheusService;
import cn.stylefeng.roses.kernel.monitor.system.holder.SystemHardwareInfoHolder;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
*
@ -35,21 +21,6 @@ public class MonitorController {
@Resource
private SystemHardwareInfoHolder systemHardwareInfoHolder;
@Value("${server.port}")
private String port;
@Value("${spring.application.name}")
private String name;
@Value("${prometheus.url}")
private String prometheusUrl;
@Value("${prometheus.instance}")
private String prometheusInstance;
@Resource
private PrometheusApi service;
/**
*
*
@ -73,228 +44,4 @@ public class MonitorController {
return "/modular/system/monitor/druid.html";
}
/**
* tomcat
*
* @author chenli
* @date 2021/1/4 16:32
*/
@GetResource(name = "tomcat监控首页", path = "/view/monitor/tomcatInfo")
public String tomcatIndex() {
return "/modular/system/monitor/tomcatInfo.html";
}
/**
* tomcat
*
* @author chenli
* @date 2021/1/4 16:32
*/
@GetResource(name = "tomcat监控数据", path = "/view/monitor/getTomcatInfo")
@ResponseBody
public String tomcatInfo() {
Map<String,Object> metricMap = getMetricInfos(getPromQl(),"tomcat_","","");
return JSON.toJSONString(metricMap);
}
/**
* jvm
*
* @author chenli
* @date 2021/1/4 16:32
*/
@GetResource(name = "jvm监控页面", path = "/view/monitor/jvmInfo")
public String jvmIndex() {
return "/modular/system/monitor/jvmInfo.html";
}
/**
* jvm
*
* @author chenli
* @date 2021/1/4 16:32
*/
@GetResource(name = "jvm监控数据", path = "/view/monitor/getJvmInfo")
@ResponseBody
public String jvmInfo(@Param("id") String id, @Param("area") String area) {
Map<String,Object> metricMap = getMetricInfos(getPromQl(id,area),"jvm_","","");
return JSON.toJSONString(metricMap);
}
/**
*
*
* @author chenli
* @date 2021/1/4 16:32
*/
@GetResource(name = "性能监控页面", path = "/view/monitor/performanceInfo")
public String performanceIndex() {
return "/modular/system/monitor/performanceInfo.html";
}
/**
*
*
* @author chenli
* @date 2021/1/4 16:32
*/
@GetResource(name = "CPU监控数据", path = "/view/monitor/getCpuInfo")
@ResponseBody
public String cpuInfo() {
Map<String,Object> metricMap = getMetricInfos(getPromQl(),"cpu_","","");
return JSON.toJSONString(metricMap);
}
/**
*
*
* @author chenli
* @date 2021/1/4 16:32
*/
@GetResource(name = "服务器负载监控数据", path = "/view/monitor/getLoadInfo")
@ResponseBody
public String loadInfo() {
Map<String,Object> metricMap = getMetricInfos(getPromQl(),"system_","","");
return JSON.toJSONString(metricMap);
}
/**
*
*
* @author chenli
* @date 2021/1/4 16:32
*/
@GetResource(name = "进程监控数据", path = "/view/monitor/getProcessInfo")
@ResponseBody
public String processInfo() {
Map<String,Object> metricMap = getMetricInfos(getPromQl(),"process_","","");
return JSON.toJSONString(metricMap);
}
/**
*
*
* @author chenli
* @date 2021/1/4 16:32
*/
@GetResource(name = "日志监控页面", path = "/view/monitor/logbackInfo")
public String logbackIndex() {
return "/modular/system/monitor/logbackInfo.html";
}
/**
*
*
* @author chenli
* @date 2021/1/4 16:32
*/
@GetResource(name = "日志监控数据", path = "/view/monitor/getLogbackInfo")
@ResponseBody
public String logbackInfo(@Param("level") String level, @Param("timeInterval") String timeInterval, @Param("isRate") String isRate, @Param("rateMetric") String rateMetric) {
if(StrUtil.isEmpty(timeInterval)){
timeInterval = "[5m]";
}
Map<String,Object> metricMap = getMetricInfos(getIratePromQl(level, timeInterval),"logback_",isRate,rateMetric);
return JSON.toJSONString(metricMap);
}
/**
* prometheussql
*
* @param id id
* @param area jvm
* @author chenli
* @date 2021/1/4 16:32
*/
private String getPromQl(String id,String area){
StringBuilder promql = new StringBuilder("{application=\"");
promql.append(name);
if(!StrUtil.isEmpty(id)){
promql.append("\",id=\"");
promql.append(id);
}
if(!StrUtil.isEmpty(area)){
promql.append("\",area=\"");
promql.append(area);
}
if(!StrUtil.isEmpty(prometheusInstance)){
promql.append("\",instance=\"");
promql.append(prometheusInstance);
}
promql.append("\"}");
return promql.toString();
}
/**
* prometheussql
*
* @author chenli
* @date 2021/1/4 16:32
*/
private String getPromQl(){
StringBuilder promql = new StringBuilder("{application=\"");
promql.append(name);
if(!StrUtil.isEmpty(prometheusInstance)){
promql.append("\",instance=\"");
promql.append(prometheusInstance);
}
promql.append("\"}");
return promql.toString();
}
/**
* prometheussql
*
* @param level infowarnerrortracedebug
* @param timeInterval (m)
* @author chenli
* @date 2021/1/4 16:32
*/
private String getIratePromQl(String level,String timeInterval){
StringBuilder promql = new StringBuilder("{application=\"");
promql.append(name);
if(!StrUtil.isEmpty(prometheusInstance)){
promql.append("\",instance=\"");
promql.append(prometheusInstance);
}
if(!StrUtil.isEmpty(level)){
promql.append("\",level=\"");
promql.append(level);
}
promql.append("\"}");
promql.append(timeInterval);
return promql.toString();
}
/**
*
*
* @param promQL prometheussql
* @param metric prometheus"jvm_"
* @param isRate prometheus"",
* @author chenli
* @date 2021/1/4 16:32
*/
private Map<String,Object> getMetricInfos(String promQL, String metric,String isRate, String rateMetric) {
Map<String,Object> metricMap = new HashMap<>();
if (!StrUtil.isEmpty(prometheusUrl)) {
List<PromResultInfo> promResultInfos = service.getMetricInfo(prometheusUrl.concat(ProjectConstants.PROMETHEUS_QUERY_RANGE), promQL,isRate,rateMetric);
if(Objects.isNull(promResultInfos)){
return metricMap;
}
for (PromResultInfo promResultInfo : promResultInfos) {
String metricName = promResultInfo.getMetric().get__name__();
JSONArray valueArray = JSONArray.parseArray(JSON.toJSONString(promResultInfo.getValues()).replaceAll("\\\\","").replace("\"", ""));
if (!StrUtil.isEmpty(metricName)) {
if (metricName.contains(metric)) {
// 得到的数据为数组需要转为json字符串去除双引号再转化为JSONArrayJSONArray是echarts时间序列图需要的数据格式
metricMap.put(metricName, valueArray);
}
} else {
// 查询指定的指标
metricMap.put(rateMetric,valueArray);
}
}
}
return metricMap;
}
}

@ -53,23 +53,3 @@ mybatis-plus:
# 是否开启websocket推送消息
web-socket:
open: true
# prometheus监控
management:
endpoints:
web:
exposure:
include: "*"
metrics:
tags:
application: ${spring.application.name}
export:
prometheus:
enabled: true
jmx:
enabled: true
prometheus:
enabled: false
url: http://localhost:9090/api/v1/
# 非必须配置项
instance:
Loading…
Cancel
Save