So I finaly found this is clearly a bug in snmp4j http://oosnmp.net/pipermail/snmp4j/2016-May/005693.html
which makes SNMP4J clearly unusable since tables and trees don't work please can you fix this bug ? -----Original Message----- From: FLORENT Philippe Sent: jeudi 11 août 2016 11:40 To: '[email protected]' Cc: 'Frank Fock' Subject: RE: [SNMP4J] java.lang.NullPointerException: Context engine ID must not be null Apprently one need to sublass pdufactory, http://stackoverflow.com/questions/8069244/how-to-add-the-snmpv3-context-name-if-using-tableutils-gettable-in-snmp4j but how do I figure the context id string ? -----Original Message----- From: FLORENT Philippe Sent: jeudi 11 août 2016 11:18 To: '[email protected]' Cc: 'Frank Fock' Subject: RE: [SNMP4J] java.lang.NullPointerException: Context engine ID must not be null I tried a getbulk table, but still that same bloody error final PDUFactory pduFactory = new DefaultPDUFactory(PDU.GETBULK); final TableUtils utils = new TableUtils(snmp, pduFactory); utils.getTable(target, new OID[]{ new OID(oid) }, null, null); Exception in thread "main" java.lang.NullPointerException: Context engine ID must not be null at org.snmp4j.ScopedPDU.setContextEngineID(ScopedPDU.java:69) at org.snmp4j.util.DefaultPDUFactory.applyContextInfoToScopedPDU(DefaultPDUFactory.java:114) at org.snmp4j.util.DefaultPDUFactory.createPDU(DefaultPDUFactory.java:99) at org.snmp4j.util.TableUtils$TableRequest.sendNextChunk(TableUtils.java:365) at org.snmp4j.util.TableUtils.getTable(TableUtils.java:116) at starrwarr.SnmpProber.getNextInt(SnmpProber.java:114) at starrwarr.Machine.<init>(Machine.java:28) at starrwarr.StarrWarr.<init>(StarrWarr.java:58) at starrwarr.StarrWarr.main(StarrWarr.java:114) Java Result: 1 -----Original Message----- From: FLORENT Philippe Sent: jeudi 11 août 2016 11:04 To: 'Frank Fock' Cc: [email protected] Subject: RE: [SNMP4J] java.lang.NullPointerException: Context engine ID must not be null Please help, I am really struggling with this, that OID exists, no matter I use GT or GETNEXT, it return "no such instance" If I look the infos on that oid : .1.3.6.1.2.1.4.20.1.2 Walk: ------- IP-MIB::ipAdEntIfIndex.127.0.0.1 = INTEGER: 1 IP-MIB::ipAdEntIfIndex.192.168.1.128 = INTEGER: 2 snmptranslate -Td : ------------------------- IP-MIB::ipAdEntIfIndex ipAdEntIfIndex OBJECT-TYPE -- FROM IP-MIB SYNTAX INTEGER (1..2147483647) MAX-ACCESS read-only STATUS deprecated DESCRIPTION "The index value which uniquely identifies the interface to which this entry is applicable. The interface identified by a particular value of this index is the same interface as identified by the same value of the IF-MIB's ifIndex." ::= { iso(1) org(3) dod(6) internet(1) mgmt(2) mib-2(1) ip(4) ipAddrTable(20) ipAddrEntry(1) 2 } -----Original Message----- From: Frank Fock [mailto:[email protected]] Sent: jeudi 11 août 2016 10:26 To: FLORENT Philippe Cc: [email protected] Subject: Re: [SNMP4J] java.lang.NullPointerException: Context engine ID must not be null Hi, If walk (which is a GETNEXT in fact) and GET behave differently then most likely you are using the wrong OID (wrong instance identifier). Sometimes the agent is broken too. In your case, you should put the Snmp instance in listen mode to receive answers from the agent. See FAQ for details. Best regards Frank > Am 11.08.2016 um 09:29 schrieb FLORENT Philippe > <[email protected]>: > > Hello everyone, > > > - Doing a get on some OID give me "unkown instance" although they > work with snmpwalk on the command line and GET works with mib explorer > > > > - So I try to implement an snmp walk in snmp4j but I keep getting > that error > > Here is the code > > I tried to use request.getContextEngineID(), but it is also null > > public class SnmpProber > { > private Snmp snmp; > private UserTarget target; > > private byte[] authEngineId0=null; > > public SnmpProber(String ipAddress,String port,String userName,String > authPass,String privPass) > { > try > { > Address targetAddress = > GenericAddress.parse("udp:"+ipAddress+"/"+port); > TransportMapping transport = new DefaultUdpTransportMapping(); > snmp = new Snmp(transport); > USM usm = new USM(SecurityProtocols.getInstance(),new > OctetString(MPv3.createLocalEngineID()), 0); > SecurityModels.getInstance().addSecurityModel(usm); > transport.listen(); > > target = new UserTarget(); > target.setAddress(targetAddress); > target.setRetries(1); > target.setTimeout(5000); > target.setVersion(SnmpConstants.version3); > target.setSecurityLevel(SecurityLevel.AUTH_PRIV); > target.setSecurityName(new OctetString(userName)); > > authEngineId0 = > snmp.discoverAuthoritativeEngineID(target.getAddress(), 5000); > > if(authEngineId0==null) > System.out.println("cannot connect"); > else > { > // add user to the USM > UsmUser usmUser=new UsmUser(new OctetString(userName), > AuthSHA.ID, > new OctetString(authPass), > PrivAES128.ID, > new OctetString(privPass)); > > snmp.getUSM().addUser(new OctetString("v3DefaultUser"), > new OctetString(authEngineId0), > usmUser); > } > } catch (IOException ex) { > Logger.getLogger(SnmpProber.class.getName()).log(Level.SEVERE, > null, ex); > } > } > > public boolean isOk() > { > return authEngineId0!=null; > } > > class WalkCounts { > public int requests; > public int objects; > } > > public List<VariableBinding> walk(String oid) > { > final List<VariableBinding> result; > result = new LinkedList<VariableBinding>(); > ScopedPDU request; > request = new ScopedPDU(); > request.setType(PDU.GETNEXT); > > request.add(new VariableBinding(new OID(oid))); > > request.setNonRepeaters(0); > > OID rootOID = request.get(0).getOid(); > > final WalkCounts counts = new WalkCounts(); > final long startTime = System.currentTimeMillis(); > TreeUtils treeUtils = new TreeUtils(snmp, new DefaultPDUFactory()); > > TreeListener treeListener = new TreeListener() > { > public boolean next(TreeEvent e) { > counts.requests++; > > if (e.getVariableBindings() != null) { > VariableBinding[] vbs = e.getVariableBindings(); > counts.objects += vbs.length; > for (int i = 0; i < vbs.length; i++) { > if (result != null) { > result.add(vbs[i]); > } > System.out.println(vbs[i].toString()); > } > } > return true; > } > > @Override > public void finished(TreeEvent e) { > if ((e.getVariableBindings() != null) > && (e.getVariableBindings().length > 0)) { > next(e); > } > > System.out.println("Total requests sent: "+ > counts.requests); > System.out.println("Total objects received: "+ counts.objects); > System.out.println("Total walk time: "+ > (System.currentTimeMillis() - startTime)+ " milliseconds"); > > if (e.isError()) { > System.err.println("The following error occurred during > walk:"); > System.err.println(e.getErrorMessage()); > } > synchronized (this) { > this.notify(); > } > } > > @Override > public boolean isFinished() { > return true; > } > }; > > synchronized (treeListener) > { > treeUtils.getSubtree(target, rootOID, null, treeListener); > <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< exception here > try > { > treeListener.wait(); > } > catch (InterruptedException ex) > { > System.out.println("Tree retrieval interrupted: "+ex); > Thread.currentThread().interrupt(); > } > } > > return result; > } > > public void Release() > { > try { > snmp.close(); > } catch (IOException ex) { > Logger.getLogger(SnmpProber.class.getName()).log(Level.SEVERE, > null, ex); > } > } > } > > > > > _______________________________________________ > SNMP4J mailing list > [email protected] > https://oosnmp.net/mailman/listinfo/snmp4j _______________________________________________ SNMP4J mailing list [email protected] https://oosnmp.net/mailman/listinfo/snmp4j
