org.snmp4j.CommunityTarget Java Examples

The following examples show how to use org.snmp4j.CommunityTarget. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SNMPClient.java    From mysql_perf_analyzer with Apache License 2.0 6 votes vote down vote up
/**
* 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;
}
 
Example #2
Source File: SNMPUtilsTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Method to create community target
 * @param community community name
 * @param address address
 * @param version SNMP version
 * @return community target
 */
protected static CommunityTarget createCommTarget(String community, String address, int version) {
    CommunityTarget target = new CommunityTarget();
    target.setVersion(version);
    target.setCommunity(new OctetString(community));
    target.setAddress(new UdpAddress(address));
    target.setRetries(0);
    target.setTimeout(500);
    return target;
}
 
Example #3
Source File: SnmpSimpleGet.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
/**
 * Create communityTarget.
 *
 * @param ip
 * @param community
 * @return CommunityTarget
 */
private static CommunityTarget createDefault(String ip, String community) {
    Address address = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip
            + "/" + DEFAULT_PORT);
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString(community));
    target.setAddress(address);
    target.setVersion(DEFAULT_VERSION);
    target.setTimeout(DEFAULT_TIMEOUT); // milliseconds
    target.setRetries(DEFAULT_RETRY);
    return target;
}
 
Example #4
Source File: SnmpSimpleSet.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
/**
 * Create communityTarget.
 *
 * @param ip
 * @param community
 * @return CommunityTarget
 */
private static CommunityTarget createDefault(String ip, String community) {

    Address address = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + ip + "/" + DEFAULT_PORT);
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString(community));
    target.setAddress(address);
    target.setVersion(DEFAULT_VERSION);
    target.setRetries(DEFAULT_RETRY);
    target.setTimeout(DEFAULT_TIMEOUT); // milliseconds
    return target;
}
 
Example #5
Source File: SnmpBinding.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
private void sendPDU(CommunityTarget target, PDU pdu) {
    try {
        snmp.send(pdu, target, null, this);
    } catch (IOException e) {
        logger.error("Error sending PDU", e);
    }
}
 
Example #6
Source File: SNMPUtils.java    From ingestion with Apache License 2.0 5 votes vote down vote up
public static void sendTrapV2(String port) throws IOException {
    PDU trap = new PDU();
    trap.setType(PDU.TRAP);

    OID oid = new OID("1.2.3.4.5");
    trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
    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));

    // Specify receiver
    Address targetaddress = new UdpAddress("127.0.0.1/" + port);
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString("public"));
    target.setVersion(SnmpConstants.version2c);
    target.setAddress(targetaddress);

    // Send
    Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
    snmp.send(trap, target, null, null);

    snmp.close();
}
 
Example #7
Source File: PolatisSnmpUtility.java    From onos with Apache License 2.0 5 votes vote down vote up
private static CommunityTarget getTarget(DriverHandler handler) {
    SnmpDevice device = getDevice(handler);
    Address targetAddress = GenericAddress.parse(device.getProtocol() +
                                                 ":" + device.getSnmpHost() +
                                                 "/" + device.getSnmpPort());
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString(getDevice(handler).getCommunity()));
    target.setAddress(targetAddress);
    target.setRetries(3);
    target.setTimeout(1000L * 3L);
    target.setVersion(SnmpConstants.version2c);
    target.setMaxSizeRequestPDU(MAX_SIZE_RESPONSE_PDU);
    return target;
}
 
Example #8
Source File: SnmpHelper.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public SnmpHelper(String address, String community) {
    _target = new CommunityTarget();
    _target.setCommunity(new OctetString(community));
    _target.setVersion(SnmpConstants.version2c);
    _target.setAddress(new UdpAddress(address));
    try {
        _snmp = new Snmp(new DefaultUdpTransportMapping());
    } catch (IOException e) {
        _snmp = null;
        throw new CloudRuntimeException(" Error in crearting snmp object, " + e.getMessage());
    }
}
 
