use of org.snmp4j.smi.VariableBinding 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.VariableBinding in project nifi by apache.
the class SNMPGetter method get.
/**
* Construct the PDU to perform the SNMP Get request and returns
* the result in order to create the flow file.
* @return {@link ResponseEvent}
*/
public ResponseEvent get() {
try {
PDU pdu = null;
if (this.target.getVersion() == SnmpConstants.version3) {
pdu = new ScopedPDU();
} else {
pdu = new PDU();
}
pdu.add(new VariableBinding(this.oid));
pdu.setType(PDU.GET);
return this.snmp.get(pdu, this.target);
} catch (IOException e) {
logger.error("Failed to get information from SNMP agent; " + this, e);
throw new ProcessException(e);
}
}
use of org.snmp4j.smi.VariableBinding in project nifi by apache.
the class SetSNMP method addVariables.
/**
* Method to construct {@link VariableBinding} based on {@link FlowFile}
* attributes in order to update the {@link PDU} that is going to be sent to
* the SNMP Agent.
* @param pdu {@link PDU} to be sent
* @param attributes {@link FlowFile} attributes
* @return true if at least one {@link VariableBinding} has been created, false otherwise
*/
private boolean addVariables(PDU pdu, Map<String, String> attributes) {
boolean result = false;
for (Entry<String, String> attributeEntry : attributes.entrySet()) {
if (attributeEntry.getKey().startsWith(SNMPUtils.SNMP_PROP_PREFIX)) {
String[] splits = attributeEntry.getKey().split("\\" + SNMPUtils.SNMP_PROP_DELIMITER);
String snmpPropName = splits[1];
String snmpPropValue = attributeEntry.getValue();
if (SNMPUtils.OID_PATTERN.matcher(snmpPropName).matches()) {
Variable var = null;
if (splits.length == 2) {
// no SMI syntax defined
var = new OctetString(snmpPropValue);
} else {
int smiSyntax = Integer.valueOf(splits[2]);
var = this.stringToVariable(snmpPropValue, smiSyntax);
}
if (var != null) {
VariableBinding varBind = new VariableBinding(new OID(snmpPropName), var);
pdu.add(varBind);
result = true;
}
}
}
}
return result;
}
use of org.snmp4j.smi.VariableBinding in project opennms by OpenNMS.
the class TrapUtils method getTrapData.
/**
* Gets the trap identity.
*
* @param trapNotification the trap notification
* @return the identity
*/
public static TrapData getTrapData(TrapInformation trapNotification) {
if (trapNotification instanceof Snmp4JV1TrapInformation) {
Snmp4JV1TrapInformation info = (Snmp4JV1TrapInformation) trapNotification;
TrapIdentity identity = info.getTrapIdentity();
TrapData data = new TrapData(identity.getEnterpriseId(), identity.getGeneric(), identity.getSpecific());
for (int i = 0; i < info.getPduLength(); i++) {
VariableBinding v = info.getVarBindAt(i);
SnmpValue value = new Snmp4JValue(v.getVariable());
data.addParameter("." + v.getOid().toString(), value.toDisplayString());
}
return data;
} else if (trapNotification instanceof Snmp4JV2TrapInformation) {
Snmp4JV2TrapInformation info = (Snmp4JV2TrapInformation) trapNotification;
TrapIdentity identity = info.getTrapIdentity();
TrapData data = new TrapData(identity.getEnterpriseId(), identity.getGeneric(), identity.getSpecific());
for (int i = 0; i < info.getPduLength(); i++) {
VariableBinding v = info.getVarBindAt(i);
SnmpValue value = new Snmp4JValue(v.getVariable());
data.addParameter("." + v.getOid().toString(), value.toDisplayString());
}
return data;
}
return null;
}
use of org.snmp4j.smi.VariableBinding in project opennms by OpenNMS.
the class MockProxy method processGet.
/**
* @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;
}
Aggregations