Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Access Windows Performance Monitor counters from Java, Part 2

Integrate NSClient4j with Java Management Extensions

  • Print
  • Feedback

NSClient4j is a pure Java API that provides simple and quick access to Windows Performance Monitor (WPM) statistics. These statistics prove invaluable for monitoring your Windows servers and providing a baseline for activities such as capacity planning and trouble shooting. Additionally, access to low-level performance or operating statistics can be useful in your low-level code, such as the current rate of GETS against your IIS (Internet Information Server) Web server.

In this article, the continuation of my series on WPM, I present a brief introduction to Java Management Extensions (JMX) and its benefits. I then explain how to implement JMX-based services for NSClient4j and describe different methods for implementing that technology.

Changes in NSClient4j since Part 1

As a brief aside, I want to enumerate some changes and enhancements that have been made in the NSClient4j package since Part 1 of this series:

  • The main package name has migrated from com.marketwide.nagios to org.nsclient4j.
  • Calls have been added to the NSClient4j class as convenience methods:
    1. getUsedDiskSpace(String diskVol)
    2. getFreeDiskSpace(String diskVol)
    3. getTotalDiskSpace(String diskVol)
    4. getUsedPercentDiskSpace(String diskVol)
    5. getFreePercentDiskSpace(String diskVol)
  • To accommodate NSClient4j implementation in the Oracle 9i internal JVM, the source code is now fully supported under J2SE 1.3.
  • Additional error handling identifies the cause of any errors emerging from the WPM engine (e.g., "Unrecognized pattern," or "Counter not found"), and the NSClient4JException has been extended to differentiate between transport errors and WPM errors.
  • New JMX-based classes provide JMX instrumentation of the WPM engine, which is this article's focus.


Recap of basic NSClient4j functionality

To provide a quick review of the NSClient4j functionality, the code below shows a few lines of Java code connecting to an NSClient server on a Windows host and acquiring the system's context-switch rate per second.

Listing 1. Source for CLStat, a simple NSClient4j example

 public class CLStat {
  public static void main(String[] args) {
    try {
      NSClient4j client = new NSClient4j("192.168.1.4", 1248);
      System.out.println("Result:" + client.getPerfMonCounter("\\System\\Context Switches/sec"));
    } catch (NSClient4JException e) {
      System.err.println("Exception Geting Stat:" + e);
    }
  }
}



Listing 2. Run the CLStat example

 c:\temp>set CLASSPATH=c:\nsclient4j-root\nsclient4j\dist\nsclient4j.jar;%CLASSPATH%

c:\temp>java org.nsclient4j.CLStat Result:4120.66700


Now that we have reviewed the basics of NSClient4j, let's look at JMX and how it can be implemented to support NSClient4j.

NSClient4j and JMX

The Java Management Extensions technology is a Java standard for building management and monitoring components for applications. Though NSClient4j provides a satisfactory low-level API for accessing WPM statistics (see Figure 1), a monitoring layer implemented in JMX provides both a higher standard of integration and compliance while significantly extending NSClient4j's functionality.

  • Print
  • Feedback

Resources