首页>代码>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; } }
最近下载更多
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日
薯片 LV1
2021年6月15日
nextstep LV11
2021年6月7日
lifanfanfan LV5
2021年4月22日
最近浏览更多
quartz LV7
3月13日
微信网友_6902352269217792 LV3
3月12日
escape1023
2月16日
暂无贡献等级
lilong007 LV20
2023年12月30日
哪里的完整版 LV7
2023年12月15日
1003480239 LV7
2023年10月31日
edpwyg LV14
2023年10月21日
小温ggggg LV6
2023年10月14日
maniheim LV7
2023年9月28日
eternity2002 LV2
2023年9月5日