Java-SpringBoot-使用Sigar采集设备信息

Java-SpringBoot-使用Sigar采集设备信息

项目要求:
采集设备信息,如CPU、内存、网络等信息,实现对设备的监控。
 
项目开源地址:
Gitee: https://gitee.com/xuhy34324/boot_sigar_acquisition
 
使用 Sigar 前的操作
在使用 sigar 采集 window 或者 linux 系统信息时,需要在系统中添加 sigar 的指定文件,否则采集报错。(采集OS的没去研究)
具体操作在末尾。

一、Sigar依赖

        <!-- https://mvnrepository.com/artifact/org.fusesource/sigar -->
        <dependency>
            <groupId>org.fusesource</groupId>
            <artifactId>sigar</artifactId>
            <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

 

二、项目讲解

项目环境:Window10
编译工具:IDEA
使用依赖:Sigar1.6.4、lombok

1. 项目结构图

项目架构图

 

2. 数据传递实体类

CpuVO.java

@Data
public class CpuVO {
    private Double user; // 用户CPU使用率
    private Double sys;  // 系统CPU使用率
    private Double wait; // 等待率
    private Double nice; // 错误率
    private Double idle; // 空闲率
    private Double combined; // 使用率
}

MemoryVO.java

@Data
public class MemoryVO {
    private Long memTotal;  // 内存总容量 /k
    private Long memUsed;   // 当前内存使用量 /k
    private Long memFree;   // 当前内存剩余量 /k
    private Long swapTotal; // 交换区总容量 /k
    private Long swapUsed;  // 当前交换区使用量 /k
    private Long swapFree;  // 当前交换区剩余量 /k
}

3. Sigar采集类

SigarLocalDeviceDataAcquisition.java

package com.example.acquisition;

import com.example.vo.CpuVO;
import com.example.vo.MemoryVO;
import org.hyperic.sigar.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author X_HuiYang
 * @Date 2022/7/21 18:17
 * @Description
 */

public class SigarLocalDeviceDataAcquisition {

    public static Sigar sigar;

    static {
        sigar = new Sigar();
    }

    /**
     * 采集本地设备内存数据
     */
    public static MemoryVO getMemory() throws SigarException {

        // 获取内存数据
        Mem mem = sigar.getMem();
        // 内存总容量
        long memTotal = mem.getTotal();
        // 内存使用量
        long memUsed = mem.getUsed();
        // 内存空闲量
        long memFree = mem.getFree();

        // 获取内存交换区数据
        Swap swap = sigar.getSwap();
        // 交换区总容量
        long swapTotal = swap.getTotal();
        // 交换区使用量
        long swapUsed = swap.getUsed();
        // 交换区剩余量
        long swapFree = swap.getFree();

        MemoryVO memoryVO = new MemoryVO();
        // 除以1024,将原始数据单位变成 /k
        memoryVO.setMemTotal(memTotal / 1024L);
        memoryVO.setMemUsed(memUsed / 1024L);
        memoryVO.setMemFree(memFree / 1024L);
        memoryVO.setSwapTotal(swapTotal / 1024L);
        memoryVO.setSwapUsed(swapUsed / 1024L);
        memoryVO.setSwapFree(swapFree / 1024L);

        return memoryVO;
    }

    /**
     * 采集本地设备CPU数据
     */
    public static CpuVO getCpu() throws SigarException {
        // 获取设备总CPU数据
        CpuPerc cpuPerc = sigar.getCpuPerc();
        return cpuFormat(cpuPerc);
    }

    /**
     * 采集本地设备所有CPU数据
     */
    public static List<CpuVO> getCpuList() throws SigarException {
        List<CpuVO> cpuVOS = new ArrayList<>();

        // 获取设备所有CPU数据
        CpuPerc[] cpuPercList = sigar.getCpuPercList();
        for (CpuPerc cpuPerc : cpuPercList) {
            cpuVOS.add(cpuFormat(cpuPerc));
        }
        return cpuVOS;
    }


