栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

文章目录
  • Java-SpringBoot-使用Sigar采集设备信息
    • 一、Sigar依赖
    • 二、项目讲解
      • 1. 项目结构图
      • 2. 数据传递实体类
      • 3. Sigar采集类
      • 4. Controller控制类
    • 三、采集失败原因以及解决方法

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

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

一、Sigar依赖
        
        
            org.fusesource
            sigar
            1.6.4
        
        
            org.projectlombok
            lombok
        

 

二、项目讲解

项目环境: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;



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;
    }

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

    
    public static List getCpuList() throws SigarException {
        List 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();
        }
    }
    
    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;



@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:WindowsSystem32 在此路径下移入 sigar-amd64-winnt.dll
Linux: /usr/lib64 在此路径下移入 libsigar-amd64-linux.so

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023851.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号