OshiUtil

Overview

Oshi is a free Java-based operating system and hardware information library based on JNA. Its Github address is: https://github.com/oshi/oshi.

Its advantage is that it does not require the installation of any other native libraries, and it aims to provide a cross-platform implementation to retrieve system information, such as operating system version, processes, memory and CPU usage, disks and partitions, devices, sensors, etc.

The content that this library can monitor includes:

  1. Computer system and firmware, motherboard
  2. Operating system and version/build
  3. Physical (core) and logical (hyper-threading) CPUs, processor groups, NUMA nodes
  4. System and load percentage and tick counter for each processor
  5. CPU uptime, processes and threads
  6. Process uptime, CPU, memory usage, user/group, command line
  7. Used/available physical and virtual memory
  8. Mounted file systems (type, available space and total space)
  9. Disk drives (model, serial number, size) and partitions
  10. Network interfaces (IP, bandwidth input/output)
  11. Battery status (percentage of charge, remaining time, battery usage statistics)
  12. Connected displays (with EDID information)
  13. USB devices
  14. Sensors (temperature, fan speed, voltage)

In other words, with a front-end interface, system monitoring can be completely handled.

Usage

First, introduce the Oshi library:

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

Then you can call the relevant API to get the relevant information.

For example, if we want to get the total amount of memory:

long total = OshiUtil.getMemory().getTotal();

We can also get some information about the CPU:

CpuInfo cpuInfo = OshiUtil.getCpuInfo();
Console.log(cpuInfo);
CpuInfo{CPU core count = 12, total CPU usage = 12595.0, system CPU usage = 1.74, user CPU usage = 6.69, current CPU wait rate = 0.0, current CPU idle rate = 91.57, CPU utilization rate = 8.43, CPU model information ='AMD Ryzen 5 4600U with Radeon Graphics         
 1 physical CPU package(s)
 6 physical CPU core(s)
 12 logical CPU(s)
Identifier: AuthenticAMD Family 23 Model 96 Stepping 1
ProcessorID: xxxxxxxxx
Microarchitecture: unknown'}