当前位置: 首页 > 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);}}

三、效果展示

http://www.dtcms.com/a/163440.html

相关文章:

  • 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 常见问题
  • 深度解析 MyBatis`@TableField(typeHandler = JacksonTypeHandler.class)`:优雅处理复杂数据存储
  • 从 BERT 到 GPT:Encoder 的 “全局视野” 如何喂饱 Decoder 的 “逐词纠结”
  • 【语法】C++继承中遇到的问题及解决方法
  • E2E 测试
  • JavaScript 相关知识点整理
  • C++ 红黑树
  • 【Vagrant+VirtualBox创建自动化虚拟环境】Ansible测试Playbook
  • git fetch和git pull的区别
  • ​【空间数据分析】缓冲区分析--泰森多边形(Voronoi Diagram)-arcgis操作
  • Vue使用Sortablejs拖拽排序 视图显示与数据不一致、拖拽结束后回跳问题