group7-陈立 性能监控——服务器信息、SQL监控两个模块,合并的时候注意一下application.yml里面druid的配置不能删

pull/65/head
陈立 4 years ago
parent da60c89600
commit 8893b0fb0d

@ -25,6 +25,7 @@
<java.version>1.8</java.version>
<mysql-connector-java.version>8.0.21</mysql-connector-java.version>
<docker.img.version>latest</docker.img.version>
<oshi.version>3.9.1</oshi.version>
</properties>
<dependencies>
@ -35,7 +36,6 @@
<artifactId>beetl</artifactId>
<version>3.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>auth-spring-boot-starter</artifactId>
@ -129,7 +129,22 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--硬件信息获取-->
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>${oshi.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
<build>

@ -0,0 +1,67 @@
package cn.stylefeng.guns.config.web;
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
import com.alibaba.druid.util.Utils;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
/***
* : <br/> Druid
* : 2021/1/5 15:14
* @author chenli
*/
@Configuration
public class DruidConfiguration {
/***
* : <br/> 广
* : 2021/1/4 20:57
* @author chenli
*/
@SuppressWarnings({"rawtypes", "unchecked"})
@Bean
// @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties) {
// 获取web监控页面的参数
DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
// 提取common.js的配置路径
String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
final String filePath = "support/http/resources/js/common.js";
Filter filter = new Filter() {
@Override
public void init(javax.servlet.FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(request, response);
String text = Utils.readFromResource(filePath);
// 正则替换banner, 除去底部的广告信息
text = text.replaceAll("<a.*?banner\"></a><br/>", "");
text = text.replaceAll("powered.*?shrek.wang</a>", "");
text = text.replaceAll("<div.*?container\"></div>", "");
text = text.replaceAll("<footer.*?footer\"></footer>", "");
response.getWriter().write(text);
}
@Override
public void destroy() {
}
};
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(filter);
registrationBean.addUrlPatterns(commonJsPattern);
return registrationBean;
}
}

@ -0,0 +1,50 @@
package cn.stylefeng.guns.modular.system.controller;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
/**
* @author chenli
* @date 2020/12/30 16:40
*/
@Controller
@Slf4j
@ApiResource(name = "项目监控")
public class MonitorController {
private String PREFIX = "/modular/frame";
@Value("${server.port}")
private String port;
/**
*
*
* @author fengshuonan
* @Date 2018/12/24 22:43
*/
@GetResource(name = "服务器监控", path = "/monitor/systemInfo", requiredPermission = false)
public String systemInfo(Model model) {
SystemHardwareWarpper systemHardwareWarpper = new SystemHardwareWarpper();
systemHardwareWarpper.copyTo();
model.addAttribute("server",systemHardwareWarpper);
return PREFIX+"/systemInfo.html";
}
/***
* : <br/> durid sql
* : 2021/1/4 16:32
* @author chenli
*/
@GetResource(name = "SQL监控", path = "/monitor/druid", requiredPermission = false,requiredLogin = false)
public String duridInfo(Model model){
model.addAttribute("port",port);
return PREFIX+"/druid.html";
}
}

@ -0,0 +1,69 @@
package cn.stylefeng.guns.modular.system.model;
import cn.hutool.core.util.NumberUtil;
import lombok.Setter;
/**
* CPU
*
* @author fengshuonan
* @Date 2019-07-13 13:42
*/
@Setter
public class CpuInfo {
/**
*
*/
private int cpuNum;
/**
* CPU使
*/
private double total;
/**
* CPU使
*/
private double sys;
/**
* CPU使
*/
private double used;
/**
* CPU
*/
private double wait;
/**
* CPU
*/
private double free;
public int getCpuNum() {
return cpuNum;
}
public double getTotal() {
return NumberUtil.round(NumberUtil.mul(total, 100), 2).doubleValue();
}
public double getSys() {
return NumberUtil.round(NumberUtil.mul(sys / total, 100), 2).doubleValue();
}
public double getUsed() {
return NumberUtil.round(NumberUtil.mul(used / total, 100), 2).doubleValue();
}
public double getWait() {
return NumberUtil.round(NumberUtil.mul(wait / total, 100), 2).doubleValue();
}
public double getFree() {
return NumberUtil.round(NumberUtil.mul(free / total, 100), 2).doubleValue();
}
}

