Search in sources :

Example 36 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project camel by apache.

the class SnmpProducer method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    this.targetAddress = GenericAddress.parse(this.endpoint.getAddress());
    LOG.debug("targetAddress: {}", targetAddress);
    this.usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
    SecurityModels.getInstance().addSecurityModel(this.usm);
    // setting up target
    this.target = new CommunityTarget();
    this.target.setCommunity(new OctetString(endpoint.getSnmpCommunity()));
    this.target.setAddress(this.targetAddress);
    this.target.setRetries(this.endpoint.getRetries());
    this.target.setTimeout(this.endpoint.getTimeout());
    this.target.setVersion(this.endpoint.getSnmpVersion());
    this.pdu = new PDU();
    for (OID oid : this.endpoint.getOids()) {
        this.pdu.add(new VariableBinding(oid));
    }
    this.pdu.setErrorIndex(0);
    this.pdu.setErrorStatus(0);
    this.pdu.setMaxRepetitions(0);
    this.pdu.setType(PDU.GET);
}
Also used : OctetString(org.snmp4j.smi.OctetString) PDU(org.snmp4j.PDU) OID(org.snmp4j.smi.OID) CommunityTarget(org.snmp4j.CommunityTarget) VariableBinding(org.snmp4j.smi.VariableBinding) USM(org.snmp4j.security.USM)

Example 37 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project camel by apache.

the class SnmpConverters method toString.

/**
     * Converts the given snmp pdu to a String body.
     *
     * @param pdu       the snmp pdu
     * @return  the text content
     */
@Converter
public static String toString(PDU pdu) {
    // the output buffer
    StringBuilder sb = new StringBuilder();
    // prepare the header
    if (pdu.getType() == PDU.V1TRAP) {
        sb.append("<" + SNMP_TAG + " messageType=\"v1\">");
    } else {
        sb.append(SNMP_TAG_OPEN);
    }
    // Extract SNMPv1 specific variables
    if (pdu.getType() == PDU.V1TRAP) {
        PDUv1 v1pdu = (PDUv1) pdu;
        entryAppend(sb, "enterprise", v1pdu.getEnterprise().toString());
        entryAppend(sb, "agent-addr", v1pdu.getAgentAddress().toString());
        entryAppend(sb, "generic-trap", Integer.toString(v1pdu.getGenericTrap()));
        entryAppend(sb, "specific-trap", Integer.toString(v1pdu.getSpecificTrap()));
        entryAppend(sb, "time-stamp", Long.toString(v1pdu.getTimestamp()));
    }
    // now loop all variables of the response
    for (Object o : pdu.getVariableBindings()) {
        VariableBinding b = (VariableBinding) o;
        sb.append(ENTRY_TAG_OPEN);
        sb.append(OID_TAG_OPEN);
        sb.append(b.getOid().toString());
        sb.append(OID_TAG_CLOSE);
        sb.append(VALUE_TAG_OPEN);
        sb.append(StringHelper.xmlEncode(b.getVariable().toString()));
        sb.append(VALUE_TAG_CLOSE);
        sb.append(ENTRY_TAG_CLOSE);
    }
    // prepare the footer
    sb.append(SNMP_TAG_CLOSE);
    return sb.toString();
}
Also used : PDUv1(org.snmp4j.PDUv1) VariableBinding(org.snmp4j.smi.VariableBinding) Converter(org.apache.camel.Converter)

Example 38 with VariableBinding

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

the class SnmpBinding method execute.

/**
     * @{inheritDoc
     */
