Search in sources :

Example 1 with Variable

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);
    }
}
Also used : PDU(org.snmp4j.PDU) Integer32(org.snmp4j.smi.Integer32) Variable(org.snmp4j.smi.Variable) SnmpBindingProvider(org.openhab.binding.snmp.SnmpBindingProvider) OID(org.snmp4j.smi.OID) CommunityTarget(org.snmp4j.CommunityTarget) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 2 with Variable

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());
        }
    }
}
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 3 with Variable

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

Example 4 with Variable

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();
    }
}
Also used : DynamicVariable(org.opennms.mock.snmp.responder.DynamicVariable) Variable(org.snmp4j.smi.Variable) SnmpErrorStatusException(org.opennms.mock.snmp.responder.SnmpErrorStatusException) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 5 with Variable

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);
}
Also used : OctetString(org.snmp4j.smi.OctetString) SnmpCommunityEntryRow(org.snmp4j.agent.mo.snmp.SnmpCommunityMIB.SnmpCommunityEntryRow) Integer32(org.snmp4j.smi.Integer32) Variable(org.snmp4j.smi.Variable)

Aggregations

Variable (org.snmp4j.smi.Variable)10 OID (org.snmp4j.smi.OID)7 VariableBinding (org.snmp4j.smi.VariableBinding)7 OctetString (org.snmp4j.smi.OctetString)4 DynamicVariable (org.opennms.mock.snmp.responder.DynamicVariable)3 Integer32 (org.snmp4j.smi.Integer32)3 SnmpBindingProvider (org.openhab.binding.snmp.SnmpBindingProvider)2 SnmpErrorStatusException (org.opennms.mock.snmp.responder.SnmpErrorStatusException)2 PDU (org.snmp4j.PDU)2 TimeTicks (org.snmp4j.smi.TimeTicks)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 Endpoint (org.apache.camel.Endpoint)1 Exchange (org.apache.camel.Exchange)1 Producer (org.apache.camel.Producer)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 Test (org.junit.Test)1 NumberItem (org.openhab.core.library.items.NumberItem)1 TransformationException (org.openhab.core.transform.TransformationException)1 State (org.openhab.core.types.State)1 CommunityTarget (org.snmp4j.CommunityTarget)1