Merge branch 'group7-monitor'

# Conflicts:
#	pom.xml
#	src/main/java/cn/stylefeng/guns/GunsApplication.java
#	src/main/java/cn/stylefeng/guns/modular/system/controller/MonitorController.java
pull/65/head
fengshuonan 2021-01-21 21:48:09 +08:00
commit 77e3694add
13 changed files with 595 additions and 15 deletions

22
pom.xml
View File

@ -161,6 +161,28 @@
<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>

View File

@ -1,9 +1,13 @@
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
@ -19,6 +23,10 @@ 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);
}
}

View File

@ -35,4 +35,27 @@ public interface ProjectConstants {
*/
Integer SUPER_ADMIN_INIT_LISTENER_SORT = 400;
/**
* prometheus
*/
String PROMETHEUS_QUERY = "query";
/**
* prometheus
*/
String PROMETHEUS_QUERY_RANGE = "query_range";
/**
* prometheus
*/
String PROMETHEUS_START = "start";
/**
* prometheus
*/
String PROMETHEUS_END = "end";
/**
* prometheus
*/
String PROMETHEUS_STEP = "step";
}

View File

@ -1,49 +1,126 @@
package cn.stylefeng.guns.modular.system.controller;
import cn.stylefeng.guns.modular.system.warpper.SystemHardwareWrapper;
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.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 java.util.List;
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;
@Value("${spring.application.name}")
private String name;
@Value("${prometheus.url}")
private String prometheusUrl;
@Value("${prometheus.instance}")
private String prometheusInstance;
@Autowired
private MetricService service;
/**
*
*
* @author fengshuonan
* @date 2021/1/10 19:09
* @Date 2018/12/24 22:43
*/
@GetResource(name = "服务器监控", path = "/view/monitor/systemInfo")
@GetResource(name = "服务器监控", path = "/monitor/systemInfo")
public String systemInfo(Model model) {
SystemHardwareWrapper systemHardwareWrapper = new SystemHardwareWrapper();
systemHardwareWrapper.copyTo();
model.addAttribute("server", systemHardwareWrapper);
return "/modular/frame/systemInfo.html";
SystemHardwareWarpper systemHardwareWarpper = new SystemHardwareWarpper();
systemHardwareWarpper.copyTo();
model.addAttribute("server",systemHardwareWarpper);
return PREFIX+"/systemInfo.html";
}
/**
* druid sql
* durid sql
*
* @author chenli
* @date 2021/1/4 16:32
* @Date 2021/1/4 16:32
*/
@GetResource(name = "SQL监控", path = "/view/monitor/druid")
public String druidInfo(Model model) {
model.addAttribute("port", port);
return "/modular/frame/druid.html";
@GetResource(name = "SQL监控", path = "/monitor/druid")
public String duridInfo(Model model){
model.addAttribute("port",port);
return PREFIX+"/druid.html";
}
/**
* tomcat
*
* @author chenli
* @Date 2021/1/4 16:32
*/
@GetResource(name = "tomcat监控", path = "/monitor/tomcatInfo")
public String tomcatInfo(Model model){
getMetricInfos("tomcat_",model);
return PREFIX+"/tomcatInfo.html";
}
/**
* jvm
*
* @author chenli
* @Date 2021/1/4 16:32
*/
@GetResource(name = "jvm监控", path = "/monitor/jvmInfo")
public String jvmInfo(Model model){
getMetricInfos("jvm_",model);
return PREFIX+"/jvmInfo.html";
}
/**
*
*
* @author chenli
* @Date 2021/1/4 16:32
*/
private void getMetricInfos(String metric,Model model){
String promQL = "";
if(!StringUtils.isEmpty(prometheusInstance)){
promQL = "{application=\""+name+"\",instance=\""+prometheusInstance+"\"}";
} else {
promQL = "{application=\""+name+"\"}";
}
if(!StringUtils.isEmpty(prometheusUrl)){
List<PromResultInfo> promResultInfos = service.getMetricInfo(prometheusUrl.concat(ProjectConstants.PROMETHEUS_QUERY_RANGE), promQL);
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("\"",""));
}
}
}
}
}
}