@ -0,0 +1,109 @@
package cn.stylefeng.guns.modular.system.model;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import lombok.Setter;
import java.lang.management.ManagementFactory;
import java.util.Date;
/**
* JVM
*
* @author fengshuonan
* @Date 2019-07-13 13:42
*/
@Setter
public class JvmInfo {
/**
* JVM(M)
*/
private double total;
/**
* JVM(M)
*/
private double max;
/**
* JVM(M)
*/
private double free;
/**
* JDK
*/
private String version;
/**
* JDK
*/
private String home;
public double getTotal() {
return NumberUtil.div(total, (1024 * 1024), 2);
}
public double getMax() {
return NumberUtil.div(max, (1024 * 1024), 2);
}
public double getFree() {
return NumberUtil.div(free, (1024 * 1024), 2);
}
public double getUsed() {
return NumberUtil.div(total - free, (1024 * 1024), 2);
}
public String getVersion() {
return version;
}
public String getHome() {
return home;
}
public double getUsage() {
return NumberUtil.mul(NumberUtil.div(total - free, total, 4), 100);
}
/**
* JDK
*/
public String getName() {
return ManagementFactory.getRuntimeMXBean().getVmName();
}
/**
* JDK
*/
public String getStartTime() {
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
Date date = new Date(time);
return DateUtil.formatDateTime(date);
}
/**
* JDK
*/
public String getRunTime() {
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
Date date = new Date(time);
//运行多少分钟
long runMS = DateUtil.between(date, new Date(), DateUnit.MS);
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
long day = runMS / nd;
long hour = runMS % nd / nh;
long min = runMS % nd % nh / nm;
return day + "天" + hour + "小时" + min + "分钟";
}
}

@ -0,0 +1,45 @@
package cn.stylefeng.guns.modular.system.model;
import cn.hutool.core.util.NumberUtil;
import lombok.Setter;
/**
*
*
* @author fengshuonan
* @Date 2019-07-13 13:42
*/
@Setter
public class MemInfo {
/**
*
*/
private double total;
/**
*
*/
private double used;
/**
*
*/
private double free;
public double getTotal() {
return NumberUtil.div(total, (1024 * 1024 * 1024), 2);
}
public double getUsed() {
return NumberUtil.div(used, (1024 * 1024 * 1024), 2);
}
public double getFree() {
return NumberUtil.div(free, (1024 * 1024 * 1024), 2);
}
public double getUsage() {
return NumberUtil.mul(NumberUtil.div(used, total, 4), 100);
}
}

@ -0,0 +1,49 @@
package cn.stylefeng.guns.modular.system.model;
import lombok.Data;
/**
*
*
* @author fengshuonan
* @Date 2019-07-13 13:42
*/
@Data
public class SysFileInfo {
/**
*
*/
private String dirName;
/**
*
*/
private String sysTypeName;
/**
*
*/
private String typeName;
/**
*
*/
private String total;
/**
*
*/
private String free;
/**
* 使
*/
private String used;
/**
* 使
*/
private double usage;
}

@ -0,0 +1,39 @@
package cn.stylefeng.guns.modular.system.model;
import lombok.Data;
/**
*
*
* @author fengshuonan
* @Date 2019-07-13 13:42
*/
@Data
public class SysInfo {
/**
*
*/
private String computerName;
/**
* Ip
*/
private String computerIp;
/**
*
*/
private String userDir;
/**
*
*/
private String osName;
/**
*
*/
private String osArch;
}

