最近由于工作需要,需要监控服务器的内存和cpu的使用情况,使用Sigar来获取服务器的基础数据,下面是依赖的sigar包
org.fusesource
sigar
1.6.4
@RequestMapping(value = "/sigar",method = RequestMethod.GET)
public String siger() {
SigerVO vo = new SigerVO();
try{
Sigar sigar = new Sigar();
int ratioCPU = new BigDecimal(sigar.getCpuPerc().getCombined()).setScale(2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).intValue();
vo.setRatioCPU(ratioCPU);
Mem mem = sigar.getMem();
double totalMemory = new BigDecimal(mem.getTotal() / 1024.00 /1024.00 /1024.00).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
vo.setTotalMemory(totalMemory);
double useMemory = new BigDecimal(mem.getUsed() / 1024.00 /1024.00 /1024.00).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
vo.setUseMemory(useMemory);
double freeMemory = new BigDecimal(mem.getFree() / 1024.00 /1024.00 /1024.00).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
vo.setFreeMemory(freeMemory);
int ratioMemory = new BigDecimal(useMemory/totalMemory).setScale(2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).intValue();
vo.setRatioMemory(ratioMemory);
}catch (SigarException ex){
ex.printStackTrace();
}
logger.info("【内存与cpu】结果{}",vo.toString());
return "success";
}
public class SigerVO {
/**
* 当前时间(HH:mm:ss)
*/
private String nowTime;
/**
* cpu利用率
*/
private Integer ratioCPU;
/**
* 当前总内存(G)
*/
private double totalMemory;
/**
* 已使用内存(G)
*/
private double useMemory;
/**
* 剩余内存(G)
*/
private double freeMemory;
/**
* 内存使用率
*/
private Integer ratioMemory;
public String getNowTime() {
return nowTime;
}
public void setNowTime(String nowTime) {
this.nowTime = nowTime;
}
public Integer getRatioCPU() {
return ratioCPU;
}
public void setRatioCPU(Integer ratioCPU) {
this.ratioCPU = ratioCPU;
}
public double getTotalMemory() {
return totalMemory;
}
public void setTotalMemory(double totalMemory) {
this.totalMemory = totalMemory;
}
public double getUseMemory() {
return useMemory;
}
public void setUseMemory(double useMemory) {
this.useMemory = useMemory;
}
public double getFreeMemory() {
return freeMemory;
}
public void setFreeMemory(double freeMemory) {
this.freeMemory = freeMemory;
}
public Integer getRatioMemory() {
return ratioMemory;
}
public void setRatioMemory(Integer ratioMemory) {
this.ratioMemory = ratioMemory;
}
@Override
public String toString() {
return "SigerVO{" +
"nowTime='" + nowTime + '\'' +
", ratioCPU=" + ratioCPU +
", totalMemory=" + totalMemory +
", useMemory=" + useMemory +
", freeMemory=" + freeMemory +
", ratioMemory=" + ratioMemory +
'}';
}
}
在调用接口的时候突然报错了,
java.lang.UnsatisfiedLinkError: org.hyperic.sigar.Cpu.gather(Lorg/hyperic/sigar/Sigar;)V
at org.hyperic.sigar.Cpu.gather(Native Method)
at org.hyperic.sigar.Cpu.fetch(Cpu.java:30)
at org.hyperic.sigar.Sigar.getCpu(Sigar.java:320)
at org.hyperic.sigar.Sigar.getCpuPerc(Sigar.java:341)
at com.zhy.open.sigar.SigerDemo.siger(SigerDemo.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:115)
at org.springframework.boot.web.support.ErrorPageFilter.access$000(ErrorPageFilter.java:59)
at org.springframework.boot.web.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:90)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:108)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
之后各种找博客帖子,里面各种说法,经过实践,找到了问题的处理方法,
首先在不同的操作系统中需要不同的依赖文件
文件的下载地址可以去
sigar包的相关文件.zip
里面文件的目录是这样的
如果是windows系统,那么需要
sigar-amd64-winnt.dll(64位的)或者是sigar-x86-winnt.dll(32位系统的)
如果是linux系统,需要
libsigar-amd64-linux.so(64位系统的)或者libsigar-x86-linux.so(32位系统的)
放的路径就需要根据一个小小的程序来判断了
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String test(){
String property = System.getProperty("java.library.path");
logger.info("【java.library.path路径】{}",property);
return property;
}
在我linux系统输出的是:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
所以我们只需要放入其中一个文件夹里面就行,我这里放的是/usr/lib64目录下面,亲测正常使用,下面是查询出来的结果
【内存与cpu】结果SigerVO{nowTime='null', ratioCPU=2, totalMemory=1.8, useMemory=1.2, freeMemory=0.6, ratioMemory=67}
总结:sigar的依赖放置位置是根据 Sysytem.getProperty("java.library.path")获取的路径来确定的,有问题记录一下,激励自己,帮助他人,欢迎关注微信公总号"干活集市",里面有海量整套视频教学的资源,总有一款适合你。