14402Views24Replies

Author Options:

Java: install, and use an API? (SIGAR) Answered

I downloaded the Sigar API for Java(http://support.hyperic.com/display/SIGAR/Home). I want to use it in Java, but I don't know how. After I've downloaded it, where do I put it? Is there a certain folder in my java JDK I need to put it?

Discussions

0
None
andy

7 years ago

Okay,

Libraries (an API) are normally provided as JAR files - a "Java Archive" file. This is a compressed file that you can think of in the same way as a zip except that you don't have to decompress it or anything like that.

JAR files are full of compiled java files - class files. A class file is what the computer runs when you run a java application. Each class file contains a java class. The files are organized into folders, which in java are called packages.

The way Java works is that it needs to know where the JAR file is. It does this with a "classpath" - literally the location of the external JAR or CLASS files that you are using. In a minute we will tell your programming software where to find the JAR.

In this particular case I would caution you that the API is a bit complex, but I'll go ahead and describe how to do as you ask. I'm assuming you are using windows? In this case you need to download the hyperic-sigar-1.6.4.zip file from sourceforge. Then you need to unpack the Zip (it doesn't matter where) and find the following file hyperic-sigar-1.6.4\hyperic-sigar-1.6.4\sigar-bin\lib\sigar.jar - this file is our JAR.

Now In your programming software you need to right click on your project and select properties. There should be an entry in properties for something like "Java Build Path" (click it). Now you need to select an option that will read something like "Add External JARs". Now you just need to specify the JAR we identified earlier and you have imported the library.

Now the examples that you can find online for this library will work in that project.

Hope this helps,
Drew

p.s. If you have any further problems, could you provide some info on your operating system and which programming software you are using? Doing so would help me and others to help you.

Thanks for the help. Yes, I'm using Windows XP, and NetBeans 6.9.1. I did what you said about the JAR(I've done that 3 times, as I've been told the same thing 3 times), but I still get the error "package org.hyperic.sigar does not exist; Unused Import" and then the other classes that it should contain all have their own error. And then at the bottom with my Main method, I get the error "class, interface, or enum expected".I've added a text file of the program here: http://www.host-a.net/u/metrogdor22/CPUInfo.txt. Thanks again for the help!

Right, I think there are multiple problems here, none the least of which is that your program would not work, because;
  • the class has no constructor (which is not critical, but it is a good idea)
  • the class has no methods
  • the method calls to sigar like getVendor() should be on an instance of the sigar class, currently what you are actually doing is implicitly calling this.getVendor() in your CPUInfo class (it doesn't have this method)
  • there is no print statement, so you would not get the data out of your class
  • the static method needs to be inside the class declaration
Fixing
Okay, here's how to fix it.
  • In the same folder as sigar.jar is log4j.jar. import both libraries at the same time, in the same way as described in my earlier post. (sigar.jar uses log4j, that's why we need it)
  • use the corrected version of your source code shown below (just copy paste)
package SigarTestPackage;

//import the classes that we will use
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

//declare the CPUInfo class
public class CPUInfo {

/**
* declare a constructor for CPUINFO
*
* Remember that a constructor normally is meant to set the class up...in
* this case we are bending the rules just to make the example clearer.
* Really the constructor should be empty and we should be doing this in a
* method.
*/
public CPUInfo() {
// declare the Sigar class (we need this because it gathers the
// statistics)
Sigar sigar = new Sigar();

// the output string is just going to hold our output string
String output = "";

// an array of CPU Info classes. The CPUInfo class is a blank data
// holder class. Just like string it can hold any data.
CpuInfo[] cpuInfoList = null;

// the try catch block means that if we get an error we are notified
try {
// get the CPU information from the sigar library
cpuInfoList = sigar.getCpuInfoList();

// if something foes wrong
} catch (SigarException e) {
// write a description of the problem to the output
e.printStackTrace();

// exit the constructor
return;
}

// for each item in the cpu info array
for (CpuInfo info : cpuInfoList) {
// add the data to the output ( output += "something" means add
// "something" to the end of output)
output += "\nCPU\n";
output += "Vendor: " + info.getVendor() + "\n";
output += "Clock: " + info.getMhz() + "Mhz\n";
}

// finally, print the data to the output
System.out.println(output);
}

public static void main(String[] args) {
CPUInfo main = new CPUInfo();
}
}



The above should work, printing out Mhz and manufacturer info. Challenge; can you make it print out the "total cores", "cache size" and "model" as well?

Regards,
Drew

All I can say is thank you again, but I really am VERY thankful for people like you :)

No problem, any time (PM me if you start a new forum post for Java help but aren't getting any replies). I take it that you got it working?

Drew

Working better than I had hoped in the first place, thank you very much(I made a change to make it output to a text area rather than the normal output area)! There's no problem with it, I'm just a bit curious as to why it outputs two of the same thing? When I run it, I get:

CPU
Vendor: AMD
Clock: 2004Mhz
Cores: 2

CPU
Vendor: AMD
Clock: 2004Mhz
Cores: 2

Again, this isn't a problem if it's not easily-fixable, but why is it doing that?

Good stuff... With regard to the double output, the documentation isn't clear but I'm almost certain your running a motherboard with dual cores. If you had a quad core it would have four entries (probably all the same). The cores can be set up differently, but normally there is no need to (that is why Sigar gets info on all the cores, rather than just telling you once).

Regards,
Drew

p.s. If you want to get info on just the first core you need to replace the "for each" loop block with CpuInfo info = cpuInfoList[0]; (but keep the middle of the block)

am new to SIGAR I am trying to get data from hosts in my network(the applications that running on hosts) and write it to openTSDB, I've installed openTSDB on CentOS. So i have the monitoring system ready only need the data from hosts, if you can find any solution can help me it will be very appreciated.

This topic was dead for many years, so you really expect someone to reply now?
;)

Dear Downunder35m,

I've searched and I've spent a lot of time to get any help but nothing helped me yet, until i found this discussion which is really useful to me, so yeah i am expecting someone to reply cuze not sure about anywhere else possible to get help

Hi,

You might want to check out the OperatingSystemMXBean as an alternative, there is some sample code on the following stack overflow, here;

http://stackoverflow.com/a/20457130/1593711

Although of course you don't want the infinite loop bit,

Cheers,

Andy

p.s. Downunder35m I'm still here :)

I don't want to spoil things too much so I will also try to offer a little bit of an idea towards the end:
The Sigar project is basically dead since 2010, which means people either moved on to other solutions or never really bothered to share their later insights online.
My best guess would to try on an old PC with the operating system and Jave version of the last release date for Sigar - this gives at least a platform that should work.
Apart from that you might want to check other projects like Jnati or System Information Monitor by Kiddington.
If you limit your Google searched by timeframe you also find Github site like this one and others.
Some might give you enough insight to find the problem, better code or links to boards where the author is active and answers questions.

thanks a lot for your contribution I really appreciate it. I will try to find out other projects as well

sigar is great. but im wondering if this sigar also can calculate the physical memory used when running a program?

I don't think you need sigar for that, simply Runtime.getRuntime().totalMemory() should tell you how much memory the current JVM is consuming. For more specific (thread specific) memory read outs is a little harder - you'll probably need to get the thread's JMX bean and ask it.

If you post more specifics on what your trying to do I'll help out as best as I can :)

Further reading; http://stackoverflow.com/questions/74674/how-to-do-i-check-cpu-and-memory-usage-in-java

Thanks,
Drew

im not trying to get the memory of JVM. i really want the cpu memory usage. but im hoping is just by writing any code, java, eclipse.

im doing multithreading program and i need to calculate the memory of the cpu too. im trying to read the cpu memory usage from the memory task manager but that is not appropriate tools when we actually wants to capture the memory and at the same time, the time when running the program.

is there any way to get the cpu memory usage, please?

Thanks.

Ah I see what you mean, although I should point out that memory is common to all cores on the system and used by each CPU as needed.

This is the documentation page for the Mem container object;

http://www.hyperic.com/support/docs/sigar/org/hyperic/sigar/Mem.html

You can get a valid reference by calling;
Sigar sigar = new Sigar();
Mem mem = sigar.getMem();

And then using whichever "get" method you need data for, on the mem object.

Hope that helps,
Drew

Sorry for double post, just a minor self correction; It's definitely because you have dual core (my reasoning is that you are printing the number of cores in your output with Cores: 2).

Regards,
Drew

Many thanks again, fixed the problem :)

hi Andy.

I'm a newbie to Sigar and wanted to use it to develop a small application to read system configs (cpu, RAM etc). I'm developing in Eclipse Indigo, and have setup my project build-path with Sigar.jar. I added sigar-bin/lib as the native lib location to sigar.jar assuming it needs to find the relevant .so files for native calls. However thusfar I've been unsuccessful with Sigar API as it doesn't provide results as expected (always null, 0 are returned from api methods). I suspect the problem is with native-libraries not been picked up at runtime by java. Can you please give some tips where I could rectify this?

Thanks in advance,
Dileepa

Hi Dileepaj,

Without the source code that your trying to run it is hard to say what the problem is, but it may be something like this; http://forums.hyperic.com/jiveforums/thread.jspa?threadID=8436 - where you are not using the sigar interface, just it's data classes.

If this doesn't help (or this does not make sence) could you include a simple example of the code your trying to run? This will help determine if the problem is with your code, or with compatability and ultimatly help us fix the issue.

Thanks,
Andrew

p.s. you can just leave comments in this thread, I get a notification when it changes so you don't need to PM me as well.

I added both the jar's u mentioned, also copied the above code as it is. But, i get the following error when i run the file in eclipse IDE :
p.s. : I run the file as java application.


Exception in thread "main" java.lang.UnsatisfiedLinkError: org.hyperic.sigar.Sigar.getCpuInfoList()[Lorg/hyperic/sigar/CpuInfo;
at org.hyperic.sigar.Sigar.getCpuInfoList(Native Method)
at org.hyperic.sigar.cmd.CPUInfo.(CPUInfo.java:32)
at org.hyperic.sigar.cmd.CPUInfo.main(CPUInfo.java:57)
0 [main] DEBUG Sigar - no sigar-x86-winnt.dll in java.library.path
org.hyperic.sigar.SigarException: no sigar-x86-winnt.dll in java.library.path
at org.hyperic.sigar.Sigar.loadLibrary(Sigar.java:172)
at org.hyperic.sigar.Sigar.(Sigar.java:100)
at org.hyperic.sigar.cmd.CPUInfo.(CPUInfo.java:20)
at org.hyperic.sigar.cmd.CPUInfo.main(CPUInfo.java:57)


Since i am new to java and sigar as well, so please help.

thanks in advance

Hmm, that's interesting. I found this forum discussing how to compile manually to get around this problem - here.

Thank-you very much.