org.snmp4j.PDU.getVariableBindings()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(16.1k)|赞(0)|评价(0)|浏览(180)

本文整理了Java中org.snmp4j.PDU.getVariableBindings()方法的一些代码示例,展示了PDU.getVariableBindings()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。PDU.getVariableBindings()方法的具体详情如下:
包路径:org.snmp4j.PDU
类名称:PDU
方法名:getVariableBindings

PDU.getVariableBindings介绍

[英]Gets the variable binding vector.
[中]获取变量绑定向量。

代码示例

代码示例来源:origin: apache/nifi

flowFile = SNMPUtils.addAttribute(SNMPUtils.SNMP_PROP_PREFIX + "error", error, flowFile, processSession);
processSession.transfer(processSession.penalize(flowFile), REL_FAILURE);
this.getLogger().error("Failed while executing SNMP Set [{}] via " + this.targetResource + ". Error = {}", new Object[]{response.getRequest().getVariableBindings(), error});

代码示例来源:origin: org.opendaylight.cardinal/cardinal-impl

/**
 * This method will be called whenever a pdu is received on the given port
 * specified in the listen() method
 */
public synchronized void processPdu(CommandResponderEvent cmdRespEvent) {
    System.out.println("Received PDU...");
    PDU pdu = cmdRespEvent.getPDU();
    if (pdu != null) {
        System.out.println("Trap Type = " + pdu.getType());
        System.out.println("Variables = " + pdu.getVariableBindings());
        }
    }
}

代码示例来源:origin: org.jboss.jbossas/jboss-snmp

private void processPDUv2c(PDU pdu){
   StringBuffer sbuf = new StringBuffer();
   sbuf.append("\nV2 Trap from agent ").append(this.bindAddress.toString());
   sbuf.append(" on port ").append(this.port);
   sbuf.append("\n");
  
   Vector bindings = pdu.getVariableBindings();
   Iterator it = bindings.iterator();
   int counter = 0;
   while (it.hasNext())
   {
     VariableBinding vb = (VariableBinding)it.next();
     if (vb != null) {
       sbuf.append("Varbind[").append(counter++).append("] := ");
       sbuf.append(vb.getOid().toString()).append(" --> ");
       sbuf.append(vb.getVariable().toString()).append("\n");
     }
   }
   sbuf.append("\nLength............. ").append(counter);
   log.debug(sbuf.toString());
}

代码示例来源:origin: org.mobicents.tools.snmp.adaptor/core

private void processPDUv2c(PDU pdu){
   StringBuffer sbuf = new StringBuffer();
   sbuf.append("\nV2 Trap from agent ").append(this.bindAddress.toString());
   sbuf.append(" on port ").append(this.port);
   sbuf.append("\n");
  
   Vector bindings = pdu.getVariableBindings();
   Iterator it = bindings.iterator();
   int counter = 0;
   while (it.hasNext())
   {
     VariableBinding vb = (VariableBinding)it.next();
     if (vb != null) {
       sbuf.append("Varbind[").append(counter++).append("] := ");
       sbuf.append(vb.getOid().toString()).append(" --> ");
       sbuf.append(vb.getVariable().toString()).append("\n");
     }
   }
   sbuf.append("\nLength............. ").append(counter);
   log.debug(sbuf.toString());
}

代码示例来源:origin: org.apache.servicemix/servicemix-snmp

public void convertToJBI(MessageExchange exchange, NormalizedMessage inMsg, PDU request, PDU response)
    throws MessagingException {
    // the output buffer
    StringBuffer sb = new StringBuffer();
    
    // prepare the header
    sb.append(SNMP_TAG_OPEN);
        
    // now loop all variables of the response
    for (Object o : response.getVariableBindings()) {
      VariableBinding b = (VariableBinding)o;

      sb.append(ENTRY_TAG_OPEN);
      sb.append(OID_TAG_OPEN);
      sb.append(b.getOid().toString());
      sb.append(OID_TAG_CLOSE);
      sb.append(VALUE_TAG_OPEN);
      sb.append(b.getVariable().toString());
      sb.append(VALUE_TAG_CLOSE);
      sb.append(ENTRY_TAG_CLOSE);
    }
    
    // prepare the footer
    sb.append(SNMP_TAG_CLOSE);
    
    // now put the buffer to the message content
    inMsg.setContent(new StringSource(sb.toString()));
  }
}

