[jboss-cvs] JBossAS SVN: r105076 - in branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp: test and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu May 20 16:26:42 EDT 2010
Author: bshim
Date: 2010-05-20 16:26:39 -0400 (Thu, 20 May 2010)
New Revision: 105076
Added:
branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpRequest.java
Modified:
branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java
branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentService.java
branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/TrapEmitter.java
branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/test/NotificationProducerService.java
branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/trapd/TrapdService.java
Log:
Traps use UDP and basic GET requests work
Modified: branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java
===================================================================
--- branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java 2010-05-20 19:34:59 UTC (rev 105075)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java 2010-05-20 20:26:39 UTC (rev 105076)
@@ -750,68 +750,68 @@
// return be;
// }
- /**
- * Return the next oid that is larger than ours.
- * @param oid the starting oid
- * @param stayInSubtree if true, the next oid will not have a different prefix than the one of oid.
- * @return the next oid or null if none found.
- */
- private ComparableSnmpObjectId getNextOid(final ComparableSnmpObjectId oid, boolean stayInSubtree) {
- ComparableSnmpObjectId coid = new ComparableSnmpObjectId(oid);
+// /**
+// * Return the next oid that is larger than ours.
+// * @param oid the starting oid
+// * @param stayInSubtree if true, the next oid will not have a different prefix than the one of oid.
+// * @return the next oid or null if none found.
+// */
+// private ComparableSnmpObjectId getNextOid(final ComparableSnmpObjectId oid, boolean stayInSubtree) {
+// ComparableSnmpObjectId coid = new ComparableSnmpObjectId(oid);
+//
+//
+// if (coid.isLeaf())
+// coid = coid.removeLastPart();
+//
+// SortedSet ret;
+// ret= oidKeys.tailSet(oid); // get oids >= oid
+// Iterator it = ret.iterator();
+// ComparableSnmpObjectId roid=null;
+//
+// /*
+// * If there are elements in the tail set, then
+// * - get first one.
+// * - if first is input (which it is supposed to be according to the contract of
+// * SortedSet.tailSet() , then get next, which is the
+// * one we look for.
+// */
+// if (it.hasNext())
+// {
+// roid = (ComparableSnmpObjectId)it.next(); // oid
+// }
+//
+// if (roid == null)
+// {
+// return null; // roid is null,
+// }
+//
+// if (roid.compareTo(coid)==0) // input elment
+// {
+// // if there is a next element, then it is ours.
+// if (it.hasNext())
+// {
+// roid = (ComparableSnmpObjectId)it.next();
+// }
+// else
+// {
+// roid = null; // end of list
+// }
+// }
+//
+// /*
+// * Check if still in subtree if requested to stay within
+// */
+// if (stayInSubtree && roid != null)
+// {
+// ComparableSnmpObjectId parent = coid.removeLastPart();
+// if (!parent.isRootOf(roid))
+// roid = null;
+// }
+//
+// return roid;
+// }
- if (coid.isLeaf())
- coid = coid.removeLastPart();
-
- SortedSet ret;
- ret= oidKeys.tailSet(oid); // get oids >= oid
- Iterator it = ret.iterator();
- ComparableSnmpObjectId roid=null;
-
- /*
- * If there are elements in the tail set, then
- * - get first one.
- * - if first is input (which it is supposed to be according to the contract of
- * SortedSet.tailSet() , then get next, which is the
- * one we look for.
- */
- if (it.hasNext())
- {
- roid = (ComparableSnmpObjectId)it.next(); // oid
- }
-
- if (roid == null)
- {
- return null; // roid is null,
- }
-
- if (roid.compareTo(coid)==0) // input elment
- {
- // if there is a next element, then it is ours.
- if (it.hasNext())
- {
- roid = (ComparableSnmpObjectId)it.next();
- }
- else
- {
- roid = null; // end of list
- }
- }
-
- /*
- * Check if still in subtree if requested to stay within
- */
- if (stayInSubtree && roid != null)
- {
- ComparableSnmpObjectId parent = coid.removeLastPart();
- if (!parent.isRootOf(roid))
- roid = null;
- }
-
- return roid;
- }
-
-
// Inner Class ---------------------------------------------------
/**
Modified: branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentService.java
===================================================================
--- branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentService.java 2010-05-20 19:34:59 UTC (rev 105075)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpAgentService.java 2010-05-20 20:26:39 UTC (rev 105076)
@@ -66,7 +66,7 @@
* @version $Revision$
*/
public class SnmpAgentService extends ListenerServiceMBeanSupport
- implements SnmpAgentServiceMBean, CommandResponder
+ implements SnmpAgentServiceMBean
{
/** Supported versions */
public static final int SNMPV1 = 1;
@@ -593,8 +593,10 @@
this.requestHandlerClassName, true, this.getClass().getClassLoader()).newInstance();
requestHandler.initialize(this.requestHandlerResName, this.getServer(), this.log, this.clock);
- Snmp snmp = new Snmp(new DefaultTcpTransportMapping(new TcpAddress(this.bindAddress, this.port)));
- snmp.addCommandResponder(this);
+ //Snmp snmp = new Snmp(new DefaultTcpTransportMapping(new TcpAddress(this.bindAddress, this.port)));
+ Snmp snmp = new Snmp(new DefaultUdpTransportMapping(new UdpAddress(this.bindAddress, this.port)));
+ SnmpRequest request = new SnmpRequest(requestHandler, log);
+ snmp.addCommandResponder(request);
snmp.listen();
/********************************************************************
*
@@ -658,102 +660,102 @@
return InetAddress.getByName(host);
}
- @Override
- public void processPdu(CommandResponderEvent event) {
- PDU pdu = event.getPDU();
-
- if (pdu == null)
- return;
-
- int type = pdu.getType();
- //switch based on pdu.getType() == ___
- PDU response = null;
- try{
- switch (type) {
- case PDU.GET:
- response = requestHandler.snmpReceivedGet(pdu, false);
-// Address address = event.getPeerAddress();
-// Snmp snmp = new Snmp(new DefaultTcpTransportMapping((TcpAddress)address));
- break;
- case PDU.GETBULK:
- //TODO maybe make a method called snmpReceivedGetBulk in requesthandler
- // requestHandler.snmpReceivedGet(pdu);
- break;
- case PDU.GETNEXT:
- response = requestHandler.snmpReceivedGet(pdu, true);
- break;
- case PDU.SET:
- response = requestHandler.snmpReceivedSet(pdu);
- break;
- default:
- log.warn("Invalid PDU type: " + PDU.getTypeString(type));
- return;
- }
- if (response != null) {
- sendResponse(event, response);
- }
- } catch (Exception e){
- log.debug("Snmp request failed: "+ PDU.getTypeString(pdu.getType()));
- }
-
- }
+// @Override
+// public void processPdu(CommandResponderEvent event) {
+// PDU pdu = event.getPDU();
+//
+// if (pdu == null)
+// return;
+//
+// int type = pdu.getType();
+// //switch based on pdu.getType() == ___
+// PDU response = null;
+// try{
+// switch (type) {
+// case PDU.GET:
+// response = requestHandler.snmpReceivedGet(pdu, false);
+//// Address address = event.getPeerAddress();
+//// Snmp snmp = new Snmp(new DefaultTcpTransportMapping((TcpAddress)address));
+// break;
+// case PDU.GETBULK:
+// //TODO maybe make a method called snmpReceivedGetBulk in requesthandler
+// // requestHandler.snmpReceivedGet(pdu);
+// break;
+// case PDU.GETNEXT:
+// response = requestHandler.snmpReceivedGet(pdu, true);
+// break;
+// case PDU.SET:
+// response = requestHandler.snmpReceivedSet(pdu);
+// break;
+// default:
+// log.warn("Invalid PDU type: " + PDU.getTypeString(type));
+// return;
+// }
+// if (response != null) {
+// sendResponse(event, response);
+// }
+// } catch (Exception e){
+// log.debug("Snmp request failed: "+ PDU.getTypeString(pdu.getType()));
+// }
+//
+// }
- private void sendResponse(CommandResponderEvent requestEvent, PDU response) {
- //MessageDispatcher disp = requestEvent.getMessageDispatcher();
- try {
- if (response.getBERLength() > requestEvent.getMaxSizeResponsePDU()) {
- // response is tooBig
- if (response.getType() != PDU.REPORT) {
- if (requestEvent.getPDU().getType() == PDU.GETBULK) {
- while ((response.size() > 0) &&
- (response.getBERLength() >
- requestEvent.getMaxSizeResponsePDU()))
- {
- response.trim();
- }
- }
- else {
- response.clear();
- response.setRequestID(requestEvent.getPDU().getRequestID());
- response.setErrorStatus(PDU.tooBig);
- }
- }
- if (response.getBERLength() > requestEvent.getMaxSizeResponsePDU()) {
- //still too big and can't send a response
- return;
- }
- }
-//---start of my addition -- Mar 29
+// private void sendResponse(CommandResponderEvent requestEvent, PDU response) {
+// //MessageDispatcher disp = requestEvent.getMessageDispatcher();
+// try {
+// if (response.getBERLength() > requestEvent.getMaxSizeResponsePDU()) {
+// // response is tooBig
+// if (response.getType() != PDU.REPORT) {
+// if (requestEvent.getPDU().getType() == PDU.GETBULK) {
+// while ((response.size() > 0) &&
+// (response.getBERLength() >
+// requestEvent.getMaxSizeResponsePDU()))
+// {
+// response.trim();
+// }
+// }
+// else {
+// response.clear();
+// response.setRequestID(requestEvent.getPDU().getRequestID());
+// response.setErrorStatus(PDU.tooBig);
+// }
+// }
+// if (response.getBERLength() > requestEvent.getMaxSizeResponsePDU()) {
+// //still too big and can't send a response
+// return;
+// }
+// }
+////---start of my addition -- Mar 29
+//
+//
+////--end of my addition
+//
+// StatusInformation status = new StatusInformation();
+// StateReference stateRef = requestEvent.getStateReference();
+// if (stateRef == null) {
+// log.warn("No state reference available for requestEvent="+
+// requestEvent+". Cannot return response="+response);
+// }
+// else {
+// stateRef.setTransportMapping(requestEvent.getTransportMapping());
+// int resp = requestEvent.getMessageDispatcher().returnResponsePdu(requestEvent.getMessageProcessingModel(),
+// requestEvent.getSecurityModel(),
+// requestEvent.getSecurityName(),
+// requestEvent.getSecurityLevel(),
+// response,
+// requestEvent.getMaxSizeResponsePDU(),
+// //requestEvent.getStateReference(),
+// stateRef,
+// status);
+// log.info("Response was OK? : "+resp +" Constant: "+SnmpConstants.SNMP_MP_OK);
+// log.info("Returned response for requestID: " +
+// requestEvent.getPDU().getRequestID().toString() +
+// "to peer: "+requestEvent.getPeerAddress().toString());
+// }
+// }
+// catch (MessageException ex) {
+// log.error("Failed to send response to request "+requestEvent, ex);
+// }
+// }
-
-//--end of my addition
-
- StatusInformation status = new StatusInformation();
- StateReference stateRef = requestEvent.getStateReference();
- if (stateRef == null) {
- log.warn("No state reference available for requestEvent="+
- requestEvent+". Cannot return response="+response);
- }
- else {
- stateRef.setTransportMapping(requestEvent.getTransportMapping());
- int resp = requestEvent.getMessageDispatcher().returnResponsePdu(requestEvent.getMessageProcessingModel(),
- requestEvent.getSecurityModel(),
- requestEvent.getSecurityName(),
- requestEvent.getSecurityLevel(),
- response,
- requestEvent.getMaxSizeResponsePDU(),
- //requestEvent.getStateReference(),
- stateRef,
- status);
- log.info("Response was OK? : "+resp +" Constant: "+SnmpConstants.SNMP_MP_OK);
- log.info("Returned response for requestID: " +
- requestEvent.getPDU().getRequestID().toString() +
- "to peer: "+requestEvent.getPeerAddress().toString());
- }
- }
- catch (MessageException ex) {
- log.error("Failed to send response to request "+requestEvent, ex);
- }
- }
-
}
Added: branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpRequest.java
===================================================================
--- branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpRequest.java (rev 0)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/SnmpRequest.java 2010-05-20 20:26:39 UTC (rev 105076)
@@ -0,0 +1,123 @@
+package org.jboss.jmx.adaptor.snmp.agent;
+
+import org.jboss.logging.Logger;
+import org.snmp4j.CommandResponder;
+import org.snmp4j.CommandResponderEvent;
+import org.snmp4j.MessageException;
+import org.snmp4j.PDU;
+import org.snmp4j.mp.SnmpConstants;
+import org.snmp4j.mp.StateReference;
+import org.snmp4j.mp.StatusInformation;
+
+public class SnmpRequest implements CommandResponder {
+
+ /** Logger object */
+ protected Logger log;
+
+ /** the request handler instance handling get/set requests */
+ private RequestHandler requestHandler;
+
+ public SnmpRequest(RequestHandler requestHandler, Logger log){
+ this.requestHandler = requestHandler;
+ this.log = log;
+ }
+
+ public void setRequestHandler(RequestHandler requestHandler){
+ this.requestHandler = requestHandler;
+ }
+
+ public RequestHandler getRequestHandler(){
+ return this.requestHandler;
+ }
+
+ @Override
+ public void processPdu(CommandResponderEvent event) {
+ PDU pdu = event.getPDU();
+
+ if (pdu == null) {
+ log.warn("Null request PDU received ... skipping");
+ return;
+ }
+
+ log.info("Received Snmp request of type: "+PDU.getTypeString(pdu.getType()));
+ int type = pdu.getType();
+
+ PDU response = null;
+ //switch based on pdu.getType() == ___
+ switch (type) {
+ case PDU.GET:
+ response = requestHandler.snmpReceivedGet(pdu, false);
+ break;
+ case PDU.GETBULK:
+ //TODO maybe make a method called snmpReceivedGetBulk in requesthandler
+ // requestHandler.snmpReceivedGet(pdu);
+ break;
+ case PDU.GETNEXT:
+ response = requestHandler.snmpReceivedGet(pdu, true);
+ break;
+ case PDU.SET:
+ response = requestHandler.snmpReceivedSet(pdu);
+ break;
+ default:
+ log.warn("Cannot process request PDU of type: " +
+ PDU.getTypeString(type) + "unsupported");
+ return;
+ }
+ if (response != null) {
+
+ //VERY IMPORTANT LINE
+ response.setRequestID(event.getPDU().getRequestID());
+ try {
+ sendResponse(event, response);
+ } catch (MessageException e) {
+ log.warn("Response may not have been sent correctly. " +
+ "An error occured snmp4j message processing: " +
+ e.getMessage());
+ }
+ }
+ }
+
+ private void sendResponse(CommandResponderEvent requestEvent, PDU response) throws MessageException{
+
+ if (response.getBERLength() > requestEvent.getMaxSizeResponsePDU()) {
+ // response is tooBig
+ if (response.getType() != PDU.REPORT) {
+ if (requestEvent.getPDU().getType() == PDU.GETBULK) {
+ while ((response.size() > 0) &&
+ (response.getBERLength() >
+ requestEvent.getMaxSizeResponsePDU()))
+ {
+ response.trim();
+ }
+ }
+ else {
+ response.clear();
+ response.setRequestID(requestEvent.getPDU().getRequestID());
+ response.setErrorStatus(PDU.tooBig);
+ }
+ }
+ if (response.getBERLength() > requestEvent.getMaxSizeResponsePDU()) {
+ //still too big and can't send a response
+ return;
+ }
+ }
+
+ StatusInformation status = new StatusInformation();
+ StateReference stateRef = requestEvent.getStateReference();
+ if (stateRef == null) {
+ log.warn("No state reference available for requestEvent="+
+ requestEvent+". Cannot return response="+response);
+ }
+ else {
+ stateRef.setTransportMapping(requestEvent.getTransportMapping());
+ int resp = requestEvent.getMessageDispatcher().returnResponsePdu(requestEvent.getMessageProcessingModel(),
+ requestEvent.getSecurityModel(),
+ requestEvent.getSecurityName(),
+ requestEvent.getSecurityLevel(),
+ response,
+ requestEvent.getMaxSizeResponsePDU(),
+ stateRef,
+ status);
+ }
+ }
+}
Modified: branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/TrapEmitter.java
===================================================================
--- branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/TrapEmitter.java 2010-05-20 19:34:59 UTC (rev 105075)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/TrapEmitter.java 2010-05-20 20:26:39 UTC (rev 105076)
@@ -56,6 +56,7 @@
import org.snmp4j.smi.IpAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;
+import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.AbstractTransportMapping;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;
@@ -407,7 +408,7 @@
//target = new CommunityTarget(new TcpAddress(newAddr), new OctetString("public"));
if (m.getAddress() != null){
- target = new CommunityTarget(new TcpAddress(InetAddress.getByName(m.getAddress()), m.getPort()), new OctetString(m.getCommunityString()));
+ target = new CommunityTarget(new UdpAddress(InetAddress.getByName(m.getAddress()), m.getPort()), new OctetString(m.getCommunityString()));
//try defining retries/timeout period in notifications.xml
//Timeout and Retries needs to be a field later on
//target.setRetries(3);
@@ -417,7 +418,7 @@
else if (version == SnmpConstants.version3) {
//won't be used at the moment
target = new UserTarget();
- target.setAddress(new TcpAddress(InetAddress.getByName(m.getAddress()), m.getPort()));
+ target.setAddress(new UdpAddress(InetAddress.getByName(m.getAddress()), m.getPort()));
}
else {
//unrecognized version
Modified: branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/test/NotificationProducerService.java
===================================================================
--- branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/test/NotificationProducerService.java 2010-05-20 19:34:59 UTC (rev 105075)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/test/NotificationProducerService.java 2010-05-20 20:26:39 UTC (rev 105076)
@@ -34,8 +34,10 @@
import org.snmp4j.smi.Address;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.TcpAddress;
+import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultTcpTransportMapping;
+import org.snmp4j.transport.DefaultUdpTransportMapping;
/**
* <tt>NotificationProducerService</tt> is a test class with an MBean interface
@@ -111,8 +113,8 @@
Target target = new CommunityTarget();
try {
- target.setAddress(new TcpAddress(InetAddress.getByName("127.0.0.1"), 1161));//could be 1162
- DefaultTcpTransportMapping transport = new DefaultTcpTransportMapping();
+ target.setAddress(new UdpAddress(InetAddress.getByName("127.0.0.1"), 1161));//could be 1162
+ DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
ResponseEvent responseEvent = snmp.send(pdu, target);
System.out.println("response " + responseEvent.toString());
Modified: branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/trapd/TrapdService.java
===================================================================
--- branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/trapd/TrapdService.java 2010-05-20 19:34:59 UTC (rev 105075)
+++ branches/snmp4j-integration/varia/src/main/java/org/jboss/jmx/adaptor/snmp/trapd/TrapdService.java 2010-05-20 20:26:39 UTC (rev 105076)
@@ -48,8 +48,10 @@
import org.snmp4j.security.USM;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;
+import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultTcpTransportMapping;
+import org.snmp4j.transport.DefaultUdpTransportMapping;
/**
* MBean wrapper class that acts as an SNMP trap receiver/logger.
@@ -160,7 +162,7 @@
mtDispatcher.addMessageProcessingModel(new MPv2c());
mtDispatcher.addMessageProcessingModel(new MPv3());
TransportMapping transport =
- new DefaultTcpTransportMapping(new TcpAddress(address,port));
+ new DefaultUdpTransportMapping(new UdpAddress(address,port));
this.snmp = new Snmp(mtDispatcher,transport);
More information about the jboss-cvs-commits
mailing list