use of org.snmp4j.smi.VariableBinding in project camel by apache.
the class SnmpProducer method doStart.
@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();
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);
}
use of org.snmp4j.smi.VariableBinding in project camel by apache.
the class SnmpConverters method toString.
/**
* 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();
}
use of org.snmp4j.smi.VariableBinding in project openhab1-addons by openhab.
the class SnmpBinding method execute.
/**
* @{inheritDoc
*/
@Override
public void execute() {
for (SnmpBindingProvider provider : providers) {
for (String itemName : provider.getInBindingItemNames()) {
int refreshInterval = provider.getRefreshInterval(itemName);
Long lastUpdateTimeStamp = lastUpdateMap.get(itemName);
if (lastUpdateTimeStamp == null) {
lastUpdateTimeStamp = 0L;
}
long age = System.currentTimeMillis() - lastUpdateTimeStamp;
boolean needsUpdate;
if (refreshInterval == 0) {
needsUpdate = false;
} else {
needsUpdate = age >= refreshInterval;
}
if (needsUpdate) {
logger.debug("Item '{}' is about to be refreshed", itemName);
// Set up the target
CommunityTarget target = new CommunityTarget();
target.setCommunity(provider.getCommunity(itemName));
target.setAddress(provider.getAddress(itemName));
target.setRetries(retries);
target.setTimeout(timeout);
target.setVersion(provider.getSnmpVersion(itemName));
// Create the PDU
PDU pdu = new PDU();
pdu.add(new VariableBinding(provider.getOID(itemName)));
pdu.setType(PDU.GET);
logger.debug("SNMP: Send PDU {} {}", provider.getAddress(itemName), pdu);
if (snmp == null) {
logger.error("SNMP: snmp not initialised - aborting request");
} else {
sendPDU(target, pdu);
}
lastUpdateMap.put(itemName, System.currentTimeMillis());
}
}
}
}
use of org.snmp4j.smi.VariableBinding in project opennms by OpenNMS.
the class Snmp4JStrategy method buildPdu.
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;
}
use of org.snmp4j.smi.VariableBinding in project opennms by OpenNMS.
the class MockAgentTest method testGetSysName.
public void testGetSysName() throws IOException {
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
snmp.listen();
Address addr = new UdpAddress(InetAddress.getLocalHost(), 9161);
// Address addr = new UdpAddress(InetAddressUtils.addr("192.168.0.100"), 161);
Target target = new CommunityTarget(addr, new OctetString("public"));
target.setVersion(SnmpConstants.version1);
target.setTimeout(3000);
target.setRetries(3);
PDUv1 getRequest = new PDUv1();
getRequest.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0")));
ResponseEvent e = snmp.get(getRequest, target);
PDU response = e.getResponse();
assertEquals(new OctetString("mockhost"), response.get(0).getVariable());
}
Aggregations