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:

http://www.hyperic.com/products/sigar

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.

Sigar>

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.

 <dependency>

<groupId>org.hyperic</groupId>

<artifactId>sigar</artifactId>

<version>${sigar.version}</version>

</dependency>

<dependency>

<groupId>org.hyperic</groupId>

<artifactId>sigar-dist</artifactId>

<version>${sigar.version}</version>

<type>zip</type>

</dependency>

Sigar version is ‘available version’ to be downloaded.

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

Add maven plugin to unzip all files:

 <plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-dependency-plugin</artifactId>

<version>2.2</version>

<executions>

<execution>

<id>unpack-dependencies</id>

<phase>compile</phase>

<goals>

<goal>unpack-dependencies</goal>

</goals>

<configuration>

<includes>**/sigar-bin/lib/*</includes>

<excludes>**/sigar-bin/lib/*jar</excludes>

<includeGroupIds>org.hyperic</includeGroupIds>

<includeArtifactIds>sigar-dist</includeArtifactIds>

<outputDirectory>

${project.build.directory}/depends

</outputDirectory>

</configuration>

</execution>

</executions>

</plugin>

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.

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.8</version>

<configuration>

<systemProperties>

<property>

<name>java.library.path</name>

<value>${project.build.directory}/depends/hyperic-sigar-1.6.5/sigar-bin/lib</value>

</property>

</systemProperties>

</configuration>

</plugin>

3. Assembly it for different hardware

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

Step 1: Include Sigar Jar

<include>org.hyperic:sigar</include>

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

<fileSet>

<directory>${project.build.directory}/sigar/hyperic-sigar-1.6.5/sigar-bin/lib</directory>

<outputDirectory>/lib/sigar/</outputDirectory>

<includes>

<include>**/*-linux.so</include>

</includes>

</fileSet>

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.

PowerMock.expectNew(Sigar.class).andReturn(sigar);

final Mem mem = createMock(Mem.class);

expect(sigar.getMem()).andReturn(mem);

expect(mem.getTotal()).andReturn(totalMemory);

replay(…)

// System In test – Call method to be tested

mySigarData = methodInCall();

// Asset it

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

References:

https://support.hyperic.com/display/SIGAR/Home

https://support.hyperic.com/display/SIGAR/PTQL

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: