JAVA的Sigar API(需要指南)

5
我已经下载了Sigar API(http://support.hyperic.com/display/SIGAR/Home),想在项目中使用它来获取不同正在运行的进程的信息。但是,我无法找到一些有用的代码片段进行学习,并且从他们网站上的javadoc中也没有什么帮助,因为我不知道应该寻找什么。您是否有任何想法,在哪里可以找到更多信息?

1
我之前使用过SIGAR,发现它非常容易上手。你能具体说明一下你需要什么吗?PTQL文档页面结合javadoc对我来说是一个很好的起点。 - Joeri Hendrickx
因此,作为起点,我需要有关特定进程(例如explorer.exe)的处理器/内存使用情况的信息。 - CosminO
1
这只是一个猜测,所以我不会将其发布为回复;我认为你只需要执行 ProcMem pm = new ProcMem(); pm.gather(sigar, yourPid); pm.getsize()。对于 CPU,使用 ProcTime 进行类比。您可以使用 ProcessFinder 从 PTQL 表达式中查找 pid。 - Joeri Hendrickx
4个回答

11
要查找特定进程的信息,需要先找出该进程的PID。您可以使用ProcessFinder来查找PID。 查找单个进程PID的方法是findSingleProcess(String expression)。示例:
    Sigar sigar=new Sigar();
    ProcessFinder find=new ProcessFinder(sigar);
    long pid=find.findSingleProcess("Exe.Name.ct=explorer");
    ProcMem memory=new ProcMem();
    memory.gather(sigar, pid);
    System.out.println(Long.toString(memory.getSize()));

表达式语法如下:

Class.Attribute.operator=value

在哪里:

Class is the name of the Sigar class minus the Proc prefix.
Attribute is an attribute of the given Class, index into an array or key in a Map class.
operator is one of the following for String values:
eq - Equal to value
ne - Not Equal to value
ew - Ends with value
sw - Starts with value
ct - Contains value (substring)
re - Regular expression value matches
operator is one of the following for numeric values:
eq - Equal to value
ne - Not Equal to value
gt - Greater than value
ge - Greater than or equal value
lt - Less than value
le - Less than or equal value

更多信息请参见:http://support.hyperic.com/display/SIGAR/PTQL


1
我正在尝试使用 findSingleProcess("Exe.Name.ct=explorer");,就像你说的那样,但是我得到了这个错误 org.hyperic.sigar.SigarException: Query did not match any processes。而且我可以在任务管理器中看到 explorer.exe 进程正在运行。 - Alvaro
你使用的是哪个操作系统? - CosminO

2
如果您正在使用Windows 7,请尝试执行某些操作。
likefindSingleProcess("State.Name.ct=explorer");

0
在他们最新的软件包中,他们在bindings\java\examples下提供了大量的使用示例。不妨看一看。

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接