代码示例来源:origin: com.rogueai/snmp2bean

private void parseV2c(PDU pdu) {
  //set the agent address the same to peer address
  valueMap.put(AGENT_ADDR, getPeerAddr());
  // parse header
  Vector vbs = pdu.getVariableBindings();
  for (Iterator it = vbs.iterator(); it.hasNext();) {
    VariableBinding vb = (VariableBinding) it.next();
    OID oid = vb.getOid();
    Variable var = vb.getVariable();
    if (oid.equals(SnmpConstants.snmpTrapOID)) {
      valueMap.put(TRAP_OID, ((OID) var).getValue().toString());
    } else if (oid.equals(SnmpConstants.sysUpTime)) {
      valueMap.put(TIME_STAMP, ((OID) var).getValue());
    } else {
      valueMap.put(oid.toString(), ((OID) var).getValue());
    }
  }
}

代码示例来源:origin: OpenNMS/opennms

private static SnmpValue[] convertResponseToValues(ResponseEvent responseEvent) {
  SnmpValue[] retvalues = new Snmp4JValue[responseEvent.getResponse().getVariableBindings().size()];
  
  for (int i = 0; i < retvalues.length; i++) {
    retvalues[i] = new Snmp4JValue(responseEvent.getResponse().get(i).getVariable());
  }
  
  return retvalues;
}

代码示例来源:origin: org.opennms.lib.snmp/org.opennms.lib.snmp.snmp4j

private SnmpValue[] convertResponseToValues(ResponseEvent responseEvent) {
  SnmpValue[] retvalues = new Snmp4JValue[responseEvent.getResponse().getVariableBindings().size()];
  
  for (int i = 0; i < retvalues.length; i++) {
    retvalues[i] = new Snmp4JValue(responseEvent.getResponse().get(i).getVariable());
  }
  
  return retvalues;
}

代码示例来源:origin: org.opennms.core.snmp/org.opennms.core.snmp.implementations.snmp4j

private static SnmpValue[] convertResponseToValues(ResponseEvent responseEvent) {
  SnmpValue[] retvalues = new Snmp4JValue[responseEvent.getResponse().getVariableBindings().size()];
  
  for (int i = 0; i < retvalues.length; i++) {
    retvalues[i] = new Snmp4JValue(responseEvent.getResponse().get(i).getVariable());
  }
  
  return retvalues;
}

代码示例来源:origin: com.rogueai/snmp2bean

protected void populateProperties(Object object, PDU pdu) throws InstantiationException, IllegalAccessException {
  Field[] propFields = SnmpServiceUtil.getPropFields(object.getClass());
  Vector<? extends VariableBinding> variableBindings = pdu.getVariableBindings();
  for (Field propField : propFields) {
    propField.setAccessible(true);
    MibObjectType mot = propField.getAnnotation(MibObjectType.class);
    OID oid = new OID(mot.oid());
    VariableBinding variableBinding = findVariableBindingByOid(oid, variableBindings);
    if (variableBinding != null) {
      Object value = null;
      
      if (mot.smiType() == SmiType.OID) {
        value = variableBinding.getOid().toString();
      }
      else 
        value = getValueFromVariableAndType(variableBinding.getVariable(), mot);
      
      if (value != null) propField.set(object, value);
    }
  }
}

代码示例来源:origin: org.snmp4j/snmp4j

@Override
 public void onResponse(ResponseEvent event) {
  SnmpUriResponse response = new SnmpUriResponse(SnmpUriResponse.Type.TIMEOUT);
  PDU responsePDU = event.getResponse();
  if (responsePDU != null) {
   if (responsePDU.getErrorStatus() != PDU.noError) {
    response = new SnmpUriResponse(responsePDU.getErrorStatus());
   }
   else {
    response = new SnmpUriResponse(
      Collections.<VariableBinding[]>singletonList(
        responsePDU.getVariableBindings().toArray(new VariableBinding[responsePDU.size()])));
   }
  }
  callback.onResponse(response, url , event.getUserObject());
 }
}

代码示例来源:origin: OpenNMS/opennms

