|
@@ -0,0 +1,215 @@
|
|
|
|
+package com.gyee.host.service;
|
|
|
|
+
|
|
|
|
+import cn.hutool.core.lang.Console;
|
|
|
|
+import cn.hutool.system.OsInfo;
|
|
|
|
+import cn.hutool.system.SystemUtil;
|
|
|
|
+import com.gyee.host.mode.CpuInfo;
|
|
|
|
+import com.gyee.host.mode.DiskInfo;
|
|
|
|
+import com.gyee.host.mode.GpuInfo;
|
|
|
|
+import com.gyee.host.mode.Memory;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import oshi.SystemInfo;
|
|
|
|
+import oshi.hardware.*;
|
|
|
|
+import oshi.software.os.OperatingSystem;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
+
|
|
|
|
+@Service
|
|
|
|
+public class HostparamService {
|
|
|
|
+
|
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(HostparamService.class);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ShellService shellService;
|
|
|
|
+
|
|
|
|
+ public Map getHostParam(){
|
|
|
|
+// Console.log("======= 操作系统信息 ======");
|
|
|
|
+ OsInfo osInfo = SystemUtil.getOsInfo();
|
|
|
|
+ Console.log("操作系统:{}", osInfo.getName());
|
|
|
|
+ Console.log("系统版本:{}", osInfo.getVersion());
|
|
|
|
+// Console.log("系统架构:{}", osInfo.getArch());
|
|
|
|
+// Console.log("JVM总内存:{}", format(SystemUtil.getTotalMemory()));
|
|
|
|
+// Console.log("JVM剩余内存:{}", format(SystemUtil.getFreeMemory()));
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ Map map =new HashMap();
|
|
|
|
+ SystemInfo systemInfo = new SystemInfo();
|
|
|
|
+ HardwareAbstractionLayer hardware = systemInfo.getHardware();
|
|
|
|
+ OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
|
|
|
|
+
|
|
|
|
+ Console.log("======= CPU信息 ======");
|
|
|
|
+ CentralProcessor cpu = hardware.getProcessor();
|
|
|
|
+ CentralProcessor.ProcessorIdentifier cpuInfo = cpu.getProcessorIdentifier();
|
|
|
|
+ CpuInfo info = new CpuInfo();
|
|
|
|
+// Console.log("cpu数量:{}", cpu.getPhysicalPackageCount());
|
|
|
|
+ info.setCpuNum(cpu.getPhysicalPackageCount());
|
|
|
|
+// Console.log("cpu核心数:{}", cpu.getPhysicalProcessorCount());
|
|
|
|
+ info.setCpuCoreNum(cpu.getPhysicalProcessorCount());
|
|
|
|
+// Console.log("cpu线程数:{}", cpu.getLogicalProcessorCount());
|
|
|
|
+ info.setCpuhreads(cpu.getLogicalProcessorCount());
|
|
|
|
+// Console.log("cpuID:{}", cpuInfo.getProcessorID());
|
|
|
|
+// Console.log("cpu名称:{}", cpuInfo.getName());
|
|
|
|
+ info.setCpuName(cpuInfo.getName());
|
|
|
|
+// Console.log("cpu标识:{}", cpuInfo.getIdentifier());
|
|
|
|
+ map.put("cpu",info);
|
|
|
|
+
|
|
|
|
+// Console.log("======= 主板信息 ======");
|
|
|
|
+// ComputerSystem computer = hardware.getComputerSystem();
|
|
|
|
+// Console.log("主板型号:{}", computer.getModel());
|
|
|
|
+// Console.log("主板序列号:{}", computer.getSerialNumber());
|
|
|
|
+
|
|
|
|
+ Console.log("======= 内存信息 ======");
|
|
|
|
+ Memory m = new Memory();
|
|
|
|
+ GlobalMemory memory = hardware.getMemory();
|
|
|
|
+// Console.log("内存大小:{}", format(memory.getTotal()));
|
|
|
|
+// Console.log("可用内存:{}", format(memory.getAvailable()));
|
|
|
|
+ m.setTotalMemory(format(memory.getTotal()));
|
|
|
|
+ m.setUseableMemory(format(memory.getAvailable()));
|
|
|
|
+ map.put("memory",m);
|
|
|
|
+ Console.log("==========");
|
|
|
|
+ List<PhysicalMemory> memoryList = memory.getPhysicalMemory();
|
|
|
|
+// memoryList.forEach((item) -> {
|
|
|
|
+// Console.log("内存型号:{}", item.getManufacturer());
|
|
|
|
+// Console.log("内存规格:{}", item.getMemoryType());
|
|
|
|
+// Console.log("内存主频:{}", format(item.getClockSpeed()));
|
|
|
|
+// Console.log("内存大小:{}", format(item.getCapacity()));
|
|
|
|
+// Console.log("==========");
|
|
|
|
+// });
|
|
|
|
+
|
|
|
|
+ Console.log("======= 物理磁盘信息 ======");
|
|
|
|
+ List<HWDiskStore> diskList = hardware.getDiskStores();
|
|
|
|
+
|
|
|
|
+ List<DiskInfo> diskInfos = new ArrayList<>();
|
|
|
|
+ diskList.forEach((disk) -> {
|
|
|
|
+ DiskInfo diskInfo = new DiskInfo();
|
|
|
|
+// Console.log("名称:{}", disk.getName());
|
|
|
|
+ diskInfo.setDiskName(disk.getName());
|
|
|
|
+// Console.log("型号:{}", disk.getModel());
|
|
|
|
+ diskInfo.setDiskMode(disk.getModel());
|
|
|
|
+// Console.log("序号:{}", disk.getSerial());
|
|
|
|
+ diskInfo.setDiskId(disk.getSerial());
|
|
|
|
+// Console.log("大小:{}", format(disk.getSize()));
|
|
|
|
+ diskInfo.setDiskSize(format(disk.getSize()));
|
|
|
|
+// Console.log("==========");
|
|
|
|
+ diskInfos.add(diskInfo);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ map.put("disk",diskInfos);
|
|
|
|
+
|
|
|
|
+// Console.log("======= 逻辑磁盘信息 ======");
|
|
|
|
+// FileSystem fileSystem = operatingSystem.getFileSystem();
|
|
|
|
+// List<OSFileStore> fileList = fileSystem.getFileStores(true);
|
|
|
|
+// fileList.forEach((file) -> {
|
|
|
|
+// Console.log("名称:{}", file.getName());
|
|
|
|
+// Console.log("UUID:{}", file.getUUID());
|
|
|
|
+// Console.log("盘符:{}", file.getMount());
|
|
|
|
+// Console.log("文件类型:{}", file.getType());
|
|
|
|
+// Console.log("总大小:{}", format(file.getTotalSpace()));
|
|
|
|
+// Console.log("剩余大小:{}", format(file.getUsableSpace()));
|
|
|
|
+// Console.log("==========");
|
|
|
|
+// });
|
|
|
|
+ return map;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private static String format(long size) {
|
|
|
|
+ if (size < 1024) {
|
|
|
|
+ return size + "B";
|
|
|
|
+ } else {
|
|
|
|
+ size = size / 1024;
|
|
|
|
+ }
|
|
|
|
+ if (size < 1024) {
|
|
|
|
+ return size + "KB";
|
|
|
|
+ } else {
|
|
|
|
+ size = size / 1024;
|
|
|
|
+ }
|
|
|
|
+ if (size < 1024) {
|
|
|
|
+ size = size * 100;
|
|
|
|
+ return size / 100 + "." + size % 100 + "MB";
|
|
|
|
+ } else {
|
|
|
|
+ size = size * 100 / 1024;
|
|
|
|
+ return size / 100 + "." + size % 100 + "GB";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public List<GpuInfo> getGpuInfos() throws IOException {
|
|
|
|
+ String gpus = null;
|
|
|
|
+
|
|
|
|
+ gpus = shellService.getGPU();
|
|
|
|
+ //命令行调用后获取的信息
|
|
|
|
+ /*String gpus = "Mon Jun 1 10:47:16 2020 \n" +
|
|
|
|
+ "+-----------------------------------------------------------------------------+\n" +
|
|
|
|
+ "| NVIDIA-SMI 418.87.01 Driver Version: 418.87.01 CUDA Version: 10.1 |\n" +
|
|
|
|
+ "|-------------------------------+----------------------+----------------------+\n" +
|
|
|
|
+ "| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\n" +
|
|
|
|
+ "| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\n" +
|
|
|
|
+ "|===============================+======================+======================|\n" +
|
|
|
|
+ "| 0 TITAN V Off | 00000000:2D:00.0 On | N/A |\n" +
|
|
|
|
+ "| 29% 43C P8 27W / 250W | 1123MiB / 12035MiB | 0% Default |\n" +
|
|
|
|
+ "+-------------------------------+----------------------+----------------------+\n" +
|
|
|
|
+ "| 1 GeForce RTX 208... Off | 00000000:99:00.0 Off | N/A |\n" +
|
|
|
|
+ "| 0% 29C P8 20W / 260W | 11MiB / 10989MiB | 0% Default |\n" +
|
|
|
|
+ "+-------------------------------+----------------------+----------------------+\n" +
|
|
|
|
+ " \n" +
|
|
|
|
+ "+-----------------------------------------------------------------------------+\n" +
|
|
|
|
+ "| Processes: GPU Memory |\n" +
|
|
|
|
+ "| GPU PID Type Process name Usage |\n" +
|
|
|
|
+ "|=============================================================================|\n" +
|
|
|
|
+ "| 0 16841 C inference_worker 1077MiB |\n" +
|
|
|
|
+ "| 0 19996 G /usr/lib/xorg/Xorg 33MiB |\n" +
|
|
|
|
+ "+-----------------------------------------------------------------------------+\n";*/
|
|
|
|
+ //System.out.println("命令行获取的结果: " + gpus);
|
|
|
|
+ //分割废物信息
|
|
|
|
+ String[] split = gpus.split("\\|===============================\\+======================\\+======================\\|");
|
|
|
|
+ String[] gpusInfo = split[1].split(" ");
|
|
|
|
+ // 分割多个gpu
|
|
|
|
+ String[] gpuInfo = gpusInfo[0].split("\\+-------------------------------\\+----------------------\\+----------------------\\+");
|
|
|
|
+ //System.out.println("000000000000000000000000000000000");
|
|
|
|
+ List<GpuInfo> gpuInfoList = new ArrayList<>();
|
|
|
|
+ for (int i = 0; i < gpuInfo.length - 1; i++) {
|
|
|
|
+ GpuInfo gpuInfo1 = new GpuInfo();
|
|
|
|
+ String[] nameAndInfo = gpuInfo[i].split("\n");
|
|
|
|
+ //只要第二块的数据
|
|
|
|
+ /*0
|
|
|
|
+ *TITAN
|
|
|
|
+ *V
|
|
|
|
+ *Off
|
|
|
|
+ * */
|
|
|
|
+ String[] split1 = nameAndInfo[1].split("\\|")[1] // 0 TITAN V Off
|
|
|
|
+ .split("\\s+");//去空格
|
|
|
|
+
|
|
|
|
+ gpuInfo1.setNumber(Integer.parseInt(split1[1]));
|
|
|
|
+ StringBuffer name = new StringBuffer();
|
|
|
|
+ for (int j = 0; j < split1.length - 1; j++) {
|
|
|
|
+ if (j > 1 && j != split1.length) {
|
|
|
|
+ name.append(split1[j] + " ");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ gpuInfo1.setName(name.toString());
|
|
|
|
+
|
|
|
|
+ String[] info = nameAndInfo[2].split("\\|")[2].split("\\s+");
|
|
|
|
+ /* System.out.println("biubiu~~~biubiu~~~biubiu~~~biubiu~~~biubiu~~~biubiu~~~biubiu~~~biubiu~~~biubiu~~~biubiu~~~");*/
|
|
|
|
+// gpuInfo1.setUsedMemory(info[1]);
|
|
|
|
+// gpuInfo1.setTotalMemory(info[3]);
|
|
|
|
+// int useable = Integer.parseInt(gpuInfo1.getTotalMemory().split("MiB")[0]) - Integer.parseInt(gpuInfo1.getUsedMemory().split("MiB")[0]);
|
|
|
|
+// gpuInfo1.setUseableMemory(useable + "MiB");
|
|
|
|
+// Double usageRate = Integer.parseInt(gpuInfo1.getUsedMemory().split("MiB")[0]) * 100.00 / Integer.parseInt(gpuInfo1.getTotalMemory().split("MiB")[0]);
|
|
|
|
+// gpuInfo1.setUsageRate(usageRate);
|
|
|
|
+ gpuInfoList.add(gpuInfo1);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ return gpuInfoList;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|