@ -0,0 +1,181 @@
package cn.stylefeng.guns.modular.system.warpper;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.NumberUtil;
import cn.stylefeng.guns.modular.system.model.*;
import cn.stylefeng.guns.core.util.IpInfoUtils;
import lombok.Data;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.CentralProcessor.TickType;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem;
import oshi.util.Util;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
/**
*
*
* @author fengshuonan
* @Date 2019-07-13 13:42
*/
@Data
public class SystemHardwareWarpper {
private static final int OSHI_WAIT_SECOND = 1000;
/**
* CPU
*/
private CpuInfo cpu = new CpuInfo();
/**
*
*/
private MemInfo mem = new MemInfo();
/**
* JVM
*/
private JvmInfo jvm = new JvmInfo();
/**
*
*/
private SysInfo sys = new SysInfo();
/**
*
*/
private List<SysFileInfo> sysFiles = new LinkedList<>();
public void copyTo() {
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
setCpuInfo(hal.getProcessor());
setMemInfo(hal.getMemory());
setSysInfo();
setJvmInfo();
setSysFiles(si.getOperatingSystem());
}
/**
* CPU
*/
private void setCpuInfo(CentralProcessor processor) {
// CPU信息
long[] prevTicks = processor.getSystemCpuLoadTicks();
Util.sleep(OSHI_WAIT_SECOND);
long[] ticks = processor.getSystemCpuLoadTicks();
long nice = ticks[TickType.NICE.getIndex()] - prevTicks[TickType.NICE.getIndex()];
long irq = ticks[TickType.IRQ.getIndex()] - prevTicks[TickType.IRQ.getIndex()];
long softirq = ticks[TickType.SOFTIRQ.getIndex()] - prevTicks[TickType.SOFTIRQ.getIndex()];
long steal = ticks[TickType.STEAL.getIndex()] - prevTicks[TickType.STEAL.getIndex()];
long cSys = ticks[TickType.SYSTEM.getIndex()] - prevTicks[TickType.SYSTEM.getIndex()];
long user = ticks[TickType.USER.getIndex()] - prevTicks[TickType.USER.getIndex()];
long iowait = ticks[TickType.IOWAIT.getIndex()] - prevTicks[TickType.IOWAIT.getIndex()];
long idle = ticks[TickType.IDLE.getIndex()] - prevTicks[TickType.IDLE.getIndex()];
long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
cpu.setCpuNum(processor.getLogicalProcessorCount());
cpu.setTotal(totalCpu);
cpu.setSys(cSys);
cpu.setUsed(user);
cpu.setWait(iowait);
cpu.setFree(idle);
}
/**
*
*/
private void setMemInfo(GlobalMemory memory) {
mem.setTotal(memory.getTotal());
mem.setUsed(memory.getTotal() - memory.getAvailable());
mem.setFree(memory.getAvailable());
}
/**
*
*/
private void setSysInfo() {
Properties props = System.getProperties();
sys.setComputerName(IpInfoUtils.getHostName());
sys.setComputerIp(NetUtil.getLocalhostStr());
sys.setOsName(props.getProperty("os.name"));
sys.setOsArch(props.getProperty("os.arch"));
sys.setUserDir(props.getProperty("user.dir"));
}
/**
* Java
*/
private void setJvmInfo() {
Properties props = System.getProperties();
jvm.setTotal(Runtime.getRuntime().totalMemory());
jvm.setMax(Runtime.getRuntime().maxMemory());
jvm.setFree(Runtime.getRuntime().freeMemory());
jvm.setVersion(props.getProperty("java.version"));
jvm.setHome(props.getProperty("java.home"));
}
/**
*
*/
private void setSysFiles(OperatingSystem os) {
FileSystem fileSystem = os.getFileSystem();
OSFileStore[] fsArray = fileSystem.getFileStores();
for (OSFileStore fs : fsArray) {
long free = fs.getUsableSpace();
long total = fs.getTotalSpace();
long used = total - free;
SysFileInfo sysFile = new SysFileInfo();
sysFile.setDirName(fs.getMount());
sysFile.setSysTypeName(fs.getType());
sysFile.setTypeName(fs.getName());
sysFile.setTotal(convertFileSize(total));
sysFile.setFree(convertFileSize(free));
sysFile.setUsed(convertFileSize(used));
if (total == 0) {
sysFile.setUsage(0);
} else {
sysFile.setUsage(NumberUtil.mul(NumberUtil.div(used, total, 4), 100));
}
sysFiles.add(sysFile);
}
}
/**
*
*
* @param size
* @return
*/
public String convertFileSize(long size) {
long kb = 1024;
long mb = kb * 1024;
long gb = mb * 1024;
if (size >= gb) {
return String.format("%.1f GB", (float) size / gb);
} else if (size >= mb) {
float f = (float) size / mb;
return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
} else if (size >= kb) {
float f = (float) size / kb;
return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
} else {
return String.format("%d B", size);
}
}
}

@ -4,8 +4,41 @@ spring:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://114.215.203.183:3306/guns?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT
username: guest
password: p8HMTBEapAPPmXB8
password: p8HMTBEapAPPmXB8!
# 连接池大小根据实际情况调整
max-active: 100
max-pool-prepared-statement-per-connection-size: 100
max-pool-prepared-statement-per-connection-size: 100
druid:
initialSize: 50
minIdle: 50
maxActive: 200
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
maxEvictableIdleTimeMillis: 900000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: false
webStatFilter:
enabled: true
statViewServlet:
enabled: true
# 设置白名单,不填则允许所有访问
allow:
url-pattern: /druid/*
# 控制台管理用户名和密码
login-username:
login-password:
filter:
stat:
enabled: true
# 慢SQL记录
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true

@ -1,11 +1,44 @@
# Mysql数据库
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/guns?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT
username: root
password: 123456
url: jdbc:mysql://192.168.1.114:3306/guns?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT
username: guest
password: p8HMTBEapAPPmXB8!
# 连接池大小根据实际情况调整
max-active: 100
max-pool-prepared-statement-per-connection-size: 100
max-pool-prepared-statement-per-connection-size: 100
druid:
initialSize: 50
minIdle: 50
maxActive: 200
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
maxEvictableIdleTimeMillis: 900000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: false
webStatFilter:
enabled: true
statViewServlet:
enabled: true
# 设置白名单,不填则允许所有访问
allow:
url-pattern: /druid/*
# 控制台管理用户名和密码
login-username:
login-password:
filter:
stat:
enabled: true
# 慢SQL记录
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true

@ -20,7 +20,8 @@ spring:
locale: zh_CN
serialization:
indent_output: false
main:
allow-bean-definition-overriding: true
scanner:
open: true
sys-log:

@ -177,7 +177,7 @@
</div>
<div class="layui-form-item layui-input-icon-group">
<i class="layui-icon layui-icon-password"></i>
<input class="layui-input" id="password" name="password" placeholder="请输入登录密码" value="111111" type="password" lay-verType="tips" lay-verify="required" required/>
<input class="layui-input" id="password" name="password" placeholder="请输入登录密码" value="123456" type="password" lay-verType="tips" lay-verify="required" required/>
</div>
@if(constants.getCaptchaOpen()){
<div class="layui-form-item layui-input-icon-group login-captcha-group">

@ -0,0 +1,32 @@
<!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-lg12">
<iframe id="iframe" src="http://localhost:${port}/druid/index.html" frameborder="no" width="100%" scrolling="auto" ></iframe>
</div>
</div>
</div>
<!-- js部分 -->
@/* 加入contextPath属性和session超时的配置 */
<script type="text/javascript">
var Feng = {
ctxPath: "${ctxPath}",
version: '${constants.getReleaseVersion()}'
};
document.getElementById("iframe").height = document.documentElement.clientHeight+'px;'
</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>
</body>
</html>
Loading…
Cancel
Save