Example #9
Source File: SNMPUtilsTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Method to create community target
 * @param community community name
 * @param address address
 * @param version SNMP version
 * @return community target
 */
protected static CommunityTarget createCommTarget(String community, String address, int version) {
    CommunityTarget target = new CommunityTarget();
    target.setVersion(version);
    target.setCommunity(new OctetString(community));
    target.setAddress(new UdpAddress(address));
    target.setRetries(0);
    target.setTimeout(500);
    return target;
}
 
Example #10
Source File: AbstractSnmpmanTest.java    From snmpman with Apache License 2.0 5 votes vote down vote up
static CommunityTarget getCommunityTarget(String community, Address targetAddress) {
    final CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString(community));
    target.setAddress(targetAddress);
    target.setRetries(2);
    target.setTimeout(1500);
    target.setVersion(SnmpConstants.version2c);
    return target;
}
 
Example #11
Source File: ProcessMessage.java    From mts with GNU General Public License v3.0 4 votes vote down vote up
public void onResponse(ResponseEvent re) {
       // Always cancel async request when response has been received
       // otherwise a memory leak is created! Not canceling a request
       // immediately can be useful when sending a request to a broadcast
       // address.
       try {
            Object source = re.getSource();

           // test to ignore REPORTS from DISCOVERY messages in SNMPv3
           if (!(source instanceof Snmp)) return;

           ((Snmp) source).cancel(re.getRequest(), this);

           //create the SnmpMsg received
           MsgSnmp msg = new MsgSnmp(listenpoint.getStack());
           msg.setPdu(re.getResponse());
           
           //TODO: how to know the version here to set communityTarget or UserTarget
           Target target = new CommunityTarget();
//           ((CommunityTarget)target).setCommunity(new OctetString(re.getStateReference().getSecurityName()));
           target.setAddress(re.getPeerAddress());
           msg.setTarget((AbstractTarget)target);
           UdpAddress add = (UdpAddress) re.getPeerAddress();
           msg.setRemotePort(add.getPort());
           msg.setRemoteHost(add.getInetAddress().getHostAddress());
           msg.setListenpoint(listenpoint);

           StackFactory.getStack(StackFactory.PROTOCOL_SNMP).receiveMessage(msg);
       } catch (Exception ex) {
       }
    }
 
Example #12
Source File: SnmpBinding.java    From openhab1-addons with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @{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());
            }
        }
    }

}
 
Example #13
Source File: AbstractSnmpmanTest.java    From snmpman with Apache License 2.0 4 votes vote down vote up
public static List<TableEvent> getResponse(final OID query, int port, final String community) throws Exception {
    final Address targetAddress = GenericAddress.parse(String.format("127.0.0.1/%d", port));
    final Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
    snmp.listen();

    final CommunityTarget target = getCommunityTarget(community, targetAddress);

    // creating PDU
    final PDUFactory pduFactory = new DefaultPDUFactory(PDU.GETBULK);
    final TableUtils utils = new TableUtils(snmp, pduFactory);

    return utils.getTable(target, new OID[]{query}, null, null);
}
 
Example #14
Source File: LumentumSnmpDevice.java    From onos with Apache License 2.0 4 votes vote down vote up
private void createDevice(String ipAddress, int port) throws IOException {
    Address targetAddress = GenericAddress.parse("udp:" + ipAddress + "/" + port);
    TransportMapping transport = new DefaultUdpTransportMapping();
    transport.listen();
    snmp = new Snmp(transport);

    // setting up target
    target = new CommunityTarget();
    target.setCommunity(new OctetString("public"));
    target.setAddress(targetAddress);
    target.setRetries(3);
    target.setTimeout(1000L * 3L);
    target.setVersion(SnmpConstants.version2c);
    target.setMaxSizeRequestPDU(MAX_SIZE_RESPONSE_PDU);
}
 
