当前位置: 首页 > >

sigar获取机器部分负载信息方法及问题解决

发布时间:


一、获取负载信息部分:
结果部分数据展示:


?
详情请看点这里↓



相关代码如下:
pojo

import java.util.Date;

import com.geespace.microservices.calculate.execute.engine.bean.ModelField;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

/**
* ResourInformation
*
* @Author: liudz
* @Date: 2019-11-07
**/
@Data
@EqualsAndHashCode(callSuper = false)
@ToString(callSuper = true)
public class MachineResourceInfo {

/**
* cpuLoadDetails
*/
@ModelField(name = "CPU负载详情信息", comment = "CPU负载详情信息", size = "500", required = false)
String cpuLoadDetails;
/**
* memoryLoadTotalUsePercent
*/
@ModelField(name = "内存总使用百分比", comment = "内存总使用百分比", size = "500", required = false)
String memoryLoadTotalUsePercent;
/**
* memoryLoadTotal
*/
@ModelField(name = "内存总量", comment = "内存总量", size = "500", required = false)
String memoryLoadTotal;
/**
* memoryLoadUsed
*/
@ModelField(name = "当前内存使用量", comment = "当前内存使用量", size = "500", required = false)
String memoryLoadUsed;
/**
* memoryLoadFree
*/
@ModelField(name = "当前内存剩余量", comment = "当前内存剩余量", size = "500", required = false)
String memoryLoadFree;
/**
* memoryExchangeTotal
*/
@ModelField(name = "内存交换区总量", comment = "内存交换区总量", size = "500", required = false)
String memoryExchangeTotal;
/**
* memoryExchangeUsed
*/
@ModelField(name = "内存当前交换区使用量", comment = "内存当前交换区使用量", size = "500", required = false)
String memoryExchangeUsed;
/**
* memoryExchangeFree
*/
@ModelField(name = "内存当前交换区剩余量", comment = "内存当前交换区剩余量", size = "500", required = false)
String memoryExchangeFree;
/**
* diskLoad
*/
@ModelField(name = "磁盘使用情况", comment = "磁盘使用情况", size = "500", required = false)
String diskLoad;
/**
* diskReadOrWrite
*/
@ModelField(name = "磁盘读写", comment = "磁盘读写", size = "500", required = false)
String diskReadOrWrite;
/**
* jvmHeapMemoryMax
*/
@ModelField(name = "JVM堆内存信息Max", comment = "JVM堆内存信息Max", size = "500", required = false)
String jvmHeapMemoryMax;
/**
* jvmHeapMemoryInit
*/
@ModelField(name = "JVM堆内存信息Init", comment = "JVM堆内存信息Init", size = "500", required = false)
String jvmHeapMemoryInit;
/**
* jvmHeapMemoryCommitted
*/
@ModelField(name = "JVM堆内存信息Committed", comment = "JVM堆内存信息Committed", size = "500", required = false)
String jvmHeapMemoryCommitted;
/**
* jvmHeapMemoryUsed
*/
@ModelField(name = "JVM堆内存信息Used", comment = "JVM堆内存信息Used", size = "500", required = false)
String jvmHeapMemoryUsed;
/**
* jvmNonHeapMemoryMax
*/
@ModelField(name = "JVM非堆内存信息Max", comment = "JVM非堆内存信息Max", size = "500", required = false)
String jvmNonHeapMemoryMax;
/**
* jvmNonHeapMemoryInit
*/
@ModelField(name = "JVM非堆内存信息Init", comment = "JVM非堆内存信息Init", size = "500", required = false)
String jvmNonHeapMemoryInit;
/**
* jvmNonHeapMemoryCommitted
*/
@ModelField(name = "JVM非堆内存信息Committed", comment = "JVM非堆内存信息Committed", size = "500", required = false)
String jvmNonHeapMemoryCommitted;
/**
* jvmNonHeapMemoryUsed
*/
@ModelField(name = "JVM非堆内存信息Used", comment = "JVM非堆内存信息Used", size = "500", required = false)
String jvmNonHeapMemoryUsed;
/**
* jvmTotalMemory
*/
@ModelField(name = "JVM已申请的内存量", comment = "JVM已申请的内存量", size = "500", required = false)
String jvmTotalMemory;
/**
* jvmFreeMemory
*/
@ModelField(name = "JVM空闲的内存量", comment = "JVM空闲的内存量", size = "500", required = false)
String jvmFreeMemory;
/**
* jvmMaxMemory
*/
@ModelField(name = "JVM最大可使用的内存量", comment = "JVM最大可使用的内存量", size = "500", required = false)
String jvmMaxMemory;
/**
* 数据产生时间
*/
private Date createTime;
}

接口

