[SNMP4J] How to implement MIB?
Deon van der Merwe
dvdm at truteq.co.za
Wed Jul 13 09:17:07 CEST 2005
Hi Jozef,
Here is a very basic example of an application that can receive and respond
to GET requests. Hope that it helps you...
>Date: Thu, 19 Aug 2004 10:28:38 +0200
>To: snmp4j at agentpp.org
>From: Deon van der Merwe <dvdm at truteq.co.za>
>Subject: Re: [SNMP4J] Example Agent
>
>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);
> }
> }
>}
At 10:03 PM 7/12/2005, jj777 at speakeasy.net wrote:
>Hello,
>
>I have managed to compile and run both SnmpRequest and TestAgent.
>Now, I am trying to figure out how to write an agent which would
>implement my simple enterprise MIB. Basically, I would need the
>agent to properly respond to a GET PDU. It should lunch a proper
>method on an external object based on the OID, receive the return
>value from the method and then send the response with the return
>value back to the client. Which classes should I look at? Can
>someone point me to the right direction, please?
>
>Jozef
>
>
>_______________________________________________
>SNMP4J mailing list
>SNMP4J at agentpp.org
>http://lists.agentpp.org/mailman/listinfo/snmp4j
-Deon
_____________________________________________________
TruTeq Wireless (Pty) Ltd. | Tel: +27 (0)12 667 1530
http://www.truteq.co.za | Fax: +27 (0)12 667 1531
Wireless communications for remote machine management
More information about the SNMP4J
mailing list