Friday May 19, 2006 ![]() |
JMX, SNMP, Java, etc...Daniel Fuchs blogs on JMX, SNMP, Java, etc... |
... In this entry I'm going to outline how you could use the Java DMK 5.1 to send an SNMP trap from within an application deployed in Sun's AppServer. So if you're feeling trapped, come and learn with me how you can send them from within a deployed Web Application ... If you're not familiar with SNMP, I'd suggest that you first start by reading [Simple Is Not Easy] which provides a brief introduction about SNMP - and some links to other materials. Although I am going to use the Java DMK for this demonstration - the principal aspect of this talk will be to lead you through the small configuration steps that you need to perform to make this happen. As a consequence - I believe you could use this talk to achieve the same thing with your favorite Java SNMP library. For this demo I'm going to use several things:
In the course of this demo we're going to:
But before we launch into the demo, let me stress a few things: A few days ago, I was blogging about traps in SNMP. Readers who are not familiar with traps in SNMP are kindly invited to read this entry first. If you have read this entry - you should now be convinced that you will, at some point, need a MIB definition. Writing a MIB definition for a trap is something that we will not address here. For the sake of the example we will simply assume that the MIB definition exists. Indeed the goal of this exercise is not to give you a course into designing an SNMP Notification system - but rather to show you what steps are necessary in order to make the plumbing work. Installing Things Which Are Not Yet Installed...First, let's install everything. If you haven't installed NetBeans and Sun's AppServer now is a good time to do so. The NetBeans IDE 5.0 Quick Start Guide for J2EE Applications will lead you through the necessary initial environment set up steps. If you have already NetBeans and Sun AppServer installed then fine, don't touch anything ;-) When this is done, you may want
to install the Java DMK 5.1. You can install it
there: Setting AS_JDMK_HOME in Sun App Server configuration env fileThe first thing you need to do is to update the App Server configuration env
file. Depending on the platform you're running on the name and syntax of the file
may vary - but you will not have any difficulty to find it out. The configuration
env file is located under Load the file in your favorite editor and look for AS_JDMK_HOME. You will need to set the value of that variable to point to the directory that contains the lib/ directory in which jdmkrt.jar is located. This will be something like:
well you get the idea. The important thing is that after you're done Finding App Server policy fileThe second thing to do is to grant the Java DMK jars the permissions it needs to start the SNMP Adaptor Server. The easiest way to do so is to grant it all permissions ;-). To grant all permission to the Java DMK jar, you will need to edit the server.policy file. The server.policy file is in your App Server domain instance config
directory. Usually it's To make it sure, simply start the AppServer with NetBeans,
Alternatively you can open the server's admin console in your Web browser:
In the admin console, if you click on 'Application Server' in the left frame, you will see the following information displayed:
The displayed Configuration Directory is the directory that contains your server.policy file. Setting up the AppServer policy fileEdit the AppServer policy file and add the following block of lines:
Updating the AppServer classpathNow the third thing you need to do is to update the App Server classpath in order to add the Java DMK 5.1 jdmkrt.jar. The easiest way to do that is to log in on the App Server through the Admin Console as previously shown. Then click on 'Application Server' at the top of the left frame. Then go to "JVM Settings"->"Path Settings" and add
${com.sun.aas.jdmkHome}/lib/jdmkrt.jar
in the "Classpath Suffix" field:
Well that's about it for the configuration. What remains to do is to code and deploy our small application in order to validate that the configuration was correctly set up. Creating the TrapEmitter Web ServiceThis is a piece of cake. In NetBeans create a new EJB Module project
called 'TrapEmitter'. Select the TrapEmitter project and right click to
get the contextual popup menu. Using the popup menu choose
Now in the 'TrapEmitter' project open the 'Web Services' node and select the 'TrapEmitter' service:
Right click on the 'TrapEmitter' node and choose to add an operation. Name it 'startServer' and give it an int parameter called 'localPort'. The operation will return a String Then repeat the steps above in order to add a corresponding 'stopServer' operation. The operation will have no parameter and will return a String. Finally, add a 'sendTrap' operation that returns String and has three parameters: String remoteHost, int remotePort, and String trapOid. That's it, we only need now to provide the code for those operations. Coding the TrapEmitter operationsDouble click on the 'TrapEmitter->Web Services->TrapEmitter' node. NetBeans will open the 'TrapEmitterBean.java' file in the editor window:
Now here is what you need to add: First, add a static variable for the SnmpAdaptorServer:
Then fill in the code for the three WebServices methods we have defined:
Fixing ImportsNow you should be seeing lots of lines underlined in red in the code
you've just pasted. This is because we need to add the appropriate Java DMK
imports. But before we can do that we need to tell NetBeans that our little
TrapEmitter project is using Java DMK. Just select the TrapEmitter project,
right click to open the popup menu, and choose 'Properties'.
When this is done, use NetBeans 'Fix Import' action to generate all the necessary import clauses in the TrapEmitterBean.java file. Now all the underlined syntax errors that were previously detected by the editor should have disappeared. What remains to do is to deploy our webservice, invoke it, and check that we can receive traps. Deploying the WebServiceBefore deploying the WebService, make sure the Sun AppServer is started. If it's already started, you may want to restart it once: we modified its Classpath earlier in this lab, and classpath modification are only taken into account after a restart. If it's not started, well just start it. You can do all of this from NetBeans' Runtime tab.
Wait until the server has started, then go back to the project's tab, select the TrapEmitter project, right-click to get the popup, and choose 'Deploy Project'. When the project is deployed, go and have a look to the Sun AppServer output - you should be seeing a trace like this:
The URL displayed
Creating a WebService client
First, create a new Web->Web Application project called 'TrapEmitterClient'.
Select the newly created project, right click to get the popup, and choose to
create a New->File/Folder->Web Services->Web Service Client.
Now you should be seeing a 'TrapEmitter' Web Service Reference under your TrapEmitterClient application node in NetBeans' project tree:
Starting the SnmpV3AdaptorServerTo start the SnmpV3AdaptorServer, simply invoke the startServer method
by double clicking on the Web Service Client 'startServer' node. In the window
that pops up, choose a random port number for the method parameter value (well
something with 4 digits, like 9654) and click on "Submit".
If it fails, then here are some hints:
in the appserver log: you have probably misspecified the permissions in the server.policy file, or you have included jdmkrt.jar in your TrapEmitter.war. Another common reason for failure is if the port number you have chosen was already taken, or if you choose a port number that requires super-user priviledges, like e.g. 161. Now that we have started the SnmpV3AdaptorServer, let's see if we can send a trap. Looking for an Easy Manager?In the Java DMK install, under From NetBeans IDE, choose to create a New Java Project With Existing Sources. Choose to name your project 'easymanager' and select the easymanager directory you've just created. Click on 'Next' and select the 'src' subdirectory as source folder. Click on 'Finish'. Now edit the easymanager project's properties and add jdmkrt.jar to its library, exactcly as we did for the TrapEmitter EJB module - except that this time you won't have any 'Package' checkbox to unselect. Now from NetBeans IDE, choose 'Run Project' for the easymanger project. You will be prompted to select the main class - there's only one choice so just accept it. You sould now be staring fixedly at this trace: easymanager initialized. SNMP Agent is [localhost:8085] Start making your request:(h for help window) If you're curious, enter 'h' and strike on return in NetBeans input line feed. Whether you're curious or not - now simply type 'trap' and strike return in NetBeans input line feed. A small window should popup:
This small window is the window where the easymanger will display trap it receives. The title of the window should display the hostname and/or IP of your machine. Just remember that name or IP, because this is where we will be sending our trap. Sending a trap from our Web ApplicationNow let's go back to our Web Service Client (TrapEmitterClient) and let's double click on the sendTrap operation node. These are the parameters we must supply:
Then click on 'Submit'. This is what you should be seing in your easymanager trap window:
Isn't that a wonderful world? Cheers, Tags: java jdmk jmx snmp Posted by dfuchs ( May 19 2006, 12:11:12 PM CEST ) Permalink Comments [5] |
Posted by dfuchs on May 31, 2006 at 05:38 PM CEST #
Posted by Cassie on July 28, 2006 at 10:37 AM CEST #
SNMP uses BER to encode the data it sends over the wire. BER encoding of SNMP PDUs is not trivial, and I wouldn't recommend to implement it from scratch.
The first step when deciding to implement an SNMP agent, or an SNMP trap emitter, or an SNMP trap receiver, is to look for a good SNMP toolkit/library in your language of choice.
You could google for it (or resp. use your preferred internet search engine) - or have a look at www.SNMPLink.org which has a good list of SNMP resources.
You could also change your mind and write your application in Java ;-))
Hope this helps,
-- daniel
Posted by daniel on July 31, 2006 at 05:40 PM CEST #
Posted by Ameya on November 28, 2006 at 05:43 PM CET #
Daniel,
Thanks for posting all this...It's very helpful!
As a newbie java programmer, I may be ignorant of what magic NetBeans does, since I'm using WebSphere, but your Fixing Imports section above indicates finding the jar files, but not the specific imports required in the source. Here they are, for other newbies who might similarly stub their toes...
import com.sun.management.comm.*;
import com.sun.management.snmp.*;
import com.sun.management.snmp.manager.*;
import org.apache.log4j.*;
Posted by anthonyr on July 13, 2009 at 05:31 AM CEST #