use of org.snmp4j.smi.Variable in project openhab1-addons by openhab.
the class SnmpBinding method internalReceiveCommand.
/**
* @{inheritDoc
*/
@Override
public void internalReceiveCommand(String itemName, Command command) {
logger.debug("SNMP receive command {} from {}", itemName, command);
SnmpBindingProvider providerCmd = null;
for (SnmpBindingProvider provider : this.providers) {
OID oid = provider.getOID(itemName, command);
if (oid != null) {
providerCmd = provider;
break;
}
}
if (providerCmd == null) {
logger.warn("No match for binding provider [itemName={}, command={}]", itemName, command);
return;
}
logger.debug("SNMP command for {} to {}", itemName, providerCmd.toString());
// Set up the target
CommunityTarget target = new CommunityTarget();
target.setCommunity(providerCmd.getCommunity(itemName, command));
target.setAddress(providerCmd.getAddress(itemName, command));
target.setRetries(retries);
target.setTimeout(timeout);
target.setVersion(providerCmd.getSnmpVersion(itemName, command));
Variable var = providerCmd.getValue(itemName, command);
OID oid = providerCmd.getOID(itemName, command);
VariableBinding varBind = new VariableBinding(oid, var);
// Create the PDU
PDU pdu = new PDU();
pdu.add(varBind);
pdu.setType(PDU.SET);
pdu.setRequestID(new Integer32(1));
logger.debug("SNMP: Send CMD PDU {} {}", providerCmd.getAddress(itemName, command), pdu);
if (snmp == null) {
logger.error("SNMP: snmp not initialised - aborting request");
} else {
sendPDU(target, pdu);
}
}
use of org.snmp4j.smi.Variable in project camel by apache.
the class TrapTest method testSendReceiveTraps.
@Test
public void testSendReceiveTraps() throws Exception {
// Create a trap PDU
PDU trap = new PDU();
trap.setType(PDU.TRAP);
OID oid = new OID("1.2.3.4.5");
trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
// put your uptime here
trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));
trap.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("System Description")));
//Add Payload
Variable var = new OctetString("some string");
trap.add(new VariableBinding(oid, var));
// Send it
LOG.info("Sending pdu " + trap);
Endpoint endpoint = context.getEndpoint("direct:snmptrap");
Exchange exchange = endpoint.createExchange();
exchange.getIn().setBody(trap);
Producer producer = endpoint.createProducer();
producer.process(exchange);
synchronized (this) {
Thread.sleep(1000);
}
// If all goes right it should come here
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.assertIsSatisfied();
List<Exchange> exchanges = mock.getExchanges();
SnmpMessage msg = (SnmpMessage) exchanges.get(0).getIn();
PDU receivedTrap = msg.getSnmpMessage();
Assert.assertEquals(trap, receivedTrap);
if (LOG.isInfoEnabled()) {
LOG.info("Received SNMP TRAP:");
Vector<? extends VariableBinding> variableBindings = receivedTrap.getVariableBindings();
for (VariableBinding vb : variableBindings) {
LOG.info(" " + vb.toString());
}
}
}
use of org.snmp4j.smi.Variable in project opennms by OpenNMS.
the class Snmp4JV2TrapBuilder method addVarBind.
@Override
public void addVarBind(SnmpObjId name, SnmpValue value) {
OID oid = new OID(name.getIds());
Variable val = ((Snmp4JValue) value).getVariable();
m_pdu.add(new VariableBinding(oid, val));
}
use of org.snmp4j.smi.Variable in project opennms by OpenNMS.
the class PropertiesBackedManagedObject method getVariable.
private void getVariable(final SubRequest request, final OID oid) {
try {
final Variable value = findValueForOID(oid);
final VariableBinding vb = request.getVariableBinding();
vb.setOid(oid);
vb.setVariable(value == null ? Null.noSuchObject : value);
request.completed();
} catch (SnmpErrorStatusException e) {
request.setErrorStatus(e.getErrorStatus());
request.completed();
}
}
use of org.snmp4j.smi.Variable in project opennms by OpenNMS.
the class MockSnmpAgent method addCommunities.
/** {@inheritDoc} */
@Override
protected void addCommunities(SnmpCommunityMIB communityMIB) {
Variable[] com2sec = new Variable[] { // community name
new OctetString("public"), // security name
new OctetString("public"), // local engine ID
getAgent().getContextEngineID(), // default context name
new OctetString(), // transport tag
new OctetString(), // storage type
new Integer32(StorageType.nonVolatile), // row status
new Integer32(RowStatus.active) };
SnmpCommunityEntryRow row = communityMIB.getSnmpCommunityEntry().createRow(new OctetString("public2public").toSubIndex(true), com2sec);
communityMIB.getSnmpCommunityEntry().addRow(row);
}
Aggregations