View File

@ -0,0 +1,28 @@
package cn.stylefeng.guns.modular.system.model;
import lombok.Data;
import java.util.List;
/**
* prometheus
*
* @author chenli
* @date 2021/1/10 19:10
*/
@Data
public class PromDataInfo {
/**
* prometheus
* vector--
* matrix--
* scalar--
* string--
*/
private String resultType;
/**
* prometheus
*/
private List<PromResultInfo> result;
}

View File

@ -0,0 +1,29 @@
package cn.stylefeng.guns.modular.system.model;
import lombok.Data;
/**
* prometheus
*
* @author chenli
* @date 2021/1/10 18:53
*/
@Data
public class PromMetricInfo {
/**
* prometheus
*/
private String __name__;
/**
* prometheus
*/
private String instance;
/**
* prometheus
*/
private String job;
}

View File

@ -0,0 +1,24 @@
package cn.stylefeng.guns.modular.system.model;
import lombok.Data;
/**
* prometheus http
*
* @author chenli
* @date 2021/1/10 19:00
*/
@Data
public class PromResponceInfo {
/**
*
* -- success
*/
private String status;
/**
* prometheus
*/
private PromDataInfo data;
}

View File

@ -0,0 +1,21 @@
package cn.stylefeng.guns.modular.system.model;
import lombok.Data;
/**
* @author chenli
* @date 2021/1/10 18:58
*/
@Data
public class PromResultInfo {
/**
* prometheus
*/
private PromMetricInfo metric;
/**
* prometheus
*/
private String[] values;
}

View File

