Search in sources :

Example 1 with Address

use of org.snmp4j.smi.Address in project openhab1-addons by openhab.

the class SnmpGenericBindingProvider method parseAddress.

private Address parseAddress(String s) throws BindingConfigParseException {
    String addressString = s.contains("/") ? s : s + "/161";
    Address address = GenericAddress.parse("udp:" + addressString);
    if (address == null) {
        throw new BindingConfigParseException(getBindingType() + " binding configuration address is invalid: " + s);
    }
    return address;
}
Also used : Address(org.snmp4j.smi.Address) GenericAddress(org.snmp4j.smi.GenericAddress) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) OctetString(org.snmp4j.smi.OctetString)

Example 2 with Address

use of org.snmp4j.smi.Address in project openhab1-addons by openhab.

the class SnmpBinding method processPdu.

/**
     * Will be called whenever a {@link PDU} is received on the given port
     * specified in the listen() method. It extracts a {@link Variable}
     * according to the configured OID prefix and sends its value to the event
     * bus.
     */
@Override
public void processPdu(CommandResponderEvent event) {
    Address addr = event.getPeerAddress();
    if (addr == null) {
        return;
    }
    String s = addr.toString().split("/")[0];
    if (s == null) {
        logger.error("TRAP: failed to translate address {}", addr);
        dispatchPdu(addr, event.getPDU());
    } else {
        // Need to change the port to 161, which is what the bindings are configured for since
        // at least some SNMP devices send traps from a random port number. Otherwise the trap
        // won't be found as the address check will fail. It feels like there should be a better
        // way to do this!!!
        Address address = GenericAddress.parse("udp:" + s + "/161");
        dispatchPdu(address, event.getPDU());
    }
}
Also used : Address(org.snmp4j.smi.Address) UdpAddress(org.snmp4j.smi.UdpAddress) GenericAddress(org.snmp4j.smi.GenericAddress) OctetString(org.snmp4j.smi.OctetString)

Example 3 with Address

use of org.snmp4j.smi.Address in project mysql_perf_analyzer by yahoo.

the class SNMPClient method getTargetV3.

private Target getTargetV3() {
    //logger.info("Use SNMP v3, "+this.privacyprotocol +"="+this.password+", "+this.privacyprotocol+"="+this.privacypassphrase);
    OID authOID = AuthMD5.ID;
    if ("SHA".equals(this.authprotocol))
        authOID = AuthSHA.ID;
    OID privOID = PrivDES.ID;
    if (this.privacyprotocol == null || this.privacyprotocol.isEmpty())
        privOID = null;
    UsmUser user = new UsmUser(new OctetString(this.username), //auth
    authOID, //auth
    new OctetString(this.password), privOID, //enc
    this.privacypassphrase != null ? new OctetString(this.privacypassphrase) : null);
    snmp.getUSM().addUser(new OctetString(this.username), user);
    Address targetAddress = GenericAddress.parse(address);
    UserTarget target = new UserTarget();
    target.setAddress(targetAddress);
    target.setRetries(2);
    target.setTimeout(1500);
    target.setVersion(this.getVersionInt());
    if (privOID != null)
        target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
    else
        target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);
    target.setSecurityName(new OctetString(this.username));
    return target;
}
Also used : OctetString(org.snmp4j.smi.OctetString) Address(org.snmp4j.smi.Address) GenericAddress(org.snmp4j.smi.GenericAddress) OID(org.snmp4j.smi.OID) UserTarget(org.snmp4j.UserTarget) UsmUser(org.snmp4j.security.UsmUser)

Example 4 with Address

use of org.snmp4j.smi.Address 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 5 with Address

use of org.snmp4j.smi.Address 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)

Aggregations

Address (org.snmp4j.smi.Address)7 OctetString (org.snmp4j.smi.OctetString)7 CommunityTarget (org.snmp4j.CommunityTarget)4 GenericAddress (org.snmp4j.smi.GenericAddress)4 InetAddress (java.net.InetAddress)3 Snmp (org.snmp4j.Snmp)3 OID (org.snmp4j.smi.OID)3 UdpAddress (org.snmp4j.smi.UdpAddress)3 PDUv1 (org.snmp4j.PDUv1)2 Target (org.snmp4j.Target)2 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)2 ByteBuffer (java.nio.ByteBuffer)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)1 MessageDispatcher (org.snmp4j.MessageDispatcher)1 MessageDispatcherImpl (org.snmp4j.MessageDispatcherImpl)1 PDU (org.snmp4j.PDU)1 TransportMapping (org.snmp4j.TransportMapping)1