@Override
public void execute() {
    for (SnmpBindingProvider provider : providers) {
        for (String itemName : provider.getInBindingItemNames()) {
            int refreshInterval = provider.getRefreshInterval(itemName);
            Long lastUpdateTimeStamp = lastUpdateMap.get(itemName);
            if (lastUpdateTimeStamp == null) {
                lastUpdateTimeStamp = 0L;
            }
            long age = System.currentTimeMillis() - lastUpdateTimeStamp;
            boolean needsUpdate;
            if (refreshInterval == 0) {
                needsUpdate = false;
            } else {
                needsUpdate = age >= refreshInterval;
            }
            if (needsUpdate) {
                logger.debug("Item '{}' is about to be refreshed", itemName);
                // Set up the target
                CommunityTarget target = new CommunityTarget();
                target.setCommunity(provider.getCommunity(itemName));
                target.setAddress(provider.getAddress(itemName));
                target.setRetries(retries);
                target.setTimeout(timeout);
                target.setVersion(provider.getSnmpVersion(itemName));
                // Create the PDU
                PDU pdu = new PDU();
                pdu.add(new VariableBinding(provider.getOID(itemName)));
                pdu.setType(PDU.GET);
                logger.debug("SNMP: Send PDU {} {}", provider.getAddress(itemName), pdu);
                if (snmp == null) {
                    logger.error("SNMP: snmp not initialised - aborting request");
                } else {
                    sendPDU(target, pdu);
                }
                lastUpdateMap.put(itemName, System.currentTimeMillis());
            }
        }
    }
}
Also used : PDU(org.snmp4j.PDU) SnmpBindingProvider(org.openhab.binding.snmp.SnmpBindingProvider) OctetString(org.snmp4j.smi.OctetString) CommunityTarget(org.snmp4j.CommunityTarget) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 39 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project opennms by OpenNMS.

the class Snmp4JStrategy method buildPdu.

protected PDU buildPdu(Snmp4JAgentConfig agentConfig, int pduType, SnmpObjId[] oids, SnmpValue[] values) {
    PDU pdu = agentConfig.createPdu(pduType);
    if (values == null) {
        for (SnmpObjId oid : oids) {
            pdu.add(new VariableBinding(new OID(oid.toString())));
        }
    } else {
        // TODO should this throw an exception?  This situation is fairly bogus and probably signifies a coding error.
        if (oids.length != values.length) {
            Exception e = new SnmpException("PDU values do not match OIDs");
            LOG.error("PDU to prepare has object values but not the same number as there are OIDs.  There are {} OIDs and {} object values.", oids.length, values.length, e);
            return null;
        }
        for (int i = 0; i < oids.length; i++) {
            pdu.add(new VariableBinding(new OID(oids[i].toString()), new Snmp4JValue(values[i].getType(), values[i].getBytes()).getVariable()));
        }
    }
    // TODO should this throw an exception?  This situation is fairly bogus.
    if (pdu.getVariableBindings().size() != oids.length) {
        Exception e = new SnmpException("PDU bindings do not match OIDs");
        LOG.error("Prepared PDU does not have as many variable bindings as there are OIDs.  There are {} OIDs and {} variable bindings.", oids.length, pdu.getVariableBindings(), e);
        return null;
    }
    return pdu;
}
Also used : PDU(org.snmp4j.PDU) ScopedPDU(org.snmp4j.ScopedPDU) SnmpException(org.opennms.netmgt.snmp.SnmpException) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) OID(org.snmp4j.smi.OID) VariableBinding(org.snmp4j.smi.VariableBinding) SnmpException(org.opennms.netmgt.snmp.SnmpException) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 40 with VariableBinding

use of org.snmp4j.smi.VariableBinding in project opennms by OpenNMS.

the class MockAgentTest method testGetSysName.

public void testGetSysName() throws IOException {
    Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
    snmp.listen();
    Address addr = new UdpAddress(InetAddress.getLocalHost(), 9161);
    // Address addr = new UdpAddress(InetAddressUtils.addr("192.168.0.100"), 161);
    Target target = new CommunityTarget(addr, new OctetString("public"));
    target.setVersion(SnmpConstants.version1);
    target.setTimeout(3000);
    target.setRetries(3);
    PDUv1 getRequest = new PDUv1();
    getRequest.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0")));
    ResponseEvent e = snmp.get(getRequest, target);
    PDU response = e.getResponse();
    assertEquals(new OctetString("mockhost"), response.get(0).getVariable());
}
Also used : OctetString(org.snmp4j.smi.OctetString) PDU(org.snmp4j.PDU) UdpAddress(org.snmp4j.smi.UdpAddress) Target(org.snmp4j.Target) CommunityTarget(org.snmp4j.CommunityTarget) Address(org.snmp4j.smi.Address) UdpAddress(org.snmp4j.smi.UdpAddress) InetAddress(java.net.InetAddress) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) ResponseEvent(org.snmp4j.event.ResponseEvent) PDUv1(org.snmp4j.PDUv1) OID(org.snmp4j.smi.OID) CommunityTarget(org.snmp4j.CommunityTarget) 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