获取电脑信息
测试
System.getProperties();
System: 是Java中的一个内置类,用于提供与系统相关的功能和信息。这个类中包含了一些静态方法和常量,可以让您方便地访问和操作系统级别的资源。
getProperties(): 是一个静态方法,它返回一个表示当前系统属性的Properties对象。这个方法可以用于获取系统相关的属性和信息,例如系统的操作系统、Java版本、路径分隔符等。、
package com.sin;import com.sun.management.OperatingSystemMXBean;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.util.Properties;@SpringBootTest
class GetComputerMessages {Properties props = System.getProperties();OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();/*** 获取Java信息*/@Testpublic void javaMessage() {System.out.println("Java的运行环境版本:" + props.getProperty("java.version"));System.out.println("Java的运行环境供应商:" + props.getProperty("java.vendor"));System.out.println("Java的运行环境供应商URL:" + props.getProperty("java.vendor.url"));System.out.println("Java的安装路径:" + props.getProperty("java.home"));System.out.println("Java的虚拟机规范名称:" + props.getProperty("java.vm.specification.name"));System.out.println("Java的虚拟机实现版本:" + props.getProperty("java.vm.version"));System.out.println("Java的虚拟机实现供应商:" + props.getProperty("java.vm.vendor"));System.out.println("Java的虚拟机实现名称:" + props.getProperty("java.vm.name"));System.out.println("Java的类格式版本号:" + props.getProperty("java.class.version"));System.out.println("Java的类路径:" + props.getProperty("java.class.path"));System.out.println("加载库时搜索的路径列表:" + props.getProperty("java.library.path"));System.out.println("默认的临时文件路径:" + props.getProperty("java.io.tmpdir"));System.out.println("一个或多个扩展目录的路径:" + props.getProperty("java.ext.dirs"));System.out.println("操作系统的名称:" + props.getProperty("os.name"));System.out.println("操作系统的构架:" + props.getProperty("os.arch"));System.out.println("操作系统的版本:" + props.getProperty("os.version"));System.out.println("分件分隔符:" + props.getProperty("file.separator"));// 在 unix 系统中是"/"System.out.println("路径分隔符:" + props.getProperty("path.separator"));// 在 unix 系统中是":"System.out.println("行分隔符:" + props.getProperty("line.separator"));// 在 unix 系统中是"/n"System.out.println("用户的账户名称:" + props.getProperty("user.name"));System.out.println("用户的主目录:" + props.getProperty("user.home"));System.out.println("用户的当前工作主目录:" + props.getProperty("user.dir"));}/*** 获取jvm信息*/@Testpublic void jvmMessage() {// 获取java虚拟机信息Runtime runtime = Runtime.getRuntime();long totalMemoryBytes = runtime.totalMemory(); // JVM的总内存(字节)long freeMemoryBytes = runtime.freeMemory(); // JVM的空闲内存(字节)long maxMemoryBytes = runtime.maxMemory(); // JVM的最大可用内存(字节)long totalMemoryMB = totalMemoryBytes / (1024 * 1024); // 转换为兆字节long freeMemoryMB = freeMemoryBytes / (1024 * 1024); // 转换为兆字节long maxMemoryMB = maxMemoryBytes / (1024 * 1024); // 转换为兆字节System.out.println("JVM的总内存:" + totalMemoryMB + "MB");System.out.println("JVM的空闲内存:" + freeMemoryMB + "MB");System.out.println("JVM的最大可用内存:" + maxMemoryMB + "MB");}/*** 获取cpu信息*/@Testpublic void cpuMessage() {String cpuArch = System.getProperty("os.arch"); // CPU架构int cpuCores = Runtime.getRuntime().availableProcessors(); // CPU核心数String cpuName = System.getenv("PROCESSOR_IDENTIFIER"); // CPU名称System.out.println("CPU架构:" + cpuArch);System.out.println("CPU名称:" + cpuName);System.out.println("CPU核心数:" + cpuCores);double systemLoadAverage = osBean.getSystemLoadAverage(); // 系统平均负载double processCpuLoad = osBean.getProcessCpuLoad(); // 当前进程的CPU负载double systemCpuLoad = osBean.getSystemCpuLoad(); // 系统的CPU负载System.out.println("系统平均负载:" + systemLoadAverage);System.out.println("当前进程的CPU负载:" + processCpuLoad);System.out.println("系统的cpu负载:" + systemCpuLoad);double userCpuLoad = osBean.getProcessCpuTime() / 1000000.0; // 用户态CPU时间double systemCpuTime = osBean.getSystemCpuLoad() * osBean.getAvailableProcessors(); // 系统态CPU时间double idleCpuTime = (1 - osBean.getSystemCpuLoad()) * osBean.getAvailableProcessors(); // 空闲CPU时间System.out.println("用户使用率:" + userCpuLoad);System.out.println("系统使用率:" + systemCpuTime);System.out.println("当前空闲率:" + idleCpuTime);}/*** 内存信息*/@Testpublic void memoryMessage() {// 获取内存long totalPhysicalMemorySize = osBean.getTotalPhysicalMemorySize(); // 物理内存总量long freePhysicalMemorySize = osBean.getFreePhysicalMemorySize(); // 空闲物理内存量long totalSwapSpaceSize = osBean.getTotalSwapSpaceSize(); // 交换空间总量long freeSwapSpaceSize = osBean.getFreeSwapSpaceSize(); // 空闲交换空间量System.out.println("物理内存总量:" + totalPhysicalMemorySize / (1024 * 1024) + " MB");System.out.println("空闲物理内存量:" + freePhysicalMemorySize / (1024 * 1024) + " MB");System.out.println("交换空间总量:" + totalSwapSpaceSize / (1024 * 1024) + " MB");System.out.println("空闲交换空间量:" + freeSwapSpaceSize / (1024 * 1024) + " MB");long totalMemorySize = osBean.getTotalPhysicalMemorySize(); // 总内存大小long freeMemorySize = osBean.getFreePhysicalMemorySize(); // 空闲内存大小long usedMemorySize = totalMemorySize - freeMemorySize; // 已用内存大小double memoryUsage = (double) usedMemorySize / totalMemorySize * 100; // 内存使用率System.out.println("总内存:" + formatMemorySize(totalMemorySize));System.out.println("已用内存:" + formatMemorySize(usedMemorySize));System.out.println("剩余内存:" + formatMemorySize(freeMemorySize));System.out.println("内存使用率:" + String.format("%.2f", memoryUsage) + "%");}/*** 获取磁盘信息*/@Testpublic void diskMessage() {File[] roots = File.listRoots();for (File root : roots) {String path = root.getAbsolutePath();long totalSpace = root.getTotalSpace(); // 总容量long usableSpace = root.getUsableSpace(); // 可用容量double usage = (double) (totalSpace - usableSpace) / totalSpace * 100; // 使用率System.out.println("磁盘路径:" + path);System.out.println("总容量:" + formatMemorySize(totalSpace));System.out.println("可用容量:" + formatMemorySize(usableSpace));System.out.println("使用率:" + String.format("%.2f", usage) + "%");try {FileStore store = Files.getFileStore(root.toPath());String fsType = store.type();String fsName = store.name();System.out.println("文件系统类型:" + fsType);System.out.println("文件系统名称:" + fsName);} catch (IOException e) {e.printStackTrace();}System.out.println();}}/*** 格式化容量大小** @param bytes* @return*/private static String formatMemorySize(long bytes) {double kiloBytes = bytes / 1024.0; // 将字节数转换为千字节double megaBytes = kiloBytes / 1024.0; // 将千字节转换为兆字节double gigaBytes = megaBytes / 1024.0; // 将兆字节转换为千兆字节if (gigaBytes >= 1) { // 如果容量大于等于1GBreturn String.format("%.2f GB", gigaBytes); // 格式化输出千兆字节容量,并带有两位小数的GB单位} else if (megaBytes >= 1) { // 如果容量大于等于1MBreturn String.format("%.2f MB", megaBytes); // 格式化输出兆字节容量,并带有两位小数的MB单位} else if (kiloBytes >= 1) { // 如果容量大于等于1KBreturn String.format("%.2f KB", kiloBytes); // 格式化输出千字节容量,并带有两位小数的KB单位} else { // 如果容量小于1KBreturn String.format("%d B", bytes); // 格式化输出字节数}}
}
java信息
内存信息
磁盘信息
jvm信息
CPU信息
整合Redis进行存储信息
package com.sin.controller;import com.fasterxml.jackson.core.JsonProcessingException;
import com.sin.utils.RedisUtil;
import com.sun.management.OperatingSystemMXBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;/*** @createTime 2023/11/30 11:52* @createAuthor SIN* @use*/
@RestController
public class RedisTestController {@Autowiredprivate RedisTemplate redisTemplate;@Autowiredprivate RedisUtil redisUtil;Properties properties = System.getProperties();OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();/*** java信息* @return*/@PostMapping("/setJavaMessage")public String setJavaMessage(){Map<String,Object> javaPropertiesMap = new HashMap<String,Object>();javaPropertiesMap.put("Java版本:" , properties.getProperty("java.version"));javaPropertiesMap.put("Java安装日期:" , properties.getProperty("java.version.date"));javaPropertiesMap.put("Java运行环境提供商:" , properties.getProperty("java.vendor"));javaPropertiesMap.put("Java安装路径:" , properties.getProperty("java.home"));javaPropertiesMap.put("Java虚拟机规范名称:" , properties.getProperty("java.vm.specification.name"));javaPropertiesMap.put("Java虚拟机版本:" , properties.getProperty("java.vm.version"));javaPropertiesMap.put("Java虚拟机提供商" , properties.getProperty("java.vm.vendor"));javaPropertiesMap.put("Java虚拟机实现名称" , properties.getProperty("java.vm.name"));javaPropertiesMap.put("Java类的格式版本号" , properties.getProperty("java.class.version"));javaPropertiesMap.put("Java类路径" , properties.getProperty("java.class.path"));javaPropertiesMap.put("加载库时搜索的路径列表:" , properties.getProperty("java.library.path"));javaPropertiesMap.put("默认的临时文件路径:" , properties.getProperty("java.io.tmpdir"));javaPropertiesMap.put("操作系统的名称:" , properties.getProperty("os.name"));javaPropertiesMap.put("操作系统的架构" , properties.getProperty("os.arch"));javaPropertiesMap.put("操作系统的版本" , properties.getProperty("os.version"));javaPropertiesMap.put("文件分隔符:" , properties.getProperty("file.separator"));// unix文件分割符 "/"javaPropertiesMap.put("路径分隔符:" , properties.getProperty("path.separator"));// unix系统分隔符 ":"javaPropertiesMap.put("行分隔符" , properties.getProperty("line.separator")); // unix系统 "/n"javaPropertiesMap.put("用户的账户名称:" , properties.getProperty("user.name"));javaPropertiesMap.put("用户的主目录:" , properties.getProperty("user.home"));javaPropertiesMap.put("用户当前工作主目录:" , properties.getProperty("user.dir"));/*** java_message : hash姓名* javaPropertiesMap : hash键值对* 60 : 该数据存活60秒*/redisUtil.hmset("java_message",javaPropertiesMap,60);return "set success";}/*** jvm信息* @return*/@PostMapping("/setJVMMessage")public String setJVMMMessage(){Map<String,Object> jvmPropertiesMap = new HashMap<String,Object>();Runtime runtime = Runtime.getRuntime();long totalMemoryBytes = runtime.totalMemory(); // JVM的总内存(字节)long freeMemoryBytes = runtime.freeMemory(); // JVM的空闲内存(字节)long maxMemoryBytes = runtime.maxMemory(); // JVM的最大可用内存(字节)long totalMemoryMB = totalMemoryBytes / (1024 * 1024); // 转换为兆字节long freeMemoryMB = freeMemoryBytes / (1024 * 1024); // 转换为兆字节long maxMemoryMB = maxMemoryBytes / (1024 * 1024); // 转换为兆字节jvmPropertiesMap.put("JVM的总内存:", totalMemoryMB + "MB");jvmPropertiesMap.put("JVM的空闲内存:" , freeMemoryMB + "MB");jvmPropertiesMap.put("JVM的最大可用内存:" , maxMemoryMB + "MB");/*** java_message : hash姓名* javaPropertiesMap : hash键值对* 60 : 该数据存活60秒*/redisUtil.hmset("jvm_message",jvmPropertiesMap,60);return "set success";}/*** cpu信息* @return*/@PostMapping("/cpuMessage")public String cpuMessage(){Map<String,Object> cpuPropertiesMap = new HashMap<String,Object>();String cpuArch = System.getProperty("os.arch"); // CPU架构int cpuCores = Runtime.getRuntime().availableProcessors(); // CPU核心数String cpuName = System.getenv("PROCESSOR_IDENTIFIER"); // CPU名称cpuPropertiesMap.put("CPU架构:" , cpuArch);cpuPropertiesMap.put("CPU名称:" , cpuName);cpuPropertiesMap.put("CPU核心数:" , cpuCores);double systemLoadAverage = osBean.getSystemLoadAverage(); // 系统平均负载double processCpuLoad = osBean.getProcessCpuLoad(); // 当前进程的CPU负载double systemCpuLoad = osBean.getSystemCpuLoad(); // 系统的CPU负载cpuPropertiesMap.put("系统平均负载:" ,systemLoadAverage);cpuPropertiesMap.put("当前进程的CPU负载:" , processCpuLoad);cpuPropertiesMap.put("系统的cpu负载:" , systemCpuLoad);double userCpuLoad = osBean.getProcessCpuTime() / 1000000.0; // 用户态CPU时间double systemCpuTime = osBean.getSystemCpuLoad() * osBean.getAvailableProcessors(); // 系统态CPU时间double idleCpuTime = (1 - osBean.getSystemCpuLoad()) * osBean.getAvailableProcessors(); // 空闲CPU时间cpuPropertiesMap.put("用户使用率:" , userCpuLoad);cpuPropertiesMap.put("系统使用率:" , systemCpuTime);cpuPropertiesMap.put("当前空闲率:" , idleCpuTime);redisUtil.hmset("cpu_message",cpuPropertiesMap);return "set success";}@PostMapping("/memoryMessage")public String memoryMessage(){Map<String,Object> cpuPropertiesMap = new HashMap<String,Object>();// 获取内存long totalPhysicalMemorySize = osBean.getTotalPhysicalMemorySize(); // 物理内存总量long freePhysicalMemorySize = osBean.getFreePhysicalMemorySize(); // 空闲物理内存量long totalSwapSpaceSize = osBean.getTotalSwapSpaceSize(); // 交换空间总量long freeSwapSpaceSize = osBean.getFreeSwapSpaceSize(); // 空闲交换空间量cpuPropertiesMap.put("物理内存总量:" , totalPhysicalMemorySize / (1024 * 1024) + " MB");cpuPropertiesMap.put("空闲物理内存量:" ,freePhysicalMemorySize / (1024 * 1024) + " MB");cpuPropertiesMap.put("交换空间总量:" , totalSwapSpaceSize / (1024 * 1024) + " MB");cpuPropertiesMap.put("空闲交换空间量:" , freeSwapSpaceSize / (1024 * 1024) + " MB");long totalMemorySize = osBean.getTotalPhysicalMemorySize(); // 总内存大小long freeMemorySize = osBean.getFreePhysicalMemorySize(); // 空闲内存大小long usedMemorySize = totalMemorySize - freeMemorySize; // 已用内存大小double memoryUsage = (double) usedMemorySize / totalMemorySize * 100; // 内存使用率cpuPropertiesMap.put("总内存:" , formatMemorySize(totalMemorySize));cpuPropertiesMap.put("已用内存:" , formatMemorySize(usedMemorySize));cpuPropertiesMap.put("剩余内存:" , formatMemorySize(freeMemorySize));cpuPropertiesMap.put("内存使用率:" , String.format("%.2f", memoryUsage) + "%");redisUtil.hmset("memory_essage",cpuPropertiesMap);return "set success";}@PostMapping("/diskMessage")public String diskMessage(){Map<String,Object> diskPropertiesMap = new HashMap<String,Object>();File[] roots = File.listRoots();for (File root : roots) {String path = root.getAbsolutePath();long totalSpace = root.getTotalSpace(); // 总容量long usableSpace = root.getUsableSpace(); // 可用容量double usage = (double) (totalSpace - usableSpace) / totalSpace * 100; // 使用率diskPropertiesMap.put("磁盘路径:" , path);diskPropertiesMap.put("总容量:" , formatMemorySize(totalSpace));diskPropertiesMap.put("可用容量:" , formatMemorySize(usableSpace));diskPropertiesMap.put("使用率:" , String.format("%.2f", usage) + "%");try {FileStore store = Files.getFileStore(root.toPath());String fsType = store.type();String fsName = store.name();diskPropertiesMap.put("文件系统类型:" , fsType);diskPropertiesMap.put("文件系统名称:" , fsName);redisUtil.hmset("disk_message"+fsName , diskPropertiesMap);} catch (IOException e) {e.printStackTrace();}}return "diskMessage set success";}/*** 格式化容量大小** @param bytes* @return*/private static String formatMemorySize(long bytes) {double kiloBytes = bytes / 1024.0; // 将字节数转换为千字节double megaBytes = kiloBytes / 1024.0; // 将千字节转换为兆字节double gigaBytes = megaBytes / 1024.0; // 将兆字节转换为千兆字节if (gigaBytes >= 1) { // 如果容量大于等于1GBreturn String.format("%.2f GB", gigaBytes); // 格式化输出千兆字节容量,并带有两位小数的GB单位} else if (megaBytes >= 1) { // 如果容量大于等于1MBreturn String.format("%.2f MB", megaBytes); // 格式化输出兆字节容量,并带有两位小数的MB单位} else if (kiloBytes >= 1) { // 如果容量大于等于1KBreturn String.format("%.2f KB", kiloBytes); // 格式化输出千字节容量,并带有两位小数的KB单位} else { // 如果容量小于1KBreturn String.format("%d B", bytes); // 格式化输出字节数}}}
获取Redis数据
@GetMapping("/getMessage/{message}")
public Map<String, Object> getMessage(@PathVariable("message") String message) throws JsonProcessingException {// 获取 Hash 操作对象HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();Map<String, Object> hashData = hashOperations.entries(message);return hashData;
}