java 获取系统硬件信息

引言

自己做的一个小项目中的需求,这里提供一些解决思路。

方案

方案一

使用WMIC 或 cat /proc/cpuinfo

Process wmic = Runtime.getRuntime().exec("WMIC cpu");
        InputStream is = wmic.getInputStream();
        while(wmic.isAlive()){
            Thread.sleep(1000);
        }
        byte[] bytes = is.readAllBytes();
        String s = new String(bytes);
        System.out.println(">>>");
        System.out.println(s);
        System.out.println("<<<");

优点:便于理解
缺点:慢、资源消耗大,平台限制

System.getProperty

Properties properties = System.getProperties();
        Set<Map.Entry<Object, Object>> entries = properties.entrySet();
        for (Map.Entry<Object, Object> entry : entries) {
            System.out.println(entry.getKey() + "\t" + entry.getValue());
        }

优点:直接通过Java内部命令获取信息
缺点:获取到的信息不够用且大多数是虚拟机相关信息

OSHI

开源地址:https://github.com/oshi/oshi

SystemInfo oshi = new oshi.SystemInfo();
        CentralProcessor processor = oshi.getHardware().getProcessor();
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        Util.sleep(1000);
        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;
        CpuInfo cpuInfo = new CpuInfo();
        cpuInfo.setCpuNum(processor.getLogicalProcessorCount());
        cpuInfo.setTotal(totalCpu);
        cpuInfo.setSys(cSys);
        cpuInfo.setUsed(user);
        cpuInfo.setWait(ioWait);
        cpuInfo.setFree(idle);

        SystemInfo si = new SystemInfo();
        GlobalMemory memory = si.getHardware().getMemory();
        List<PhysicalMemory> physicalMemory = memory.getPhysicalMemory();
        VirtualMemory virtualMemory = memory.getVirtualMemory();
        MemoryInfo memoryInfo = new MemoryInfo();
        memoryInfo.setPhy_number(physicalMemory.size());
        memoryInfo.setPhy_total(memory.getTotal());
        memoryInfo.setPhy_free(memory.getAvailable());
        memoryInfo.setPhy_used(memory.getTotal() - memory.getAvailable());
        System.out.println(cpuInfo);
        System.out.println(memoryInfo);

CpuInfo(cpuNum=16, total=16252.0, sys=391.0, used=1328.0, wait=0.0, free=14470.0)
MemoryInfo(phy_total=3.407325184E10, phy_number=2.0, phy_used=1.853145088E10, phy_free=1.554180096E10)

引用Github段落:
Supported Features

  • Computer System and firmware, baseboard
  • Operating System and Version/Build
  • Physical (core) and Logical (hyperthreaded) CPUs, processor groups, NUMA nodes
  • System and per-processor load, usage tick counters, interrupts, uptime
  • Process uptime, CPU, memory usage, user/group, command line args, thread details
  • Physical and virtual memory used/available
  • Mounted filesystems (type, usable and total space, options, reads and writes)
  • Disk drives (model, serial, size, reads and writes) and partitions
  • Network interfaces (IPs, bandwidth in/out), network parameters, TCP/UDP statistics
  • Battery state (% capacity, time remaining, power usage stats)
  • USB Devices
  • Connected displays (with EDID info), graphics and audio cards
  • Sensors (temperature, fan speeds, voltage) on some hardware

评论 0

还没有评论,来说点什么吧。

提交后需要审核才会公开。