Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem in implementing code using snmp4j.jar

843790Jul 4 2008 — edited Jul 7 2008
Hi,

I am using snmp4j.jar to create a class that will execute SNMP commands such as GET, GETNEXT and GETTABLE.

The issue I am facing is extremely peculiar. When running the program for the first time, the output is seen correctly. However, in the subsequent executions, the program hangs (at the point where the object of my snmp class is to be created).

I am unable to understand if some socket/port has been left open in the first run because of which successive executions are not happening. Also, the SNMP session may not be getting closed properly.

Could someone please help me out with this issue? I am pasting the code snippets here for reference.
Please do respond with your comments on the same. Thank you.



******************************************************************************************************************
import java.io.IOException;
import java.util.List;
import java.util.Vector;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.TableEvent;
import org.snmp4j.util.TableUtils;

public class MySNMP 
{
	
	/*
	 *  Class variable declarations
	 */ 
	PDU requestPDU = null;
	Snmp snmp = null;
	CommunityTarget target = null;
	String responseString = null;
	
	
	
	/*
	 *  Main method; point of execution start.
	 */  
	public static void main(String[] args) throws IOException
	{
			snmpUtils obj = new snmpUtils("XX.XX.XX.XX", "public", 1, 1500);
			
			String arrGet = obj.snmpGet(".1.3.6.1.2.1.1.1.0");
			System.out.println("\nGET RESPONSE");
			System.out.println(arrGet);
			
			String arrGetNext = obj.snmpGetNext(".1.3.6.1.2.1.1.1.0");
			System.out.println("\nGETNEXT RESPONSE");
			System.out.println(arrGetNext);
			
			String[] arr = { ".1.3.6.1.2.1.2.2.1.1.", ".1.3.6.1.2.1.2.2.1.5" };
			String arrGetTable = obj.snmpGetTable(arr);
			System.out.println("\nGETTABLE RESPONSE");
			System.out.println(arrGetTable);
	}
	
	
	
	/*
	 *  Parameterized constructor
	 */ 
	public MySNMP(String IP, String commString, int version, long timeout) 
	{
		
		// Create an instance of the Snmp class, CommunityTarget class 
		try 
		{
			snmp = new Snmp(new DefaultUdpTransportMapping());
		} 
		catch (IOException e) 
		{		
			e.printStackTrace();
		}
		
		target = new CommunityTarget();
		Address targetAddress = GenericAddress.parse("udp:" + IP + "/161");

		// Set the address of the target device; This is a mandatory value to be passed by the calling script
		target.setAddress(targetAddress);

		/*
		 *  Set the version of the target device;
		 *  In cases where the version provided is not 1,2 or 3, the default value set is 1  
		 */  		
		if (version == 1)
			target.setVersion(SnmpConstants.version1);
		else if (version == 2)
			target.setVersion(SnmpConstants.version2c);
		else if (version == 3)
			target.setVersion(SnmpConstants.version3);
		
		// Set the timeout of the target device
		target.setTimeout(timeout);
		
		/*
		 *  Set the community string of the target device;
		 *  In cases where the community string provided is not null, the default value is set as "public"  
		 */  
		if (commString == null)
			target.setCommunity(new OctetString("public"));
		else
			target.setCommunity(new OctetString(commString));		
	}
	

	
	/*
	 *  SNMP get/getNext operation logic
	 */ 
	public void get(String oid, int pduType)  
	{
		
		// Create a PDU with the OID and type
		requestPDU = new PDU();
		requestPDU.add(new VariableBinding(new OID(oid))); 
		requestPDU.setType(pduType);
			
		ResponseEvent response = null;
		try
		{
			// Invoke the listen() method on the Snmp object
			snmp.listen();
	
			// Send the PDU constructed, to the target
			response = snmp.send(requestPDU, target);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
			
		// Retrieve the response details and put into an array called "responseArray"
		responseString = new String();
		if (!(response.getResponse() == null)) 
		{
			//Extract the response
		}
	}

	
	
	/*
	 *  SNMP GET method API
	 */	
	public String snmpGet(String oid) 
	{
		get(oid, PDU.GET);
		return(responseString);		
	}

	
	
	/*
	 *  SNMP GETNEXT method API
	 */	
	public String snmpGetNext(String oid) throws IOException 
	{
		get(oid, PDU.GETNEXT);
		return(responseString);	
	}
	
	
	
	/*
	 *  SNMP getTable operation
	 */
	public String snmpGetTable(String[] oid) 
	{
		
		// Invoke the listen() method on the Snmp object
		try 
		{
			snmp.listen();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		      
		// Create a TableUtils 
		TableUtils utils = new TableUtils(snmp, new DefaultPDUFactory());
				
		// Set the lower/upper bounds for the table operation
		OID lowerIndex = null;
		OID upperIndex = null;
		
		// Create an array of the OID's that need to be checked
		OID[] arr = new OID[oid.length];
		for (int i=0; i<oid.length; i++)
			arr[i] = new OID(oid);

// Transfer output to a data structure
List list = utils.getTable(target, arr, lowerIndex, upperIndex);

// Dump the response into an array called "responseArray"
....
return responseString;
}
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 4 2008
Added on Jul 4 2008
2 comments
1,281 views