    /**
     * 采集本地设备所有网络数据
     */
    public static void getNetwork() throws SigarException {
        // 获取所有网络设备名称
        String[] netInterfaceList = sigar.getNetInterfaceList();
        for (String netInterface : netInterfaceList) {
            // 按照名称查询所属数据
            NetInterfaceConfig netInterfaceConfig = sigar.getNetInterfaceConfig(netInterface);

            // IP地址
            String address = netInterfaceConfig.getAddress();
            // 子网掩码
            String netmask = netInterfaceConfig.getNetmask();

            NetInterfaceStat netInterfaceStat = sigar.getNetInterfaceStat(netInterface);
            // 接收的总包数量
            long rxPackets = netInterfaceStat.getRxPackets();
            // 发送的总包数量
            long txPackets = netInterfaceStat.getTxPackets();
            // 接收的总字节数量
            long rxBytes = netInterfaceStat.getRxBytes();
            // 发送的总字节数量
            long txBytes = netInterfaceStat.getTxBytes();
            // 接收的错误包数量
            long rxErrors = netInterfaceStat.getRxErrors();
            // 发送的错误包数量
            long txErrors = netInterfaceStat.getTxErrors();
            // 接收时丢包数量
            long rxDropped = netInterfaceStat.getRxDropped();
            // 发送时丢包数量
            long txDropped = netInterfaceStat.getTxDropped();
        }
    }
    /**
     * CPU数据返回值格式化
     * */
    private static CpuVO cpuFormat(CpuPerc cpuPerc) {
        // 用户使用率
        String cpuUser = CpuPerc.format(cpuPerc.getUser());  // CpuPerc.format() 此方法是将采集到的原始数据 计算成带 % 的使用率
        // 系统使用率
        String cpuSys = CpuPerc.format(cpuPerc.getSys());
        // 当前等待率
        String cpuWait = CpuPerc.format(cpuPerc.getWait());
        // 当前错误率
        String cpuNice = CpuPerc.format(cpuPerc.getNice());
        // 当前空闲率
        String cpuIdle = CpuPerc.format(cpuPerc.getIdle());
        // 当前使用率
        String cpuCombined = CpuPerc.format(cpuPerc.getCombined());


        CpuVO cpuVO = new CpuVO();
        // 将String类型的数据 转换为Double类型的数据。(个人项目需求 <非必要>)
        cpuVO.setUser(Double.parseDouble(cpuUser.replace("%","")));
        cpuVO.setSys(Double.parseDouble(cpuSys.replace("%","")));
        cpuVO.setWait(Double.parseDouble(cpuWait.replace("%","")));
        cpuVO.setNice(Double.parseDouble(cpuNice.replace("%","")));
        cpuVO.setIdle(Double.parseDouble(cpuIdle.replace("%","")));
        cpuVO.setCombined(Double.parseDouble(cpuCombined.replace("%","")));

        return cpuVO;
    }
}

4. Controller控制类

SigarController.java

package com.example.controller;

import com.example.acquisition.SigarLocalDeviceDataAcquisition;
import com.example.vo.CpuVO;
import com.example.vo.MemoryVO;
import lombok.extern.slf4j.Slf4j;
import org.hyperic.sigar.SigarException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author X_HuiYang
 * @Date 2022/7/21 18:16
 * @Description
 */

@Slf4j
@RestController
public class SigarController {

    @PostMapping("/cpu")
    public CpuVO getCpuData() {
        try {
            return SigarLocalDeviceDataAcquisition.getCpu();
        } catch (SigarException e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("/memory")
    public MemoryVO getMemory() {
        try {
            return SigarLocalDeviceDataAcquisition.getMemory();
        } catch (SigarException e) {
            e.printStackTrace();
            return null;
        }
    }
}

三、采集失败原因以及解决方法

当你第一次在 Window 或 Linux 上运行 Sigar 采集数据时, 大概率会出现如下错误: 👇
在这里插入图片描述
在 Window 系统上运行是这个错误: no sigar-amd64-winnt.dll in java.library.path
  在 Linux 系统上运行是这个错误: no libsigar-amd64-linux.so in java.library.path

错误原因:
系统缺少 Sigar 指定文件.

解决办法:
在系统文件夹中放入指定文件, 方法如下: 点击跳转文件下载页面
Window: C:\Windows\System32 在此路径下移入 sigar-amd64-winnt.dll
Linux: /usr/lib64 在此路径下移入 libsigar-amd64-linux.so

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot是一种用于构建独立的、可扩展的Java应用程序的开发框架。而SIGAR是一个Java库,用于获取系统信息,如CPU使用率、内存使用率、磁盘空间等。集成SIGAR可以让我们在Spring Boot应用程序中获取系统的详细信息。 要在Spring Boot中集成SIGAR,我们首先需要将SIGAR库添加到项目的依赖中。可以通过在pom.xml文件中添加如下依赖来实现: ```xml <dependency> <groupId>org.fusesource</groupId> <artifactId>sigar</artifactId> <version>1.6.4</version> <scope>runtime</scope> </dependency> ``` 添加依赖后,我们可以使用SIGAR库提供的API来获取系统信息。例如,我们可以编写一个类来获取CPU使用率的示例: ```java import org.hyperic.sigar.CpuPerc; import org.hyperic.sigar.Sigar; import org.springframework.stereotype.Component; @Component public class SystemInfo { private Sigar sigar; public SystemInfo() { sigar = new Sigar(); } public double getCpuUsage() { try { CpuPerc cpuPerc = sigar.getCpuPerc(); return cpuPerc.getCombined(); } catch (Exception e) { e.printStackTrace(); return 0.0; } } } ``` 在这个示例中,我们使用Sigar类和CpuPerc类来获取CPU使用率。通过调用getCpuPerc方法,我们可以得到一个CpuPerc对象,然后通过调用getCombined方法获取CPU的使用率。 在这个类上添加@Component注解可以让Spring Boot自动扫描并将其作为一个Bean进行管理。我们可以在其他地方注入SystemInfo类,并调用getCpuUsage方法来获取CPU使用率。 除了获取CPU使用率,SIGAR库还提供了很多其他的功能,如获取内存使用率、磁盘空间等。使用SIGAR库可以让我们更方便地在Spring Boot应用程序中获取系统信息,帮助我们监控和管理应用程序的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值