package com.iailab.module.infra.controller.admin.actuator; import cn.hutool.core.util.NumberUtil; import com.alibaba.fastjson.JSONObject; import com.iailab.framework.common.pojo.CommonResult; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.swing.filechooser.FileSystemView; import java.io.File; import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Description: 硬盘内存监控等 * @author: iailab */ @Slf4j @RestController @RequestMapping("/infra/actuator") public class ActuatorController { /** * 内存详情 * @return * @throws Exception */ @GetMapping("/memory/info") public CommonResult getRedisInfo() throws Exception { OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean(); JSONObject operatingSystemJson = JSONObject.parseObject(JSONObject.toJSONString(operatingSystemMXBean)); long totalPhysicalMemory = operatingSystemJson.getLongValue("totalPhysicalMemorySize"); long freePhysicalMemory = operatingSystemJson.getLongValue("freePhysicalMemorySize"); long usedPhysicalMemory = totalPhysicalMemory - freePhysicalMemory; Runtime runtime = Runtime.getRuntime(); Map result = new HashMap<>(); result.put("memory.physical.total", totalPhysicalMemory); result.put("memory.physical.used", freePhysicalMemory); result.put("memory.physical.free", usedPhysicalMemory); result.put("memory.physical.usage", NumberUtil.div(usedPhysicalMemory, totalPhysicalMemory)); result.put("memory.runtime.total", runtime.totalMemory()); result.put("memory.runtime.used", runtime.freeMemory()); result.put("memory.runtime.max", runtime.totalMemory() - runtime.freeMemory()); result.put("memory.runtime.free", runtime.maxMemory() - runtime.totalMemory() + runtime.freeMemory()); result.put("memory.runtime.usage", NumberUtil.div(runtime.totalMemory() - runtime.freeMemory(), runtime.totalMemory())); return CommonResult.success(result); } /** * @功能:获取磁盘信息 * @return */ @GetMapping("/disk/info") public CommonResult>> queryDiskInfo(){ CommonResult>> res = new CommonResult<>(); try { // 当前文件系统类 FileSystemView fsv = FileSystemView.getFileSystemView(); // 列出所有windows 磁盘 File[] fs = File.listRoots(); log.info("查询磁盘信息:"+fs.length+"个"); List> list = new ArrayList<>(); for (int i = 0; i < fs.length; i++) { if(fs[i].getTotalSpace()==0) { continue; } Map map = new HashMap(5); map.put("name", fsv.getSystemDisplayName(fs[i])); map.put("max", fs[i].getTotalSpace()); map.put("rest", fs[i].getFreeSpace()); map.put("restPPT", (fs[i].getTotalSpace()-fs[i].getFreeSpace())*100/fs[i].getTotalSpace()); list.add(map); log.info(map.toString()); } res.setData(list); res.success("查询成功"); } catch (Exception e) { res.setMsg("查询失败"+e.getMessage()); } return res; } }