当前位置: 首页 > news >正文

SpringBoot中获取系统及硬件信息

今天给大家介绍下,如何在SpringBoot中获取系统及硬件信息,话不多说,直接上干货 🔮

一.、引入maven依赖包

<dependency><groupId>com.github.oshi</groupId><artifactId>oshi-core</artifactId><version>6.4.1</version>
</dependency>

二、编写工具类

import cn.hutool.system.oshi.CpuInfo;
import cn.hutool.system.oshi.OshiUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import oshi.SystemInfo;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem;import java.net.InetAddress;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Properties;public class OSUtils {/*** 获取CPU使用率* @return*/public static String getCpuUsage() {CpuInfo cpuInfo = OshiUtil.getCpuInfo();double free = cpuInfo.getFree();DecimalFormat format = new DecimalFormat("#.00");return format.format(100D - free)+" %";}/*** 获取内存信息* @return*/public static JSONObject getMemoryInfo() {JSONObject memoryInfo = new JSONObject();memoryInfo.put("total", formatByte(OshiUtil.getMemory().getTotal()));memoryInfo.put("free", formatByte(OshiUtil.getMemory().getAvailable()));memoryInfo.put("used", formatByte(OshiUtil.getMemory().getTotal() - OshiUtil.getMemory().getAvailable()));memoryInfo.put("usage", new DecimalFormat("#.## %").format(1.0 - OshiUtil.getMemory().getAvailable() * 1.0 / OshiUtil.getMemory().getTotal()));return memoryInfo;}/*** 获取磁盘信息* @return*/public static JSONArray getDiskInfo() {JSONObject diskInfo = null;JSONArray diskInfoArr = new JSONArray();SystemInfo systemInfo = new SystemInfo();OperatingSystem operatingSystem = systemInfo.getOperatingSystem();FileSystem fileSystem = operatingSystem.getFileSystem();List<OSFileStore> fileStores = fileSystem.getFileStores();for (OSFileStore fileStore : fileStores) {diskInfo = new JSONObject();// 盘符路径diskInfo.put("mountName", fileStore.getMount());// 磁盘类型diskInfo.put("diskType", fileStore.getType());// 磁盘容量diskInfo.put("total", formatByte(fileStore.getTotalSpace()));// 磁盘剩余容量diskInfo.put("free", formatByte(fileStore.getUsableSpace()));// 磁盘已使用容量diskInfo.put("used", formatByte(fileStore.getTotalSpace() - fileStore.getUsableSpace()));if (fileStore.getTotalSpace() == 0) {// 磁盘容量使用率diskInfo.put("usage", 0);} else {diskInfo.put("usage",new DecimalFormat("#.## %").format((fileStore.getTotalSpace() - fileStore.getUsableSpace()) * 1.0 / fileStore.getTotalSpace()));}diskInfoArr.add(diskInfo);}return diskInfoArr;}/*** 获取系统信息* @return*/public static JSONObject getSysInfo() {JSONObject sysInfo = new JSONObject();try {Properties props = System.getProperties();// 操作系统名称sysInfo.put("osName", props.getProperty("os.name"));// 系统架构sysInfo.put("osArch", props.getProperty("os.arch"));// 服务器名称sysInfo.put("hostName", InetAddress.getLocalHost().getHostName());// 服务器IpsysInfo.put("hostAddress", InetAddress.getLocalHost().getHostAddress());} catch (Exception e) {e.printStackTrace();}return sysInfo;}/*** 获取Java虚拟机信息* @return*/public static JSONObject getJvmInfo() {JSONObject jvmInfo = new JSONObject();Properties props = System.getProperties();Runtime runtime = Runtime.getRuntime();long jvmMaxMemory = runtime.maxMemory();long jvmTotalMemoryByte = runtime.totalMemory();long jvmFreeMemoryByte = runtime.freeMemory();long jvmUsedMemoryByte = jvmTotalMemoryByte - jvmFreeMemoryByte;// jvm最大可申请内存jvmInfo.put("max", formatByte(jvmMaxMemory));// jvm总内存jvmInfo.put("total", formatByte(jvmTotalMemoryByte));// jvm剩余内存jvmInfo.put("free", formatByte(jvmFreeMemoryByte));// jvm已使用内存jvmInfo.put("used", formatByte(jvmUsedMemoryByte));// jvm内存使用率jvmInfo.put("usage", new DecimalFormat("#.## %").format((jvmUsedMemoryByte) * 1.0 / jvmTotalMemoryByte));// jdk路径jvmInfo.put("jdkHome", props.getProperty("java.home"));// jdk版本jvmInfo.put("jdkVersion", props.getProperty("java.version"));// 进程idjvmInfo.put("pid", props.getProperty("PID"));// 项目路径jvmInfo.put("projectDir", props.getProperty("user.dir"));// 时区jvmInfo.put("timeZone", props.getProperty("user.timezone"));// 账户名称jvmInfo.put("userName", props.getProperty("user.name"));return jvmInfo;}/*** 单位转换*/private static String formatByte(long byteNumber) {// 换算单位double FORMAT = 1024.0;double kbNumber = byteNumber / FORMAT;if (kbNumber < FORMAT) {return new DecimalFormat("#.## KB").format(kbNumber);}double mbNumber = kbNumber / FORMAT;if (mbNumber < FORMAT) {return new DecimalFormat("#.## MB").format(mbNumber);}double gbNumber = mbNumber / FORMAT;if (gbNumber < FORMAT) {return new DecimalFormat("#.## GB").format(gbNumber);}double tbNumber = gbNumber / FORMAT;return new DecimalFormat("#.## TB").format(tbNumber);}}

三、效果展示

相关文章:

  • C++学习:六个月从基础到就业——模板编程:模板元编程基础
  • mermaid 序列图 解析
  • 如何用python脚本把一个表格有4万多条数据分为两个文件表,每个2万条数据?
  • 华为云IoT平台与MicroPython实战:从MQTT协议到物联网设备开发
  • 基于PHP的宠物用品商城
  • TCL科技2025一季度归母净利润10.1亿,半导体显示业务业绩创新高
  • 大模型备案实操手册:材料准备、流程解析与常见问题避坑指南
  • Spark GraphX 机器学习:图计算
  • 数据库所有知识
  • 如何设计一个会员码表!唯一索引的使用,字段区分度不高如何处理
  • 【AI面试准备】深度学习、大模型原理,算法项目经验
  • jthread是否可以完全取代thread?
  • Java高频面试之并发编程-11
  • Git 操作命令
  • 1.PowerBi保姆级安装教程
  • 驱动开发硬核特训 · Day 24(下篇):深入理解 Linux 内核时钟子系统结构
  • PSO详解变体上新!新型混合蛾焰粒子群优化(MFPSO)算法
  • 如何搭建一个简单的文件服务器的方法
  • 使用 DBeaver 将数据从 PostgreSQL 导出到 SQLite
  • Kotlin 常见问题
  • 智能终端出海服务创新联合体成立
  • 史学巨擘的思想地图与学术路径——王汎森解析梁启超、陈寅恪、傅斯年
  • 中老铁路跨境国际旅客突破50万人次
  • 国泰海通合并后首份业绩报告出炉:一季度净利润增逾391%
  • 圆桌|特朗普上台百日未能结束俄乌冲突,若美国“退出”会发生什么?
  • 工信部:加快自动驾驶系统安全要求强制性国家标准研制