SpringBoot整合sigar实现服务端监控

SpringBoot整合sigar实现服务端监控,第1张

SpringBoot整合sigar实现服务端监控 sigar简介
  • Sigar全名是System Information Gatherer And Reporter,中文名是系统信息收集和报表的开源工具,它用来从许多平台收集系统和处理信息
  • sigar可以运行的平台:Linux,Windows,Mac OSX
  • https://github.com/hyperic/sigar/
  • 依赖汇编文件下载地址:https://sourceforge.net/projects/sigar/files/sigar/1.6/

由于sigar采集的信息时通过底层C语言来执行的,所以需要以下对应的依赖文件【依赖文件依据平台而定】

依赖文件下载地址:

Windows :sigar-amd64-winnt.dll或sigar-x86-winnt.dll

Linux :libsigar-amd64-linux.so或libsigar-x86-linux.so

MacOX :libsigar-universal64-macosx.dylib或libsigar-universal-macosx.dylib

  • sigar可以搜集的信息

CPU信息:包括基本信息(vendor、model、mhz、cacheSize)和统计信息(user、sys、idle、nice、wait)
文件系统信息:包括Filesystem、Size、Used、Avail、Use%、Type
事件信息:类似Service Control Manager
内存信息:物理内存和交换内存的总数、使用数、剩余数;RAM的大小
网络信息:包括网络接口信息和网络路由信息
进程信息:包括每个进程的内存、CPU占用数、状态、参数、句柄
IO信息:包括IO的状态,读写大小等
服务状态信息
系统信息:包括 *** 作系统版本,系统资源限制情况,系统运行时间以及负载,JAVA的版本信息等

springboot整合sigar使用【以Linux系统为例】
  1. 首先pom.xml引入依赖包

    org.fusesource
    sigar
    1.6.4

  1. 通过controller写个demo
import cn.hutool.core.util.NumberUtil;
import com.nio.commons.common.ResponseResult;
import com.nio.commons.entity.monitor.ResourcePoolMonitorInfo;
import com.nio.commons.enums.common.ResponseCodeEnums;
import com.nio.resourcepool.utils.BigDecimalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.net.InetAddress;

@Slf4j
@RestController
public class TestController {
    private Sigar sigar = new Sigar();
    private static final String UNIT = "g";

    @GetMapping("/monitor")
    public ResponseResult getRes(){
        return new ResponseResult(ResponseCodeEnums.SUCCESS,monitor("xxxx"));
    }

