SIGAR – Access operating system and hardware level information

SIGAR (System Information Gatherer and Reporter) provides efficient way to access information of underneath hardware/operating system.

Information of Sigar is available here:

I am just touching few areas while working with Sigar.

1. Testing independently

After downloading sigar bundle, you want to explore posssiblities of Sigar. Best way to use command line program on available hardware/operating system:

java -jar sigar.jar

It will allow following prompt for further command. Type help for detail of commands.


2. Configure project with Maven:

Sigar has two sets of files -> to be downloaded from maven repository:

a. Sigar jar that can be used in Java program to execute respective command

b. Zip for operating system and hardware based JNI files. These files are used runtime to interact with operating system.

For that, you need to add following dependencies in your pom.xml file.












Sigar version is ‘available version’ to be downloaded.

Now, to read sigar files, you need to unzip downloaded to defined folder (preferably folder in class-path, else update your class-path).

Add maven plugin to unzip all files:
























In above configuration, choose your ‘output’ directory where you want to copy all JNI based files.

Internally Sigar API is using ‘java.library.path‘ to load Sigar libs/jars. All your JNIs should be available in same path.














3. Assembly it for different hardware

For assembly. update your assembly.xml (or respective file):

Step 1: Include Sigar Jar


Step 2: Include all system dependent JNI file. Ex. I am adding all linux based files in this build.








4. Program with it

Sigar APIs can be used in 2 ways:

a. Implement the SigarProxy interface which provides caching at the Java level – It is helpful if you want to cache fetched hardware information for some time to avoid unnecessary calling hardware for each request. It depends on application use case like static/fix load pattern where chances of alarming change of hardware parameters are not significant.

b. Direct access without cache – Create instance of Sigar class.

Sigar sigar = new Sigar();

final Mem mem = sigar.getMem(); // Memory

CpuPerc cpuPerc = sigar.getCpuPerc(); // Percentage CPU

Swap swap = sigar.getSwap(); // Swap Space

FileSystemUsage fsu = sigar.getFileSystemUsage(“Directory for File system”); //  File system – “.” for existing directory

There are several method available in Each class. These methods can be used as per requirement.

Ex: mem.getTotal(), mem.getActualUsed(), fsu.getTotal(), cpuPerc.getIdle(), swap.getTotal()

Sigar provides PTQL (Process Table Query Language) – PTQL is specially useful to identify process like we are using PID to record/identify Unix process.

All possible command, combination and regular expression information is provided here.

 Ex: I want to know number of servers running on a host-

Sigar sigar = new Sigar();

final ProcessFinder processFinder = new ProcessFinder(sigar);

final long[] pids = processFinder.find(“State.Name.eq=java,Args.-1.ct=myserver”);

System.out.println(“Total number of server on this host: “+ pids.length)

4. Test it

For testing of Sigar based APIs, make sure you have configured sure-fire plugin as given in step 2.

You can simply do mock for Sigar APIs to avoid making JNI calls to hardware:

private final long totalMemory = 1000;

final Sigar sigar = createMock(Sigar.class);

// Use any mocking platform like easy mock, powermock.


final Mem mem = createMock(Mem.class);




// System In test – Call method to be tested

mySigarData = methodInCall();

// Asset it

assertEquals(“Total memory is not correct.”, totalMemory, mySigarData.getTotalMemory() );



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: