新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章将为大家详细讲解有关Java中怎么通过oshi获取系统和硬件信息,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
创新互联建站长期为上千多家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为衡南企业提供专业的网站建设、成都网站制作,衡南网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。
Java通过oshi获取系统和硬件信息
OSHI是什么
OSHI是一个基于JNA的免费的本地操作系统和Java的硬件信息库。 它不需要安装任何额外的本机库,旨在提供跨平台的实现来检索系统信息, 如操作系统版本、进程、内存和CPU使用情况、磁盘和分区、设备、传感器等。
如何使用
添加依赖
com.github.oshi oshi-core 5.3.7
主要代码
代码主要参考
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); Listprocesses = os.getProcesses(5, OperatingSystem.ProcessSort.CPU); printOSProcess(processes,memory); List diskStores = hardware.getDiskStores(); printDiskStores(diskStores); List networkIFs = hardware.getNetworkIFs(); printNetworkInterfaces(networkIFs); NetworkParams networkParams = os.getNetworkParams(); printNetworkParameters(networkParams); List usbDevices = hardware.getUsbDevices(true); printUsbDevices(usbDevices); Sensors sensors = hardware.getSensors(); printSensors(sensors); List powerSources = hardware.getPowerSources(); printPowerSources(powerSources); List 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 keys = Arrays.asList("cpu数量", "cpu核心数", "cpu线程数", "cpu编号"); List 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 keys = Arrays.asList("内存大小", "可用内存大小"); List values = Arrays.asList(FormatUtil.formatBytes(memory.getTotal()), FormatUtil.formatBytes(memory.getAvailable())); printTools.printAll("内存", keys, values); List memoryList = memory.getPhysicalMemory(); memoryList.forEach((item) -> { List keyList = Arrays.asList("内存型号", "内存规格", "内存主频", "内存大小"); List valueList = Arrays.asList(item.getManufacturer(), item.getMemoryType(), FormatUtil.formatBytes(item.getClockSpeed()), FormatUtil.formatBytes(item.getCapacity())); printTools.printAll("内存条信息", keyList, valueList); }); } public static void printUsbDevices(List usbDeviceList) { usbDeviceList.forEach((item) -> { // List keyList = Arrays.asList("Usb驱动名称", "Usb产品ID", "Usb编码", "Usb唯一设备号", "Usb供应商", "UsbUsb供应商ID"); // List valueList = Arrays.asList(item.getName(), item.getProductId(), item.getSerialNumber(), item.getUniqueDeviceId(), item.getVendor(), item.getVendorId()); List keyList = Arrays.asList("Usb驱动"); List valueList = Arrays.asList(item.toString()); printTools.printAll("Usb驱动信息", keyList, valueList); }); } private static void printFileSystem(FileSystem fileSystem) { List fileStores = fileSystem.getFileStores(); fileStores.forEach((item) -> { List keyList = Arrays.asList("名称", "UUID", "盘符", "文件类型", "总大小", "剩余大小", "使用比例%"); List 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 diskList) { diskList.forEach((item) -> { List keyList = Arrays.asList("名称", "型号", "序号", "大小"); List valueList = Arrays.asList(item.getName(), item.getModel(), item.getSerial(), FormatUtil.formatBytes(item.getSize())); printTools.printAll("磁盘存储", keyList, valueList); List partitions = item.getPartitions(); partitions.forEach(e->{ List keyLists = Arrays.asList("分区名称", "分区类型", "分区大小"); List valueLists = Arrays.asList(e.getName(), e.getType(),FormatUtil.formatBytesDecimal(e.getSize())); printTools.printAll("磁盘分区", keyLists, valueLists); }); }); } private static void printNetworkInterfaces(List networkIFs) { networkIFs.forEach((item) -> { List keyList = Arrays.asList("名称", "MAC", "MTU", "网速bps", "IPv4", "IPv6"); List 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 keyList = Arrays.asList("Host", "Domain", "DNS", "IPv4", "IPv6"); List 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 keyList = Arrays.asList("CPU温度", "风扇", "CPU电压"); List valueList = Arrays.asList(String.valueOf(sensors.getCpuTemperature()), Arrays.toString(sensors.getFanSpeeds()), String.valueOf(sensors.getCpuVoltage())); printTools.printAll("传感器", keyList, valueList); } private static void printPowerSources(List powerSources) { powerSources.stream().forEach(item -> { List keyList = Arrays.asList("电源名称", "剩余百分比"); List valueList = Arrays.asList(item.getName(), String.valueOf(item.getRemainingCapacityPercent()*100)); printTools.printAll("电源", keyList, valueList); }); } private static void printDisplay(List displays) { displays.stream().forEach(item -> { List keyList = Arrays.asList("显示"); List valueList = Arrays.asList(item.toString()); printTools.printAll("显示", keyList, valueList); }); } private static void printOSProcess(List processes,GlobalMemory memory) { processes.stream().forEach(item -> { List keyList = Arrays.asList("PID","CPU","MEM","VSZ","RSS Name"); List 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获取系统和硬件信息就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。