- 引入依赖
com.github.oshi oshi-core3.9.1 net.java.dev.jna jna4.5.2 cn.hutool hutool-all5.3.2 org.projectlombok lombok1.18.6
- 添加5个性能参数实体类
import cn.hutool.core.util.NumberUtil; import lombok.Data; import java.io.Serializable; @Data public class Cpu implements Serializable { private static final long serialVersionUID = 1L; private int cpuNum; private double total; private double sys; private double used; private double wait; private double free; public double getTotal() { return NumberUtil.round(NumberUtil.mul(total, 100), 2).doublevalue(); } public double getSys() { return NumberUtil.round(NumberUtil.mul(sys / total, 100), 2).doublevalue(); } public double getUsed() { return NumberUtil.round(NumberUtil.mul(used / total, 100), 2).doublevalue(); } public double getWait() { return NumberUtil.round(NumberUtil.mul(wait / total, 100), 2).doublevalue(); } public double getFree() { return NumberUtil.round(NumberUtil.mul(free / total, 100), 2).doublevalue(); } }
import cn.hutool.core.date.DateUnit; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.NumberUtil; import lombok.Data; import java.io.Serializable; import java.lang.management.ManagementFactory; import java.util.Date; @Data public class Jvm implements Serializable { private static final long serialVersionUID = 1L; private double total; private double max; private double free; private String version; private String home; public double getTotal() { return NumberUtil.div(total, (1024 * 1024), 2); } public double getMax() { return NumberUtil.div(max, (1024 * 1024), 2); } public double getFree() { return NumberUtil.div(free, (1024 * 1024), 2); } public double getUsed() { return NumberUtil.div(total - free, (1024 * 1024), 2); } public String getVersion() { return version; } public String getHome() { return home; } public double getUsage() { return NumberUtil.mul(NumberUtil.div(total - free, total, 4), 100); } public String getName() { return ManagementFactory.getRuntimeMXBean().getVmName(); } public String getStartTime() { long time = ManagementFactory.getRuntimeMXBean().getStartTime(); Date date = new Date(time); return DateUtil.formatDateTime(date); } public String getRunTime() { long time = ManagementFactory.getRuntimeMXBean().getStartTime(); Date date = new Date(time); //运行多少分钟 long runMS = DateUtil.between(date, new Date(), DateUnit.MS); long nd = 1000 * 24 * 60 * 60; long nh = 1000 * 60 * 60; long nm = 1000 * 60; long day = runMS / nd; long hour = runMS % nd / nh; long min = runMS % nd % nh / nm; return day + "天" + hour + "小时" + min + "分钟"; } }
import cn.hutool.core.util.NumberUtil; import lombok.Data; import java.io.Serializable; @Data public class Mem implements Serializable { private static final long serialVersionUID = 1L; private double total; private double used; private double free; public double getTotal() { return NumberUtil.div(total, (1024 * 1024 * 1024), 2); } public double getUsed() { return NumberUtil.div(used, (1024 * 1024 * 1024), 2); } public double getFree() { return NumberUtil.div(free, (1024 * 1024 * 1024), 2); } public double getUsage() { return NumberUtil.mul(NumberUtil.div(used, total, 4), 100); } }
import lombok.Data; import java.io.Serializable; @Data public class Sys implements Serializable { private static final long serialVersionUID = 1L; private String computerName; private String computerIp; private String userDir; private String osName; private String osArch; }
import lombok.Data; import java.io.Serializable; @Data public class SysFile implements Serializable { private static final long serialVersionUID = 1L; private String dirName; private String sysTypeName; private String typeName; private String total; private String free; private String used; private double usage; }
- 查询服务器相关信息
public class Server { private static final int OSHI_WAIT_SECOND = 1000; private Cpu cpu = new Cpu(); private Mem mem = new Mem(); private Jvm jvm = new Jvm(); private Sys sys = new Sys(); private ListsysFiles = new linkedList<>(); public Cpu getCpu() { return cpu; } public void setCpu(Cpu cpu) { this.cpu = cpu; } public Mem getMem() { return mem; } public void setMem(Mem mem) { this.mem = mem; } public Jvm getJvm() { return jvm; } public void setJvm(Jvm jvm) { this.jvm = jvm; } public Sys getSys() { return sys; } public void setSys(Sys sys) { this.sys = sys; } public List getSysFiles() { return sysFiles; } public void setSysFiles(List sysFiles) { this.sysFiles = sysFiles; } public void copyTo() throws Exception { SystemInfo si = new SystemInfo(); HardwareAbstractionLayer hal = si.getHardware(); setCpuInfo(hal.getProcessor()); setMemInfo(hal.getMemory()); setSysInfo(); setJvmInfo(); setSysFiles(si.getOperatingSystem()); } private void setCpuInfo(CentralProcessor processor) { // CPU信息 long[] prevTicks = processor.getSystemCpuLoadTicks(); Util.sleep(OSHI_WAIT_SECOND); long[] ticks = processor.getSystemCpuLoadTicks(); long nice = ticks[TickType.NICE.getIndex()] - prevTicks[TickType.NICE.getIndex()]; long irq = ticks[TickType.IRQ.getIndex()] - prevTicks[TickType.IRQ.getIndex()]; long softirq = ticks[TickType.SOFTIRQ.getIndex()] - prevTicks[TickType.SOFTIRQ.getIndex()]; long steal = ticks[TickType.STEAL.getIndex()] - prevTicks[TickType.STEAL.getIndex()]; long cSys = ticks[TickType.SYSTEM.getIndex()] - prevTicks[TickType.SYSTEM.getIndex()]; long user = ticks[TickType.USER.getIndex()] - prevTicks[TickType.USER.getIndex()]; long iowait = ticks[TickType.IOWAIT.getIndex()] - prevTicks[TickType.IOWAIT.getIndex()]; long idle = ticks[TickType.IDLE.getIndex()] - prevTicks[TickType.IDLE.getIndex()]; long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal; cpu.setCpuNum(processor.getLogicalProcessorCount()); cpu.setTotal(totalCpu); cpu.setSys(cSys); cpu.setUsed(user); cpu.setWait(iowait); cpu.setFree(idle); } private void setMemInfo(GlobalMemory memory) { mem.setTotal(memory.getTotal()); mem.setUsed(memory.getTotal() - memory.getAvailable()); mem.setFree(memory.getAvailable()); } private void setSysInfo() { Properties props = System.getProperties(); sys.setComputerName(NetUtil.getLocalhost().getHostName()); sys.setComputerIp(NetUtil.getLocalhost().getHostAddress()); sys.setOsName(props.getProperty("os.name")); sys.setOsArch(props.getProperty("os.arch")); sys.setUserDir(props.getProperty("user.dir")); } private void setJvmInfo() { Properties props = System.getProperties(); jvm.setTotal(Runtime.getRuntime().totalMemory()); jvm.setMax(Runtime.getRuntime().maxMemory()); jvm.setFree(Runtime.getRuntime().freeMemory()); jvm.setVersion(props.getProperty("java.version")); jvm.setHome(props.getProperty("java.home")); } private void setSysFiles(OperatingSystem os) { FileSystem fileSystem = os.getFileSystem(); OSFileStore[] fsArray = fileSystem.getFileStores(); for (OSFileStore fs : fsArray) { long free = fs.getUsableSpace(); long total = fs.getTotalSpace(); long used = total - free; SysFile sysFile = new SysFile(); sysFile.setDirName(fs.getMount()); sysFile.setSysTypeName(fs.getType()); sysFile.setTypeName(fs.getName()); sysFile.setTotal(convertFileSize(total)); sysFile.setFree(convertFileSize(free)); sysFile.setUsed(convertFileSize(used)); sysFile.setUsage(NumberUtil.round(NumberUtil.mul(used, total, 4), 100).doublevalue()); sysFiles.add(sysFile); } } public String convertFileSize(long size) { long kb = 1024; long mb = kb * 1024; long gb = mb * 1024; if (size >= gb) { return String.format("%.1f GB", (float) size / gb); } else if (size >= mb) { float f = (float) size / mb; return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f); } else if (size >= kb) { float f = (float) size / kb; return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f); } else { return String.format("%d B", size); } } }
- 测试
public static void main(String[] args) throws Exception { Server server = new Server(); server.copyTo(); System.out.println(JSON.toJSONString(server)); }
输出:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)