潘志宝
2024-12-25 8a6b19bf5c50ebb54e965645a9366f6cfea02351
提交 | 用户 | 时间
7da8f1 1 package com.iailab.module.infra.controller.admin.actuator;
H 2
3 import cn.hutool.core.util.NumberUtil;
4 import com.alibaba.fastjson.JSONObject;
5 import com.iailab.framework.common.pojo.CommonResult;
6 import lombok.extern.slf4j.Slf4j;
7 import org.springframework.web.bind.annotation.GetMapping;
8 import org.springframework.web.bind.annotation.RequestMapping;
9 import org.springframework.web.bind.annotation.RestController;
10
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import javax.swing.filechooser.FileSystemView;
14 import java.io.File;
15 import java.lang.management.ManagementFactory;
16 import java.lang.management.OperatingSystemMXBean;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
22 /**
23  * @Description: 硬盘内存监控等
24  * @author: iailab
25  */
26 @Slf4j
27 @RestController
28 @RequestMapping("/infra/actuator")
29 public class ActuatorController {
30
31
32     /**
33      * 内存详情
34      * @return
35      * @throws Exception
36      */
37     @GetMapping("/memory/info")
38     public CommonResult<?> getRedisInfo() throws Exception {
39         OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
40         JSONObject operatingSystemJson = JSONObject.parseObject(JSONObject.toJSONString(operatingSystemMXBean));
41         long totalPhysicalMemory = operatingSystemJson.getLongValue("totalPhysicalMemorySize");
42         long freePhysicalMemory = operatingSystemJson.getLongValue("freePhysicalMemorySize");
43         long usedPhysicalMemory = totalPhysicalMemory - freePhysicalMemory;
44         Runtime runtime = Runtime.getRuntime();
45         Map<String,Number> result = new HashMap<>();
46         result.put("memory.physical.total", totalPhysicalMemory);
47         result.put("memory.physical.used", freePhysicalMemory);
48         result.put("memory.physical.free", usedPhysicalMemory);
49         result.put("memory.physical.usage", NumberUtil.div(usedPhysicalMemory, totalPhysicalMemory));
50         result.put("memory.runtime.total", runtime.totalMemory());
51         result.put("memory.runtime.used", runtime.freeMemory());
52         result.put("memory.runtime.max", runtime.totalMemory() - runtime.freeMemory());
53         result.put("memory.runtime.free", runtime.maxMemory() - runtime.totalMemory() + runtime.freeMemory());
54         result.put("memory.runtime.usage", NumberUtil.div(runtime.totalMemory() - runtime.freeMemory(), runtime.totalMemory()));
55         return CommonResult.success(result);
56     }
57
58     /**
59      * @功能:获取磁盘信息
60      * @return
61      */
62     @GetMapping("/disk/info")
63     public CommonResult<List<Map<String,Object>>> queryDiskInfo(){
64         CommonResult<List<Map<String,Object>>> res = new CommonResult<>();
65         try {
66             // 当前文件系统类
67             FileSystemView fsv = FileSystemView.getFileSystemView();
68             // 列出所有windows 磁盘
69             File[] fs = File.listRoots();
70             log.info("查询磁盘信息:"+fs.length+"个");
71             List<Map<String,Object>> list = new ArrayList<>();
72             for (int i = 0; i < fs.length; i++) {
73                 if(fs[i].getTotalSpace()==0) {
74                     continue;
75                 }
76                 Map<String,Object> map = new HashMap(5);
77                 map.put("name", fsv.getSystemDisplayName(fs[i]));
78                 map.put("max", fs[i].getTotalSpace());
79                 map.put("rest", fs[i].getFreeSpace());
80                 map.put("restPPT", (fs[i].getTotalSpace()-fs[i].getFreeSpace())*100/fs[i].getTotalSpace());
81                 list.add(map);
82                 log.info(map.toString());
83             }
84             res.setData(list);
85             res.success("查询成功");
86         } catch (Exception e) {
87             res.setMsg("查询失败"+e.getMessage());
88         }
89         return res;
90     }
91
92 }