使用oshi获取系统信息

maven依赖

     	
            com.github.oshi
            oshi-core
            6.3.2
        

获取CUP信息代码

获取时与windows窗口等查看CUP利用率的信息有差异,本身CUP利用率存在很大的波动。

public static CpuEntity getCpu() throws InterruptedException {
        SystemInfo systemInfo = new SystemInfo();
        GlobalConfig.set(GlobalConfig.OSHI_OS_WINDOWS_CPU_UTILITY, Boolean.TRUE);
        CentralProcessor processor = systemInfo.getHardware().getProcessor();
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        // 睡眠1s
        TimeUnit.SECONDS.sleep(1);
        long[] ticks = processor.getSystemCpuLoadTicks();
        long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
        long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
        long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
        long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
        long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
        long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
        long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
        long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
        long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
        CpuEntity cpuEntity = new CpuEntity();
        cpuEntity.setSys(new DecimalFormat("#.##").format(cSys * 1.0 / totalCpu));
        cpuEntity.setUser(new DecimalFormat("#.##").format(user * 1.0 / totalCpu));
        cpuEntity.setWait(new DecimalFormat("#.##").format(iowait * 1.0 / totalCpu));
        cpuEntity.setWait(new DecimalFormat("#.##").format(idle * 1.0 / totalCpu));
        //  user + system + nice + iowait + irq + softirq + steal
        long cpuUtilization = user + nice + cSys + iowait + irq + softirq + steal;
        cpuEntity.setCombined(new DecimalFormat("#.##").format((cpuUtilization * 1.0 / totalCpu)*100));
        return cpuEntity;
    }

获取内存信息

    public static MemoryEntity getMemory() {
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        MemoryEntity memoryEntity = new MemoryEntity();
        memoryEntity.setMemTotal(osmxb.getTotalPhysicalMemorySize() / 1024 / 1024 / 1024);
        memoryEntity.setMemUsed((osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / 1024 / 1024 / 1024);
        return memoryEntity;
    }

获取磁盘信息

  			File[] roots = File.listRoots();
            Long useSum = 0l;
            Long totalSum = 0l;
            for (File file : roots) {
                long free = file.getFreeSpace();
                long total = file.getTotalSpace();
                long use = total - free;
                useSum += change(use);
                totalSum += change(total);
            }

你可能感兴趣的:(java基础理论,工具类,java)