@Override public void onResponse(final ResponseEvent responseEvent) { try { future.complete(processResponse(agentConfig, responseEvent)); } catch (final Exception e) { future.completeExceptionally(new SnmpException(e)); } finally { // Close the tracker using a separate thread // This allows the SnmpWalker to clean up properly instead // of interrupting execution as it's executing the callback REAPER_EXECUTOR.submit(new Runnable() { @Override public void run() { closeQuietly(mySession); } }); } } });
@Override public void onResponse(final ResponseEvent responseEvent) { try { future.complete(processResponse(agentConfig, responseEvent)); } catch (final Exception e) { future.completeExceptionally(new SnmpException(e)); } finally { // Close the tracker using a separate thread // This allows the SnmpWalker to clean up properly instead // of interrupting execution as it's executing the callback REAPER_EXECUTOR.submit(new Runnable() { @Override public void run() { closeQuietly(mySession); } }); } } });
@Override protected void sendNextPdu(WalkerPduBuilder pduBuilder) throws SnmpException { JoeSnmpPduBuilder joePduBuilder = (JoeSnmpPduBuilder)pduBuilder; if (m_session == null) try { m_session = new SnmpSession(m_peer); } catch (final SocketException e) { throw new SnmpException("Failed to create session to peer " + m_peer.toString(), e); } LOG.debug("Sending tracker pdu of size {}", joePduBuilder.getPdu().getLength()); m_session.send(joePduBuilder.getPdu(), m_handler); }
private ChildTrackerPduBuilder getChildBuilder(int zeroBasedIndex) throws SnmpException { int canonicalIndex = getCanonicalIndex(zeroBasedIndex); for (ChildTrackerPduBuilder childBuilder : m_childPduBuilders) { if (childBuilder.isNonRepeater(canonicalIndex) || childBuilder.isRepeater(canonicalIndex)) { return childBuilder; } } throw new SnmpException("Unable to find childBuilder for index "+zeroBasedIndex); }
@Override protected void sendNextPdu(WalkerPduBuilder pduBuilder) throws SnmpException { JoeSnmpPduBuilder joePduBuilder = (JoeSnmpPduBuilder)pduBuilder; if (m_session == null) try { m_session = new SnmpSession(m_peer); } catch (final SocketException e) { throw new SnmpException("Failed to create session to peer " + m_peer.toString(), e); } LOG.debug("Sending tracker pdu of size {}", joePduBuilder.getPdu().getLength()); m_session.send(joePduBuilder.getPdu(), m_handler); }
@Override public ResponseProcessor buildNextPdu(PduBuilder pduBuilder) throws SnmpException { if (pduBuilder.getMaxVarsPerPdu() < 1) { throw new SnmpException("maxVarsPerPdu < 1"); throw new SnmpException("Unable to handle tooBigError for next oid request after "+m_last); } else if (status == ErrorStatus.GEN_ERR) { reportGenErr("Received genErr requesting next oid after "+m_last+". Marking column is finished.");
public int getChildIndex(int canonicalIndex) throws SnmpException { if (isNonRepeater(canonicalIndex)) { return canonicalIndex - getNonRepeaterStartIndex(); } if (isRepeater(canonicalIndex)) { return canonicalIndex - getRepeaterStartIndex() + getNonRepeaters(); } throw new SnmpException("index out of range for tracker "+this); } }
@Override public void validate() throws SnmpException { // // verify the type // if (m_pdu.typeId() != (byte) (SnmpPduPacket.V2TRAP)) { // if not V2 trap, do nothing throw new SnmpException("Received not SNMPv2 Trap from host " + getTrapAddress() + "PDU Type = " + m_pdu.getCommand()); } LOG.debug("V2 trap numVars or pdu length: {}", getPduLength()); if (getPduLength() < 2) // check number of varbinds { throw new SnmpException("V2 trap from " + getTrapAddress() + " IGNORED due to not having the required varbinds. Have " + getPduLength() + ", needed 2"); } // The first varbind has the sysUpTime // Modify the sysUpTime varbind to add the trailing 0 if it is // missing // The second varbind has the snmpTrapOID // Confirm that these two are present // String varBindName0 = m_pdu.getVarBindAt(0).getName().toString(); String varBindName1 = m_pdu.getVarBindAt(1).getName().toString(); if (varBindName0.equals(V2TrapInformation.EXTREME_SNMP_SYSUPTIME_OID)) { LOG.info("V2 trap from {} has been corrected due to the sysUptime.0 varbind not having been sent with a trailing 0.\n\tVarbinds received are : {} and {}", getTrapAddress(), varBindName0, varBindName1); varBindName0 = V2TrapInformation.SNMP_SYSUPTIME_OID; } if ((!(varBindName0.equals(V2TrapInformation.SNMP_SYSUPTIME_OID))) || (!(varBindName1.equals(V2TrapInformation.SNMP_TRAP_OID)))) { throw new SnmpException("V2 trap from " + getTrapAddress() + " IGNORED due to not having the required varbinds.\n\tThe first varbind must be sysUpTime.0 and the second snmpTrapOID.0\n\tVarbinds received are : " + varBindName0 + " and " + varBindName1); } }
@Override protected void sendNextPdu(WalkerPduBuilder pduBuilder) throws SnmpException { Snmp4JPduBuilder snmp4JPduBuilder = (Snmp4JPduBuilder)pduBuilder; try { if (m_session == null) { m_session = m_agentConfig.createSnmpSession(); Snmp4JStrategy.trackSession(m_session); m_session.listen(); } } catch (final IOException e) { close(); throw new SnmpException(e); } LOG.debug("Sending tracker pdu of size {}", snmp4JPduBuilder.getPdu().size()); try { m_session.send(snmp4JPduBuilder.getPdu(), m_tgt, null, m_listener); } catch (final IOException e) { LOG.debug("Failed to send pdu of size {}", snmp4JPduBuilder.getPdu().size(), e); close(); throw new SnmpException(e); } }
@Override public void validate() throws SnmpException { // // verify the type // if (m_pdu.typeId() != (byte) (SnmpPduPacket.V2TRAP)) { // if not V2 trap, do nothing throw new SnmpException("Received not SNMPv2 Trap from host " + getTrapAddress() + "PDU Type = " + m_pdu.getCommand()); } LOG.debug("V2 trap numVars or pdu length: {}", getPduLength()); if (getPduLength() < 2) // check number of varbinds { throw new SnmpException("V2 trap from " + getTrapAddress() + " IGNORED due to not having the required varbinds. Have " + getPduLength() + ", needed 2"); } // The first varbind has the sysUpTime // Modify the sysUpTime varbind to add the trailing 0 if it is // missing // The second varbind has the snmpTrapOID // Confirm that these two are present // String varBindName0 = m_pdu.getVarBindAt(0).getName().toString(); String varBindName1 = m_pdu.getVarBindAt(1).getName().toString(); if (varBindName0.equals(V2TrapInformation.EXTREME_SNMP_SYSUPTIME_OID)) { LOG.info("V2 trap from {} has been corrected due to the sysUptime.0 varbind not having been sent with a trailing 0.\n\tVarbinds received are : {} and {}", getTrapAddress(), varBindName0, varBindName1); varBindName0 = V2TrapInformation.SNMP_SYSUPTIME_OID; } if ((!(varBindName0.equals(V2TrapInformation.SNMP_SYSUPTIME_OID))) || (!(varBindName1.equals(V2TrapInformation.SNMP_TRAP_OID)))) { throw new SnmpException("V2 trap from " + getTrapAddress() + " IGNORED due to not having the required varbinds.\n\tThe first varbind must be sysUpTime.0 and the second snmpTrapOID.0\n\tVarbinds received are : " + varBindName0 + " and " + varBindName1); } }
@Override protected void sendNextPdu(WalkerPduBuilder pduBuilder) throws SnmpException { Snmp4JPduBuilder snmp4JPduBuilder = (Snmp4JPduBuilder)pduBuilder; try { if (m_session == null) { m_session = m_agentConfig.createSnmpSession(); Snmp4JStrategy.trackSession(m_session); m_session.listen(); } } catch (final IOException e) { close(); throw new SnmpException(e); } LOG.debug("Sending tracker pdu of size {}", snmp4JPduBuilder.getPdu().size()); try { m_session.send(snmp4JPduBuilder.getPdu(), m_tgt, null, m_listener); } catch (final IOException e) { LOG.debug("Failed to send pdu of size {}", snmp4JPduBuilder.getPdu().size(), e); close(); throw new SnmpException(e); } }
protected PDU buildPdu(Snmp4JAgentConfig agentConfig, int pduType, SnmpObjId[] oids, SnmpValue[] values) { PDU pdu = agentConfig.createPdu(pduType); if (values == null) { for (SnmpObjId oid : oids) { pdu.add(new VariableBinding(new OID(oid.toString()))); } } else { // TODO should this throw an exception? This situation is fairly bogus and probably signifies a coding error. if (oids.length != values.length) { Exception e = new SnmpException("PDU values do not match OIDs"); LOG.error("PDU to prepare has object values but not the same number as there are OIDs. There are {} OIDs and {} object values.", oids.length, values.length, e); return null; } for (int i = 0; i < oids.length; i++) { pdu.add(new VariableBinding(new OID(oids[i].toString()), new Snmp4JValue(values[i].getType(), values[i].getBytes()).getVariable())); } } // TODO should this throw an exception? This situation is fairly bogus. if (pdu.getVariableBindings().size() != oids.length) { Exception e = new SnmpException("PDU bindings do not match OIDs"); LOG.error("Prepared PDU does not have as many variable bindings as there are OIDs. There are {} OIDs and {} variable bindings.", oids.length,pdu.getVariableBindings(), e); return null; } return pdu; }
protected SnmpAgentConfig buildAgentConfig(String address, int port, int securityLevel, String securityName, String authPassPhrase, String authProtocol, String privPassPhrase, String privProtocol, PDU pdu) throws UnknownHostException, Exception { if (! (pdu instanceof ScopedPDU)) { throw new SnmpException("PDU is not a ScopedPDU (this should not happen)"); } SnmpAgentConfig config = new SnmpAgentConfig(); config.setAddress(InetAddress.getByName(address)); config.setPort(port); config.setVersion(SnmpAgentConfig.VERSION3); config.setSecurityLevel(securityLevel); config.setSecurityName(securityName); config.setAuthPassPhrase(authPassPhrase); config.setAuthProtocol(authProtocol); config.setPrivPassPhrase(privPassPhrase); config.setPrivProtocol(privProtocol); return config; }
int pduType = getPdu().getType(); if (pduType != PDU.TRAP && pduType != PDU.INFORM) { throw new SnmpException("Received not SNMPv2 Trap|Inform from host " + getTrapAddress() + " PDU Type = " + PDU.getTypeString(getPdu().getType())); throw new SnmpException("V2 "+m_pduTypeString+" from " + getTrapAddress() + " IGNORED due to not having the required varbinds. Have " + getPduLength() + ", needed at least 2"); throw new SnmpException("V2 "+m_pduTypeString+" from " + getTrapAddress() + " IGNORED due to not having the required varbinds.\n\tThe first varbind must be sysUpTime.0 and the second snmpTrapOID.0\n\tVarbinds received are : " + varBindName0 + " and " + varBindName1);
int pduType = getPdu().getType(); if (pduType != PDU.TRAP && pduType != PDU.INFORM) { throw new SnmpException("Received not SNMPv2 Trap|Inform from host " + getTrapAddress() + " PDU Type = " + PDU.getTypeString(getPdu().getType())); throw new SnmpException("V2 "+m_pduTypeString+" from " + getTrapAddress() + " IGNORED due to not having the required varbinds. Have " + getPduLength() + ", needed at least 2"); throw new SnmpException("V2 "+m_pduTypeString+" from " + getTrapAddress() + " IGNORED due to not having the required varbinds.\n\tThe first varbind must be sysUpTime.0 and the second snmpTrapOID.0\n\tVarbinds received are : " + varBindName0 + " and " + varBindName1);
protected PDU buildPdu(Snmp4JAgentConfig agentConfig, int pduType, SnmpObjId[] oids, SnmpValue[] values) { PDU pdu = agentConfig.createPdu(pduType); if (values == null) { for (SnmpObjId oid : oids) { pdu.add(new VariableBinding(new OID(oid.toString()))); } } else { // TODO should this throw an exception? This situation is fairly bogus and probably signifies a coding error. if (oids.length != values.length) { Exception e = new SnmpException("PDU values do not match OIDs"); LOG.error("PDU to prepare has object values but not the same number as there are OIDs. There are {} OIDs and {} object values.", oids.length, values.length, e); return null; } for (int i = 0; i < oids.length; i++) { pdu.add(new VariableBinding(new OID(oids[i].toString()), new Snmp4JValue(values[i].getType(), values[i].getBytes()).getVariable())); } } // TODO should this throw an exception? This situation is fairly bogus. if (pdu.getVariableBindings().size() != oids.length) { Exception e = new SnmpException("PDU bindings do not match OIDs"); LOG.error("Prepared PDU does not have as many variable bindings as there are OIDs. There are {} OIDs and {} variable bindings.", oids.length,pdu.getVariableBindings(), e); return null; } return pdu; }
protected SnmpAgentConfig buildAgentConfig(String address, int port, int securityLevel, String securityName, String authPassPhrase, String authProtocol, String privPassPhrase, String privProtocol, PDU pdu) throws UnknownHostException, Exception { if (! (pdu instanceof ScopedPDU)) { throw new SnmpException("PDU is not a ScopedPDU (this should not happen)"); } SnmpAgentConfig config = new SnmpAgentConfig(); config.setAddress(InetAddress.getByName(address)); config.setPort(port); config.setVersion(SnmpAgentConfig.VERSION3); config.setSecurityLevel(securityLevel); config.setSecurityName(securityName); config.setAuthPassPhrase(authPassPhrase); config.setAuthProtocol(authProtocol); config.setPrivPassPhrase(privPassPhrase); config.setPrivProtocol(privProtocol); return config; }
@Override public boolean processErrors(int errorStatus, int errorIndex) throws SnmpException { //LOG.trace("processErrors: errorStatus={}, errorIndex={}", errorStatus, errorIndex); final ErrorStatus status = ErrorStatus.fromStatus(errorStatus); // handle special cases first if (status == ErrorStatus.TOO_BIG) { int maxVarsPerPdu = m_pduBuilder.getMaxVarsPerPdu(); if (maxVarsPerPdu <= 1) { throw new SnmpException("Unable to handle tooBigError when maxVarsPerPdu = "+maxVarsPerPdu); } m_pduBuilder.setMaxVarsPerPdu(maxVarsPerPdu/2); m_tracker.reportTooBigErr("Reducing maxVarsPerPDU for this request."); return true; } else if (status.isFatal()) { final ErrorStatusException ex = new ErrorStatusException(status); m_tracker.reportFatalErr(ex); throw ex; } else { return processChildError(errorStatus, errorIndex); } } }
throw new SnmpException("Unable to handle tooBigError for oid request "+m_oid.decrement()); } else if (status == ErrorStatus.GEN_ERR) { reportGenErr("Received genErr requesting oid "+m_oid.decrement()+". Marking column as finished.");
throw new SnmpException("Unable to handle tooBigError for next oid request after "+m_last); } else if (status == ErrorStatus.GEN_ERR) { reportGenErr("Received genErr requesting next oid after "+m_last+". Marking column is finished.");