[SNMP4J] Problem with SNMP data downloading, please help
Tejo Prayaga
tejovamsi at yahoo.com
Mon Mar 20 01:54:56 CET 2006
Hi Marcin,
I don't think it to be a problem with the agent. Now since the node is in a different sub net, the round-trip delay is relatively more than it when it was in the local sub net. Probably, you can try increasing the time out and see if it works.
The second possibility could be something to do with NAT. The node could
have a different IP address when it is behind a NAT enabled router. Check
with your sys admin.
Also, before trying to run this program, just try to get a single variable like
sysDescr using the SNMP4J tool. If it works, then it has some thing to do with either NAT or timeouts.
Let us see if any one else has more inputs on this.
~Tejo
szikken at interia.pl wrote: Hello
I'm trying to write an application with SNMP4J, but I have a small problem,
I have a special device (snmp agent) with external IP address, from which
I'm downloading data.
Problem is that I can do this in local area network, but when I'm trying to
the same from other network it doesn't work and I have always Time Out (I
checked firewalls and everything seems to be ok)
I don't have no ideas if it is problem with this agent or if this is problm
with my code.
If you don't mind I will paste my class resposible for snmp data downloading
form device (as a base for this code I use an example from SNMP4J)
I will be gratefull for any ideas.
Best regards
Marcin Kokoszka
public class DeviceGetSetData
{
private static Vector vbs;
private static Vector downloadedData;
private static TransportMapping transport;
private static Snmp snmp;
public static boolean checkConncetionStatus()
{
boolean status = false;
if(transport.isListening())
status = true;
return status;
}
public static void closeConnection()
{
try
{
transport.close();
snmp.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void LogOff()
{
Logger.getRootLogger().setLevel(Level.OFF);
try
{
if (System.getProperty("log4j.configuration") == null)
{
BasicConfigurator.configure();
}
}
catch (IllegalArgumentException iaex)
{
System.err.print("Error: " + iaex.getMessage());
}
}
private static Snmp createSnmpSession()
{
try
{
SecurityProtocols.getInstance().addDefaultProtocols();
transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
return snmp;
}
catch (IOException e)
{
System.out.println("Error during connection: " + e);
}
return null;
}
private static Target createTarget(String communityStr)
{
OctetString community = new OctetString(communityStr);
CommunityTarget target = new CommunityTarget();
target.setCommunity(community);
return target;
}
private static PDU createPDU(Target target, int pduType)
{
PDUv1 v1TrapPDU = new PDUv1();
PDU request;
if(pduType == -92)
request = v1TrapPDU;
else
request = new PDU();
request.setType(pduType);
return request;
}
private static PDU walk(Snmp snmp, PDU request, Target target)
{
request.setNonRepeaters(0);
OID rootOID = request.get(0).getOid();
PDU response = null;
int objects = 0;
int requests = 0;
long startTime = System.currentTimeMillis();
do
{
requests++;
ResponseEvent responseEvent = null;
try
{
responseEvent = snmp.send(request, target);
}
catch (IOException e)
{}
if(responseEvent == null)
break;
response = responseEvent.getResponse();
if(response != null)
objects += response.size();
} while(!processWalk(response, request, rootOID));
if(requests != objects)
AdditionalFunc.downloadStatus = 1;
System.out.println();
System.out.println("Total requests sent: " + requests);
System.out.println("Total objects received: " + objects);
System.out.println("Total walk time: " +
(System.currentTimeMillis() - startTime) + " milliseconds");
return response;
}
private static boolean processWalk(PDU response, PDU request, OID
rootOID)
{
if(response == null || response.getErrorStatus() != 0 ||
response.getType() == -88)
return true;
boolean finished = false;
OID lastOID = request.get(0).getOid();
for(int i = 0; !finished && i < response.size(); i++)
{
VariableBinding vb = response.get(i);
if(vb.getOid() == null || vb.getOid().size() < rootOID.size() ||
rootOID.leftMostCompare(rootOID.size(), vb.getOid()) != 0)
{
finished = true;
continue;
}
if(Null.isExceptionSyntax(vb.getVariable().getSyntax()))
{
System.out.println(vb.toString());
finished = true;
continue;
}
if(vb.getOid().compareTo(lastOID) <= 0)
{
System.out.println("Variable received is not lexicographic
successor of requested one:");
System.out.println(vb.toString() + " <= " + lastOID);
finished = true;
}
else
{
// System.out.println(vb.toString());
downloadedData.add(AdditionalFunc.cutDataFromResponse(vb.toString()));
lastOID = vb.getOid();
}
}
if(!finished)
{
VariableBinding next = response.get(response.size() - 1);
next.setVariable(new Null());
request.set(0, next);
request.setRequestID(new Integer32(0));
}
return finished;
}
private static PDU send(String communityStr, String address, int
retries, long timeout, int pduType, String pduStr, int operation) //throws
IOException
{
Address targetaddress = new UdpAddress(address+"/161");
long time = timeout;
vbs = new Vector();
vbs.add(new VariableBinding(new OID(pduStr)));
Snmp snmp = createSnmpSession();
Target target = createTarget(communityStr);
target.setVersion(SnmpConstants.version2c);
target.setAddress(targetaddress);
target.setRetries(retries);
target.setTimeout(time);
try
{
snmp.listen();
PDU request = createPDU(target, pduType);
//if it is "GETBULK" operation
// if(request.getType() == -91)
// {
// request.setMaxRepetitions(maxRepetitions);
// request.setNonRepeaters(nonRepeaters);
// }
for(int i = 0; i < vbs.size(); i++)
request.add((VariableBinding)vbs.get(i));
PDU response = null;
if(operation == 1)
{
response = walk(snmp, request, target);
}
else
{
ResponseEvent responseEvent = snmp.send(request, target);
if(responseEvent != null)
response = responseEvent.getResponse();
}
snmp.close();
return response;
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
private static void printReport(PDU response)
{
......
}
private static void printVariableBindings(PDU response)
{
VariableBinding vb = null;
for(int i = 0; i < response.size(); i++)
{
vb = response.get(i);
System.out.println(vb.toString());
}
}
public Vector agentGetSet(int pduType, int operation, final String
address, String oid)
{
final String community;
long timeout = 2;
int retries = 2;
downloadedData = new Vector();
if(operation == 0 || operation == 1)
{
if(FileOperations.CfgFileExist("config.cfg"))
{
timeout =
Integer.parseInt(FileOperations.ReadFromCfgFile("config.cfg").get(2).toString())*1000;
retries =
Integer.parseInt(FileOperations.ReadFromCfgFile("config.cfg").get(3).toString());
}
community = AdditionalFunc.getFromShadow(address, "get");
}
else
{
timeout = operation;
operation = 0;
community = "public";
}
LogOff();
PDU response = null;
if(!community.equals("special"))
response = send(community, address, retries, timeout, pduType,
oid, operation);
else
{
Display.getDefault().syncExec(new Runnable()
{
public void run()
{
if(!community.equals("special"))
LogWindow.addMsg("[" + address + "] Problem with
authorization, check get community string...", "red");
}
});
}
if(pduType == -89 || pduType == -88 || pduType == -92 || pduType
== -94)
System.out.println(PDU.getTypeString(pduType) + " sent
successfully");
else
if(response == null)
System.out.println("Request timed out.");
else
if(response.getType() == -88)
printReport(response);
else
if(operation == 0)
{
System.out.println("Response received with requestID=" +
response.getRequestID() + ", errorIndex=" + response.getErrorIndex() + ", "
+ "errorStatus=" + response.getErrorStatus());
printVariableBindings(response);
if(response.getErrorStatus()==0)
downloadedData.add(response.toString());
}
else
if(operation == 1)
{
System.out.println("End of walked subtree '" +
((VariableBinding)vbs.get(0)).getOid() + "' reached at:");
printVariableBindings(response);
}
else
{
System.out.println("Received something strange: requestID=" +
response.getRequestID() + ", errorIndex=" + response.getErrorIndex() + ", "
+ "errorStatus=" + response.getErrorStatus());
printVariableBindings(response);
}
return downloadedData;
}
}
_______________________________________________
SNMP4J mailing list
SNMP4J at agentpp.org
http://lists.agentpp.org/mailman/listinfo/snmp4j
---------------------------------
Brings words and photos together (easily) with
PhotoMail - it's free and works with Yahoo! Mail.
More information about the SNMP4J
mailing list