Search in sources :

Example 1 with CommunityTarget

use of org.snmp4j.CommunityTarget 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 CommunityTarget

use of org.snmp4j.CommunityTarget in project mysql_perf_analyzer by yahoo.

the class SNMPClient method getTarget.

/**
	* This method returns a Target, which contains information about
	* where the data should be fetched and how.
	* @return
	*/
private Target getTarget() {
    if ("3".equals(this.version))
        return getTargetV3();
    Address targetAddress = GenericAddress.parse(address);
    CommunityTarget target = new CommunityTarget();
    //logger.info("snmp version "+this.version+", community: "+this.community);
    if (this.community == null || this.community.isEmpty())
        target.setCommunity(new OctetString("public"));
    else
        target.setCommunity(new OctetString(this.community));
    target.setAddress(targetAddress);
    target.setRetries(2);
    target.setTimeout(5000);
    target.setVersion(this.getVersionInt());
    return target;
}
Also used : OctetString(org.snmp4j.smi.OctetString) Address(org.snmp4j.smi.Address) GenericAddress(org.snmp4j.smi.GenericAddress) CommunityTarget(org.snmp4j.CommunityTarget)

Example 3 with CommunityTarget

use of org.snmp4j.CommunityTarget in project opennms by OpenNMS.

the class Snmp4JUtils method convertPduToBytes.

/**
	 * @param address
	 * @param port
	 * @param community
	 * @param pdu
	 * 
	 * @return Byte array representing the {@link PDU} in either SNMPv1 or SNMPv2 
	 * format, depending on the type of the {@link PDU} object.
	 */
public static byte[] convertPduToBytes(InetAddress address, int port, String community, PDU pdu) throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<byte[]> bytes = new AtomicReference<>();
    // IP address is optional when using the DummyTransport because
    // all requests are sent to the {@link DummyTransportResponder}
    final DummyTransport<IpAddress> transport = new DummyTransport<IpAddress>(null);
    final AbstractTransportMapping<IpAddress> responder = transport.getResponder(null);
    // Add a DummyTransportResponder listener that will receive the raw bytes of the PDU
    responder.addTransportListener(new TransportListener() {

        @Override
        public void processMessage(TransportMapping transport, Address address, ByteBuffer byteBuffer, TransportStateReference state) {
            byteBuffer.rewind();
            final byte[] byteArray = new byte[byteBuffer.remaining()];
            byteBuffer.get(byteArray);
            bytes.set(byteArray);
            byteBuffer.rewind();
            latch.countDown();
        }
    });
    // Create our own MessageDispatcher since we don't need to do all
    // of the crypto operations necessary to initialize SNMPv3 which is slow
    MessageDispatcher dispatcher = new MessageDispatcherImpl();
    dispatcher.addMessageProcessingModel(new MPv1());
    dispatcher.addMessageProcessingModel(new MPv2c());
    Snmp snmp = new Snmp(dispatcher, responder);
    snmp.listen();
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString(community));
    if (pdu instanceof PDUv1) {
        target.setVersion(SnmpConstants.version1);
    } else {
        target.setVersion(SnmpConstants.version2c);
    }
    target.setAddress(Snmp4JAgentConfig.convertAddress(address, port));
    snmp.send(pdu, target, transport);
    latch.await();
    return bytes.get();
}
Also used : OctetString(org.snmp4j.smi.OctetString) Address(org.snmp4j.smi.Address) IpAddress(org.snmp4j.smi.IpAddress) InetAddress(java.net.InetAddress) TransportMapping(org.snmp4j.TransportMapping) AbstractTransportMapping(org.snmp4j.transport.AbstractTransportMapping) MPv2c(org.snmp4j.mp.MPv2c) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageDispatcherImpl(org.snmp4j.MessageDispatcherImpl) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) TransportListener(org.snmp4j.transport.TransportListener) MessageDispatcher(org.snmp4j.MessageDispatcher) TransportStateReference(org.snmp4j.TransportStateReference) Snmp(org.snmp4j.Snmp) IpAddress(org.snmp4j.smi.IpAddress) MPv1(org.snmp4j.mp.MPv1) DummyTransport(org.snmp4j.transport.DummyTransport) PDUv1(org.snmp4j.PDUv1) CommunityTarget(org.snmp4j.CommunityTarget)

Example 4 with CommunityTarget

use of org.snmp4j.CommunityTarget in project opennms by OpenNMS.

the class Snmp4JAgentConfig method createCommunityTarget.

private Target createCommunityTarget() {
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(getReadCommunity());
    return target;
}
Also used : CommunityTarget(org.snmp4j.CommunityTarget)

Example 5 with CommunityTarget

use of org.snmp4j.CommunityTarget in project opennms by OpenNMS.

the class MockSnmpAgentIT method sendRequestV1V2.

private PDU sendRequestV1V2(PDU pdu, int version) {
    PDU response = null;
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString("public"));
    target.setAddress(new UdpAddress(InetAddressUtils.addr("127.0.0.1"), m_agent.getPort()));
    target.setVersion(version);
    TransportMapping<UdpAddress> transport = null;
    Snmp snmp = null;
    try {
        transport = new DefaultUdpTransportMapping();
        snmp = new Snmp(transport);
        transport.listen();
        ResponseEvent e = snmp.send(pdu, target);
        response = e.getResponse();
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        if (snmp != null) {
            try {
                snmp.close();
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
        if (transport != null) {
            try {
                transport.close();
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
    }
    return response;
}
Also used : ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) UdpAddress(org.snmp4j.smi.UdpAddress) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) ResponseEvent(org.snmp4j.event.ResponseEvent) IOException(java.io.IOException) CommunityTarget(org.snmp4j.CommunityTarget)

Aggregations

CommunityTarget (org.snmp4j.CommunityTarget)14 OctetString (org.snmp4j.smi.OctetString)12 PDU (org.snmp4j.PDU)8 Snmp (org.snmp4j.Snmp)8 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)7 UdpAddress (org.snmp4j.smi.UdpAddress)6 OID (org.snmp4j.smi.OID)5 ScopedPDU (org.snmp4j.ScopedPDU)4 ResponseEvent (org.snmp4j.event.ResponseEvent)4 Address (org.snmp4j.smi.Address)4 VariableBinding (org.snmp4j.smi.VariableBinding)4 InetAddress (java.net.InetAddress)3 USM (org.snmp4j.security.USM)3 IOException (java.io.IOException)2 SnmpBindingProvider (org.openhab.binding.snmp.SnmpBindingProvider)2 PDUv1 (org.snmp4j.PDUv1)2 Target (org.snmp4j.Target)2 ByteBuffer (java.nio.ByteBuffer)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1