OSHI is a free JNA-based (native) Java operating system and hardware information library

OSHI is a free JNA-based (native) Java operating system and hardware information library. It does not require the installation of any additional native libraries and 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.

Supported platforms

Windows • Linux • macOS • Unix(AIX、FreeBSD、OpenBSD、Solaris)

Download and dependency management

stable version

Current Development (SNAPSHOT) Download

document

usage

  1. Include OSHI and its dependencies on your classpath. We strongly recommend that you add OSHI as a dependency to your project dependency manager (such as Maven or Gradle).
  2. Create a new instanceSystemInfo
  3. Use getters from SystemInfoto access hardware or operating system components, for example:
<span style="color:#333333"><span style="background-color:#f5f5f5"><code class="language-java"><span style="color:#660066">SystemInfo</span><span style="color:#000000"> si </span><span style="color:#666600">=</span> <span style="color:#000088">new</span> <span style="color:#660066">SystemInfo</span><span style="color:#666600">();</span>
<span style="color:#660066">HardwareAbstractionLayer</span><span style="color:#000000"> hal </span><span style="color:#666600">=</span><span style="color:#000000"> si</span><span style="color:#666600">.</span><span style="color:#000000">getHardware</span><span style="color:#666600">();</span>
<span style="color:#660066">CentralProcessor</span><span style="color:#000000"> cpu </span><span style="color:#666600">=</span><span style="color:#000000"> hal</span><span style="color:#666600">.</span><span style="color:#000000">getProcessor</span><span style="color:#666600">();</span></code></span></span>

See SystemInfoTest.java for an example . To see sample output for your machine:

<span style="color:#333333"><span style="background-color:#f5f5f5"><code class="language-sh"><span style="color:#000000">git clone https</span><span style="color:#666600">://</span><span style="color:#000000">github</span><span style="color:#666600">.</span><span style="color:#000000">com</span><span style="color:#666600">/</span><span style="color:#000000">oshi</span><span style="color:#666600">/</span><span style="color:#000000">oshi</span><span style="color:#666600">.</span><span style="color:#000000">git </span><span style="color:#666600">&&</span><span style="color:#000000"> cd oshi

</span><span style="color:#666600">./</span><span style="color:#000000">mvnw test</span><span style="color:#666600">-</span><span style="color:#000000">compile </span><span style="color:#666600">-</span><span style="color:#000000">pl oshi</span><span style="color:#666600">-</span><span style="color:#000000">core exec</span><span style="color:#666600">:</span><span style="color:#000000">java \
  </span><span style="color:#666600">-</span><span style="color:#660066">Dexec</span><span style="color:#666600">.</span><span style="color:#000000">mainClass</span><span style="color:#666600">=</span><span style="color:#008800">"oshi.SystemInfoTest"</span><span style="color:#000000"> \
  </span><span style="color:#666600">-</span><span style="color:#660066">Dexec</span><span style="color:#666600">.</span><span style="color:#000000">classpathScope</span><span style="color:#666600">=</span><span style="color:#008800">"test"</span></code></span></span>

Some settings can be configured in the oshi.properties file or manipulated using the GlobalConfig class. This should be done at startup, as configuration is not thread-safe and OSHI does not guarantee re-reading of configuration during operation.

This oshi-demoartifact includes several proof-of-concept examples of using OSHI to obtain information, including a basic Swing GUI.

Supported features

  • Computer systems and firmware, substrates
  • OS and version/build
  • Physical (Core) and Logical (Hyper-Threading) CPUs, Processor Groups, NUMA Nodes
  • System and per-processor load, usage tick counters, interrupts, uptime
  • Process uptime, CPU, memory usage, user/group, command line parameters, thread details
  • Physical and virtual memory used/available
  • Mounted file system (type, free and total space, options, read and write)
  • Disk drives (model, serial number, size, read and write) and partitions
  • Network interfaces (IP, bandwidth input/output), network parameters, TCP/UDP statistics
  • Battery status (capacity percentage, remaining time, power usage statistics)
  • USB device
  • Connected monitor (with EDID information), graphics card, and sound card
  • Sensors on some hardware (temperature, fan speed, voltage)

Sample output from OSHI

OSHI provides output for each of its interfaces directly through Java methods. By periodically polling dynamic information (for example, every second), users can calculate and track changes.

System information test

You can see more examples and run SystemInfoTest  and see the complete output of your system by cloning the project and building it with Maven.

oshi-performance

Additionally, this oshi-demomodule includes an OshiGui class that implements a basic Swing GUI to provide suggestions for potential visualizations using OSHI in UI, monitoring or alerting applications, as shown below. For a more advanced GUI based on this approach, see the MooInfo project .

output

General information about operating systems and computer system hardware:

 