Example #15
Source File: PolatisSnmpUtility.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Retrieves static object value.
 *
 * @param handler parent driver handler
 * @param oid object identifier
 * @return the variable
 * @throws IOException if unable to retrieve the object value
 */
public static Variable get(DriverHandler handler, String oid) throws IOException {
    List<VariableBinding> vbs = new ArrayList<>();
    vbs.add(new VariableBinding(new OID(oid)));
    PDU pdu = new PDU(PDU.GET, vbs);
    Snmp session = getSession(handler);
    CommunityTarget target = getTarget(handler);
    ResponseEvent event = session.send(pdu, target);
    return event.getResponse().get(0).getVariable();
}
 
Example #16
Source File: SNMPUtils.java    From ingestion with Apache License 2.0 4 votes vote down vote up
public static void sendTrapV1(String port) throws IOException {

        TransportMapping<?> transport = new DefaultUdpTransportMapping();
        transport.listen();

        CommunityTarget comtarget = new CommunityTarget();
        comtarget.setCommunity(new OctetString(new OctetString("public")));
        comtarget.setVersion(SnmpConstants.version1);
        comtarget.setAddress(new UdpAddress("127.0.0.1/" + port));
        comtarget.setRetries(2);
        comtarget.setTimeout(5000);

        PDU trap = new PDUv1();
        trap.setType(PDU.V1TRAP);

        OID oid = new OID("1.2.3.4.5");
        trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
        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
        Snmp snmp = new Snmp(transport);
        snmp.send(trap, comtarget);
        transport.close();
        snmp.close();

    }
 
Example #17
Source File: SnmpmanSetTest.java    From snmpman with Apache License 2.0 3 votes vote down vote up
private ResponseEvent setVariableToOID(OID oid, Variable variable) throws IOException {

        final CommunityTarget target = getCommunityTarget(COMMUNITY, GenericAddress.parse(String.format("127.0.0.1/%d", PORT)));

        PDU pdu = new PDU();
        pdu.setType(PDU.SET);

        VariableBinding variableBinding = new VariableBinding(oid);
        variableBinding.setVariable(variable);

        pdu.add(variableBinding);
        return snmp.set(pdu, target);
    }
 
Example #18
Source File: PolatisSnmpUtility.java    From onos with Apache License 2.0 3 votes vote down vote up
/**
 * Sends a synchronous SET request to the supplied target.
 *
 * @param handler parent driver handler
 * @param vbs a list of variable bindings
 * @return the response event
 * @throws IOException if unable to set the target
 */
public static ResponseEvent set(DriverHandler handler, List<? extends VariableBinding> vbs) throws IOException {
    Snmp session = getSession(handler);
    CommunityTarget target = getTarget(handler);
    PDU pdu = new PDU(PDU.SET, vbs);
    return session.set(pdu, target);
}
 
Example #19
Source File: PolatisSnmpUtility.java    From onos with Apache License 2.0 3 votes vote down vote up
/**
 * Retrieves a table.
 *
 * @param handler parent driver handler
 * @param columnOIDs column oid object identifiers
 * @return the table
 * @throws IOException if unable to retrieve the object value
 */
public static List<TableEvent> getTable(DriverHandler handler, OID[] columnOIDs) throws IOException {
    Snmp session = getSession(handler);
    CommunityTarget target = getTarget(handler);
    TableUtils tableUtils = new TableUtils(session, new DefaultPDUFactory());
    return tableUtils.getTable(target, columnOIDs, null, null);
}
 
Example #20
Source File: PolatisSnmpUtility.java    From onos with Apache License 2.0 2 votes vote down vote up
/**
 * Sends a SET request.
 *
 * @param handler parent driver handler
 * @param pdu SNMP protocol data unit
 * @return the response event
 * @throws IOException if unable to send a set request
 */
public static ResponseEvent set(DriverHandler handler, PDU pdu) throws IOException {
    Snmp session = getSession(handler);
    CommunityTarget target = getTarget(handler);
    return session.set(pdu, target);
}