/**
* 获取机器资源信息
*
* @return MachineResourInformation 结果
* @author: liudz
* @date: 2020/4/25 16:45
*/
MachineResourceInfo getMachineResourceInfo();

接口实现类

/**
* 向ES写机器负载信息,并返回给前端
*
* @return
* @author: liudz
* @date: 2020/4/25 16:53
*/
@Override
public MachineResourceInfo getMachineResourceInfo() {
MachineResourceInfo machineResourInfo = null;
try {
machineResourInfo = addParameter1();
MachineResourceInfo finalMachineResourInfo = machineResourInfo;
esExecutorService.submit(() -> {
JSONObject params = (JSONObject) JSONObject.toJSON(finalMachineResourInfo);
ResponseEntity response = HttpUtils.sendPostRequest(monitorUrl, params);
if (!response.getStatusCode().equals(HttpStatus.CREATED)) {
log.error("spark write ES error:{}", response);
}
});
} catch (SigarException e) {
log.error("AccessEsLogServiceImpl--getMachineResourInfo--error!");
}
return machineResourInfo;
}

/**
* addParameter
*
* @return 结果
* @throws SigarException
* SigarException
* @author: liudz
* @date: 16:52
*/
public MachineResourceInfo addParameter1() throws SigarException {
MachineResourceInfo machineResourceInfo = new MachineResourceInfo();
Sigar sigar = new Sigar();
CpuInfo[] infos = sigar.getCpuInfoList();
CpuPerc[] cpuList = null;
cpuList = sigar.getCpuPercList();
String cpuLoadDetailsUsed = "";
for (int i = 0; i < infos.length; i++) {
cpuLoadDetailsUsed += "第" + (i + 1) + "块CPU总的使用率: " + CpuPerc.format(cpuList[i].getCombined()) + ", ";
}
Mem mem = sigar.getMem();
DecimalFormat df = new DecimalFormat("0.00");
Swap swap = sigar.getSwap();
FileSystem[] fslist = sigar.getFileSystemList();
String diskLoad = "";
String diskReadOrWrite = "";
for (int i = 0; i < fslist.length; i++) {
FileSystem fs = fslist[i];
FileSystemUsage usage = null;
usage = sigar.getFileSystemUsage(fs.getDirName());
double usePercent = usage.getUsePercent() * Double.parseDouble("100D");
diskLoad += fs.getDevName() + "总大小: " + usage.getTotal() + "KB, " + fs.getDevName() + "已经使用量: "
+ usage.getUsed() + "KB, " + fs.getDevName() + "剩余大小: " + usage.getFree() + "KB, " + fs.getDevName()
+ "资源的利用率: " + usePercent + "%";
diskReadOrWrite += fs.getDevName() + "读出:" + usage.getDiskReads() + "扇区, " + fs.getDevName() + "写入:"
+ usage.getDiskWrites() + "扇区";
}
machineResourceInfo = addParameter2(df, mem, swap, diskLoad, diskReadOrWrite);
machineResourceInfo.setCpuLoadDetails(cpuLoadDetailsUsed);
return machineResourceInfo;
}

