You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "Claus Ibsen (Jira)" <ji...@apache.org> on 2019/09/04 18:20:00 UTC

[jira] [Commented] (CAMEL-13936) SNMP Component support ‘snmp walk’

    [ https://issues.apache.org/jira/browse/CAMEL-13936?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16922737#comment-16922737 ] 

Claus Ibsen commented on CAMEL-13936:
-------------------------------------

I wonder if you can submit the patch as a github PR ?

> SNMP Component support ‘snmp walk’
> ----------------------------------
>
>                 Key: CAMEL-13936
>                 URL: https://issues.apache.org/jira/browse/CAMEL-13936
>             Project: Camel
>          Issue Type: New Feature
>          Components: camel-snmp
>    Affects Versions: 3.0.0.M4
>         Environment: SnmpEndpoint.java>>
>  public Producer createProducer() throws Exception {
>  if (this.type == SnmpActionType.TRAP) {
>  return new SnmpTrapProducer(this);
>  } else {
> // return new SnmpProducer(this);
>  return new SnmpProducer(this, this.type); // add the snmp walk
>  }
>  }
>  
> SnmpProducer.java>>
> public class SnmpProducer extends DefaultProducer {
>  
>  private SnmpEndpoint endpoint;
>  
>  private Address targetAddress;
>  private USM usm;
>  private CommunityTarget target;
>  private PDU pdu;
>  private SnmpActionType actionType;
>  
>  public SnmpProducer(SnmpEndpoint endpoint) {
>  super(endpoint);
>  this.endpoint = endpoint;
>  }
>  public SnmpProducer(SnmpEndpoint endpoint, SnmpActionType actionType) {
>  super(endpoint);
>  this.endpoint = endpoint;
>  this.actionType = actionType;
>  }
>  
>  @Override
>  protected void doStart() throws Exception {
>  super.doStart();
>  this.targetAddress = GenericAddress.parse(this.endpoint.getAddress());
>  log.debug("targetAddress: {}", targetAddress);
>  this.usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
>  SecurityModels.getInstance().addSecurityModel(this.usm);
>  
>  // setting up target
>  this.target = new CommunityTarget();
>  this.target.setCommunity(new OctetString(endpoint.getSnmpCommunity()));
>  this.target.setAddress(this.targetAddress);
>  this.target.setRetries(this.endpoint.getRetries());
>  this.target.setTimeout(this.endpoint.getTimeout());
>  this.target.setVersion(this.endpoint.getSnmpVersion());
>  this.pdu = new PDU();
>  // only POLL do
>  if (this.actionType == SnmpActionType.POLL) {
>  for (OID oid : this.endpoint.getOids()) {
>  this.pdu.add(new VariableBinding(oid));
>  }
>  }
>  this.pdu.setErrorIndex(0);
>  this.pdu.setErrorStatus(0);
>  this.pdu.setMaxRepetitions(0);
> // this.pdu.setType(PDU.GET);
>  // add the snmp walk
>  if (this.actionType == SnmpActionType.POLL)
>  this.pdu.setType(PDU.GET);
>  else if (this.actionType == SnmpActionType.GET_NEXT)
>  this.pdu.setType(PDU.GETNEXT);
>  else
>  this.pdu.setType(PDU.GET);
>  }
>  
>  @Override
>  protected void doStop() throws Exception {
>  super.doStop();
>  try {
>  SecurityModels.getInstance().removeSecurityModel(new Integer32(this.usm.getID()));
>  } finally {
>  this.targetAddress = null;
>  this.usm = null;
>  this.target = null;
>  this.pdu = null;
>  }
>  }
>  
>  @Override
>  public void process(final Exchange exchange) throws Exception {
>  // load connection data only if the endpoint is enabled
>  Snmp snmp = null;
>  TransportMapping<? extends Address> transport = null;
>  try {
>  log.debug("Starting SNMP producer on {}", this.endpoint.getAddress());
>  
>  // either tcp or udp
>  if ("tcp".equals(this.endpoint.getProtocol())) {
>  transport = new DefaultTcpTransportMapping();
>  } else if ("udp".equals(this.endpoint.getProtocol())) {
>  transport = new DefaultUdpTransportMapping();
>  } else {
>  throw new IllegalArgumentException("Unknown protocol: " + this.endpoint.getProtocol());
>  }
>  
>  snmp = new Snmp(transport);
>  
>  log.debug("Snmp: i am sending");
>  
>  snmp.listen();
>  if (this.actionType == SnmpActionType.GET_NEXT) {
>  // snmp walk
>  List<SnmpMessage> smLst = new ArrayList<>();
>  for (OID oid : this.endpoint.getOids()) {
>  this.pdu.clear();
>  this.pdu.add(new VariableBinding(oid));
>  boolean matched = true;
>  while (matched) {
>  ResponseEvent responseEvent = snmp.send(this.pdu, this.target);
>  if (responseEvent == null || responseEvent.getResponse() == null) {
>  break;
>  }
>  PDU response = responseEvent.getResponse();
>  String nextOid = null;
>  Vector<? extends VariableBinding> variableBindings = response.getVariableBindings();
>  for (int i = 0; i < variableBindings.size(); i++) {
>  VariableBinding variableBinding = variableBindings.elementAt(i);
>  Variable variable = variableBinding.getVariable();
>  nextOid = variableBinding.getOid().toDottedString();
>  if (!nextOid.startsWith(oid.toDottedString())) {
>  matched = false;
>  break;
>  }
>  }
>  if (!matched) {
>  break;
>  }
>  this.pdu.clear();
>  pdu.add(new VariableBinding(new OID(nextOid)));
>  smLst.add(new SnmpMessage(getEndpoint().getCamelContext(), response));
>  }
>  }
>  exchange.getIn().setBody(smLst);
>  } else {
>  // snmp get
>  ResponseEvent responseEvent = snmp.send(this.pdu, this.target);
>  log.debug("Snmp: sended");
>  if (responseEvent.getResponse() != null) {
>  exchange.getIn().setBody(new SnmpMessage(getEndpoint().getCamelContext(), responseEvent.getResponse()));
>  } else {
>  throw new TimeoutException("SNMP Producer Timeout");
>  }
>  }
>  } finally {
>  try {
>  transport.close(); 
>  } catch (Exception e) \{ }
>  try {
>  snmp.close(); 
>  } catch (Exception e) \{ }
>  }
>  } //end process
> }
>            Reporter: wwei
>            Priority: Minor
>             Fix For: Future
>
>
> The current version, the SNMP Component can not support 'snmp walk'.
> Although the source code has appear 'GET_NEXT' in SnmpActionType.java, but no found in processor.
> Now, i add the 'snmp walk' and in my application it run successfully.
> So, if it need add to the next camel version, please check.
> Thanks!



--
This message was sent by Atlassian Jira
(v8.3.2#803003)