[SNMP4J] Example Agent

Deon van der Merwe dvdm at truteq.co.za
Thu Aug 19 10:28:38 CEST 2004


Hi Hector,

At 08:18 PM 8/18/2004, Hector Aquiles Aranguren Gonzalez wrote:

>  Hi, where can i found an example about Make a SNMP Agent with SNMP4J?
>    Receive and respond the message to a NMS??

I have been able to do this (if I understand your question 
correctly...)  Here is
a little snippet of how I did it.  This was done using the CommandResponder (as
Frank suggested)

This is part of an existing system, and I removed what I could that has no 
direct
relation on this.  Hope that it makes more or less some sense...



public class Handler
      implements org.snmp4j.CommandResponder {
   protected java.lang.String mAddress = null;
   protected int mPort = 0;
   protected java.lang.String mMyCommunityName = null;
   protected org.snmp4j.TransportMapping mServerSocket = null;
   protected org.snmp4j.Snmp mSNMP = null;
   public Handler() {
   }
   public void configure(
       com.truteq.Configuration aConfiguration)
        throws com.truteq.ConfigurationException {
     mAddress = aConfiguration.getChild(Constants.ADDRESS).getAsString();
     mPort = aConfiguration.getChild(Constants.PORT).getAsInt();
     mMyCommunityName = 
aConfiguration.getChild(Constants.COMMUNITY).getAsString();
   }
   public void start()
        throws com.truteq.ServiceException {
     try {
       mServerSocket = new 
org.snmp4j.transport.DefaultUdpTransportMapping(new 
org.snmp4j.smi.UdpAddress(java.net.InetAddress.getByName(mAddress), mPort));
       mSNMP = new org.snmp4j.Snmp(mServerSocket);
       mSNMP.addCommandResponder(this);
       mServerSocket.listen();
     } catch (java.net.UnknownHostException vException) {
       mLogger.error(vException.getMessage(), vException);
     } catch (java.io.IOException vException) {
       mLogger.error(vException.getMessage(), vException);
     }
   }
   /**
    * this is called by the application that processed the actual SNMP 
request in
    * order to send response...
    */
   public synchronized void submit(
       com.truteq.protocol.Message aMessage)
        throws com.truteq.net.HandlerException {
     if (aMessage.getType() == com.truteq.protocol.Message.SNMP4J) {
       com.truteq.protocol.counter.snmp4j.Message vMessage = 
(com.truteq.protocol.counter.snmp4j.Message) aMessage;
       org.snmp4j.CommandResponderEvent vEvent = vMessage.getPDU();
       org.snmp4j.PDU vPDU = vEvent.getPdu();
       if (mLogger.isDebugEnabled()) {
         mLogger.debug("(snd) " + vPDU.toString());
       } else {
       }
       org.snmp4j.mp.StatusInformation statusInformation = new 
org.snmp4j.mp.StatusInformation();
       org.snmp4j.mp.StateReference ref = vEvent.getStateReference();
       try {
         vEvent.getMessageDispatcher().returnResponsePdu(vEvent.getMessageProcessingModel(), 
vEvent.getSecurityModel(), vEvent.getSecurityName(), 
vEvent.getSecurityLevel(), vPDU, vEvent.getMaxSizeResponsePDU(), ref, 
statusInformation);
       } catch (org.snmp4j.MessageException vException) {
         mLogger.error(vException.getMessage(), vException);
       }
     } else {
     }
   }
   public synchronized void processPdu(
       org.snmp4j.CommandResponderEvent aEvent) {
     java.lang.String vCommunityName = new 
java.lang.String(aEvent.getSecurityName());
     if (mMyCommunityName.equals(vCommunityName)) {
       org.snmp4j.PDU vPDU = aEvent.getPdu();
       if (vPDU == null) {
       } else {
         if (mLogger.isDebugEnabled()) {
           mLogger.debug("(rcv) " + vPDU.toString());
         } else {
         }
         switch (vPDU.getType()) {
           case org.snmp4j.PDU.GET:
           case org.snmp4j.PDU.GETNEXT:
             try {
               setChanged();
               notifyObservers(new 
com.truteq.protocol.counter.snmp4j.Message(aEvent));
             } catch (java.lang.Exception vNotifyException) {
               mLogger.error(vNotifyException.getMessage(), vNotifyException);
             }
             break;
         }
       }
     } else {
       mLogger.warning("supplied community string does not match mine... 
ignore request: " + aEvent);
     }
   }
}




More information about the SNMP4J mailing list