Recent top five:
Let's talk about exceptions ...
How do you handle exceptions? Do you think upfront about the type of exceptions that you want to catch or do you just let
the outside world handle it?
-- Jeroen van Bergen in JW Blogs
| Enterprise AJAX - Transcend the Hype |
| Memory Analysis in Eclipse |
| Oracle Compatibility Developer's Guide |
| Memory Analysis in Eclipse |
For those running Java applications or services, the Windows Performance Monitor (WPM) contains a treasure trove of valuable information—see Figure 1. There are hundreds (or thousands) of performance statistics called counters that are available in real time and measure critical system performance factors. This article outlines a simple way to access those performance statistics from Java.
Figure 1. Windows Performance Manager. Click on thumbnail to view full-sized image.
The problem is that accessing WPM can be quite challenging as it is a native Windows service and few, if any, Java APIs are available for accessing the information it provides from Java. Some research revealed three main ways of reading the WPM data with Java:
java.io or java.nio packages. If you are fortunate enough to be able to log directly to an ODBC datasource, then the WPM counter data will be
accessible through the java.sql JDBC (Java Database Connectivity) packages. The deficiencies of this approach are:A simple and pure Java API provides a better approach for accessing WPM.
NSClient4j is a pure Java client that provides a simple API for accessing WPM performance counter data. It uses a Windows service called NSClient. NSClient was originally written as a plug-in component for a system called Nagios, which is a system-monitoring package that runs on Linux. Nagios is a larger monitoring and alerting system that needed a solution for querying NT performance statistics from Linux, and, as a result, NSClient was born. Figure 2 outlines how Nagios works.
Figure 2. What Nagios does. Click on thumbnail to view full-sized image.
NSClient is a native Windows service that listens on a configurable port for requests, receives requests, looks up the corresponding WPM counter value (among other things), and returns the value as a simple string. In its original inception, it was called by a native Linux Nagios plug-in called checknt. However, since the protocol is fairly straightforward, and Java can manage communication through sockets, NSClient4j is simply a Java class that communicates with NSClient. Using the Java API, a developer can connect to NSClient, issue a request for a WPM counter value, and read the response—see Figure 3.
Figure 3. Relationship between NSClient and NSClient4j. Click on thumbnail to view full-sized image.
In Figure 3, NSClient4j asks the NT system how many context switches currently occur per second. Since NSClient4j is pure Java, it can run on any platform with a supported JVM. Figure 4 shows a class diagram of NSClient4j's main classes.
Figure 4. NSClient4j class diagram. Click on thumbnail to view full-sized image.
Before we get into some examples of how to use NSClient4j, let's review how to install NSClient, since it is a prerequisite. Installing NSClient is simple:
C:\NSClient.
Win_NT4_Bin or Win_2k_XP_Bin, respectively.
pNSClient.exe /install. You should see a message pop up like this:
Figure 5. Successful install message
pNSClient.exe /uninstall.
net start NSClient or go to the services applet in the Administrative Control panel for Windows and start the service named Net Saint NT Agent.
Now that NSClient is installed, we're ready to test NSClient4j.
The following example shows how to code a simple command line client that displays the WPM counter value for the passed host name and counter name. There are two phases to getting a WPM counter reading:
NSClient4j class. There is one mandatory parameter, the host name (or IP address). The optional parameters are the port number and password.
The port number defaults to 1248 and password defaults to None but, for security considerations, these can be changed. See "A Final Note on Securing NSClient" below. Accordingly, the constructor
options are as follows: public NSClient4j(String hostName) throws NSClient4JException
public NSClient4j(String hostName, int portNumber) throws NSClient4JException
public NSClient4j(String hostName, int portNumber, String password) throws NSClient4JException
public NSClient4j(String hostName, String password) throws NSClient4JException
getPerfMonCounter(String counterName) method.
That's pretty much it. Here's a simple command line example:
1 package com.marketwide.nagios;
2
3 /**
4 * <p>Title: CLStat</p>
5 * <p>Description: Net Saint NT Client For Java Command Line Example</p>
6 * <p>
7 * <p>
8 * @author Whitehead (nwhitehe@yahoo.com)
9 * @version 1.0
10 */
11
12 public class CLStat {
13 public static void main(String[] args) {
14 try {
15 NSClient4j client = new NSClient4j(args[0]);
16 System.out.print("Result:" + client.getPerfMonCounter(args[1]));
17 } catch (NSClient4JException e) {
18 System.err.println("Exception Geting Stat:" + e);
19 }
20 }
21 }
Here is an example of how I can get my file server's context switch rate:
C:\JBProjects\NSClient4J> set CLASSPATH=%CLASSPATH%;.\nsclient4j.jar
C:\JBProjects\NSClient4J>java com.marketwide.nagios.CLStat 192.168.1.103 "\System\Context Switches/sec"
Result:3393.68772
That's about as simple as an NSClient4j example gets. One counter you may be interested in accessing frequently is the CPU utilization. Here is an example:
C:\JBProjects\NSClient4J>java com.marketwide.nagios.CLStat 192.168.1.103 "\Processor(_Total)\% Processor Time"
Result:99.99995
Two things to note: First, the counter's naming convention may seem a bit peculiar. The reason: because it is peculiar; but it is somewhat consistent. I provide further explanation in the following section.
Second, the result indicates my processor time is about 100 percent, which is not actually true. It appears that the inquiry
into the CPU utilization rate actually causes the CPU utilization to jump to (nearly) 100 percent during the instant of time
which the inquiry occurs. This seems in line with the Heisenberg Uncertainty Principle, which can be summarized as nothing can be measured without affecting what you are trying to measure. All the same, NSClient offers an alternate CPU measurement provided in NSClient4j as the method getCPUUsage(). To offer some consistency, a symbolic shortcut is provided in the getPerfMonCounter(String counterName) call. If you request the counter name CPU, under the covers, the alternate method will be called and return a better reflection
of your system's CPU utilization. The following call displays how to retrieve the CPU utilization of the target NT server.
C:\JBProjects\NSClient4J>java com.marketwide.nagios.CLStat 192.168.1.103 CPU
Result:8
I now return to a discussion on counter naming. For the most part, counter naming is intuitive. A performance counter has somewhere between two and four parts to its fully qualified name. The first part is the host name, which we disregard in this case since it is not needed. (It can prove useful in other scenarios, but that's another story.) The remaining three parts are:
Processor, Memory, TCP.Processor contains the following counters, among others: % Processor Time, % User Time and Interrupts/sec.
Processor's counters can apply to a specific CPU number or the total CPU resources, which on my super 32 CPU file server would be either
a number between 0 and 32, or _Total.
When a counter lacks an instance, it has an intuitive name. For example:
Memory% Committed Bytes In UseThe instances, however, add some confusion. Consider this name that tells us the total bytes per second being transferred by my wireless 802.11G network card (as opposed to my internal regular 10/100 Ethernet Card):
Network InterfaceBytes Total/secD-Link AirPlus DWL-G650 Wireless Cardbus Adapter[rev.C] - Packet Scheduler MiniportIn summary:
\<performance object>\<counter name>\<performance object>(<instance name>)\<counter name>Figure 6 should help you navigate the conversion of a WPM counter from the application to your code.
Figure 6. How full counter names map to the WPM GUI. Click on thumbnail to view full-sized image.
NSClient has some extra built-in features to enhance Nagios's ability to monitor the health of Windows servers. Most are also available in NSClient4j as follows:
java.util.Date getFileDate(String filename): Returns the date a file was last modified.
java.lang.String getNSClientVersion(): Returns the version of the NSClient service.
java.util.Date getUpTimeDate(): Returns the date the Windows server started. There are also methods to return the days, hours, minutes, or seconds since
the Windows server started.
boolean isServiceUp(String serviceName): Returns true if the named service is running on the target Windows server and false if it is not.
boolean isProcessUp(String processName): Returns true if the named process is running on the target Windows server and false if it is not.
As mentioned previously, NSClient's default password is None, which is built into NSClient4j, as it also is in its Linux brethren, checknt. The same applies to the default port of 1248. For added security, these can be overridden as follows:
In the next example, I change the password to mypassword and the port to 10007. The example is a follow up to the simple CLStat we looked at before, enhanced to account for a custom port and password. It also uses the parameter-less constructor and
the correspondingly required init() method (see line 24):
1 package com.marketwide.nagios;
2
3 /**
4 * <p>Title: CLStat2</p>
5 * <p>Description: Net Saint NT Client For Java Command Line Example 2</p>
6 * <p>
7 * <p>
8 * @author Whitehead (nwhitehe@yahoo.com)
9 * @version 1.0
10 */
11
12 public class CLStat2 {
13 public static void main(String[] args) {
14 try {
15 if(args.length < 4) {
16 System.out.println("Usage: CLStat2 <hostName> <port>" +
17 " <password> <counter name>" );
18 System.exit(-1);
19 }
20 NSClient4j client = new NSClient4j();
21 client.setHostName(args[0]);
22 client.setPortNumber(Integer.parseInt(args[1]));
23 client.setPassword(args[2]);
24 client.init();
25 System.out.print("Result:" + client.getPerfMonCounter(args[3]));
26 } catch (NSClient4JException e) {
27 System.err.println("Exception Geting Stat:" + e);
28 }
29 }
30 }
Here are two test runs:
C:\JBProjects\NSClient4J>java com.marketwide.nagios.CLStat2 192.168.1.103 10007 mypassword CPU
Result:18
C:\JBProjects\NSClient4J>java com.marketwide.nagios.CLStat2 192.168.1.103 10007 notmypassword CPU
Result:ERROR:Wrong password
NSClient4j in conjunction with NSClient makes accessing WPM data from Java really easy, and in conjunction with its multiplatform capabilities, it can be integrated into a wide variety of monitoring solutions. The API is still being refined and I would be happy to receive your feedback and requests for enhancements.
In Part 2, we will investigate how to integrate with JMX and leverage the benefits of monitoring these statistics through JMX.
| Subject | Replies |
Last post
|
|
By JavaWorld |
0 |
08/17/08 07:07 PM
by Anonymous |
|
By databias |
0 |
10/24/07 11:25 PM
by Anonymous |