您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Java中怎么通過oshi獲取系統和硬件信息,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
Java通過oshi獲取系統和硬件信息
OSHI是什么
OSHI是一個基于JNA的免費的本地操作系統和Java的硬件信息庫。 它不需要安裝任何額外的本機庫,旨在提供跨平臺的實現來檢索系統信息, 如操作系統版本、進程、內存和CPU使用情況、磁盤和分區、設備、傳感器等。
如何使用
添加依賴
<dependency> <groupId>com.github.oshi</groupId> <artifactId>oshi-core</artifactId> <version>5.3.7</version> </dependency>
主要代碼
代碼主要參考
https://github.com/oshi/oshi/blob/master/oshi-core/src/test/java/oshi/SystemInfoTest.java
代碼如下
package com.java.system.tool; import oshi.hardware.*; import oshi.software.os.*; import oshi.util.FormatUtil; import java.util.Arrays; import java.util.List; public class SystemInfo { public static void main(String[] args) { HardwareAbstractionLayer hardware = OshiTools.getHardware(); ComputerSystem computerSystem = hardware.getComputerSystem(); printComputerSystem(computerSystem); OperatingSystem os = OshiTools.getOs(); printOperatingSystem(os); FileSystem fileSystem = os.getFileSystem(); printFileSystem(fileSystem); CentralProcessor processor = hardware.getProcessor(); printProcessor(processor); GlobalMemory memory = hardware.getMemory(); printMemory(memory); List<OSProcess> processes = os.getProcesses(5, OperatingSystem.ProcessSort.CPU); printOSProcess(processes,memory); List<HWDiskStore> diskStores = hardware.getDiskStores(); printDiskStores(diskStores); List<NetworkIF> networkIFs = hardware.getNetworkIFs(); printNetworkInterfaces(networkIFs); NetworkParams networkParams = os.getNetworkParams(); printNetworkParameters(networkParams); List<UsbDevice> usbDevices = hardware.getUsbDevices(true); printUsbDevices(usbDevices); Sensors sensors = hardware.getSensors(); printSensors(sensors); List<PowerSource> powerSources = hardware.getPowerSources(); printPowerSources(powerSources); List<Display> displays = hardware.getDisplays(); printDisplay(displays); } public static void printComputerSystem(ComputerSystem computerSystem) { Firmware firmware = computerSystem.getFirmware(); Baseboard baseboard = computerSystem.getBaseboard(); printTools.printAll("硬件環境", Arrays.asList("硬件制造商", "硬件型號", "硬件編碼"), Arrays.asList(computerSystem.getManufacturer(), computerSystem.getModel(), computerSystem.getSerialNumber())); printTools.printAll("固件信息", Arrays.asList("固件制造商", "固件名稱", "固件版本", "固件說明", "固件日期"), Arrays.asList(firmware.getManufacturer(), firmware.getName(), firmware.getVersion(), firmware.getDescription(), firmware.getReleaseDate())); printTools.printAll("主板信息", Arrays.asList("主板制造商", "主板型號", "主板版本", "主板編碼"), Arrays.asList(baseboard.getManufacturer(), baseboard.getModel(), baseboard.getVersion(), baseboard.getSerialNumber())); } public static void printOperatingSystem(OperatingSystem os) { printTools.printAll("操作系統", Arrays.asList("操作系統制造商", "操作系統", "操作系統進程數", "操作系統線程數", "操作系統開始時間", "操作系統位數", "操作系統結束時間"), Arrays.asList(os.getManufacturer(), String.valueOf(os.getFamily()), String.valueOf(os.getProcessCount()), String.valueOf(os.getThreadCount()), String.valueOf(os.getSystemBootTime()), String.valueOf(os.getBitness()), String.valueOf(os.getSystemUptime()))); } public static void printProcessor(CentralProcessor centralProcessor) { List<String> keys = Arrays.asList("cpu數量", "cpu核心數", "cpu線程數", "cpu編號"); List<String> values = Arrays.asList(String.valueOf(centralProcessor.getPhysicalPackageCount()), String.valueOf(centralProcessor.getPhysicalProcessorCount()), String.valueOf(centralProcessor.getLogicalProcessorCount()), String.valueOf(centralProcessor.getProcessorIdentifier())); printTools.printAll("中央處理器", keys, values); } public static void printMemory(GlobalMemory memory) { List<String> keys = Arrays.asList("內存大小", "可用內存大小"); List<String> values = Arrays.asList(FormatUtil.formatBytes(memory.getTotal()), FormatUtil.formatBytes(memory.getAvailable())); printTools.printAll("內存", keys, values); List<PhysicalMemory> memoryList = memory.getPhysicalMemory(); memoryList.forEach((item) -> { List<String> keyList = Arrays.asList("內存型號", "內存規格", "內存主頻", "內存大小"); List<String> valueList = Arrays.asList(item.getManufacturer(), item.getMemoryType(), FormatUtil.formatBytes(item.getClockSpeed()), FormatUtil.formatBytes(item.getCapacity())); printTools.printAll("內存條信息", keyList, valueList); }); } public static void printUsbDevices(List<UsbDevice> usbDeviceList) { usbDeviceList.forEach((item) -> { // List<String> keyList = Arrays.asList("Usb驅動名稱", "Usb產品ID", "Usb編碼", "Usb唯一設備號", "Usb供應商", "UsbUsb供應商ID"); // List<String> valueList = Arrays.asList(item.getName(), item.getProductId(), item.getSerialNumber(), item.getUniqueDeviceId(), item.getVendor(), item.getVendorId()); List<String> keyList = Arrays.asList("Usb驅動"); List<String> valueList = Arrays.asList(item.toString()); printTools.printAll("Usb驅動信息", keyList, valueList); }); } private static void printFileSystem(FileSystem fileSystem) { List<OSFileStore> fileStores = fileSystem.getFileStores(); fileStores.forEach((item) -> { List<String> keyList = Arrays.asList("名稱", "UUID", "盤符", "文件類型", "總大小", "剩余大小", "使用比例%"); List<String> valueList = Arrays.asList(item.getName(), item.getUUID(), item.getMount(), item.getType(), FormatUtil.formatBytes(item.getTotalSpace()), FormatUtil.formatBytes(item.getUsableSpace()), String.valueOf(100d * item.getUsableSpace() / item.getTotalSpace())); printTools.printAll("文件存儲", keyList, valueList); }); } private static void printDiskStores(List<HWDiskStore> diskList) { diskList.forEach((item) -> { List<String> keyList = Arrays.asList("名稱", "型號", "序號", "大小"); List<String> valueList = Arrays.asList(item.getName(), item.getModel(), item.getSerial(), FormatUtil.formatBytes(item.getSize())); printTools.printAll("磁盤存儲", keyList, valueList); List<HWPartition> partitions = item.getPartitions(); partitions.forEach(e->{ List<String> keyLists = Arrays.asList("分區名稱", "分區類型", "分區大小"); List<String> valueLists = Arrays.asList(e.getName(), e.getType(),FormatUtil.formatBytesDecimal(e.getSize())); printTools.printAll("磁盤分區", keyLists, valueLists); }); }); } private static void printNetworkInterfaces(List<NetworkIF> networkIFs) { networkIFs.forEach((item) -> { List<String> keyList = Arrays.asList("名稱", "MAC", "MTU", "網速bps", "IPv4", "IPv6"); List<String> valueList = Arrays.asList(item.getName(), item.getMacaddr(), String.valueOf(item.getMTU()), FormatUtil.formatValue(item.getSpeed(), "bps"), Arrays.toString(item.getIPv4addr()), Arrays.toString(item.getIPv6addr())); printTools.printAll("網絡設置", keyList, valueList); }); } private static void printNetworkParameters(NetworkParams networkParams) { List<String> keyList = Arrays.asList("Host", "Domain", "DNS", "IPv4", "IPv6"); List<String> valueList = Arrays.asList(networkParams.getHostName(), networkParams.getDomainName(), Arrays.toString(networkParams.getDnsServers()), networkParams.getIpv4DefaultGateway() , networkParams.getIpv6DefaultGateway()); printTools.printAll("網絡", keyList, valueList); } private static void printSensors(Sensors sensors) { List<String> keyList = Arrays.asList("CPU溫度", "風扇", "CPU電壓"); List<String> valueList = Arrays.asList(String.valueOf(sensors.getCpuTemperature()), Arrays.toString(sensors.getFanSpeeds()), String.valueOf(sensors.getCpuVoltage())); printTools.printAll("傳感器", keyList, valueList); } private static void printPowerSources(List<PowerSource> powerSources) { powerSources.stream().forEach(item -> { List<String> keyList = Arrays.asList("電源名稱", "剩余百分比"); List<String> valueList = Arrays.asList(item.getName(), String.valueOf(item.getRemainingCapacityPercent()*100)); printTools.printAll("電源", keyList, valueList); }); } private static void printDisplay(List<Display> displays) { displays.stream().forEach(item -> { List<String> keyList = Arrays.asList("顯示"); List<String> valueList = Arrays.asList(item.toString()); printTools.printAll("顯示", keyList, valueList); }); } private static void printOSProcess(List<OSProcess> processes,GlobalMemory memory) { processes.stream().forEach(item -> { List<String> keyList = Arrays.asList("PID","CPU","MEM","VSZ","RSS Name"); List<String> valueList = Arrays.asList(String.valueOf(item.getProcessID()),String.valueOf(100d * (item.getKernelTime() + item.getUserTime()) / item.getUpTime()), String.valueOf(100d * item.getResidentSetSize() / memory.getTotal()),FormatUtil.formatBytes(item.getVirtualSize()), FormatUtil.formatBytes(item.getResidentSetSize()),item.getName()); printTools.printAll("進程ID", keyList, valueList); }); } }
進階
圖形化界面參考
oshi-demo
oshi的監控指標
1、計算機系統和固件,底板 2、操作系統和版本/內部版本 3、物理(核心)和邏輯(超線程)CPU,處理器組,NUMA節點 4、系統和每個處理器的負載百分比和滴答計數器 5、CPU正常運行時間,進程和線程 6、進程正常運行時間,CPU,內存使用率,用戶/組,命令行 7、已使用/可用的物理和虛擬內存 8、掛載的文件系統(類型,可用空間和總空間) 9、磁盤驅動器(型號,序列號,大小)和分區 10、網絡接口(IP,帶寬輸入/輸出) 11、電池狀態(電量百分比,剩余時間,電量使用情況統計信息) 12、連接的顯示器(帶有EDID信息) 13、USB設備 14、傳感器(溫度,風扇速度,電壓)
關于Java中怎么通過oshi獲取系統和硬件信息就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。