Resource Obtain system info using OSHI library.

Discussion in 'Spigot Plugin Development' started by TheViperShow, Feb 27, 2020.

  1. Using the OSHI (Operating System & Hardware Information) library is one of the easiest way to obtain accurate hardware/software information about the machine that will run our code.
    OSHI aims to provide a cross-platform library to obtain system information, such as OS info, processes. disks and partitions, memory & CPU info, devices, sensors, etc.
    Here's the complete list of features from their github repository:
    upload_2020-2-27_13-34-41.png
    How would I implement it?
    It is very simple to implement this library into a minecraft plugin, in this resource i'll be showing how to do it using Maven and shading.
    You will first need to go to the current pom.xml used to build the plugin, and find add this to the repositories list:

    Code (Text):
    <repository>
      <id>oshi-core</id>
      <url>https://mvnrepository.com/artifact/com.github.oshi/oshi-core</url>
    </repository>

    After having added this, you will need to add it in the dependencies list by adding this:
    Code (Text):
    <dependency
      <groupId>com.github.oshi</groupId>
      <artifactId>oshi-core</artifactId>
      <version>4.3.1</version>
    </dependency>

    You then need to shade the library into the plugin, this will increase the plugin size since we're shading the library in, this is how I've done it using the maven-shade-plugin:
    upload_2020-2-27_15-21-26.png

    How to use it?
    Using the OSHI's library is very simple, you'll first want to instantiate SystemInfo in your code then you can get different types of "Hardware" classes, such as GlobalMemory which represents the system Memory, or CentralProcessor which represents the system CPU.
    As you can see below I've declared the variables final , that's because their value is not going to be assigned more than once and should not change.

    Here is a little snippet to show how to get the CPU Model , and some general info:

    Code (Java):
    private static final SystemInfo SYSTEM_INFO = new SystemInfo();
    private static final HardwareAbstractionLayer HAL = SYSTEM_INFO.getHardware();
    private static final CentralProcessor CPU = HAL.getProcessor();
    private static final CentralProcessor.ProcessorIdentifier CPU_IDENTIFIER = CPU.getProcessorIdentifier();

    public static String getCpuModel() {
            return CPU_IDENTIFIER.getModel();
        }
    public static String getCpuModelName() {
            return CPU_IDENTIFIER.getName();
        }
    public static String getCpuMaxFrequency() {
            return String.format("%.2f GHz", CPU.getMaxFreq()/1E9F);
        }
    public static int getCpuCores() {
        return centralProcessor.getPhysicalProcessorCount();
    }

    public static int getCpuThreads() {
        return centralProcessor.getLogicalProcessorCount();
    }
     
    Feel free to correct any of the things above as I'm not a professional, rather someone who wants to share this beatiful lib.
     
    #1 TheViperShow, Feb 27, 2020
    Last edited: Feb 28, 2020
    • Like Like x 2
    • Useful Useful x 1