By measuring the ticks (user, nice, system, idle, iowait, and irq) between intervals, the usage percentage can be calculated. Information for each processor is also provided.

 

Process information includes CPU and memory available for each process.

Memory and swap file information is available.

 

Provides system battery statistics:

Power Sources:
 Name: InternalBattery-0, Device Name: bq20z451,
 RemainingCapacityPercent: 100.0%, Time Remaining: 5:42, Time Remaining Instant: 5:42,
 Power Usage Rate: -16045.216mW, Voltage: 12.694V, Amperage: -1264.0mA,
 Power OnLine: false, Charging: false, Discharging: true,
 Capacity Units: MAH, Current Capacity: 7213, Max Capacity: 7315, Design Capacity: 7336,
 Cycle Count: 6, Chemistry: LIon, Manufacture Date: 2019-06-11, Manufacturer: SMP,
 SerialNumber: D869243A2U3J65JAB, Temperature: 30.46°C

The EDID of each display is provided. This can be parsed with various utilities to obtain detailed information. OSHI provides a summary of selected data.

Displays:
 Display 0:
  Manuf. ID=SAM, Product ID=2ad, Analog, Serial=HA19, ManufDate=3/2008, EDID v1.3
  41 x 27 cm (16.1 x 10.6 in)
  Preferred Timing: Clock 106MHz, Active Pixels 3840x2880
  Range Limits: Field Rate 56-75 Hz vertical, 30-81 Hz horizontal, Max clock: 140 MHz
  Monitor Name: SyncMaster
  Serial Number: H9FQ345476
 Display 1:
  Manuf. ID=SAM, Product ID=226, Analog, Serial=HA19, ManufDate=4/2007, EDID v1.3
  41 x 26 cm (16.1 x 10.2 in)
  Preferred Timing: Clock 106MHz, Active Pixels 3840x2880
  Range Limits: Field Rate 56-75 Hz vertical, 30-81 Hz horizontal, Max clock: 140 MHz
  Monitor Name: SyncMaster
  Serial Number: HMCP431880

Displays disk and usage (read, write, transfer times) and can map partitions to file systems.

Disks:
 disk0: (model: SanDisk Ultra II 960GB - S/N: 161008800550) size: 960.2 GB, reads: 1053132 (23.0 GiB), writes: 243792 (11.1 GiB), xfer: 73424854 ms
 |-- disk0s1: EFI (EFI System Partition) Maj:Min=1:1, size: 209.7 MB
 |-- disk0s2: Macintosh HD (Macintosh SSD) Maj:Min=1:2, size: 959.3 GB @ /
 disk1: (model: Disk Image - S/N: ) size: 960.0 GB, reads: 3678 (60.0 MiB), writes: 281 (8.6 MiB), xfer: 213627 ms
 |-- disk1s1: EFI (EFI System Partition) Maj:Min=1:4, size: 209.7 MB
 |-- disk1s2: Dropbox (disk image) Maj:Min=1:5, size: 959.7 GB @ /Volumes/Dropbox

Sensor readings are available with some hardware (see notes in the API ).

Sensors:
 CPU Temperature: 69.8°C
 Fan Speeds:[4685, 4687]
 CPU Voltage: 3.9V

Connected USB devices can be listed:

USB Devices:
 AppleUSBEHCI
 |-- Root Hub Simulation Simulation (Apple Inc.)
     |-- IOUSBHostDevice
         |-- IR Receiver (Apple Computer, Inc.)
         |-- USB Receiver (Logitech)
 AppleUSBEHCI
 |-- Root Hub Simulation Simulation (Apple Inc.)
     |-- FaceTime HD Camera (Built-in) (Apple Inc.) [s/n: DJHB1V077FDH5HL0]
     |-- IOUSBHostDevice
         |-- Apple Internal Keyboard / Trackpad (Apple Inc.)
         |-- BRCM2070 Hub (Apple Inc.)
             |-- Bluetooth USB Host Controller (Apple Inc.)
 AppleUSBEHCI
 |-- Root Hub Simulation Simulation (Apple Inc.)
     |-- IOUSBHostDevice
         |-- Apple Thunderbolt Display (Apple Inc.) [s/n: 162C0C25]
         |-- Display Audio (Apple Inc.) [s/n: 162C0C25]
         |-- FaceTime HD Camera (Display) (Apple Inc.) [s/n: CCGCAN000TDJ7DFX]
         |-- USB2.0 Hub
             |-- ANT USBStick2 (Dynastream Innovations) [s/n: 051]
             |-- Fitbit Base Station (Fitbit Inc.)

 https://github.com/oshi/oshiicon-default.png?t=M4ADhttps://github.com/oshi/oshi

Guess you like

Origin blog.csdn.net/allway2/article/details/124844898#comments_28968427