Springboot + oshi 实现对服务器硬件的监控

908 阅读3分钟

一、开源项目情况

在网上我们可以看到很多项目都有服务监控功能,类似这样的

image-20230612215929657.png 或者这样的

image-20230612220011437-1686580610491.png

经过查看他们的源码后,发现他们是用的同一套代码,使用了同一个依赖。那就是 oshi,接下来我们详细学习一下这个工具和它的使用方法

以上两个开源项目的地址分别是:

gitee.com/tony2y/RuoY…

gitee.com/dromara/J2E…

二、oshi 介绍

开源地址:github.com/oshi/oshi

介绍

OSHI is a free JNA-based (native) Operating System and Hardware Information library for Java. It does not require the installation of any additional native libraries and aims to provide a cross-platform implementation to retrieve system information, such as OS version, processes, memory and CPU usage, disks and partitions, devices, sensors, etc.

OSHI是一个免费的基于JNA的(本机)Java操作系统和硬件信息库。它不需要安装任何额外的本地库,旨在提供跨平台实现来检索系统信息,如操作系统版本、进程、内存和CPU使用情况、磁盘和分区、设备、传感器等。

注意: 针对不同的jdk版本,需要对应不同的版本,如下

image-20230612220601341-1686580632249.png

支持的功能:

三、支持的功能

  • Computer System and firmware, baseboard 计算机系统和固件,踢脚线
  • Operating System and Version/Build 操作系统和版本/内部版本
  • Physical (core) and Logical (hyperthreaded) CPUs, processor groups, NUMA nodes 物理(核心)和逻辑(超线程)CPU、处理器组、NUMA 节点
  • System and per-processor load, usage tick counters, interrupts, uptime 系统和每个处理器负载、使用时钟周期计数器、中断、正常运行时间
  • Process uptime, CPU, memory usage, user/group, command line args, thread details 进程正常运行时间、CPU、内存使用情况、用户/组、命令行参数、线程详细信息
  • Physical and virtual memory used/available 物理和虚拟内存已用/可用
  • Mounted filesystems (type, usable and total space, options, reads and writes) 挂载的文件系统(类型、可用空间和总空间、选项、读写)
  • Disk drives (model, serial, size, reads and writes) and partitions 磁盘驱动器(型号、串行、大小、读取和写入)和分区
  • Network interfaces (IPs, bandwidth in/out), network parameters, TCP/UDP statistics 网络接口(IP、带宽输入/输出)、网络参数、TCP/UDP 统计信息
  • Battery state (% capacity, time remaining, power usage stats) 电池状态(容量百分比、剩余时间、电量使用情况统计信息)
  • USB Devices
  • Connected displays (with EDID info), graphics and audio cards 连接的显示器(带有 EDID 信息)、图形和音频卡
  • Sensors (temperature, fan speeds, voltage) on some hardware 某些硬件上的传感器(温度、风扇速度、电压)

四、如何使用

  1. 首先看其官方介绍,这是最靠谱的,csdn等博客网站虽然也不错,但也是从官网来的,直接官网,方便快捷

image-20230612220601341-1686580632249.png

看不懂没关系,百度翻译咔咔快

  1. 需要在maven或者gradle引入依赖
  2. 创建一个 SystemInfo实例
  3. 使用 SystemInfo 中的 getter 访问硬件或操作系统组件

我们看到上面有个 See SystemInfoTest.java for examples,我们点击他这个例子,看看他咋使用的,我这里只截取一部分

```
 /**
     * The main method, demonstrating use of classes.
     *
     * @param args the arguments (unused)
     */
    public static void main(String[] args) {
​
        logger.info("Initializing System...");
        SystemInfo si = new SystemInfo();
​
        HardwareAbstractionLayer hal = si.getHardware();
        OperatingSystem os = si.getOperatingSystem();
​
        printOperatingSystem(os);
​
        logger.info("Checking computer system...");
        printComputerSystem(hal.getComputerSystem());
​
        logger.info("Checking Processor...");
        printProcessor(hal.getProcessor());
​
        logger.info("Checking Memory...");
        printMemory(hal.getMemory());
​
        logger.info("Checking CPU...");
        printCpu(hal.getProcessor());
​
        logger.info("Checking Processes...");
        printProcesses(os, hal.getMemory());
​
        logger.info("Checking Services...");
        printServices(os);
    }
```

使用超级简单,new 一个实例,然后直接get就能获取到硬件信息

  1. 在springboot中使用,展现出开源项目的效果

    1. 首先引入依赖

      <oshi.version>6.4.0</oshi.version><!-- 跨平台的系统及硬件信息库 -->
              <dependency>
                  <groupId>com.github.oshi</groupId>
                  <artifactId>oshi-core</artifactId>
                  <version>${oshi.version}</version>
              </dependency>
  2. 将开源项目的代码复制过来,都是实体类没必要重新写(我懒。。)

image-20230612223125462.png

  1. 浏览器访问看效果,拿到json结果后,剩下的就看前端想要的展示效果了,结束撒花。

image-20230612223031229.png