/**
 * TODO: Merge this logic with {@link Snmp4JWalker.Snmp4JResponseListener} #processResponse(PDU response)
 */
private static SnmpValue[] processResponse(Snmp4JAgentConfig agentConfig, ResponseEvent responseEvent) throws IOException {
  SnmpValue[] retvalues = { null };
  if (responseEvent.getResponse() == null) {
    LOG.warn("processResponse: Timeout.  Agent: {}, requestID={}", agentConfig, responseEvent.getRequest().getRequestID());
  } else if (responseEvent.getError() != null) {
    LOG.warn("processResponse: Error during get operation.  Error: {}, requestID={}", responseEvent.getError().getLocalizedMessage(), responseEvent.getError(), responseEvent.getRequest().getRequestID());
  } else if (responseEvent.getResponse().getType() == PDU.REPORT) {
    LOG.warn("processResponse: Error during get operation.  Report returned with varbinds: {}, requestID={}", responseEvent.getResponse().getVariableBindings(), responseEvent.getRequest().getRequestID());
  } else if (responseEvent.getResponse().getVariableBindings().size() < 1) {
    LOG.warn("processResponse: Received PDU with 0 varbinds. Agent: {}, requestID={}", agentConfig, responseEvent.getRequest().getRequestID());
  } else if (responseEvent.getResponse().get(0).getSyntax() == SMIConstants.SYNTAX_NULL) {
    LOG.info("processResponse: Null value returned in varbind: {}. Agent: {}, requestID={}", responseEvent.getResponse().get(0), agentConfig, responseEvent.getRequest().getRequestID());
  } else {
    retvalues = convertResponseToValues(responseEvent);
    LOG.debug("processResponse: SNMP operation successful, value: {}", (Object)retvalues);
  }
  return retvalues;
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.snmp4j

@Override
 public void onResponse(ResponseEvent event) {
  SnmpUriResponse response = new SnmpUriResponse(SnmpUriResponse.Type.TIMEOUT);
  PDU responsePDU = event.getResponse();
  if (responsePDU != null) {
   if (responsePDU.getErrorStatus() != PDU.noError) {
    response = new SnmpUriResponse(responsePDU.getErrorStatus());
   }
   else {
    response = new SnmpUriResponse(
      Collections.<VariableBinding[]>singletonList(
        responsePDU.getVariableBindings().toArray(new VariableBinding[responsePDU.size()])));
   }
  }
  callback.onResponse(response, url , event.getUserObject());
 }
}

代码示例来源:origin: org.opennms.core.snmp/org.opennms.core.snmp.implementations.snmp4j

/**
 * TODO: Merge this logic with {@link Snmp4JWalker.Snmp4JResponseListener} #processResponse(PDU response)
 */
private static SnmpValue[] processResponse(Snmp4JAgentConfig agentConfig, ResponseEvent responseEvent) throws IOException {
  SnmpValue[] retvalues = { null };
  if (responseEvent.getResponse() == null) {
    LOG.warn("processResponse: Timeout.  Agent: {}, requestID={}", agentConfig, responseEvent.getRequest().getRequestID());
  } else if (responseEvent.getError() != null) {
    LOG.warn("processResponse: Error during get operation.  Error: {}, requestID={}", responseEvent.getError().getLocalizedMessage(), responseEvent.getError(), responseEvent.getRequest().getRequestID());
  } else if (responseEvent.getResponse().getType() == PDU.REPORT) {
    LOG.warn("processResponse: Error during get operation.  Report returned with varbinds: {}, requestID={}", responseEvent.getResponse().getVariableBindings(), responseEvent.getRequest().getRequestID());
  } else if (responseEvent.getResponse().getVariableBindings().size() < 1) {
    LOG.warn("processResponse: Received PDU with 0 varbinds. Agent: {}, requestID={}", agentConfig, responseEvent.getRequest().getRequestID());
  } else if (responseEvent.getResponse().get(0).getSyntax() == SMIConstants.SYNTAX_NULL) {
    LOG.info("processResponse: Null value returned in varbind: {}. Agent: {}, requestID={}", responseEvent.getResponse().get(0), agentConfig, responseEvent.getRequest().getRequestID());
  } else {
    retvalues = convertResponseToValues(responseEvent);
    LOG.debug("processResponse: SNMP operation successful, value: {}", (Object)retvalues);
  }
  return retvalues;
}

