Search in sources :

Example 11 with VariableBinding

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());
        }
    }
}
Also used : PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) Variable(org.snmp4j.smi.Variable) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) TimeTicks(org.snmp4j.smi.TimeTicks) OID(org.snmp4j.smi.OID) Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Producer(org.apache.camel.Producer) VariableBinding(org.snmp4j.smi.VariableBinding) Test(org.junit.Test)

Example 12 with VariableBinding

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);
    }
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) ProcessException(org.apache.nifi.processor.exception.ProcessException) ScopedPDU(org.snmp4j.ScopedPDU) IOException(java.io.IOException) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 13 with VariableBinding

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;
}
Also used : OctetString(org.snmp4j.smi.OctetString) AbstractVariable(org.snmp4j.smi.AbstractVariable) Variable(org.snmp4j.smi.Variable) AssignableFromString(org.snmp4j.smi.AssignableFromString) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 14 with VariableBinding

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;
}
Also used : Snmp4JV2TrapInformation(org.opennms.netmgt.snmp.snmp4j.Snmp4JTrapNotifier.Snmp4JV2TrapInformation) TrapIdentity(org.opennms.netmgt.snmp.TrapIdentity) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) Snmp4JV1TrapInformation(org.opennms.netmgt.snmp.snmp4j.Snmp4JTrapNotifier.Snmp4JV1TrapInformation) VariableBinding(org.snmp4j.smi.VariableBinding) TrapData(org.opennms.netmgt.alarmd.northbounder.snmptrap.TrapData)

Example 15 with VariableBinding

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;
}
Also used : PDU(org.snmp4j.PDU) PDUv1(org.snmp4j.PDUv1) VariableBinding(org.snmp4j.smi.VariableBinding)

Aggregations

VariableBinding (org.snmp4j.smi.VariableBinding)52 OID (org.snmp4j.smi.OID)37 OctetString (org.snmp4j.smi.OctetString)31 PDU (org.snmp4j.PDU)30 ScopedPDU (org.snmp4j.ScopedPDU)17 Test (org.junit.Test)11 PDUv1 (org.snmp4j.PDUv1)10 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)8 ResponseEvent (org.snmp4j.event.ResponseEvent)8 Variable (org.snmp4j.smi.Variable)8 ArrayList (java.util.ArrayList)7 IpAddress (org.snmp4j.smi.IpAddress)7 TimeTicks (org.snmp4j.smi.TimeTicks)6 DefaultPDUFactory (org.snmp4j.util.DefaultPDUFactory)6 TableEvent (org.snmp4j.util.TableEvent)6 TableUtils (org.snmp4j.util.TableUtils)6 TrapInformation (org.opennms.netmgt.snmp.TrapInformation)5 Map (java.util.Map)4