[SNMP4J] snmp agent implementation

saurabh suresh sarbaliya kukku17 at rediffmail.com
Sat Jan 15 12:44:37 CET 2005


hello,
i have been trying to implement a snmp agent using snmp4j and net-snmp or MS snmp services.
what i am doing is that if any request for my enterprise OID is coming i am dealing with it, and if any mib-2 request comes i pass it on to port 161 of the system get data from there. however when i tried to test this agent with SNMPc the NMS failed to recognize it and showed the device down error. i am attaching the code, please help me with it.
i am new to both java and snmp so please bash me mildly:)

import org.snmp4j.*;
import org.snmp4j.PDU;
import org.snmp4j.smi.*;
import org.snmp4j.transport.*;
import org.snmp4j.mp.*;
import org.snmp4j.event.*;
import org.snmp4j.PDUv1;
import java.util.*;
import java.util.EventListener;
import java.util.EventObject;
import java.net.*;
import java.io.*;




class  NetRet
{
public static 	UdpAddress ud=null;
public static 	UdpAddress ud1=null;
public static 	UdpAddress ud2=null;
public static 	UdpAddress ud3=null;
public static 	UdpAddress ud4=null;
	public static TransportMapping transport =null;
	public static TransportMapping trans =null;
	public static TransportMapping train =null;
		//public static Variable var=null;
	public static void main(String[] args) throws Exception
	{
		InetAddress a=InetAddress.getLocalHost();
 ud=new UdpAddress(a,10161);
 ud1=new UdpAddress(a,161);
 ud2=new UdpAddress(a,1161);
  ud3=new UdpAddress(a,1162);
    ud4=new UdpAddress(a,162);
 transport =new DefaultUdpTransportMapping(ud);
  trans=new DefaultUdpTransportMapping(ud2);
    train=new DefaultUdpTransportMapping(ud3);
   MessageDispatcherImpl mt=new MessageDispatcherImpl();
Snmp snmp = new Snmp(mt,transport);
transport.listen();
trans.listen();
PDUv1 pd = new PDUv1();
pd.setType(PDU.V1TRAP);
pd.setGenericTrap(PDUv1.COLDSTART);
CommunityTarget targe = new CommunityTarget();
targe.setCommunity(new OctetString("public"));
targe.setRetries(5);
targe.setAddress(ud4);
targe.setTimeout(200000);
targe.setVersion(SnmpConstants.version1);

try{
Snmp sn = new Snmp(train);
//System.out.println("He!");
PDU respons = sn.sendPDU(pd, targe);
 System.out.println(pd.toString());
}

catch(IOException ex){}
   CommandResponder trapPrinter = new CommandResponder() {
     public synchronized void processPdu(CommandResponderEvent e) {
       PDU command = e.getPdu();
       if (command != null) {
         System.out.println(command.toString());
	
       }	
	   Vector vc=command.getVariableBindings();
	   int siz=vc.size();
	     PDU pdu = new PDU();
	   for(int k=0;k<siz;k++)
		 {
	  VariableBinding v=command.get(k);
      // System.out.println(v);
	 
	  OID o1=new OID("1.3.6.1.4.1.xyz.1.1.0");
	  OID s=new OID("1.3.6.1.4.1.xyz.1.1");
	OID t=new OID("1.3.6.1.4.1.xyz.2.1.0");
	OID u=new OID("1.3.6.1.4.1.xyz.1.2.0");
	OID w=new OID("1.3.6.1.4.1.xyz.1.3.0");
		OID o=v.getOid();
		
	 if(o.equals(o1) || o.equals(s))
		 {
		  Integer32 i=new Integer32(34);
	//var=i;
     
	  v.setVariable(i);
	  v.setOid(o1);
     pdu.add(v);
	 System.out.println("Hello World!");
}
else if ( o.equals(t) )
{
OctetString st=new OctetString("http://www.down.com");
  v.setVariable(st);
	  v.setOid(t);
     pdu.add(v);
	
}
else if ( o.equals(u) )
{
Integer32 i2=new Integer32(343);
	//var=i;
     
	  v.setVariable(i2);
	  v.setOid(u);
     pdu.add(v);
	
}

else if ( o.equals(w) )
{
Integer32 i3=new Integer32(343);
	//var=i;
     
	  v.setVariable(i3);
	  v.setOid(w);
     pdu.add(v);
	
}

else {
			CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setRetries(5);
target.setAddress(ud1);
target.setTimeout(200000);
target.setVersion(SnmpConstants.version1);
   //System.out.println(" 1");
    
try{
	//TransportMapping transport1 =new DefaultUdpTransportMapping(ud1);
	//System.out.println(" 2");
	
	PDU p=new PDU();
	p.add(new VariableBinding(o));
	p.setType(command.getType());
Snmp snm = new Snmp(trans);
//System.out.println(" 3");
    //  System.out.println(command.toString());
 
  
PDU response = snm.sendPDU(p, target);
if (response == null) {
    System.out.println(" request timed out");
  }
else {
    // dump response
   // System.out.println(response.toString());
}
  VariableBinding vr=response.get(0);
 // System.out.println(" 5");
  Variable var=vr.getVariable();
 //System.out.println(" 6");
   v.setVariable(var);
//System.out.println(" 7");
 pdu.add(v);
//System.out.println(" 4");
/*if (response == null) {
    // request timed out
    System.out.println(" War");
}
else {
    // dump response
    System.out.println(response.toString());
}*/


//System.out.println("Hello ");
}

catch(IOException ex){}

}
		 }
		 	 pdu.setType(PDU.RESPONSE);
	 CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(e.getPeerAddress());
System.out.println(e.getPeerAddress());
target.setRetries(5);

target.setTimeout(200000);
try{
Snmp snm = new Snmp(transport);
//System.out.println("He!");
PDU response = snm.sendPDU(pdu, target);
 System.out.println(pdu.toString());
}

catch(IOException ex){}
	 
		 
	 }
	
   };
   snmp.addCommandResponder(trapPrinter);
	}
}



More information about the SNMP4J mailing list