@ -0,0 +1,25 @@
package cn.stylefeng.guns.modular.system.service;
import cn.stylefeng.guns.modular.system.model.PromResultInfo;
import java.util.List;
/**
* prometheus
*
* @author chenli
* @date 2021/1/10 16:09
*/
public interface MetricService {
/**
* 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);
}

View File

@ -0,0 +1,58 @@
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.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;
/**
* prometheus
*
* @author chenli
* @date 2021/1/10 16:09
*/
@Service
public class MetricServiceImpl implements MetricService {
/**
* prometheus
*
* @author chenli
* @date 2021/1/10 16:09
*/
@Override
public List<PromResultInfo> getMetricInfo(String promURL, String promQL) {
Map<String, Object> paramMap = new HashMap<>();
paramMap.put(ProjectConstants.PROMETHEUS_QUERY, promQL);
Calendar calendar = Calendar.getInstance();
// 查询5分钟数据
calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - 5);
// 查询开始时间
paramMap.put(ProjectConstants.PROMETHEUS_START, (calendar.getTime().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);
if (StringUtils.isEmpty(responseInfo)) {
// prometheus未开启
return null;
}
if (StringUtils.isEmpty(responseInfo.getStatus()) || !"success".equals(responseInfo.getStatus())) {
// prometheus查询失败
return null;
}
return responseInfo.getData().getResult();
}
}

View File

@ -49,3 +49,21 @@ mybatis-plus:
db-config:
id-type: assign_id
table-underline: true
# prometheus监控
management:
endpoints:
web:
exposure:
include: "*"
metrics:
tags:
application: ${spring.application.name}
export:
prometheus:
enabled: true
jmx:
enabled: true
prometheus:
url: http://localhost:9090/api/v1/
# 非必须配置项
instance:

View File

@ -0,0 +1,60 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>${constants.getSystemName()}</title>
<link rel="stylesheet" href="${ctxPath}/assets/common/libs/layui/css/layui.css?v=${constants.getReleaseVersion()}"/>
<link rel="stylesheet" href="${ctxPath}/assets/common/module/admin.css?v=${constants.getReleaseVersion()}"/>
<link rel="stylesheet" href="${ctxPath}/assets/expand/css/style.css?v=${constants.getReleaseVersion()}" media="all"/>
</head>
<body>
<div class="layui-fluid">
<div class="layui-row layui-col-space15" >
<div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
<div class="layui-card">
<div class="layui-card-body">
<div id="edenSpace" style="width: 100%;min-height:300px"></div>
</div>
</div>
</div>
</div>
</div>
<!-- js部分 -->
@/* 加入contextPath属性和session超时的配置 */
<script type="text/javascript">
var Feng = {
ctxPath: "${ctxPath}",
version: '${constants.getReleaseVersion()}'
};
</script>
<script type="text/javascript" src="${ctxPath}/assets/common/libs/layui/layui.js?v=${constants.getReleaseVersion()}"></script>
<script type="text/javascript" src="${ctxPath}/assets/common/js/common.js?v=${constants.getReleaseVersion()}"></script>
<script src="${ctxPath}/assets/common/libs/echarts/echarts.min.js?v=${constants.getReleaseVersion()}"></script>
<script src="${ctxPath}/assets/common/libs/echarts/echartsTheme.js?v=${constants.getReleaseVersion()}"></script>
<script>
layui.use(['layer'], function () {
const $ = layui.jquery;
initEdenSpace();
function initEdenSpace(){
const edenSpace = echarts.init(document.getElementById('edenSpace'), myEchartsTheme);
let edenSpaceData = ${jvm_memory_used_bytes};
const option = {
title: { text: 'Eden SpaceData', x: 'center' },
tooltip: { trigger: 'axis' },
xAxis: {
type: 'time'
},
yAxis: {
type: 'value',
scale: true
},
series: [{type : 'line', name : 'Eden SpaceData', data: edenSpaceData, showSymbol: true}]
};
edenSpace.setOption(option);
}
});
</script>
</body>
</html>

View File

@ -0,0 +1,187 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>${constants.getSystemName()}</title>
<link rel="stylesheet" href="${ctxPath}/assets/common/libs/layui/css/layui.css?v=${constants.getReleaseVersion()}"/>
<link rel="stylesheet" href="${ctxPath}/assets/common/module/admin.css?v=${constants.getReleaseVersion()}"/>
<link rel="stylesheet" href="${ctxPath}/assets/expand/css/style.css?v=${constants.getReleaseVersion()}" media="all"/>
</head>
<body>
<div class="layui-fluid">
<div class="layui-row layui-col-space15" >
<div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
<div class="layui-card">
<div class="layui-card-body">
<div id="currentSession" style="width: 100%;min-height:300px"></div>
</div>
</div>
</div>
<div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
<div class="layui-card">
<div class="layui-card-body">
<div id="maxSession" style="width: 100%;min-height:300px"></div>
</div>
</div>
</div>
<div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
<div class="layui-card">
<div class="layui-card-body">
<div id="maxSeconds" style="width: 100%;min-height:300px"></div>
</div>
</div>
</div>
</div>
<div class="layui-row layui-col-space15" >
<div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
<div class="layui-card">
<div class="layui-card-body">
<div id="createTotal" style="width: 100%;min-height:300px"></div>
</div>
</div>
</div>
<div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
<div class="layui-card">
<div class="layui-card-body">
<div id="rejectTotal" style="width: 100%;min-height:300px"></div>
</div>
</div>
</div>
<div class="layui-col-xs12 layui-col-sm6 layui-col-md4">
<div class="layui-card">
<div class="layui-card-body">
<div id="expiredTotal" style="width: 100%;min-height:300px"></div>
</div>
</div>
</div>
</div>
</div>
<!-- js部分 -->
@/* 加入contextPath属性和session超时的配置 */
<script type="text/javascript">
var Feng = {
ctxPath: "${ctxPath}",
version: '${constants.getReleaseVersion()}'
};
</script>
<script type="text/javascript" src="${ctxPath}/assets/common/libs/layui/layui.js?v=${constants.getReleaseVersion()}"></script>
<script type="text/javascript" src="${ctxPath}/assets/common/js/common.js?v=${constants.getReleaseVersion()}"></script>
<script src="${ctxPath}/assets/common/libs/echarts/echarts.min.js?v=${constants.getReleaseVersion()}"></script>
<script src="${ctxPath}/assets/common/libs/echarts/echartsTheme.js?v=${constants.getReleaseVersion()}"></script>
<script>
layui.use(['layer'], function () {
const $ = layui.jquery;
initCurrentSession();
initMaxSession();
initMaxSeconds();
initCreateTotal();
initRejectTotal();
initExpiredTotal();
function initCurrentSession(){
const currentSession = echarts.init(document.getElementById('currentSession'), myEchartsTheme);
let currentSessionData = ${tomcat_sessions_active_current_sessions};
const option = {
title: { text: 'Current Sessions', x: 'center' },
tooltip: { trigger: 'axis' },
xAxis: {
type: 'time'
},
yAxis: {
type: 'value',
scale: true
},
series: [{type : 'line', name : 'Current Sessions', data: currentSessionData, showSymbol: true}]
};
currentSession.setOption(option);
}
function initMaxSession(){
const maxSession = echarts.init(document.getElementById('maxSession'), myEchartsTheme);
let maxSessionData = ${tomcat_sessions_active_max_sessions};
const option = {
title: { text: 'Max Sessions', x: 'center' },
tooltip: { trigger: 'axis' },
xAxis: {
type: 'time'
},
yAxis: {
type: 'value',
scale: true
},
series: [{type : 'line', name : 'Max Sessions', data: maxSessionData, showSymbol: true}]
};
maxSession.setOption(option);
}
function initMaxSeconds(){
const maxSeconds = echarts.init(document.getElementById('maxSeconds'), myEchartsTheme);
const maxSecondsData = ${tomcat_sessions_alive_max_seconds};
const option = {
title: { text: 'Max Seconds', x: 'center' },
tooltip: { trigger: 'axis' },
xAxis: {
type: 'time'
},
yAxis: {
type: 'value',
scale: true
},
series: [{type : 'line', name : 'Max Seconds', data: maxSecondsData, showSymbol: true}]
};
maxSeconds.setOption(option);
}
function initCreateTotal(){
const createTotal = echarts.init(document.getElementById('createTotal'), myEchartsTheme);
const createTotalData = ${tomcat_sessions_created_sessions_total};
const option = {
title: { text: 'Total Create', x: 'center' },
tooltip: { trigger: 'axis' },
xAxis: {
type: 'time'
},
yAxis: {
type: 'value',
scale: true
},
series: [{type : 'line', name : 'Total Create', data: createTotalData, showSymbol: true}]
};
createTotal.setOption(option);
}
function initRejectTotal(){
const rejectTotal = echarts.init(document.getElementById('rejectTotal'), myEchartsTheme);
const rejectTotalData = ${tomcat_sessions_rejected_sessions_total};
const option = {
title: { text: 'Total Reject', x: 'center' },
tooltip: { trigger: 'axis' },
xAxis: {
type: 'time'
},
yAxis: {
type: 'value',
scale: true
},
series: [{type : 'line', name : 'Total Reject', data: rejectTotalData, showSymbol: true}]
};
rejectTotal.setOption(option);
}
function initExpiredTotal(){
const expiredTotal = echarts.init(document.getElementById('expiredTotal'), myEchartsTheme);
const expiredTotalData = ${tomcat_sessions_expired_sessions_total};
const option = {
title: { text: 'Total Expired', x: 'center' },
tooltip: { trigger: 'axis' },
xAxis: {
type: 'time'
},
yAxis: {
type: 'value',
scale: true
},
series: [{type : 'line', name : 'Total Expired', data: expiredTotalData, showSymbol: true}]
};
expiredTotal.setOption(option);
}
});
</script>
</body>
</html>