OSHI Documentation

repository·master·Indexed 26 days ago

https://github.com/oshi/oshi

A free, cross-platform native library for Java that provides detailed information about Operating Systems and Hardware, including CPU, memory, disks, network, and sensors, without requiring additional native library installations. It offers multiple implementations including JNA (oshi-core), FFM (oshi-core-ffm for JDK 25+), and a native-free version for Linux (oshi-common). The library includes a metrics module for Micrometer and OpenTelemetry integration, as well as JMX support for exposing system metrics.

Tokens
15K
Snippets
36
Records
75
Agent score
89%

What's inside OSHI

  1. Compare OSHI with SIGAR

    master

    OSHI is a modern alternative to Hyperic's SIGAR for cross-platform system monitoring. Key differences include:

    • No Additional DLLs: Unlike SIGAR, which requires platform-specific native C DLLs, OSHI uses JNA to access native APIs, eliminating the need for manual installation of platform-specific binaries.
    • Active Development: SIGAR has been abandoned (last release in 2010), whereas OSHI is actively maintained by open-source volunteers.
    • Support: OSHI has an active community and developer support, unlike SIGAR which is unsupported.
  2. Configure OSHI for Java Module System (JPMS)

    master

    OSHI provides three named JPMS modules. Add the one that matches your native access preference to your module-info.java:

    • com.github.oshi.common: Public API interfaces and native-free implementation (oshi-common). Works on JDK 8+ on the classpath; JDK 9+ on the module path. Currently supports Linux only.
    • com.github.oshi: JNA-based implementation (oshi-core). Works on JDK 8+ on the classpath; JDK 9+ on the module path.
    • com.github.oshi.ffm: FFM-based implementation (oshi-core-ffm). Requires JDK 25+.
    requires com.github.oshi.common; // API + native-free implementation (Linux only)
    requires com.github.oshi;        // JNA
    requires com.github.oshi.ffm;    // FFM
  3. Optimize polling by reusing SystemInfo

    master

    When building a monitoring agent that samples metrics repeatedly, you should hold a single SystemInfo instance and its subordinate objects rather than creating a new one for every poll.

    Why?

    • Reconstructing the CentralProcessor (CPU topology/identifiers) is expensive (milliseconds per poll on Windows/macOS/Linux).
    • Holding the object graph costs very little memory (roughly ~150 KB for hardware/OS caches, plus ~0.65 MB if you also hold a process-list snapshot).

    Exceptions:

    • Memory constraints: If you have extremely tight memory limits, discard SystemInfo between collections to release memory.
    • GlobalMemory: Recreating is cheap; there is no significant CPU win for reusing GlobalMemory.
    • Process Lists: getProcesses() is always a live, expensive query (~10ms and 1-15MB per poll). To track specific processes efficiently, hold the individual OSProcess objects and call updateAttributes() instead of re-querying the entire list.
  4. Export OSHI metrics to OpenTelemetry via Java Agent

    master

    If running with the OpenTelemetry Java Agent, enable the Micrometer bridge using the system property -Dotel.instrumentation.micrometer.enabled=true. Then, register the metrics with Micrometer's global registry. The agent will automatically bridge the global registry to the OpenTelemetry SDK.

    -Dotel.instrumentation.micrometer.enabled=true
    import io.micrometer.core.instrument.Metrics;
    
    OshiMetrics.bindTo(Metrics.globalRegistry, SystemInfoFactory.create());
  5. Start a JMXOshiAgent server

    master

    To run an OSHI agent that exposes system metrics via JMX, use the CreateJmxOshiAgent factory class. You can either specify a port and host directly or provide a HashMap of properties for advanced configuration (such as custom RMISocketFactory or TLS settings).

    After creating the agent, you must call .start() to begin the server.

  6. Configure privilege escalation on Linux

    master

    OSHI can access restricted system information (like dmidecode hardware details or /proc/<pid>/io stats) without running the entire application as root. You can configure OSHI to use a command prefix (like sudo -n) for specific allowed commands or file paths.

    Steps to implement:

    1. Configure passwordless sudo for the required commands in your system's sudoers file.
    2. Set the OSHI configuration properties via GlobalConfig, oshi.properties, or Java System Properties.

    Available Configuration Properties:

    • oshi.os.linux.privileged.prefix: The command prefix to use (e.g., sudo -n).
    • oshi.os.linux.privileged.allowlist: A comma-separated list of commands eligible for the prefix (e.g., dmidecode,lshw).
    • oshi.os.linux.privileged.file.allowlist: A comma-separated list of file paths or glob patterns eligible for privileged reading via the prefix + cat (e.g., /proc/*/io).
    GlobalConfig.set(GlobalConfig.OSHI_OS_LINUX_PRIVILEGED_PREFIX, "sudo -n");
    GlobalConfig.set(GlobalConfig.OSHI_OS_LINUX_PRIVILEGED_ALLOWLIST, "dmidecode,lshw");
    GlobalConfig.set(GlobalConfig.OSHI_OS_LINUX_PRIVILEGED_FILE_ALLOWLIST, "/proc/*/io");
  7. Optimize Windows WMI queries via COM initialization

    master
    Each WMI query in OSHI performs COM initialization/uninitialization, adding 3-5ms of latency per query. To improve performance, you can initialize COM externally in your application and extend OSHI's WmiQueryHandler with a version that omits this overhead. See the UserComInit and WmiNoComInitQueryHandler classes in the oshi-demo artifact for implementation examples.
  8. Upgrade OSHI from 6.x to 7.x: Module path and JPMS changes

    master

    In 7.x, oshi-core contains a full module-info.class. If using the module path, it is now a named module with explicit exports and opens directives.

    FFM Module Rename

    The FFM module artifact is now oshi-core-ffm (previously oshi-core-java25). The JPMS module name is now com.github.oshi.ffm. Update your module-info.java as follows:

    requires com.github.oshi.ffm;

    Handling Module Path behavior

    If you rely on accessing non-exported internal packages via the module path, you may need to add --add-opens flags or switch to the classpath. To force classpath behavior in Maven Surefire:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <useModulePath>false</useModulePath>
        </configuration>
    </plugin>
  9. Get reliable sensor information on Windows

    master

    Standard Windows APIs for sensor information can be unreliable. For more reliable data, add jLibreHardwareMonitor as a dependency to your project. OSHI uses this as an optional dependency.

    <!-- Maven -->
    <dependency>
        <groupId>io.github.pandalxb</groupId>
        <artifactId>jLibreHardwareMonitor</artifactId>
    </dependency>
    // Gradle
    implementation("io.github.pandalxb:jLibreHardwareMonitor")
  10. Contributing new features to OSHI

    master

    Feature implementation is driven by community contribution. If you want a feature implemented:

    • Submit Code: If you can provide the full implementation, it is highly likely to be added.
    • Provide Information: If you cannot code, providing cross-platform documentation or pointers to where information can be found is also highly valued.
    • Submit Issues: You can submit an issue to request a feature, though implementation depends on developer availability and documentation availability.