代码示例来源:origin: OpenNMS/opennms

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;
}

代码示例来源:origin: org.opennms.core.snmp/org.opennms.core.snmp.implementations.snmp4j

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;
}

代码示例来源:origin: org.opennms.lib.snmp/org.opennms.lib.snmp.snmp4j

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 Exception("This is a bogus exception so we can get a stack backtrace");
      log().error("PDU to prepare has object values but not the same number as there are OIDs.  There are " + oids.length + " OIDs and " + values.length + " object values.", 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 Exception("This is a bogus exception so we can get a stack backtrace");
    log().error("Prepared PDU does not have as many variable bindings as there are OIDs.  There are " + oids.length + " OIDs and " + pdu.getVariableBindings() + " variable bindings.", e);
    return null;
  }
  
  return pdu;
}

代码示例来源:origin: org.opennms.lib.snmp/org.opennms.lib.snmp.snmp4j

/**
 * TODO: Merge this logic with {@link Snmp4JWalker.Snmp4JResponseListener#processResponse(PDU response)}
 */
private SnmpValue[] processResponse(Snmp4JAgentConfig agentConfig, ResponseEvent responseEvent) throws IOException {
  SnmpValue[] retvalues = { null };
  if (responseEvent.getResponse() == null) {
    log().warn("send: Timeout.  Agent: "+agentConfig);
  } else if (responseEvent.getError() != null) {
    log().warn("send: Error during get operation.  Error: "+responseEvent.getError().getLocalizedMessage(), responseEvent.getError());
  } else if (responseEvent.getResponse().getType() == PDU.REPORT) {
    log().warn("send: Error during get operation.  Report returned with varbinds: "+responseEvent.getResponse().getVariableBindings());
  } else if (responseEvent.getResponse().getVariableBindings().size() < 1) {
    log().warn("send: Received PDU with 0 varbinds.");
  } else if (responseEvent.getResponse().get(0).getSyntax() == SMIConstants.SYNTAX_NULL) {
    log().info("send: Null value returned in varbind: " + responseEvent.getResponse().get(0));
  } else {
    retvalues = convertResponseToValues(responseEvent);
    if (log().isDebugEnabled()) {
      log().debug("send: Snmp operation successful. Value: "+Arrays.toString(retvalues));
    }
  }
  return retvalues;
}

代码示例来源:origin: OpenNMS/opennms

/**
 * @param request
 * @return
 */
private PDU processGet(PDU request) {
  PDU response = request;
  response.setErrorIndex(0);
  response.setErrorStatus(0);
  response.setType(PDU.RESPONSE);
  
  Vector<? extends VariableBinding> varBinds = response.getVariableBindings();
  for(int i = 0; i < varBinds.size(); i++) {
    VariableBinding varBind = varBinds.get(i);
    VariableBinding nextVarBind = m_agent.get(varBind.getOid());
    if (nextVarBind == null) {
      if (response instanceof PDUv1) {
        if (response.getErrorIndex() == 0) {
          response.setErrorIndex(i+1);
          response.setErrorStatus(PDU.noSuchName);
        } 
      } else {
        varBind.setVariable(Null.endOfMibView);
      }
    } else {
      response.set(i, nextVarBind);
    }
  }
  
  return response;
}

代码示例来源:origin: OpenNMS/opennms

/**
 * @param request
 * @return
 */
private PDU processGetNext(PDU request) {
  PDU response = request;
  response.setErrorIndex(0);
  response.setErrorStatus(0);
  response.setType(PDU.RESPONSE);
  
  Vector<? extends VariableBinding> varBinds = response.getVariableBindings();
  for(int i = 0; i < varBinds.size(); i++) {
    VariableBinding varBind = varBinds.get(i);
    VariableBinding nextVarBind = m_agent.getNext(varBind.getOid());
    if (nextVarBind == null) {
      if (response instanceof PDUv1) {
        if (response.getErrorIndex() == 0) {
          response.setErrorIndex(i+1);
          response.setErrorStatus(PDU.noSuchName);
        } 
      } else {
        varBind.setVariable(Null.endOfMibView);
      }
    } else {
      response.set(i, nextVarBind);
    }
  }
  
  return response;
}

相关文章