This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nezha-nz-talon/src/main/java/net/geedge/confagent/util/OSHIUtils.java

224 lines
10 KiB
Java

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;
import java.util.function.Function;
import java.util.stream.Collectors;
@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<String,Object> getSystemInfo(){
HashMap<Object, Object> 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<Object, Object> 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<Object, Object> 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<Object, Object> 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<Object, Object> 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<HashMap<Object, Object>> disk = Tool.ListUtil.list(false);
List<HWDiskStore> diskStores = hal.getDiskStores();
for(HWDiskStore diskStore : diskStores) {
if ( diskStore.getSize() > 0){
HashMap<Object, Object> 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<HashMap<Object, Object>> network = Tool.ListUtil.list(false);
List<NetworkIF> networkIFs = hal.getNetworkIFs();
for (NetworkIF networkIF : networkIFs) {
HashMap<Object, Object> 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<Object, Object> sensor = Tool.MapUtil.newHashMap();
Sensors sensors = hal.getSensors();
sensor.put("cpuTemperature",sensors.getCpuTemperature());
sensor.put("cpuVoltage",sensors.getCpuVoltage());
sensor.put("fanSpeeds",sensors.getFanSpeeds());
HashMap<String, Object> 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<Map> getProcessInfo() throws InterruptedException {
List<Map> result = Tool.ListUtil.list(false);
List<OSProcess> oldProcess = operatingSystem.getProcesses();
Thread.sleep(500);
List<OSProcess> currentProcess = operatingSystem.getProcesses();
Map<Integer, Object> cpuUsage = getCpuUsage(currentProcess, oldProcess);
GlobalMemory memory = hal.getMemory();
for (OSProcess proc : currentProcess) {
HashMap<String, Object> 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());
procsData.put("cpuUsage",(double)cpuUsage.get(proc.getProcessID()) * 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;
}
/**
* 获取进程cpu利用率
* @param currentProc
* @param oldProcess
* @return
*/
private static Map<Integer,Object> getCpuUsage(List<OSProcess> currentProc, List<OSProcess> oldProcess) {
Map<Integer, OSProcess> procs = oldProcess.stream().collect(Collectors.toMap(OSProcess::getProcessID, Function.identity()));
HashMap<Integer, Object> map = Tool.MapUtil.newHashMap();
for (OSProcess proc : currentProc) {
OSProcess old = procs.get(proc.getProcessID());
map.put(proc.getProcessID(),proc.getProcessCpuLoadBetweenTicks(old));
}
return map;
}
/**
* 网络连接信息
* @return result
*/
public static List<Map> getNetInfo() throws UnknownHostException {
List<Map> result = Tool.ListUtil.list(false);
InternetProtocolStats internetProtocolStats = operatingSystem.getInternetProtocolStats();
//获取网络连接
List<InternetProtocolStats.IPConnection> connections = internetProtocolStats.getConnections();
for (InternetProtocolStats.IPConnection connection : connections) {
HashMap<String, Object> 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;
}
}