本文整理汇总了Java中org.snmp4j.PDU.getVariableBindings方法的典型用法代码示例。如果您正苦于以下问题:Java PDU.getVariableBindings方法的具体用法?Java PDU.getVariableBindings怎么用?Java PDU.getVariableBindings使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.snmp4j.PDU
的用法示例。
在下文中一共展示了PDU.getVariableBindings方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.snmp4j.PDU; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException, InterruptedException {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setVersion(SnmpConstants.version2c);
target.setAddress(new UdpAddress("23.23.52.11/161"));
target.setTimeout(3000); //3s
target.setRetries(1);
PDU pdu = new PDU();
pdu.setType(PDU.GETBULK);
pdu.setMaxRepetitions(1);
pdu.setNonRepeaters(0);
VariableBinding[] array = {new VariableBinding(new OID("1.3.6.1.4.1.2000.1.2.5.1.3")),
new VariableBinding(new OID("1.3.6.1.4.1.2000.1.3.1.1.7")),
new VariableBinding(new OID("1.3.6.1.4.1.2000.1.3.1.1.10")),
new VariableBinding(new OID("1.3.6.1.4.1.2000.1.2.5.1.19"))};
pdu.addAll(array);
//pdu.add(new VariableBinding(new OID("1.3.6.1.4.1.2000.1.2.5.1.3")));
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
if (response == null) {
System.out.println("TimeOut...");
} else {
if (response.getErrorStatus() == PDU.noError) {
Vector<? extends VariableBinding> vbs = response.getVariableBindings();
for (VariableBinding vb : vbs) {
System.out.println(vb.getVariable().toString());
}
} else {
System.out.println("Error:" + response.getErrorStatusText());
}
}
}
示例2: testSnmpGet
import org.snmp4j.PDU; //导入方法依赖的package包/类
@Ignore ("Need Mock SSH Server")
@Test
public void testSnmpGet() throws InterruptedException {
out.setMinimumExpectedMessageCount(1);
out.await(10,TimeUnit.SECONDS);
out.assertIsSatisfied();
CamelContext context = context();
List<Exchange> exchanges = out.getExchanges();
for (Exchange exchange : exchanges) {
Message message = exchange.getOut();
SnmpMessage snmpMessage = message.getBody(SnmpMessage.class);
LOG.info("body: ",snmpMessage);
assertNotNull("Body is null",snmpMessage);
PDU pdu = snmpMessage.getSnmpMessage();
for (Object o : pdu.getVariableBindings()) {
VariableBinding b = (VariableBinding)o;
LOG.info("oid: {}, value: {}",b.getOid().toString(),b.getVariable().toString());
}
}
}
示例3: convertToMap
import org.snmp4j.PDU; //导入方法依赖的package包/类
public static Map<OID, Variable> convertToMap(PDU pdu){
Map<OID, Variable> retVal = null;
if(pdu != null){
retVal = new HashMap<OID, Variable>();
Vector<? extends VariableBinding> vect = pdu.getVariableBindings();
for(VariableBinding curr : vect){
retVal.put(curr.getOid(), curr.getVariable());
}
}
return retVal;
}
示例4: extractVarBinds
import org.snmp4j.PDU; //导入方法依赖的package包/类
static public Vector<VariableBinding> extractVarBinds(Exchange exchange) {
PDU pdu = SnmpExchangeUtils.extractPDU(exchange);
Vector<VariableBinding> varBinds = new Vector<VariableBinding>();
for (VariableBinding var : pdu.getVariableBindings()) {
varBinds.addElement(var);
}
LOG.debug("Extracting {} variable bindings from PDU",varBinds.size());
return varBinds;
}
示例5: get
import org.snmp4j.PDU; //导入方法依赖的package包/类
public Map<OID, Variable> get(Collection<OID> oids) throws IOException {
Map<OID, Variable> map = new LinkedHashMap<OID, Variable>();
PDU request = createPDU();
request.setType(PDU.GET);
request.setVariableBindings(wrap(oids));
boolean debug = log.isDebugEnabled();
if (debug)
log.debug("request " + request);
ResponseEvent re = snmp.send(request, this.target);
PDU response = re.getResponse();
if (debug)
log.debug("response " + response);
if (response == null) {
throw new NoResponseException("no response querying " + oids);
}
if (re.getError() != null)
throw new IOException(re.getError());
Vector<? extends VariableBinding> vbs = response.getVariableBindings();
if (vbs.size() == 1) {
String error = Errors.get(vbs.get(0).getOid());
if (error != null)
throw new IOException("got SNMP error " + error);
}
for (VariableBinding vb : vbs) {
OID oid = vb.getOid();
map.put(oid, vb.getVariable());
}
return map;
}
示例6: processGet
import org.snmp4j.PDU; //导入方法依赖的package包/类
/**
* @param request
* @return
*/
@SuppressWarnings("unchecked")
private PDU processGet(PDU request) {
PDU response = request;
response.setErrorIndex(0);
response.setErrorStatus(0);
response.setType(PDU.RESPONSE);
Vector<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;
}
示例7: processGetNext
import org.snmp4j.PDU; //导入方法依赖的package包/类
/**
* @param request
* @return
*/
@SuppressWarnings("unchecked")
private PDU processGetNext(PDU request) {
PDU response = request;
response.setErrorIndex(0);
response.setErrorStatus(0);
response.setType(PDU.RESPONSE);
Vector<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;
}
示例8: toString
import org.snmp4j.PDU; //导入方法依赖的package包/类
/**
* Converts the given snmp pdu to a String body.
*
* @param pdu the snmp pdu
* @return the text content
*/
@Converter
public static String toString(PDU pdu) {
// the output buffer
StringBuilder sb = new StringBuilder();
// prepare the header
if (pdu.getType() == PDU.V1TRAP) {
sb.append("<" + SNMP_TAG + " messageType=\"v1\">");
} else {
sb.append(SNMP_TAG_OPEN);
}
// Extract SNMPv1 specific variables
if (pdu.getType() == PDU.V1TRAP) {
PDUv1 v1pdu = (PDUv1) pdu;
entryAppend(sb, "enterprise", v1pdu.getEnterprise().toString());
entryAppend(sb, "agent-addr", v1pdu.getAgentAddress().toString());
entryAppend(sb, "generic-trap", Integer.toString(v1pdu.getGenericTrap()));
entryAppend(sb, "specific-trap", Integer.toString(v1pdu.getSpecificTrap()));
entryAppend(sb, "time-stamp", Long.toString(v1pdu.getTimestamp()));
}
// now loop all variables of the response
for (Object o : pdu.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(StringHelper.xmlEncode(b.getVariable().toString()));
sb.append(VALUE_TAG_CLOSE);
sb.append(ENTRY_TAG_CLOSE);
}
// prepare the footer
sb.append(SNMP_TAG_CLOSE);
return sb.toString();
}
示例9: processV2Trap
import org.snmp4j.PDU; //导入方法依赖的package包/类
/**
* Converts an {@link SnmpMessage}/{@link PDU} into a {@link RawEvent}
*
* @param message - Contains a {@link SnmpMessage}
* @param pdu - Contains a {@link PDU} with the actual trap contents
* @param event - {@link RawEvent} to be populated.
*/
public void processV2Trap(SnmpMessage message, PDU pdu, RawEvent event) {
String specificTrap = "";
String hostname = getPeerAddress(message);
// CREATED_AT
event.setCreatedAt(new Date());
// FINGERPRINT_FIELDS
event.addFingerprintField(HOSTNAME_PROPERTY_NAME);
// ORGANIZATION_ID - TBD: Override based on content of the PDU?
// PROPERTIES
event.addProperty(HOSTNAME_PROPERTY_NAME, hostname);
event.addProperty("error_status", pdu.getErrorStatusText());
// Get the variable bindings from the trap and create properties in the event
Vector<? extends VariableBinding> varBinds = pdu.getVariableBindings();
for (VariableBinding var : varBinds) {
OID oid = var.getOid();
if (oid.startsWith(SnmpConstants.snmpTraps) ||
oid.startsWith(SnmpConstants.snmpTrapOID)) {
specificTrap = var.toValueString();
event.addProperty("trap",specificTrap);
// MESSAGE
event.setMessage(var.toValueString());
}
else {
event.addProperty(var.getOid().toString(),var.toValueString());
event.addFingerprintField(var.getOid().toString());
}
mapVarbindToEvent(event,var);
}
// RECEIVED_AT - Default to value set by Boundary
// SENDER
event.getSender().setRef(SENDER_REF).setType(SENDER_TYPE);
// SEVERITY - TBD: set the severity based on content of the trap
event.setSeverity(Severity.WARN);
// SOURCE
event.getSource().setRef(hostname).setType("host");
//STATUS - TBD: Set status based on severity??
event.setStatus(Status.OPEN);
// TAGS
event.addTag(hostname);
// TITLE
event.setTitle(specificTrap + " trap received from " + hostname);
}
示例10: process
import org.snmp4j.PDU; //导入方法依赖的package包/类
@Override
public Status process() throws EventDeliveryException {
Status status = null;
counterGroup = new CounterGroup();
try {
// This try clause includes whatever Channel operations you want to do
Event event = new SimpleEvent();
Map <String, String> headers = new HashMap<String, String>();
StringBuilder stringBuilder = new StringBuilder();
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
DateFormat dateFormat = new SimpleDateFormat("MM dd HH:mm:ss yyyy");
Date date = new Date();
stringBuilder.append(date + ",");
stringBuilder.append(bindAddress + ",");
if (response == null) {
logger.info("TimeOut...");
} else {
if (response.getErrorStatus() == PDU.noError) {
Vector<? extends VariableBinding> vbs = response.getVariableBindings();
for (VariableBinding vb : vbs) {
//System.out.println(vb.getVariable().toString());
stringBuilder.append(vb.getVariable().toString() + ",");
}
} else {
logger.info("Error:" + response.getErrorStatusText());
}
}
String messageString = stringBuilder.toString();
// trick: remove the last comma
messageString = messageString.replaceAll(",$", "");
byte[] message = messageString.getBytes();
headers.put("timestamp", String.valueOf(System.currentTimeMillis()));
logger.info("Message: {}", messageString);
event.setBody(message);
event.setHeaders(headers);
// Store the Event into this Source's associated Channel(s)
getChannelProcessor().processEvent(event);
counterGroup.incrementAndGet("events.success");
Thread.sleep(delayQuery*1000);
status = Status.READY;
} catch (ChannelException|IOException|InterruptedException ex) {
counterGroup.incrementAndGet("events.dropped");
logger.error("Error writting to channel", ex);
// Log exception, handle individual exceptions as needed
status = Status.BACKOFF;
// re-throw all Errors
//if (t instanceof Error) {
// throw (Error);
//}
}
return status;
}