Java Code Examples for org.snmp4j.CommunityTarget#setVersion()

The following examples show how to use org.snmp4j.CommunityTarget#setVersion() . 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 Project: mysql_perf_analyzer   File: SNMPClient.java    License: 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 Project: localization_nifi   File: SNMPUtilsTest.java    License: 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 Project: yuzhouwan   File: SnmpSimpleGet.java    License: 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 Project: yuzhouwan   File: SnmpSimpleSet.java    License: 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 Project: ingestion   File: SNMPUtils.java    License: 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 6
Source Project: onos   File: PolatisSnmpUtility.java    License: 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 7
Source Project: snmpman   File: AbstractSnmpmanTest.java    License: 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 8
Source Project: nifi   File: SNMPUtilsTest.java    License: 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 9
Source Project: cloudstack   File: SnmpHelper.java    License: 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 10
Source Project: ingestion   File: SNMPUtils.java    License: 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 11
Source Project: onos   File: LumentumSnmpDevice.java    License: 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 12
/**
 * @{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());
            }
        }
    }

}