我正在做一个项目,需要显示CPU使用情况以及远程机器的其他系统信息。人们建议使用SIGAR来实现这一点,但我不知道如何使用它。源代码对我来说不是很有意义。基本上,我的问题是:在提供主机IP和JMX端口的情况下,如何将SIGAR提供的MBeans注册到服务器,以及之后如何从其他计算机获取系统信息。如果我对JMX的工作方式理解错误,请纠正我。提前谢谢。
发布于 2012-06-12 01:48:14
在我看来,您必须编写一些包装对象来将各种SIGAR输出公开为JMX mbean属性。我会为各种不同类型的SIGAR输出编写一个包装对象:内存、磁盘、...
我已经写了一个可能会有帮助的SimpleJMX library。我将使用它的格式提供一个示例对象,您可以使用该对象通过JMX公开信息。您可以调整它以适应您用来发布JMX的任何机制。我对SIGAR不够熟悉,不知道下面的sigar代码是否正确,无法获取ProcMem实例。
@JmxResource(description = "Show SIGAR Info", domainName = "foo")
public class SigarProcMem {
private ProcMem procMem;
{
// sorry, I'm not up on sigar so I'm not sure if this works
Sigar sigar = new Sigar();
procMem = sigar.getProcMem(sigar.getPid());
}
@JmxAttributeMethod(description = "Resident memory")
public long residentMemory() {
return procMem.getResident();
}
@JmxAttributeMethod(description = "Get the Total process virtual memory")
public long totalVirtualMemory() {
return procMem.getSize();
}
}发布于 2012-06-12 05:13:03
以下是您可以注册的内置在MBeans中的Sigar类的名称:
然而,远程部署它们将非常复杂,因为Sigar依赖于本地库,当加载MBeans时,本地库必须位于目标JVM的lib-path中。这意味着,您需要在要监视的每个目标主机上主动加载库和MBeans。
您也许能够破解一种方法,使目标JVM通过远程调用加载它,但这不是一件容易的事情,需要您绕过JVM中的任何安全设置,因为在默认情况下,这是您不应该做的事情。
发布于 2013-07-25 00:02:55
您可以对系统部分进行一些修改,以获得Sigjar的轻松部署:
private String before;
private Sigar sigar;
/**
* Constructor - don't forget to call unload later!
*/
public SetlogSigar() throws Exception {
before = System.getProperty("java.library.path");
String path = "";
String add = getJarFolder();
if (before.contains(";"))
path = before + ";./;" + add;
else
path = before + ":./:" + add;
setSystemPath(path);
sigar = new Sigar();
}
/**
* This is needed to dynamically update the JAVA Path environment in order to load the needed native library
* Yes -rather an ugly hack...
*/
private String getJarFolder() {
// get name and path
String path = SetlogSigar.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = path;
try {
decodedPath = URLDecoder.decode(path, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
File f = new File(decodedPath);
String absolutePath = f.getParentFile().getParentFile().getParentFile().getParent()+"/lib";
return absolutePath;
}
/**
* Unloads the JNI bindings
*/
public void unload() {
this.sigar.close();
setSystemPath(before);
}这个技巧会动态地将sigjar.jar所在的文件夹添加到环境变量中。只需将所有本地库放在其中,部署就会变得不那么复杂。
https://stackoverflow.com/questions/10984938
复制相似问题