mirror of https://gitee.com/stylefeng/guns
整理监控代码
parent
77e3694add
commit
522a132750
12
pom.xml
12
pom.xml
|
@ -161,28 +161,18 @@
|
|||
<version>${oshi.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.1.10</version>
|
||||
</dependency>
|
||||
|
||||
<!-- actuator monitor -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- prometheus meter -->
|
||||
<dependency>
|
||||
<groupId>io.micrometer</groupId>
|
||||
<artifactId>micrometer-registry-prometheus</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.11</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -1,13 +1,9 @@
|
|||
package cn.stylefeng.guns;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* SpringBoot方式启动类
|
||||
|
@ -23,10 +19,6 @@ public class GunsApplication {
|
|||
SpringApplication.run(GunsApplication.class, args);
|
||||
log.info(GunsApplication.class.getSimpleName() + " is success!");
|
||||
}
|
||||
@Bean
|
||||
MeterRegistryCustomizer<MeterRegistry> configurer(
|
||||
@Value("${spring.application.name}") String applicationName) {
|
||||
return (registry) -> registry.config().commonTags("application", applicationName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package cn.stylefeng.guns.config;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 监控埋点配置
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/1/21 22:07
|
||||
*/
|
||||
@Configuration
|
||||
public class ActuatorConfiguration {
|
||||
|
||||
/**
|
||||
* 应用名称
|
||||
*/
|
||||
@Value("${spring.application.name}")
|
||||
private String applicationName;
|
||||
|
||||
@Bean
|
||||
public MeterRegistryCustomizer<MeterRegistry> configurer() {
|
||||
return (registry) -> registry.config().commonTags("application", applicationName);
|
||||
}
|
||||
|
||||
}
|
|
@ -44,6 +44,7 @@ public interface ProjectConstants {
|
|||
* prometheus查询区间向量命令
|
||||
*/
|
||||
String PROMETHEUS_QUERY_RANGE = "query_range";
|
||||
|
||||
/**
|
||||
* prometheus查询开始时间
|
||||
*/
|
||||
|
@ -58,4 +59,5 @@ public interface ProjectConstants {
|
|||
* prometheus查询步长
|
||||
*/
|
||||
String PROMETHEUS_STEP = "step";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,21 +1,19 @@
|
|||
package cn.stylefeng.guns.modular.system.controller;
|
||||
|
||||
import cn.hutool.core.util.EscapeUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.stylefeng.guns.core.consts.ProjectConstants;
|
||||
import cn.stylefeng.guns.modular.system.model.PromResultInfo;
|
||||
import cn.stylefeng.guns.modular.system.service.MetricService;
|
||||
import cn.stylefeng.guns.modular.system.warpper.SystemHardwareWarpper;
|
||||
import cn.stylefeng.guns.modular.system.warpper.SystemHardwareWrapper;
|
||||
import cn.stylefeng.roses.kernel.resource.api.annotation.ApiResource;
|
||||
import cn.stylefeng.roses.kernel.resource.api.annotation.GetResource;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
|
@ -23,15 +21,12 @@ import java.util.Objects;
|
|||
* 项目监控
|
||||
*
|
||||
* @author chenli
|
||||
* @Date 2020/12/30 16:40
|
||||
* @date 2020/12/30 16:40
|
||||
*/
|
||||
@Controller
|
||||
@Slf4j
|
||||
@ApiResource(name = "项目监控")
|
||||
public class MonitorController {
|
||||
|
||||
private String PREFIX = "/modular/frame";
|
||||
|
||||
@Value("${server.port}")
|
||||
private String port;
|
||||
|
||||
|
@ -44,83 +39,84 @@ public class MonitorController {
|
|||
@Value("${prometheus.instance}")
|
||||
private String prometheusInstance;
|
||||
|
||||
@Autowired
|
||||
@Resource
|
||||
private MetricService service;
|
||||
|
||||
/**
|
||||
* 系统硬件信息页面
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @Date 2018/12/24 22:43
|
||||
* @date 2018/12/24 22:43
|
||||
*/
|
||||
@GetResource(name = "服务器监控", path = "/monitor/systemInfo")
|
||||
@GetResource(name = "服务器监控", path = "/view/monitor/systemInfo")
|
||||
public String systemInfo(Model model) {
|
||||
SystemHardwareWarpper systemHardwareWarpper = new SystemHardwareWarpper();
|
||||
systemHardwareWarpper.copyTo();
|
||||
model.addAttribute("server",systemHardwareWarpper);
|
||||
return PREFIX+"/systemInfo.html";
|
||||
SystemHardwareWrapper systemHardwareWrapper = new SystemHardwareWrapper();
|
||||
systemHardwareWrapper.copyTo();
|
||||
model.addAttribute("server", systemHardwareWrapper);
|
||||
return "/modular/frame/systemInfo.html";
|
||||
}
|
||||
|
||||
/**
|
||||
* durid sql监控页面
|
||||
* druid sql监控页面
|
||||
*
|
||||
* @author chenli
|
||||
* @Date 2021/1/4 16:32
|
||||
* @date 2021/1/4 16:32
|
||||
*/
|
||||
@GetResource(name = "SQL监控", path = "/monitor/druid")
|
||||
public String duridInfo(Model model){
|
||||
model.addAttribute("port",port);
|
||||
return PREFIX+"/druid.html";
|
||||
@GetResource(name = "SQL监控", path = "/view/monitor/druid")
|
||||
public String druidInfo(Model model) {
|
||||
model.addAttribute("port", port);
|
||||
return "/modular/frame/druid.html";
|
||||
}
|
||||
|
||||
/**
|
||||
* tomcat监控页面
|
||||
*
|
||||
* @author chenli
|
||||
* @Date 2021/1/4 16:32
|
||||
* @date 2021/1/4 16:32
|
||||
*/
|
||||
@GetResource(name = "tomcat监控", path = "/monitor/tomcatInfo")
|
||||
public String tomcatInfo(Model model){
|
||||
getMetricInfos("tomcat_",model);
|
||||
return PREFIX+"/tomcatInfo.html";
|
||||
@GetResource(name = "tomcat监控", path = "/view/monitor/tomcatInfo")
|
||||
public String tomcatInfo(Model model) {
|
||||
getMetricInfos("tomcat_", model);
|
||||
return "/modular/frame/tomcatInfo.html";
|
||||
}
|
||||
|
||||
/**
|
||||
* jvm监控
|
||||
*
|
||||
* @author chenli
|
||||
* @Date 2021/1/4 16:32
|
||||
* @date 2021/1/4 16:32
|
||||
*/
|
||||
@GetResource(name = "jvm监控", path = "/monitor/jvmInfo")
|
||||
public String jvmInfo(Model model){
|
||||
getMetricInfos("jvm_",model);
|
||||
return PREFIX+"/jvmInfo.html";
|
||||
@GetResource(name = "jvm监控", path = "/view/monitor/jvmInfo")
|
||||
public String jvmInfo(Model model) {
|
||||
getMetricInfos("jvm_", model);
|
||||
return "/modular/frame/jvmInfo.html";
|
||||
}
|
||||
|
||||
/**
|
||||
* 分别输出监控名称以及对应的值
|
||||
*
|
||||
* @author chenli
|
||||
* @Date 2021/1/4 16:32
|
||||
* @date 2021/1/4 16:32
|
||||
*/
|
||||
private void getMetricInfos(String metric,Model model){
|
||||
private void getMetricInfos(String metric, Model model) {
|
||||
String promQL = "";
|
||||
if(!StringUtils.isEmpty(prometheusInstance)){
|
||||
promQL = "{application=\""+name+"\",instance=\""+prometheusInstance+"\"}";
|
||||
if (!StrUtil.isEmpty(prometheusInstance)) {
|
||||
promQL = "{application=\"" + name + "\",instance=\"" + prometheusInstance + "\"}";
|
||||
} else {
|
||||
promQL = "{application=\""+name+"\"}";
|
||||
promQL = "{application=\"" + name + "\"}";
|
||||
}
|
||||
if(!StringUtils.isEmpty(prometheusUrl)){
|
||||
if (!StrUtil.isEmpty(prometheusUrl)) {
|
||||
List<PromResultInfo> promResultInfos = service.getMetricInfo(prometheusUrl.concat(ProjectConstants.PROMETHEUS_QUERY_RANGE), promQL);
|
||||
for(PromResultInfo promResultInfo : promResultInfos){
|
||||
for (PromResultInfo promResultInfo : promResultInfos) {
|
||||
String metricName = promResultInfo.getMetric().get__name__();
|
||||
if(!Objects.isNull(metricName)){
|
||||
if(metricName.contains(metric)){
|
||||
String metricValues = StringEscapeUtils.unescapeJava(JSON.toJSONString(promResultInfo.getValues()));
|
||||
model.addAttribute(metricName, metricValues.replace("\"",""));
|
||||
if (!Objects.isNull(metricName)) {
|
||||
if (metricName.contains(metric)) {
|
||||
String metricValues = EscapeUtil.unescape(JSON.toJSONString(promResultInfo.getValues()));
|
||||
model.addAttribute(metricName, metricValues.replace("\"", ""));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -12,12 +12,14 @@ import java.util.List;
|
|||
*/
|
||||
@Data
|
||||
public class PromDataInfo {
|
||||
|
||||
/**
|
||||
* prometheus结果类型
|
||||
* vector--瞬时向量
|
||||
* matrix--区间向量
|
||||
* scalar--标量
|
||||
* string--字符串
|
||||
* prometheus结果类型:
|
||||
* <p>
|
||||
* vector -- 瞬时向量
|
||||
* matrix -- 区间向量
|
||||
* scalar -- 标量
|
||||
* string -- 字符串
|
||||
*/
|
||||
private String resultType;
|
||||
|
||||
|
@ -25,4 +27,5 @@ public class PromDataInfo {
|
|||
* prometheus指标属性和值
|
||||
*/
|
||||
private List<PromResultInfo> result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -25,5 +25,6 @@ public class PromMetricInfo {
|
|||
* prometheus任务名称
|
||||
*/
|
||||
private String job;
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -9,11 +9,12 @@ import lombok.Data;
|
|||
* @date 2021/1/10 19:00
|
||||
*/
|
||||
@Data
|
||||
public class PromResponceInfo {
|
||||
public class PromResponseInfo {
|
||||
|
||||
/**
|
||||
* 状态
|
||||
* 成功-- success
|
||||
* 状态:
|
||||
* <p>
|
||||
* 成功-success
|
||||
*/
|
||||
private String status;
|
||||
|
||||
|
@ -21,4 +22,5 @@ public class PromResponceInfo {
|
|||
* prometheus指标属性和值
|
||||
*/
|
||||
private PromDataInfo data;
|
||||
}
|
||||
|
||||
}
|
|
@ -3,11 +3,14 @@ package cn.stylefeng.guns.modular.system.model;
|
|||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* prometheus结果
|
||||
*
|
||||
* @author chenli
|
||||
* @date 2021/1/10 18:58
|
||||
*/
|
||||
@Data
|
||||
public class PromResultInfo {
|
||||
|
||||
/**
|
||||
* prometheus指标属性
|
||||
*/
|
||||
|
|
|
@ -15,11 +15,12 @@ public interface MetricService {
|
|||
/**
|
||||
* prometheus采集监控指标
|
||||
*
|
||||
* @param promURL prometheus地址
|
||||
* @param promURL prometheus地址
|
||||
* @param promQL prometheus查询表达式
|
||||
* @return Map<String, Object> 指标名称与指标数据的集合
|
||||
* @author chenli
|
||||
* @date 2021/1/10 17:37
|
||||
*/
|
||||
List<PromResultInfo> getMetricInfo(String promURL, String promQL);
|
||||
|
||||
}
|
||||
|
|
|
@ -2,19 +2,14 @@ package cn.stylefeng.guns.modular.system.service.impl;
|
|||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.stylefeng.guns.core.consts.ProjectConstants;
|
||||
import cn.stylefeng.guns.core.exception.BusinessException;
|
||||
import cn.stylefeng.guns.modular.system.model.PromResponceInfo;
|
||||
import cn.stylefeng.guns.modular.system.model.PromResponseInfo;
|
||||
import cn.stylefeng.guns.modular.system.model.PromResultInfo;
|
||||
import cn.stylefeng.guns.modular.system.service.MetricService;
|
||||
import com.alibaba.excel.util.StringUtils;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 监控管理prometheus
|
||||
|
@ -34,17 +29,25 @@ public class MetricServiceImpl implements MetricService {
|
|||
@Override
|
||||
public List<PromResultInfo> getMetricInfo(String promURL, String promQL) {
|
||||
Map<String, Object> paramMap = new HashMap<>();
|
||||
|
||||
// prometheus查询命令
|
||||
paramMap.put(ProjectConstants.PROMETHEUS_QUERY, promQL);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
|
||||
// 查询5分钟数据
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - 5);
|
||||
|
||||
// 查询开始时间
|
||||
paramMap.put(ProjectConstants.PROMETHEUS_START, (calendar.getTime().getTime())/1000L);
|
||||
paramMap.put(ProjectConstants.PROMETHEUS_START, (calendar.getTime().getTime()) / 1000L);
|
||||
|
||||
// 查询结束时间
|
||||
paramMap.put(ProjectConstants.PROMETHEUS_END, (new Date().getTime())/1000L);
|
||||
paramMap.put(ProjectConstants.PROMETHEUS_END, (new Date().getTime()) / 1000L);
|
||||
|
||||
// 查询步长
|
||||
paramMap.put(ProjectConstants.PROMETHEUS_STEP,15);
|
||||
PromResponceInfo responseInfo = JSON.parseObject(HttpUtil.get(promURL, paramMap), PromResponceInfo.class);
|
||||
paramMap.put(ProjectConstants.PROMETHEUS_STEP, 15);
|
||||
|
||||
// 获取查询结果
|
||||
PromResponseInfo responseInfo = JSON.parseObject(HttpUtil.get(promURL, paramMap), PromResponseInfo.class);
|
||||
if (StringUtils.isEmpty(responseInfo)) {
|
||||
// prometheus未开启
|
||||
return null;
|
||||
|
@ -55,4 +58,5 @@ public class MetricServiceImpl implements MetricService {
|
|||
}
|
||||
return responseInfo.getData().getResult();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -178,4 +178,5 @@ public class SystemHardwareWrapper {
|
|||
return String.format("%d B", size);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -42,13 +42,14 @@ mybatis-plus:
|
|||
cache-enabled: true
|
||||
lazy-loading-enabled: true
|
||||
multiple-result-sets-enabled: true
|
||||
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
global-config:
|
||||
banner: false
|
||||
enable-sql-runner: true
|
||||
db-config:
|
||||
id-type: assign_id
|
||||
table-underline: true
|
||||
|
||||
# prometheus监控
|
||||
management:
|
||||
endpoints:
|
||||
|
@ -66,4 +67,4 @@ management:
|
|||
prometheus:
|
||||
url: http://localhost:9090/api/v1/
|
||||
# 非必须配置项
|
||||
instance:
|
||||
instance:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* 高频方法集
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @Date 2019/7/29 21:20
|
||||
* @date 2019/7/29 21:20
|
||||
*/
|
||||
layui.define(['jquery', 'layer', 'admin', 'table'], function (exports) {
|
||||
var $ = layui.$;
|
||||
|
@ -100,4 +100,4 @@ layui.define(['jquery', 'layer', 'admin', 'table'], function (exports) {
|
|||
};
|
||||
|
||||
exports('func', func);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue