From a759d9fe44aef78534ffee575081bcd8f7394cbd Mon Sep 17 00:00:00 2001 From: zhaojun1998 Date: Sat, 22 Feb 2020 12:53:35 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=A2=9E=E5=8A=A0=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E7=9B=91=E6=8E=A7=E5=8F=8A=E6=97=A5=E5=BF=97=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/controller/AdminController.java | 30 ++++++++ .../java/im/zhaojun/common/model/Jvm.java | 39 +++++++++++ .../java/im/zhaojun/common/model/Mem.java | 41 +++++++++++ .../java/im/zhaojun/common/model/Sys.java | 58 ++++++++++++++++ .../common/model/SystemMonitorInfo.java | 34 ++++++++++ .../common/service/SystemMonitorService.java | 15 ++++ .../java/im/zhaojun/common/util/FileUtil.java | 68 +++++++++++++++++++ .../local/controller/LocalController.java | 35 +--------- 8 files changed, 287 insertions(+), 33 deletions(-) create mode 100644 src/main/java/im/zhaojun/common/model/Jvm.java create mode 100644 src/main/java/im/zhaojun/common/model/Mem.java create mode 100644 src/main/java/im/zhaojun/common/model/Sys.java create mode 100644 src/main/java/im/zhaojun/common/model/SystemMonitorInfo.java create mode 100644 src/main/java/im/zhaojun/common/service/SystemMonitorService.java create mode 100644 src/main/java/im/zhaojun/common/util/FileUtil.java diff --git a/src/main/java/im/zhaojun/common/controller/AdminController.java b/src/main/java/im/zhaojun/common/controller/AdminController.java index 19ac8f8..f1f9dd6 100644 --- a/src/main/java/im/zhaojun/common/controller/AdminController.java +++ b/src/main/java/im/zhaojun/common/controller/AdminController.java @@ -1,7 +1,10 @@ package im.zhaojun.common.controller; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.util.ZipUtil; import im.zhaojun.common.config.StorageTypeFactory; import im.zhaojun.common.model.StorageConfig; +import im.zhaojun.common.model.SystemMonitorInfo; import im.zhaojun.common.model.dto.ResultBean; import im.zhaojun.common.model.dto.StorageStrategyDTO; import im.zhaojun.common.model.dto.SystemConfigDTO; @@ -10,8 +13,10 @@ import im.zhaojun.common.service.AbstractFileService; import im.zhaojun.common.service.FileAsyncCacheService; import im.zhaojun.common.service.StorageConfigService; import im.zhaojun.common.service.SystemConfigService; +import im.zhaojun.common.util.FileUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -19,7 +24,10 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; +import java.io.File; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.Objects; @@ -92,6 +100,9 @@ public class AdminController { return ResultBean.success(storageConfigList); } + /** + * 返回支持的存储引擎. + */ @GetMapping("/support-strategy") public ResultBean supportStrategy() { List result = new ArrayList<>(); @@ -170,4 +181,23 @@ public class AdminController { } } + /** + * 系统日志下载 + */ + @GetMapping("/log") + public ResponseEntity downloadLog(HttpServletResponse response) { + String userHome = System.getProperty("user.home"); + File fileZip = ZipUtil.zip(userHome + "/.zfile/logs"); + String currentDate = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"); + return FileUtil.export(fileZip, "ZFile 诊断日志 - " + currentDate + ".zip"); + } + + /** + * 获取系统监控信息 + */ + @GetMapping("monitor") + public ResultBean monitor() { + return ResultBean.success(new SystemMonitorInfo()); + } + } diff --git a/src/main/java/im/zhaojun/common/model/Jvm.java b/src/main/java/im/zhaojun/common/model/Jvm.java new file mode 100644 index 0000000..74de00f --- /dev/null +++ b/src/main/java/im/zhaojun/common/model/Jvm.java @@ -0,0 +1,39 @@ +package im.zhaojun.common.model; + +import lombok.Data; + +/** + * @author zhaojun + */ +@Data +public class Jvm { + + /** + * 当前 JVM 占用的内存总数 (M) + */ + private double total; + + /** + * JVM 最大可用内存总数 (M) + */ + private double max; + + /** + * JVM 空闲内存 (M) + */ + private double free; + + /** + * JDK 版本 + */ + private String version; + + public Jvm() { + Runtime runtime = Runtime.getRuntime(); + this.total = runtime.totalMemory(); + this.free = runtime.freeMemory(); + this.max = runtime.maxMemory(); + this.version = System.getProperty("java.version"); + } + +} diff --git a/src/main/java/im/zhaojun/common/model/Mem.java b/src/main/java/im/zhaojun/common/model/Mem.java new file mode 100644 index 0000000..6c4b7c5 --- /dev/null +++ b/src/main/java/im/zhaojun/common/model/Mem.java @@ -0,0 +1,41 @@ +package im.zhaojun.common.model; + +import com.sun.management.OperatingSystemMXBean; +import lombok.Data; + +import java.lang.management.ManagementFactory; + +/** + * @author zhaojun + */ +@Data +public class Mem { + + /** + * 内存总量 + */ + private double total; + + /** + * 已用内存 + */ + private double used; + + /** + * 剩余内存 + */ + private double free; + + public Mem() { + OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); + // 总的物理内存+虚拟内存 + long totalVirtualMemory = operatingSystemMXBean.getTotalSwapSpaceSize(); + // 剩余的物理内存 + long freePhysicalMemorySize = operatingSystemMXBean.getFreePhysicalMemorySize(); + this.total = totalVirtualMemory; + this.free = freePhysicalMemorySize; + this.used = totalVirtualMemory - freePhysicalMemorySize; + } + + +} diff --git a/src/main/java/im/zhaojun/common/model/Sys.java b/src/main/java/im/zhaojun/common/model/Sys.java new file mode 100644 index 0000000..6807d95 --- /dev/null +++ b/src/main/java/im/zhaojun/common/model/Sys.java @@ -0,0 +1,58 @@ +package im.zhaojun.common.model; + +import cn.hutool.core.date.BetweenFormater; +import cn.hutool.core.date.DateUtil; +import lombok.Data; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; + +import java.io.IOException; +import java.lang.management.ManagementFactory; + +/** + * @author zhaojun + */ +@Data +public class Sys { + + /** + * 项目路径 + */ + private String projectDir; + + /** + * 操作系统 + */ + private String osName; + + /** + * 系统架构 + */ + private String osArch; + + /** + * 系统版本 + */ + private String osVersion; + + /** + * 启动时间 + */ + private String upTime; + + public Sys() { + this.osName = System.getProperty("os.name"); + this.osArch = System.getProperty("os.arch"); + this.osVersion = System.getProperty("os.version"); + Resource resource = new ClassPathResource(""); + try { + this.projectDir = resource.getFile().getAbsolutePath(); + } catch (IOException e) { + e.printStackTrace(); + } + + long uptime = ManagementFactory.getRuntimeMXBean().getUptime(); + this.upTime = DateUtil.formatBetween(uptime, BetweenFormater.Level.SECOND); + } + +} diff --git a/src/main/java/im/zhaojun/common/model/SystemMonitorInfo.java b/src/main/java/im/zhaojun/common/model/SystemMonitorInfo.java new file mode 100644 index 0000000..8d392e4 --- /dev/null +++ b/src/main/java/im/zhaojun/common/model/SystemMonitorInfo.java @@ -0,0 +1,34 @@ +package im.zhaojun.common.model; + +import lombok.Data; + +import java.io.Serializable; + +/** + * @author zhaojun + */ +@Data +public class SystemMonitorInfo implements Serializable { + + /** + * 服务器基本信息 + */ + private Sys sys; + + /** + * JVM 信息 + */ + private Jvm jvm; + + /** + * 系统内存 + */ + private Mem mem; + + public SystemMonitorInfo() { + this.jvm = new Jvm(); + this.sys = new Sys(); + this.mem = new Mem(); + } + +} diff --git a/src/main/java/im/zhaojun/common/service/SystemMonitorService.java b/src/main/java/im/zhaojun/common/service/SystemMonitorService.java new file mode 100644 index 0000000..f519259 --- /dev/null +++ b/src/main/java/im/zhaojun/common/service/SystemMonitorService.java @@ -0,0 +1,15 @@ +package im.zhaojun.common.service; + +import im.zhaojun.common.model.SystemMonitorInfo; +import org.springframework.stereotype.Service; + +/** + * @author zhaojun + */ +@Service +public class SystemMonitorService { + + public SystemMonitorInfo systemMonitorInfo() { + return new SystemMonitorInfo(); + } +} diff --git a/src/main/java/im/zhaojun/common/util/FileUtil.java b/src/main/java/im/zhaojun/common/util/FileUtil.java new file mode 100644 index 0000000..1061138 --- /dev/null +++ b/src/main/java/im/zhaojun/common/util/FileUtil.java @@ -0,0 +1,68 @@ +package im.zhaojun.common.util; + +import cn.hutool.core.util.URLUtil; +import org.springframework.core.io.FileSystemResource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; + +import java.io.File; +import java.util.Date; + +/** + * @author zhaojun + */ +public class FileUtil { + + public static ResponseEntity export(File file, String fileName) { + if (!file.exists()) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("404 FILE NOT FOUND"); + } + + MediaType mediaType = MediaType.APPLICATION_OCTET_STREAM; + + HttpHeaders headers = new HttpHeaders(); + headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); + + if (StringUtils.isNullOrEmpty(fileName)) { + fileName = file.getName(); + } + + headers.setContentDispositionFormData("attachment", URLUtil.encode(fileName)); + + headers.add("Pragma", "no-cache"); + headers.add("Expires", "0"); + headers.add("Last-Modified", new Date().toString()); + headers.add("ETag", String.valueOf(System.currentTimeMillis())); + return ResponseEntity + .ok() + .headers(headers) + .contentLength(file.length()) + .contentType(mediaType) + .body(new FileSystemResource(file)); + } + + public static ResponseEntity export(File file) { + if (!file.exists()) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("404 FILE NOT FOUND"); + } + + MediaType mediaType = MediaType.APPLICATION_OCTET_STREAM; + + HttpHeaders headers = new HttpHeaders(); + headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); + headers.setContentDispositionFormData("attachment", URLUtil.encode(file.getName())); + + headers.add("Pragma", "no-cache"); + headers.add("Expires", "0"); + headers.add("Last-Modified", new Date().toString()); + headers.add("ETag", String.valueOf(System.currentTimeMillis())); + return ResponseEntity + .ok() + .headers(headers) + .contentLength(file.length()) + .contentType(mediaType) + .body(new FileSystemResource(file)); + } +} diff --git a/src/main/java/im/zhaojun/local/controller/LocalController.java b/src/main/java/im/zhaojun/local/controller/LocalController.java index cdbc473..b474727 100644 --- a/src/main/java/im/zhaojun/local/controller/LocalController.java +++ b/src/main/java/im/zhaojun/local/controller/LocalController.java @@ -1,12 +1,8 @@ package im.zhaojun.local.controller; -import cn.hutool.core.util.URLUtil; +import im.zhaojun.common.util.FileUtil; import im.zhaojun.common.util.StringUtils; import im.zhaojun.local.service.LocalServiceImpl; -import org.springframework.core.io.FileSystemResource; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.util.AntPathMatcher; @@ -17,7 +13,6 @@ import org.springframework.web.servlet.HandlerMapping; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.io.File; -import java.util.Date; /** * @author zhaojun @@ -37,32 +32,6 @@ public class LocalController { AntPathMatcher apm = new AntPathMatcher(); String filePath = apm.extractPathWithinPattern(bestMatchPattern, path); - return export(new File(StringUtils.concatPath(localServiceImpl.getFilePath(), filePath))); + return FileUtil.export(new File(StringUtils.concatPath(localServiceImpl.getFilePath(), filePath))); } - - private ResponseEntity export(File file) { - - if (!file.exists()) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body("404 FILE NOT FOUND"); - } - - - MediaType mediaType = MediaType.APPLICATION_OCTET_STREAM; - - HttpHeaders headers = new HttpHeaders(); - headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); - headers.setContentDispositionFormData("attachment", URLUtil.encode(file.getName())); - - headers.add("Pragma", "no-cache"); - headers.add("Expires", "0"); - headers.add("Last-Modified", new Date().toString()); - headers.add("ETag", String.valueOf(System.currentTimeMillis())); - return ResponseEntity - .ok() - .headers(headers) - .contentLength(file.length()) - .contentType(mediaType) - .body(new FileSystemResource(file)); - } - }