系统内存硬盘cpu数据获取
package com.tod.bim.monitoring.util; import org.hyperic.sigar.*; /** * @author zj * @version 1.0 * @functin * @date 2020/5/19 16:41 */ public class SystemMonitoryUtils { /** * cpu情况 * * @return [空闲率,使用率] * @throws SigarException */ public static Double[] cpu() throws SigarException { Sigar sigar = new Sigar(); CpuPerc[] cpuList = sigar.getCpuPercList(); double idles = 0; double combined = 0; int length = cpuList.length; for (int i = 0; i < cpuList.length; i++) {// 不管是单块CPU还是多CPU都适用 CpuPerc cpu = cpuList[i]; idles += cpu.getIdle(); combined += cpu.getCombined(); } return new Double[]{idles / length, combined / length}; } /** * 内存情况 * * @return [内存剩余量,内存使用量] * @throws SigarException */ public static Double[] momery() throws SigarException { Sigar sigar = new Sigar(); Mem mem = sigar.getMem(); Double[] momerys = { mem.getFree() / 1024D / 1024 / 1024, mem.getUsed() / 1024D / 1024 / 1024 }; return momerys; } /** * 硬盘情况 * @return [硬盘剩余量,硬盘使用量] * @throws Exception */ public static Double[] disk() throws Exception { Sigar sigar = new Sigar(); FileSystem fslist[] = sigar.getFileSystemList(); double total = 0; double free = 0; double avai = 0; double used = 0; double usePercent = 0; for (int i = 0; i < fslist.length; i++) { FileSystem fs = fslist[i]; FileSystemUsage usage = sigar.getFileSystemUsage(fs.getDirName()); switch (fs.getType()) { case 0: // TYPE_UNKNOWN :未知 break; case 1: // TYPE_NONE break; case 2: // TYPE_LOCAL_DISK : 本地硬盘 //total += (usage.getTotal() / 1024D / 1024); free += usage.getFree() / 1024D / 1024; //avai += usage.getAvail() / 1024D / 1024; used += usage.getUsed() / 1024D / 1024; //usePercent += usage.getUsePercent(); break; case 3:// TYPE_NETWORK :网络 break; case 4:// TYPE_RAM_DISK :闪存 break; case 5:// TYPE_CDROM :光驱 break; case 6:// TYPE_SWAP :页面交换 break; } } return new Double[]{free,used}; } }
maven依赖
<dependency> <groupId>org.fusesource</groupId> <artifactId>sigar</artifactId> <version>1.6.4</version> </dependency>
上一篇:
Python 安装包管理工具 pip