package net.geedge.confagent.util; import cn.hutool.log.Log; import oshi.SystemInfo; import oshi.hardware.*; import oshi.software.os.*; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.HashMap; import java.util.List; import java.util.Map; @SuppressWarnings("all") public class OSHIUtils { private static Log log = Log.get(); private static final SystemInfo si = new SystemInfo(); private static final OperatingSystem operatingSystem = si.getOperatingSystem(); private static final HardwareAbstractionLayer hal = si.getHardware(); /** * 系统信息 * @return result */ public static Map getSystemInfo(){ HashMap os = Tool.MapUtil.newHashMap(); NetworkParams networkParams = operatingSystem.getNetworkParams(); os.put("versionInfo",operatingSystem.getVersionInfo().toString()); os.put("platform",si.getCurrentPlatform()); os.put("family",operatingSystem.getFamily()); os.put("manufacturer",operatingSystem.getManufacturer()); os.put("bitness",operatingSystem.getBitness()); os.put("bootTime",operatingSystem.getSystemBootTime()); os.put("hostName",networkParams.getHostName()); os.put("ipv4DefaultGateway",networkParams.getIpv4DefaultGateway()); os.put("ipv6DefaultGateway",networkParams.getIpv6DefaultGateway()); os.put("dnsServers",networkParams.getDnsServers()); HashMap system = Tool.MapUtil.newHashMap(); ComputerSystem computerSystem = hal.getComputerSystem(); system.put("serialNumber",computerSystem.getSerialNumber()); system.put("model",computerSystem.getModel()); system.put("manufacturer",computerSystem.getManufacturer()); system.put("hardwareUUID",computerSystem.getHardwareUUID()); HashMap baseBoardMap = Tool.MapUtil.newHashMap(); Baseboard baseboard = computerSystem.getBaseboard(); baseBoardMap.put("serialNumber",baseboard.getSerialNumber()); baseBoardMap.put("model",baseboard.getModel()); baseBoardMap.put("manufacturer",baseboard.getManufacturer()); baseBoardMap.put("version",baseboard.getVersion()); HashMap cpu = Tool.MapUtil.newHashMap(); CentralProcessor processor = hal.getProcessor(); cpu.put("physicalPackageCount",processor.getPhysicalPackageCount()); cpu.put("physicalProcessorCount",processor.getPhysicalProcessorCount()); cpu.put("logicalProcessorCount",processor.getLogicalProcessorCount()); cpu.put("contextSwitches",processor.getContextSwitches()); cpu.put("interrupts",processor.getInterrupts()); cpu.put("maxFreq",processor.getMaxFreq()); CentralProcessor.ProcessorIdentifier processorIdentifier = processor.getProcessorIdentifier(); cpu.put("family",processorIdentifier.getFamily()); cpu.put("identifier",processorIdentifier.getIdentifier()); cpu.put("microarchitecture",processorIdentifier.getMicroarchitecture()); cpu.put("model",processorIdentifier.getModel()); cpu.put("name",processorIdentifier.getName()); cpu.put("vendor",processorIdentifier.getVendor()); cpu.put("stepping",processorIdentifier.getStepping()); cpu.put("cpu64bit",processorIdentifier.isCpu64bit()); HashMap memory = Tool.MapUtil.newHashMap(); GlobalMemory memoryInfo = hal.getMemory(); VirtualMemory virtualMemory = memoryInfo.getVirtualMemory(); memory.put("total",memoryInfo.getTotal()); memory.put("swapTotal",virtualMemory.getSwapTotal()); memory.put("virtualMax",virtualMemory.getVirtualMax()); memory.put("pageSize",memoryInfo.getPageSize()); List> disk = Tool.ListUtil.list(false); List diskStores = hal.getDiskStores(); for(HWDiskStore diskStore : diskStores) { if ( diskStore.getSize() > 0){ HashMap diskData = Tool.MapUtil.newHashMap(); diskData.put("name",diskStore.getName()); diskData.put("model",diskStore.getModel()); diskData.put("serial",diskStore.getSerial()); diskData.put("size",diskStore.getSize()); disk.add(diskData); } } List> network = Tool.ListUtil.list(false); List networkIFs = hal.getNetworkIFs(); for (NetworkIF networkIF : networkIFs) { HashMap networkData = Tool.MapUtil.newHashMap(); networkData.put("index", networkIF.getIndex()); networkData.put("name",networkIF.getName()); networkData.put("ifAlias",networkIF.getIfAlias()); networkData.put("ifType",networkIF.getIfType()); networkData.put("speed",networkIF.getSpeed()); networkData.put("mtu",networkIF.getMTU()); networkData.put("ifOperStatus",networkIF.getIfOperStatus().getValue()); networkData.put("macaddr",networkIF.getMacaddr()); networkData.put("iPv4addr",networkIF.getIPv4addr()); networkData.put("subnetMasks",networkIF.getSubnetMasks()); networkData.put("iPv6addr",networkIF.getIPv6addr()); network.add(networkData); } HashMap sensor = Tool.MapUtil.newHashMap(); Sensors sensors = hal.getSensors(); sensor.put("cpuTemperature",sensors.getCpuTemperature()); sensor.put("cpuVoltage",sensors.getCpuVoltage()); sensor.put("fanSpeeds",sensors.getFanSpeeds()); HashMap result = Tool.MapUtil.newHashMap(); result.put("os",os); result.put("system",system); result.put("baseBoard",baseBoardMap); result.put("cpu",cpu); result.put("memory",memory); result.put("disk",disk); result.put("networkIF",network); result.put("sensor",sensor); return result; } /** * 进程信息 * @return result */ public static List getProcessInfo(){ List result = Tool.ListUtil.list(false); List procs = operatingSystem.getProcesses(); GlobalMemory memory = hal.getMemory(); for (OSProcess proc : procs) { HashMap procsData = Tool.MapUtil.newHashMap(); procsData.put("name",proc.getName()); procsData.put("commandLine",proc.getCommandLine()); procsData.put("processID",proc.getProcessID()); procsData.put("parentProcessID",proc.getParentProcessID()); procsData.put("startTime",proc.getStartTime()); procsData.put("priority",proc.getPriority()); /** * For per-Process CPU ticks, there is no "idle" counter available, so the calculation ends up being (active time / up time) * This interpretation matches the value displayed in ps or top on Unix-based operating systems * If you want per-Process CPU load to match the Windows Task Manager display, you should divide OSHI's calculation by the number of logical processors */ procsData.put("cpuUsage",Tool.NumberUtil.div(proc.getKernelTime() + proc.getUserTime(), proc.getUpTime()) * 100d); procsData.put("memUsage",Tool.NumberUtil.div(proc.getResidentSetSize(), memory.getTotal()) * 100d); procsData.put("state",proc.getState().toString()); procsData.put("openFiles",proc.getOpenFiles()); procsData.put("threadCount",proc.getThreadCount()); procsData.put("user",proc.getUser()); procsData.put("group",proc.getGroup()); procsData.put("path",proc.getPath()); procsData.put("virtualSize",proc.getVirtualSize()); procsData.put("residentSetSize",proc.getResidentSetSize()); result.add(procsData); } return result; } /** * 网络连接信息 * @return result */ public static List getNetInfo() throws UnknownHostException { List result = Tool.ListUtil.list(false); InternetProtocolStats internetProtocolStats = operatingSystem.getInternetProtocolStats(); //获取网络连接 List connections = internetProtocolStats.getConnections(); for (InternetProtocolStats.IPConnection connection : connections) { HashMap map = Tool.MapUtil.newHashMap(); map.put("type",connection.getType()); map.put("localAddress", InetAddress.getByAddress(connection.getLocalAddress()).toString().substring(1)); map.put("localPort",connection.getLocalPort()); map.put("foreignAddress",InetAddress.getByAddress(connection.getForeignAddress()).toString().substring(1)); map.put("foreignPort",connection.getForeignPort()); map.put("state",connection.getState().toString()); map.put("transmitQueue",connection.getTransmitQueue()); map.put("receiveQueue",connection.getReceiveQueue()); if (connection.getowningProcessId() == -1 ){ //未知进程 getowningProcessId 返回 -1 为避免获取进程name error map.put("processId","unknown"); map.put("processName","unknown"); }else { map.put("processId",connection.getowningProcessId()); map.put("processName",operatingSystem.getProcess(connection.getowningProcessId()).getName()); } result.add(map); } return result; } }