/**
* addParameter2
*
* @param df
* df
* @param mem
* mem
* @param swap
* swap
* @param diskLoad
* diskLoad
* @param diskReadOrWrite
* diskReadOrWrite
* @return machineResourceInfo
* @author: liudz
* @date: 2020/4/27 10:27
*/
public MachineResourceInfo addParameter2(DecimalFormat df, Mem mem, Swap swap, String diskLoad,
String diskReadOrWrite) {
MemoryMXBean memorymbean = ManagementFactory.getMemoryMXBean();
MachineResourceInfo machineResourceInfo = new MachineResourceInfo();
machineResourceInfo.setCreateTime(new Date());
machineResourceInfo.setMemoryLoadTotalUsePercent(
"内存总使用百分比: " + df.format((float) mem.getUsed() / mem.getTotal() * Integer.parseInt("100")) + "%");
machineResourceInfo.setMemoryLoadTotal("内存总量: " + mem.getTotal() / Integer.parseInt("1024") + "K av");

machineResourceInfo.setMemoryLoadUsed("当前内存使用量: " + mem.getUsed() / Integer.parseInt("1024") + "K used");
machineResourceInfo.setMemoryLoadFree("当前内存剩余量: " + mem.getFree() / Integer.parseInt("1024") + "K free");
machineResourceInfo.setMemoryExchangeTotal("内存交换区总量: " + swap.getTotal() / Integer.parseInt("1024") + "K av");
machineResourceInfo
.setMemoryExchangeUsed("内存当前交换区使用量: " + swap.getUsed() / Integer.parseInt("1024") + "K used");
machineResourceInfo
.setMemoryExchangeFree("内存当前交换区剩余量: " + swap.getFree() / Integer.parseInt("1024") + "K free");
machineResourceInfo.setDiskLoad(diskLoad);
machineResourceInfo.setDiskReadOrWrite(diskReadOrWrite);
machineResourceInfo.setJvmHeapMemoryMax("堆内存信息Max: "
+ memorymbean.getHeapMemoryUsage().getMax() / Integer.parseInt("1024") / Integer.parseInt("1024") + " MB");
machineResourceInfo.setJvmHeapMemoryInit("堆内存信息Init: "
+ memorymbean.getHeapMemoryUsage().getInit() / Integer.parseInt("1024") / Integer.parseInt("1024") + " MB");
machineResourceInfo.setJvmHeapMemoryCommitted("堆内存信息Committed: "
+ memorymbean.getHeapMemoryUsage().getCommitted() / Integer.parseInt("1024") / Integer.parseInt("1024")
+ " MB");
machineResourceInfo.setJvmHeapMemoryUsed("堆内存信息Used: "
+ memorymbean.getHeapMemoryUsage().getUsed() / Integer.parseInt("1024") / Integer.parseInt("1024") + " MB");
machineResourceInfo.setJvmNonHeapMemoryMax("非堆内存信息Max: "
+ memorymbean.getNonHeapMemoryUsage().getMax() / Integer.parseInt("1024") / Integer.parseInt("1024")
+ " MB");
machineResourceInfo.setJvmNonHeapMemoryInit("非堆内存信息Init: "
+ memorymbean.getNonHeapMemoryUsage().getInit() / Integer.parseInt("1024") / Integer.parseInt("1024")
+ " MB");
machineResourceInfo.setJvmNonHeapMemoryCommitted("非堆内存信息Committed: "
+ memorymbean.getNonHeapMemoryUsage().getCommitted() / Integer.parseInt("1024") / Integer.parseInt("1024")
+ " MB");
machineResourceInfo.setJvmNonHeapMemoryUsed("非堆内存信息Used: "
+ memorymbean.getNonHeapMemoryUsage().getUsed() / Integer.parseInt("1024") / Integer.parseInt("1024")
+ " MB");
machineResourceInfo.setJvmTotalMemory("JVM已申请的内存量: "
+ Runtime.getRuntime().totalMemory() / Integer.parseInt("1024") / Integer.parseInt("1024") + " MB");
machineResourceInfo.setJvmFreeMemory("JVM空闲的内存量: "
+ Runtime.getRuntime().freeMemory() / Integer.parseInt("1024") / Integer.parseInt("1024") + " MB");
machineResourceInfo.setJvmMaxMemory("JVM最大可使用的内存量: "
+ Runtime.getRuntime().maxMemory() / Integer.parseInt("1024") / Integer.parseInt("1024") + " MB");
return machineResourceInfo;
}


二、使用问题解答

第一种问题:在华为云ARM部署报错:
org.hyperic.sigar.SigarException: no libsigar-aarch64-linux.so in java.libra


解决办法:遇到华为ARM服务器时,需要libsigar-aarch64-linux.so库支持aarch64*台,把“libsigar-aarch64-linux.so”放在linux下的 /usr/lib 和 /usr/lib64 下(都要进行拷贝)
该朋友已经具体介绍,详情点击这里


第二种问题:使用该类常常会遇到问题:


java.lang.UnsatisfiedLinkError: org.hyperic.sigar.SysInfo.gather(Lorg/hyperic/sigar/Sigar;)V
org.hyperic.sigar.SysInfo.gather(Native Method)
org.hyperic.sigar.OperatingSystem.getInstance(OperatingSystem.java:90)
com.shuhaiserver.page.servlet.LoginServlet.doLogin(LoginServlet.java:146)
com.shuhaiserver.page.servlet.LoginServlet.doPost(LoginServlet.java:54)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.shuhaiserver.page.servlet.MyFilter.doFilter(MyFilter.java:82)


解决办法:
第一种办法:此时,需要将libsigar-amd64-linux.so、libsigar-x86-linux.so、sigar-amd64-winnt.dll、sigar-x86-winnt.dll、sigar-x86-winnt.lib文件放在项目的lib文件夹下
第二种办法:将上面的5个文件放在linux下的 /usr/lib 和 /usr/lib64 下(都要进行拷贝) -----使用场景是linux下启动springboot的jar包 以命令: java -jar xx.jar 运行才会这么做


补充:至于有的水友说的放在tomcat目录或者其他目录,因为我没碰到,需要你们自己测试
还有的朋友说放在java.library.path库下面,我感觉我的/usr/lib或/usr/lib64应该就是这个,具体是不是我不清楚(另外,查看linux下的java.library.path可新建springboot项目,在Application下新建一行打印输出,并打包在linux下以命令 java -jar xx.jar运行即可显示,帖图如下)



友情链接: