首页>代码>SpringBoot+Maven+Echarts实现实时展示CPU内存硬盘性能>/system_performance/src/main/java/cn/temptation/web/IndexController.java
package cn.temptation.web; import org.hyperic.sigar.*; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.text.DecimalFormat; import java.util.*; @Controller public class IndexController { Sigar sigar = new Sigar(); @RequestMapping("/") public String index() { return "index"; } @RequestMapping("/getData") @ResponseBody public Map<String, String> getData() throws SigarException { DecimalFormat df = new DecimalFormat("#.##"); Map<String, String> map = new HashMap<>(); map.put("cpu", df.format(getCPUInfo())); map.put("memory", df.format(getMemoryInfo())); map.put("disk", df.format(getDiskInfo())); return map; } /** * 获取CPU利用率(多核CPU取的利用率最高的数据) * * @return * @throws SigarException */ private Double getCPUInfo() throws SigarException { ArrayList<Double> result = new ArrayList<>(); CpuInfo infos[] = sigar.getCpuInfoList(); CpuPerc cpuList[] = null; cpuList = sigar.getCpuPercList(); // 不管是单块CPU还是多CPU都适用 for (int i = 0; i < infos.length; i++) { result.add(cpuList[i].getSys() * 100); } return Collections.max(result); } /** * 获取内存利用率 * * @return * @throws SigarException */ private Double getMemoryInfo() throws SigarException { // 物理内存信息 Mem mem = sigar.getMem(); return mem.getUsed() * 100 / (double) mem.getTotal(); } /** * 获取内存利用率 * * @return * @throws SigarException */ private Double getDiskInfo() throws SigarException { FileSystem fslist[] = sigar.getFileSystemList(); long total = 0; long used = 0; for (int i = 0; i < fslist.length; i++) { FileSystem fs = fslist[i]; FileSystemUsage usage = sigar.getFileSystemUsage(fs.getDirName()); total += usage.getTotal(); used += usage.getUsed(); } return used * 100 / (double) total; } }

依旧原地徘徊 LV12
1月16日
yulinfeng LV15
2024年7月15日
微信网友_6450287428947968 LV2
2024年5月9日
lsq54365 LV14
2023年7月10日
matintalorr LV10
2023年5月9日
fellowfun LV12
2023年3月16日
内心向阳 LV4
2022年11月30日
pet185377 LV1
2022年6月4日
陈齐尧 LV11
2021年12月15日
初相识 LV6
2021年8月20日

fantesy LV17
3月20日
tinguo002 LV6
3月17日
微信网友_7371218154688512 LV4
2月5日
依旧原地徘徊 LV12
1月16日
大boss LV1
2024年11月5日
yulinfeng LV15
2024年7月15日
ma406805131 LV19
2024年6月29日
f22m1a2b2 LV17
2024年5月31日
微信网友_6450287428947968 LV2
2024年5月9日
quartz LV8
2024年3月13日