Springboot uses OSHI to obtain and manipulate operating system and hardware information

1. Introduction to OSHI

OSHI (Operating System and Hardware Information) is an open source Java library for obtaining and manipulating operating system and hardware information. It provides a set of easy-to-use APIs that can be used to retrieve and monitor system and hardware-related data such as operating system type, processor information, memory usage, hard disk information, network interface, etc.

The main functions of OSHI include:

  1. Obtain operating system information: You can obtain the name, version, build information, number of bits, etc. of the operating system. You can also obtain information such as the startup time of the operating system, system load, and the number of current processes.

  2. Obtain hardware information: You can obtain processor (CPU) information, including model, frequency, core number, temperature, etc. You can also get memory (RAM) information, including total capacity, available capacity, usage, etc. In addition, you can also get hard disk (disk) information, including model, capacity, usage, etc. You can also get information about hardware such as batteries, network cards, and USB devices.

  3. Obtain network information: You can obtain information about the network interface (network card), including name, IP address, MAC address, received and sent data volume, etc.

  4. Monitoring system and hardware: You can monitor and collect system and hardware data in real time through the API provided by OSHI, such as CPU usage, memory usage, network traffic, etc.

Using OSHI is very simple, just add the OSHI library to the dependencies of your Java project, and use the API provided by OSHI to get the required system and hardware information. You can selectively obtain and process various information according to your needs.

Overall, OSHI is a powerful and easy-to-use Java library that can be used to obtain and monitor operating system and hardware information, helping you write more interactive and functional applications for system monitoring, hardware information acquisition and performance tuning program.

2. Use of OSHI

2.1 Introducing dependencies

            <dependency>
                <groupId>com.github.oshi</groupId>
                <artifactId>oshi-core</artifactId>
                <version>6.4.0</version>
            </dependency>

2.2 Define the parametric model

We mainly focus on information such as the server's CPU, memory, and the space capacity usage of the storage disk on which it is mounted. The specific model is as follows:

2.2.1 CPU

public class Cpu
{
    
    
    /**
     * 核心数
     */
    private int cpuNum;

    /**
     * CPU总的使用率
     */
    private double total;

    /**
     * CPU系统使用率
     */
    private double sys;

    /**
     * CPU用户使用率
     */
    private double used;

    /**
     * CPU当前等待率
     */
    private double wait;

    /**
     * CPU当前空闲率
     */
    private double free;

    public int getCpuNum()
    {
    
    
        return cpuNum;
    }

    public void setCpuNum(int cpuNum)
    {
    
    
        this.cpuNum = cpuNum;
    }

    public double getTotal()
    {
    
    
        return Arith.round(Arith.mul(total, 100), 2);
    }

    public void setTotal(double total)
    {
    
    
        this.total = total;
    }

    public double getSys()
    {
    
    
        return Arith.round(Arith.mul(sys / total, 100), 2);
    }

    public void setSys(double sys)
    {
    
    
        this.sys = sys;
    }

    public double getUsed()
    {
    
    
        return Arith.round(Arith.mul(used / total, 100), 2);
    }

    public void setUsed(double used)
    {
    
    
        this.used = used;
    }

    public double getWait()
    {
    
    
        return Arith.round(Arith.mul(wait / total, 100), 2);
    }

    public void setWait(double wait)
    {
    
    
        this.wait = wait;
    }

    public double getFree()
    {
    
    
        return Arith.round(Arith.mul(free / total, 100), 2);
    }

    public void setFree(double free)
    {
    
    
        this.free = free;
    }
}

2.2.2 Memory

public class Mem
{
    
    
    /**
     * 内存总量
     */
    private double total;

    /**
     * 已用内存
     */
    private double used;

    /**
     * 剩余内存
     */
    private double free;

    public double getTotal()
    {
    
    
        return Arith.div(total, (1024 * 1024 * 1024), 2);
    }

    public void setTotal(long total)
    {
    
    
        this.total = total;
    }

    public double getUsed()
    {
    
    
        return Arith.div(used, (1024 * 1024 * 1024), 2);
    }

    public void setUsed(long used)
    {
    
    
        this.used = used;
    }

    public double getFree()
    {
    
    
        return Arith.div(free, (1024 * 1024 * 1024), 2);
    }

    public void setFree(long free)
    {
    
    
        this.free = free;
    }

    public double getUsage()
    {
    
    
        return Arith.mul(Arith.div(used, total, 4), 100);
    }
}

2.2.3 Hard Disk

public class SysFile
{
    
    
    /**
     * 盘符路径
     */
    private String dirName;

    /**
     * 盘符类型
     */
    private String sysTypeName;

    /**
     * 文件类型
     */
    private String typeName;

    /**
     * 总大小
     */
    private String total;

    /**
     * 剩余大小
     */
    private String free;

    /**
     * 已经使用量
     */
    private String used;

    /**
     * 资源的使用率
     */
    private double usage;

    public String getDirName()
    {
    
    
        return dirName;
    }

    public void setDirName(String dirName)
    {
    
    
        this.dirName = dirName;
    }

    public String getSysTypeName()
    {
    
    
        return sysTypeName;
    }

    public void setSysTypeName(String sysTypeName)
    {
    
    
        this.sysTypeName = sysTypeName;
    }

    public String getTypeName()
    {
    
    
        return typeName;
    }

    public void setTypeName(String typeName)
    {
    
    
        this.typeName = typeName;
    }

    public String getTotal()
    {
    
    
        return total;
    }

    public void setTotal(String total)
    {
    
    
        this.total = total;
    }

    public String getFree()
    {
    
    
        return free;
    }

    public void setFree(String free)
    {
    
    
        this.free = free;
    }

    public String getUsed()
    {
    
    
        return used;
    }

    public void setUsed(String used)
    {
    
    
        this.used = used;
    }

    public double getUsage()
    {
    
    
        return usage;
    }

    public void setUsage(double usage)
    {
    
    
        this.usage = usage;
    }
}

2.2.4 Implementation

public class Server
{
    
    
    private static final int OSHI_WAIT_SECOND = 1000;
    
    /**
     * CPU相关信息
     */
    private Cpu cpu = new Cpu();

    /**
     * 內存相关信息
     */
    private Mem mem = new Mem();

    /**
     * 磁盘相关信息
     */
    private List<SysFile> sysFiles = new LinkedList<SysFile>();

    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 List<SysFile> getSysFiles()
    {
    
    
        return sysFiles;
    }

    public void setSysFiles(List<SysFile> sysFiles)
    {
    
    
        this.sysFiles = sysFiles;
    }

    public void copyTo() throws Exception
    {
    
    
        SystemInfo si = new SystemInfo();
        HardwareAbstractionLayer hal = si.getHardware();

        setCpuInfo(hal.getProcessor());

        setMemInfo(hal.getMemory());

        setSysFiles(si.getOperatingSystem());
    }

    /**
     * 设置CPU信息
     */
    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 setSysFiles(OperatingSystem os)
    {
    
    
        FileSystem fileSystem = os.getFileSystem();
        List<OSFileStore> fsArray = fileSystem.getFileStores();
        for (OSFileStore fs : fsArray)
        {
    
    
            if (fs.getType().equals("ext4") || fs.getType().equals("nfs4")){
    
    
                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(Arith.mul(Arith.div(used, total, 4), 100));
                sysFiles.add(sysFile);
            }

        }
    }
    /**
     * 字节转换
     * 
     * @param size 字节大小
     * @return 转换后值
     */
    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);
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_44727769/article/details/130860777