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

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.xdnf.cn/news/2994.html

相关文章:

  • I2C、SPI、UART 协议详解:优缺点与使用场景
  • Git操作指令
  • Nacos源码—2.Nacos服务注册发现分析三
  • 数据库概论速成期中版
  • RHCE第七章:SElinux
  • 初中九年级学生体测准考证照片采集软件使用说明
  • 机器学习之五:基于解释的学习
  • 【codeforces 2104D,E】欧拉筛,字符串上dp
  • 深度剖析 RocketMQ 5.0 之消息进阶:如何支撑复杂业务消息场景?
  • 飞鸟游戏模拟器 1.0.3 | 完全免费无广告,内置大量经典童年游戏,重温美好回忆
  • React 19 用到的新特性
  • CosyVoice、F5-TTS、GPT-SoVITS、Fish-Speech声音模型项目深度对比:选型指南
  • Linux 下编译BusyBox
  • SVMSPro平台获取Websocket视频流规则
  • 设计模式每日硬核训练 Day 17:中介者模式(Mediator Pattern)完整讲解与实战应用
  • 顺序结构双链表的实现
  • 【LLM】解析RAG增强检索技术:原理、实现与应用
  • 【HCIA】VRRP
  • LeetCode 2906 统计最大元素出现至少K次的子数组(滑动窗口)
  • 软件测试基础知识详解
  • 【AI面试准备】负责所有Al产品的模型能力评估及测试,保障AI产品的质量
  • AI Agent Protocols:现状、挑战与未来展望
  • 使用VS2022开发并部署QT应用
  • Karmada 多 Kubernetes集群管理实战
  • 如何查看和验证AWS CloudFront的托管区域ID
  • unity在编辑器模式调试音频卡顿电流声
  • 什么是向量库和数据向量化?建设向量库有什么作用?
  • vue.js中的一些事件修饰符【前端】
  • Pytest中的fixture装饰器详解
  • OpenCV 图形API(72)图像与通道拼接函数-----根据指定的方式翻转图像(GMat)函数 flip()