		public ResourcePoolMonitorInfo monitor(String id) {
        ResourcePoolMonitorInfo monitorInfo = new ResourcePoolMonitorInfo();
        try {
            //设置机器IP地址
            monitorInfo.setIp(InetAddress.getLocalHost().getHostAddress());
            // 内存
            memoryHandle(monitorInfo);
            //cpu
            cpuHandle(monitorInfo);
            return monitorInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    
    private void memoryHandle(ResourcePoolMonitorInfo monitorInfo) throws SigarException {
        Mem mem = sigar.getMem();
        long total = mem.getTotal();
        long free = mem.getFree();
        long used = mem.getUsed();
        monitorInfo.setMemTotal(this.getCountByUnit(total, UNIT));
        monitorInfo.setMemUsed(this.getCountByUnit(used, UNIT));
        monitorInfo.setMemFree(this.getCountByUnit(free, UNIT));
        monitorInfo.setMemUsedRate(getPercent(used, total));
        monitorInfo.setMemFreeRate(getPercent(free, total));
    }
    
    private void cpuHandle(ResourcePoolMonitorInfo monitorInfo) throws SigarException {
        CpuPerc cpuPerc = sigar.getCpuPerc();
        org.hyperic.sigar.CpuInfo[] cpuInfoList = sigar.getCpuInfoList();
        double user = cpuPerc.getUser();
        double sys = cpuPerc.getSys();
        String userPercent = getPercent(user, 1);
        String sysPercent = getPercent(sys, 1);
        String totalPercent = NumberUtil.add(userPercent, sysPercent).toString();
        BigDecimal idleDecimal = NumberUtil.sub("100", totalPercent);
        String idleRate = NumberUtil.decimalFormat("#.#", idleDecimal);
        monitorInfo.setCpuCore(String.valueOf(cpuInfoList.length));
        monitorInfo.setCpuUseRate(userPercent);
        monitorInfo.setCpuSysRate(sysPercent);
        monitorInfo.setCpuIdleRate(idleRate);
        monitorInfo.setCpuTotalRate(totalPercent);
    }
    
    private String getCountByUnit(Long param, String unit) {
        double c = 1024d;
        double count = 0d;
        double num = 0d;

        if (StringUtils.isEmpty(unit)) {
            unit = "m";
        }
        try {
            if ("g".equalsIgnoreCase(unit)) {
                count = BigDecimalUtil.mul(c, BigDecimalUtil.mul(c, c));
            } else if ("m".equalsIgnoreCase(unit)) {
                count = BigDecimalUtil.mul(c, c);
            } else if ("k".equalsIgnoreCase(unit)) {
                count = c;
            }
            num = BigDecimalUtil.div(param.doublevalue(), count, 2);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return NumberUtil.decimalFormat("#.#", num);
    }

    private String getPercent(Number num1, Number num2) {
        Number div = NumberUtil.div(num1, num2);
        Number mul = NumberUtil.mul(div, 100);
        return NumberUtil.decimalFormat("#.#", mul);
    }
}
  1. ResourcePoolMonitorInfoclass文件内容
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResourcePoolMonitorInfo {
    
    private String ip;
    
    private String memTotal;
    
    private String memUsed;
    
    private String memUsedRate;
    
    private String memFree;
    
    private String memFreeRate;
    
    private String cpuCore;
    
    private String cpuUseRate;
    
    private String cpuSysRate;
    
    private String cpuIdleRate;
    
    private String cpuTotalRate;
}
  1. 将libsigar-amd64-linux.so或libsigar-x86-linux.so两个文件部署到linux下/usr/lib目录下

可以通过System.getProperty("java.library.path");查看java库的安装路径


5. 请求:http://ip:port/monitor 查看响应内容

  1. 返回指标与linux top命令比较

内存指标对比:
memTotal 对标 top中Mem total
memUsed对标 top中Mem used
memUsedRate对标 top中Mem used/total * 100
memFree对标 top中Mem free
memFreeRate对标 top中Mem free/total * 100
CPU指标对比:
cpuUseRate对标CPU中 us
cpuSysRate对标CPU中 sy
cpuIdleRate对标CPU中 id
cpuTotalRate = us + sy

另一种资源监控框架oshi介绍
  • OSHI.是一个基于JNA的免费的本地 *** 作系统和Java的硬件信息库。它不需要安装任何额外的本机库
  • 通过API可以获取到: *** 作系统版本、进程、内存和CPU使用情况、磁盘和分区、设备、传感器等信息
  • https://github.com/oshi/oshi
OSHI使用
  1. pom.xml引入依赖包

    com.github.oshi
    oshi-core
    6.0.0

  1. demo
package nio.seq.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import oshi.hardware.*;
import oshi.software.os.NetworkParams;
import oshi.software.os.OSFileStore;
import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem;
import oshi.hardware.CentralProcessor;
import oshi.util.FormatUtil;
import oshi.util.Util;
import oshi.SystemInfo;
import oshi.software.os.FileSystem;

import java.util.Arrays;
import java.util.List;


public class SystemInfoTest {

    private static void printComputerSystem(final ComputerSystem computerSystem) {
        System.out.println("manufacturer: " + computerSystem.getManufacturer());
        System.out.println("model: " + computerSystem.getModel());
        System.out.println("serialnumber: " + computerSystem.getSerialNumber());
        final Firmware firmware = computerSystem.getFirmware();
        System.out.println("firmware:");
        System.out.println("  manufacturer: " + firmware.getManufacturer());
        System.out.println("  name: " + firmware.getName());
        System.out.println("  description: " + firmware.getDescription());
        System.out.println("  version: " + firmware.getVersion());
        System.out.println("  release date: " + (firmware.getReleaseDate() == null ? "unknown"
                : firmware.getReleaseDate() == null ? "unknown" : firmware.getReleaseDate()));
        final baseboard baseboard = computerSystem.getbaseboard();

        System.out.println("baseboard:");
        System.out.println("  manufacturer: " + baseboard.getManufacturer());
        System.out.println("  model: " + baseboard.getModel());
        System.out.println("  version: " + baseboard.getVersion());
        System.out.println("  serialnumber: " + baseboard.getSerialNumber());
    }

    private static void printProcessor(CentralProcessor processor) {
        System.out.println(processor);
        System.out.println(" " + processor.getPhysicalProcessorCount() + " physical CPU(s)");
        System.out.println(" " + processor.getLogicalProcessorCount() + " logical CPU(s)");
        System.out.println("Identifier: " + processor.getProcessorIdentifier());
    }

    private static void printMemory(GlobalMemory memory) {
        System.out.println("以使用内存: " + FormatUtil.formatBytes(memory.getAvailable()) + "总共内存"
                + FormatUtil.formatBytes(memory.getTotal()));
        System.out.println("Swap used: " + FormatUtil.formatBytes(memory.getVirtualMemory().getSwapUsed()) + "/"
                + FormatUtil.formatBytes(memory.getVirtualMemory().getSwapTotal()));
    }

    private static void printCpu(CentralProcessor processor) {
        System.out.println("Uptime: " + FormatUtil.formatElapsedSecs(processor.getSystemCpuLoadTicks()[0]));
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        System.out.println("CPU, IOWait, and IRQ ticks @ 0 sec:" + Arrays.toString(prevTicks));
        // Wait a second...
        Util.sleep(1000);
        long[] ticks = processor.getSystemCpuLoadTicks();
        System.out.println("CPU, IOWait, and IRQ ticks @ 1 sec:" + Arrays.toString(ticks));
        long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
        long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
        long sys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
        long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
        long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
        long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
        long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
        long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
        long totalCpu = user + nice + sys + idle + iowait + irq + softirq + steal;
        long[] systemCpuLoadTicks = processor.getSystemCpuLoadTicks();
        System.out.format(
                "User: %.1f%% Nice: %.1f%% System: %.1f%% Idle: %.1f%% IOwait: %.1f%% IRQ: %.1f%% SoftIRQ: %.1f%% Steal: %.1f%%%n",
                100d * user / totalCpu, 100d * nice / totalCpu, 100d * sys / totalCpu, 100d * idle / totalCpu,
                100d * iowait / totalCpu, 100d * irq / totalCpu, 100d * softirq / totalCpu, 100d * steal / totalCpu);
        System.out.format("CPU load: %.1f%% (counting ticks)%n", processor.getSystemCpuLoadBetweenTicks(systemCpuLoadTicks) * 100);
        System.out.format("CPU load: %.1f%% (OS MXBean)%n", processor.getSystemCpuLoadBetweenTicks(systemCpuLoadTicks) * 100);
        double[] loadAverage = processor.getSystemLoadAverage(3);
        System.out.println("CPU load averages:" + (loadAverage[0] < 0 ? " N/A" : String.format(" %.2f", loadAverage[0]))
                + (loadAverage[1] < 0 ? " N/A" : String.format(" %.2f", loadAverage[1]))
                + (loadAverage[2] < 0 ? " N/A" : String.format(" %.2f", loadAverage[2])));
        // per core CPU
        StringBuilder procCpu = new StringBuilder("CPU load per processor:");
    }

    private static void printProcesses(OperatingSystem os, GlobalMemory memory) {
        System.out.println("Processes: " + os.getProcessCount() + ", Threads: " + os.getThreadCount());
        // Sort by highest CPU
        List procs = os.getProcesses();
        System.out.println("   PID  %CPU %MEM       VSZ       RSS Name");
        for (int i = 0; i < procs.size() && i < 5; i++) {
            OSProcess p = procs.get(i);
            System.out.format(" %5d %5.1f %4.1f %9s %9s %s%n", p.getProcessID(),
                    100d * (p.getKernelTime() + p.getUserTime()) / p.getUpTime(),
                    100d * p.getResidentSetSize() / memory.getTotal(), FormatUtil.formatBytes(p.getVirtualSize()),
                    FormatUtil.formatBytes(p.getResidentSetSize()), p.getName());
        }
    }

    private static void printSensors(Sensors sensors) {
        System.out.println("Sensors:");
        System.out.format(" CPU Temperature: %.1f°C%n", sensors.getCpuTemperature());
        System.out.println(" Fan Speeds: " + Arrays.toString(sensors.getFanSpeeds()));
        System.out.format(" CPU Voltage: %.1fV%n", sensors.getCpuVoltage());
    }

    private static void printPowerSources(List powerSources) {
        StringBuilder sb = new StringBuilder("Power: ");
        if (powerSources.size() == 0) {
            sb.append("Unknown");
        } else {
            double timeRemaining = powerSources.get(0).getTimeRemainingEstimated();
            if (timeRemaining < -1d) {
                sb.append("Charging");
            } else if (timeRemaining < 0d) {
                sb.append("Calculating time remaining");
            } else {
                sb.append(String.format("%d:%02d remaining", (int) (timeRemaining / 3600),
                        (int) (timeRemaining / 60) % 60));
            }
        }
        for (PowerSource pSource : powerSources) {
            sb.append(String.format("%n %s @ %.1f%%", pSource.getName(), pSource.getRemainingCapacityPercent() * 100d));
        }
        System.out.println(sb.toString());
    }

    private static void printDisks(List diskStores) {
        System.out.println("Disks:");
        for (HWDiskStore disk : diskStores) {
            boolean readwrite = disk.getReads() > 0 || disk.getWrites() > 0;
            System.out.format(" %s: (model: %s - S/N: %s) size: %s, reads: %s (%s), writes: %s (%s), xfer: %s ms%n",
                    disk.getName(), disk.getModel(), disk.getSerial(),
                    disk.getSize() > 0 ? FormatUtil.formatBytesDecimal(disk.getSize()) : "?",
                    readwrite ? disk.getReads() : "?", readwrite ? FormatUtil.formatBytes(disk.getReadBytes()) : "?",
                    readwrite ? disk.getWrites() : "?", readwrite ? FormatUtil.formatBytes(disk.getWriteBytes()) : "?",
                    readwrite ? disk.getTransferTime() : "?");
            List partitions = disk.getPartitions();
            if (partitions == null) {
                // TODO Remove when all OS's implemented
                continue;
            }
            for (HWPartition part : partitions) {
                System.out.format(" |-- %s: %s (%s) Maj:Min=%d:%d, size: %s%s%n", part.getIdentification(),
                        part.getName(), part.getType(), part.getMajor(), part.getMinor(),
                        FormatUtil.formatBytesDecimal(part.getSize()),
                        part.getMountPoint().isEmpty() ? "" : " @ " + part.getMountPoint());
            }
        }
    }

    private static void printFileSystem(FileSystem fileSystem) {
        System.out.println("File System:");
        System.out.format(" File Descriptors: %d/%d%n", fileSystem.getOpenFileDescriptors(),
                fileSystem.getMaxFileDescriptors());
        List fsArray = fileSystem.getFileStores();
        for (OSFileStore fs : fsArray) {
            long usable = fs.getUsableSpace();
            long total = fs.getTotalSpace();
            System.out.format(
                    " %s (%s) [%s] %s of %s free (%.1f%%) is %s "
                            + (fs.getLogicalVolume() != null && fs.getLogicalVolume().length() > 0 ? "[%s]" : "%s")
                            + " and is mounted at %s%n",
                    fs.getName(), fs.getDescription().isEmpty() ? "file system" : fs.getDescription(), fs.getType(),
                    FormatUtil.formatBytes(usable), FormatUtil.formatBytes(fs.getTotalSpace()), 100d * usable / total,
                    fs.getVolume(), fs.getLogicalVolume(), fs.getMount());
        }
    }

    private static void printNetworkInterfaces(List networkIFs) {
        System.out.println("Network interfaces:");
        for (NetworkIF net : networkIFs) {
            System.out.format(" Name: %s (%s)%n", net.getName(), net.getDisplayName());
            System.out.format("   MAC Address: %s %n", net.getMacaddr());
            System.out.format("   MTU: %s, Speed: %s %n", net.getMTU(), FormatUtil.formatValue(net.getSpeed(), "bps"));
            System.out.format("   IPv4: %s %n", Arrays.toString(net.getIPv4addr()));
            System.out.format("   IPv6: %s %n", Arrays.toString(net.getIPv6addr()));
            boolean hasData = net.getBytesRecv() > 0 || net.getBytesSent() > 0 || net.getPacketsRecv() > 0
                    || net.getPacketsSent() > 0;
            System.out.format("   Traffic: received %s/%s%s; transmitted %s/%s%s %n",
                    hasData ? net.getPacketsRecv() + " packets" : "?",
                    hasData ? FormatUtil.formatBytes(net.getBytesRecv()) : "?",
                    hasData ? " (" + net.getInErrors() + " err)" : "",
                    hasData ? net.getPacketsSent() + " packets" : "?",
                    hasData ? FormatUtil.formatBytes(net.getBytesSent()) : "?",
                    hasData ? " (" + net.getOutErrors() + " err)" : "");
        }
    }

    private static void printNetworkParameters(NetworkParams networkParams) {
        System.out.println("Network parameters:");
        System.out.format(" Host name: %s%n", networkParams.getHostName());
        System.out.format(" Domain name: %s%n", networkParams.getDomainName());
        System.out.format(" DNS servers: %s%n", Arrays.toString(networkParams.getDnsServers()));
        System.out.format(" IPv4 Gateway: %s%n", networkParams.getIpv4DefaultGateway());
        System.out.format(" IPv6 Gateway: %s%n", networkParams.getIpv6DefaultGateway());
    }

    private static void printDisplays(List displays) {
        System.out.println("Displays:");
        int i = 0;
        for (Display display : displays) {
            System.out.println(" Display " + i + ":");
            System.out.println(display.toString());
            i++;
        }
    }

    private static void printUsbDevices(List usbDevices) {
        System.out.println("USB Devices:");
        for (UsbDevice usbDevice : usbDevices) {
            System.out.println(usbDevice.toString());
        }
    }

    public static void main(String[] args) {
        Logger LOG = LoggerFactory.getLogger(SystemInfoTest.class);
        LOG.info("Initializing System...");
        SystemInfo si = new SystemInfo();
        HardwareAbstractionLayer hal = si.getHardware();
        OperatingSystem os = si.getOperatingSystem();
        System.out.println("os platform===>" + os);
        LOG.info("Checking computer system...");
        printComputerSystem(hal.getComputerSystem());
        LOG.info("Checking Processor...");
        printProcessor(hal.getProcessor());
        LOG.info("Checking Memory...");
        printMemory(hal.getMemory());
        LOG.info("Checking CPU...");
        printCpu(hal.getProcessor());
        LOG.info("Checking Processes...");
        printProcesses(os, hal.getMemory());
        LOG.info("Checking Sensors...");
        printSensors(hal.getSensors());
        LOG.info("Checking Power sources...");
        printPowerSources(hal.getPowerSources());
        LOG.info("Checking Disks...");
        printDisks(hal.getDiskStores());
        LOG.info("Checking File System...");
        printFileSystem(os.getFileSystem());
        LOG.info("Checking Network interfaces...");
        printNetworkInterfaces(hal.getNetworkIFs());
        LOG.info("Checking Network parameterss...");
        printNetworkParameters(os.getNetworkParams());
        // hardware: displays
        LOG.info("Checking Displays...");
        printDisplays(hal.getDisplays());
        // hardware: USB devices
        LOG.info("Checking USB Devices...");
        printUsbDevices(hal.getUsbDevices(true));
    }

}
总结

Sigar 与OSHI都能够实现服务端资源监控,但是由于sigar是无法直接获取到 *** 作系统资源,所以需要通过C语言的汇编文件获取,从而 *** 作步骤会比较麻烦,相比于OSHI直接引入jar就可以获取硬件资源,若使用个人推荐OSHI

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5697810.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存