Skip to content

Commit

Permalink
✨ 增加系统监控及日志下载功能
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaojun1998 committed Feb 22, 2020
1 parent 7a24fd1 commit a759d9f
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 33 deletions.
30 changes: 30 additions & 0 deletions src/main/java/im/zhaojun/common/controller/AdminController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,16 +13,21 @@
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;
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;
Expand Down Expand Up @@ -92,6 +100,9 @@ public ResultBean getFormByStorageType(StorageTypeEnum storageType) {
return ResultBean.success(storageConfigList);
}

/**
* 返回支持的存储引擎.
*/
@GetMapping("/support-strategy")
public ResultBean supportStrategy() {
List<StorageStrategyDTO> result = new ArrayList<>();
Expand Down Expand Up @@ -170,4 +181,23 @@ public void refreshStorageStrategy(StorageTypeEnum storageStrategy) throws Excep
}
}

/**
* 系统日志下载
*/
@GetMapping("/log")
public ResponseEntity<Object> 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());
}

}
39 changes: 39 additions & 0 deletions src/main/java/im/zhaojun/common/model/Jvm.java
Original file line number Diff line number Diff line change
@@ -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");
}

}
41 changes: 41 additions & 0 deletions src/main/java/im/zhaojun/common/model/Mem.java
Original file line number Diff line number Diff line change
@@ -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;
}


}
58 changes: 58 additions & 0 deletions src/main/java/im/zhaojun/common/model/Sys.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
34 changes: 34 additions & 0 deletions src/main/java/im/zhaojun/common/model/SystemMonitorInfo.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
15 changes: 15 additions & 0 deletions src/main/java/im/zhaojun/common/service/SystemMonitorService.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
68 changes: 68 additions & 0 deletions src/main/java/im/zhaojun/common/util/FileUtil.java
Original file line number Diff line number Diff line change
@@ -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<Object> 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<Object> 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));
}
}
35 changes: 2 additions & 33 deletions src/main/java/im/zhaojun/local/controller/LocalController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +13,6 @@
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;

/**
* @author zhaojun
Expand All @@ -37,32 +32,6 @@ public ResponseEntity<Object> downAttachment(final HttpServletRequest request) {
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<Object> 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));
}

}

0 comments on commit a759d9f

Please sign in to comment.