Java Code Examples for org.snmp4j.mp.SnmpConstants

The following examples show how to use org.snmp4j.mp.SnmpConstants. These examples are extracted from open source projects. 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: SkaETL   Author: skalogs   File: SnmpService.java    License: Apache License 2.0 5 votes vote down vote up
public void send(String value) {

        // Create Target
        UserTarget target = new UserTarget();
        target.setAddress(targetAddress);
        target.setRetries(1);
        target.setTimeout(11500);
        target.setVersion(SnmpConstants.version3);
        target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
        target.setSecurityName(new OctetString("MD5DES"));

        // Create PDU
        ScopedPDU pdu = new ScopedPDU();
        pdu.setType(ScopedPDU.NOTIFICATION);
        pdu.add(new VariableBinding(SnmpConstants.sysUpTime));
        pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, SnmpConstants.linkDown));
        pdu.add(new VariableBinding(new OID(snmpConfiguration.getTrapOid()), new OctetString("Major")));
        pdu.add(new VariableBinding(new OID(snmpConfiguration.getTrapOid()), new OctetString(value)));

        // Send the PDU
        try {
            Snmp s = this.getSnmp();
            if (s != null)
                s.send(pdu, target);

            //this.getSnmp().send(pdu, target);
            log.info("Sending Trap [{}] to ({}:{})", value, snmpConfiguration.getIpAddress(), snmpConfiguration.getPort());
            this.getSnmp().addCommandResponder(new CommandResponder() {
                public void processPdu(CommandResponderEvent arg0) {
                    log.info(arg0.toString());
                }
            });
        } catch (IOException ex) {
            log.error("Exception during SNMP sending", ex.getMessage());
            ex.printStackTrace();
        }
    }
 
Example #2
Source Project: localization_nifi   Author: wangrenlei   File: SNMPUtilsTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Method to create a user target
 * @param address address
 * @param securityLevel security level
 * @param securityName security name
 * @return user target
 */
private static UserTarget createUserTarget(String address, int securityLevel, String securityName) {
    UserTarget target = new UserTarget();
    target.setVersion(SnmpConstants.version3);
    target.setSecurityLevel(securityLevel);
    target.setSecurityName(new OctetString(securityName));
    target.setAddress(new UdpAddress(address));
    target.setRetries(0);
    target.setTimeout(500);
    return target;
}
 
Example #3
Source Project: mysql_perf_analyzer   Author: yahoo   File: SNMPClient.java    License: Apache License 2.0 5 votes vote down vote up
public int getVersionInt()
{
	if("1".equals(this.version))
		return SnmpConstants.version1;
	else if("3".equals(this.version))
		return SnmpConstants.version3;
	else 
		return SnmpConstants.version2c;
}
 
Example #4
Source Project: yuzhouwan   Author: asdf2014   File: SnmpH3CTest.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void before() {

    /**
     * snmp-agent target-host trap address udp-domain 192.168.112.155 udp-port 5000 params securityname yuzhouwan
     * <p/>
     * snmp-agent usm-user v3 yuzhouwan managev3group simple authentication-mode md5 helloyuzhouwan privacy-mode 3des helloyuzhouwan acl 2222
     */
    String securityName = "yuzhouwan2"; //"managev3group"; //"yuzhouwan"; //"yuzhouwan";
    AuthGeneric authenticationProtocol = new AuthMD5();
    String authenticationPassphrase = "helloyuzhouwan";
    PrivacyProtocol privacyProtocol = new PrivAES128();      //new Priv3DES();
    String privacyPassphrase = "helloyuzhouwan";

    H3CSnmpV3User h3CSnmpV3User = new H3CSnmpV3User(securityName,
            authenticationProtocol, authenticationPassphrase,
            privacyProtocol, privacyPassphrase);

    String userName = "yuzhouwan2";   //same as securityName ?

    String address = "192.168.6.201";
    String securityName2 = "yuzhouwan2"; //"managev3group"; //"yuzhouwan"; //"yuzhouwan";
    int securityLevel = SecurityLevel.AUTH_PRIV;
    int securityModel = 3;
    /*int maxSizeRequestPDU = '\uffff';*/
    int retries = 3;
    long timeout = 1000 * 10;
    int version = SnmpConstants.version3;

    H3CUserTarget h3CUserTarget = new H3CUserTarget(address,
            securityName2, securityLevel,
            securityModel, retries,
            timeout, version);

    H3CInfos h3CInfos = new H3CInfos(h3CSnmpV3User, userName, h3CUserTarget);

    snmpH3C = new SnmpH3C(h3CInfos);
}
 
Example #5
Source Project: ingestion   Author: Stratio   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   Author: opennetworkinglab   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   Author: 1and1   File: MOGroup.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * If the prepare method doesn't set {@link org.snmp4j.agent.request.RequestStatus RequestStatus} to
 * {@link  org.snmp4j.mp.SnmpConstants#SNMP_ERROR_SUCCESS SNMP_ERROR_SUCCESS} the new values are written.
 * Otherwise the commit fails and forces an undo operation.
 *
 * @param request The SubRequest to handle.
 */
@Override
public void commit(final SubRequest request) {
    Variable newValue = request.getVariableBinding().getVariable();
    OID oid = request.getVariableBinding().getOid();
    if (variableBindings.getOrDefault(oid, newValue).getSyntax() == newValue.getSyntax()) {
        variableBindings.put(oid, newValue);
    } else {
        request.getStatus().setErrorStatus(SnmpConstants.SNMP_ERROR_INCONSISTENT_VALUE);
    }
    request.getStatus().setPhaseComplete(true);
}
 
Example #8
Source Project: snmpman   Author: 1and1   File: SnmpmanSetTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSetterForValidValue() throws Exception {
    String newValue = "New interface";
    ResponseEvent responseEvent = setVariableToOID(new OID(OID_OCTETSTRING).append(OID_ROW_INDEX), new OctetString(newValue));
    assertEquals(SnmpConstants.SNMP_ERROR_SUCCESS, responseEvent.getResponse().getErrorStatus());
    assertThatOidHasValue(OID_OCTETSTRING, newValue);
}
 
Example #9
Source Project: snmpman   Author: 1and1   File: SnmpmanSetTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSetterForInvalidValue() throws Exception {
    Integer newValue = 9999;
    ResponseEvent responseEvent = setVariableToOID(new OID(OID_OCTETSTRING).append(OID_ROW_INDEX), new Integer32(newValue));
    assertEquals(SnmpConstants.SNMP_ERROR_INCONSISTENT_VALUE, responseEvent.getResponse().getErrorStatus());
    assertThatOidHasValue(OID_OCTETSTRING, PRIMAL_OCTECT_VALUE);
}
 
Example #10
Source Project: snmpman   Author: 1and1   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 #11
Source Project: nifi   Author: apache   File: SNMPUtilsTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Method to create a user target
 * @param address address
 * @param securityLevel security level
 * @param securityName security name
 * @return user target
 */
private static UserTarget createUserTarget(String address, int securityLevel, String securityName) {
    UserTarget target = new UserTarget();
    target.setVersion(SnmpConstants.version3);
    target.setSecurityLevel(securityLevel);
    target.setSecurityName(new OctetString(securityName));
    target.setAddress(new UdpAddress(address));
    target.setRetries(0);
    target.setTimeout(500);
    return target;
}
 
Example #12
Source Project: cloudstack   Author: apache   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 #13
Source Project: cloudstack   Author: apache   File: SnmpHelper.java    License: Apache License 2.0 5 votes vote down vote up
private PDU createPDU(SnmpTrapInfo snmpTrapInfo) {
    PDU trap = new PDU();
    trap.setType(PDU.TRAP);

    int alertType = snmpTrapInfo.getAlertType() + 1;
    if (alertType > 0) {
        long sysUpTimeTicks = ManagementFactory.getRuntimeMXBean().getUptime() / 10;
        trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(sysUpTimeTicks)));
        trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, getOID(CsSnmpConstants.TRAPS_PREFIX + alertType)));
        if (snmpTrapInfo.getDataCenterId() != 0) {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.DATA_CENTER_ID), new UnsignedInteger32(snmpTrapInfo.getDataCenterId())));
        }

        if (snmpTrapInfo.getPodId() != 0) {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.POD_ID), new UnsignedInteger32(snmpTrapInfo.getPodId())));
        }

        if (snmpTrapInfo.getClusterId() != 0) {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.CLUSTER_ID), new UnsignedInteger32(snmpTrapInfo.getClusterId())));
        }

        if (snmpTrapInfo.getMessage() != null) {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.MESSAGE), new OctetString(snmpTrapInfo.getMessage())));
        } else {
            throw new CloudRuntimeException(" What is the use of alert without message ");
        }

        if (snmpTrapInfo.getGenerationTime() != null) {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.GENERATION_TIME), new OctetString(snmpTrapInfo.getGenerationTime().toString())));
        } else {
            trap.add(new VariableBinding(getOID(CsSnmpConstants.GENERATION_TIME)));
        }
    } else {
        throw new CloudRuntimeException(" Invalid alert Type ");
    }

    return trap;
}
 
Example #14
Source Project: SuitAgent   Author: DevopsJK   File: SNMPV3Session.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 创建SNMPV3会话
 * @param userInfo
 * @throws IOException
 * @throws AgentArgumentException
 */
public SNMPV3Session(SNMPV3UserInfo userInfo) throws IOException, AgentArgumentException {
    if(StringUtils.isEmpty(userInfo.getAddress())){
        throw new AgentArgumentException("SNMPV3Session创建失败:snmp v3协议的访问地址不能为空");
    }
    if(StringUtils.isEmpty(userInfo.getUsername())){
        throw new AgentArgumentException("SNMPV3Session创建失败:snmp v3协议的访问用户名不能为空");
    }
    if(!StringUtils.isEmpty(userInfo.getAythType()) && StringUtils.isEmpty(userInfo.getAuthPswd())){
        throw new AgentArgumentException("SNMPV3Session创建失败:snmp v3协议指定了认证算法 aythType,就必须要指定认证密码");
    }
    if(!StringUtils.isEmpty(userInfo.getPrivType()) && StringUtils.isEmpty(userInfo.getPrivPswd())){
        throw new AgentArgumentException("SNMPV3Session创建失败:snmp v3协议指定了加密算法 privType,就必须要指定加密密码");
    }

    this.userInfo = userInfo;
    snmp = new Snmp(new DefaultUdpTransportMapping());
    USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID(
            new OctetString(HostUtil.getHostIp() + UUID.randomUUID().toString())
    )), 0);
    SecurityModels.getInstance().addSecurityModel(usm);
    snmp.listen();

    UsmUser user = new UsmUser(
            new OctetString(userInfo.getUsername()),
            getAuthProtocol(userInfo.getAythType()), new OctetString(userInfo.getAuthPswd()),
            getPrivProtocol(userInfo.getPrivType()), new OctetString(userInfo.getPrivPswd()));

    snmp.getUSM().addUser(new OctetString(userInfo.getUsername()), user);

    target = new UserTarget();
    target.setSecurityName(new OctetString(userInfo.getUsername()));
    target.setVersion(SnmpConstants.version3);
    target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
    target.setAddress(GenericAddress.parse(userInfo.getProtocol() + ":" + userInfo.getAddress() + "/" + userInfo.getPort()));
    target.setTimeout(TIMEOUT);
    target.setRetries(1);
}
 
Example #15
Source Project: OpenFalcon-SuitAgent   Author: cqyijifu   File: SNMPV3Session.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 创建SNMPV3会话
 * @param userInfo
 * @throws IOException
 * @throws AgentArgumentException
 */
public SNMPV3Session(SNMPV3UserInfo userInfo) throws IOException, AgentArgumentException {
    if(StringUtils.isEmpty(userInfo.getAddress())){
        throw new AgentArgumentException("SNMPV3Session创建失败:snmp v3协议的访问地址不能为空");
    }
    if(StringUtils.isEmpty(userInfo.getUsername())){
        throw new AgentArgumentException("SNMPV3Session创建失败:snmp v3协议的访问用户名不能为空");
    }
    if(!StringUtils.isEmpty(userInfo.getAythType()) && StringUtils.isEmpty(userInfo.getAuthPswd())){
        throw new AgentArgumentException("SNMPV3Session创建失败:snmp v3协议指定了认证算法 aythType,就必须要指定认证密码");
    }
    if(!StringUtils.isEmpty(userInfo.getPrivType()) && StringUtils.isEmpty(userInfo.getPrivPswd())){
        throw new AgentArgumentException("SNMPV3Session创建失败:snmp v3协议指定了加密算法 privType,就必须要指定加密密码");
    }

    this.userInfo = userInfo;
    snmp = new Snmp(new DefaultUdpTransportMapping());
    USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
    SecurityModels.getInstance().addSecurityModel(usm);
    snmp.listen();

    UsmUser user = new UsmUser(
            new OctetString(userInfo.getUsername()),
            getAuthProtocol(userInfo.getAythType()), new OctetString(userInfo.getAuthPswd()),
            getPrivProtocol(userInfo.getPrivType()), new OctetString(userInfo.getPrivPswd()));

    snmp.getUSM().addUser(new OctetString(userInfo.getUsername()), user);

    target = new UserTarget();
    target.setSecurityName(new OctetString(userInfo.getUsername()));
    target.setVersion(SnmpConstants.version3);
    target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
    target.setAddress(GenericAddress.parse(userInfo.getProtocol() + ":" + userInfo.getAddress() + "/" + userInfo.getPort()));
    target.setTimeout(8000);
    target.setRetries(1);
}
 
Example #16
Source Project: ingestion   Author: Stratio   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 #17
Source Project: ingestion   Author: Stratio   File: SNMPUtils.java    License: Apache License 2.0 4 votes vote down vote up
public static void sendTrapV3(String port) {
    try {
        Address targetAddress = GenericAddress.parse("udp:127.0.0.1/" + port);
        TransportMapping<?> transport = new DefaultUdpTransportMapping();
        Snmp snmp = new Snmp(transport);
        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(
                MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
        transport.listen();

        snmp.getUSM().addUser(new OctetString("MD5DES"),
                new UsmUser(new OctetString("MD5DES"), null, null, null, null));

        // Create Target
        UserTarget target = new UserTarget();
        target.setAddress(targetAddress);
        target.setRetries(1);
        target.setTimeout(11500);
        target.setVersion(SnmpConstants.version3);
        target.setSecurityLevel(SecurityLevel.NOAUTH_NOPRIV);
        target.setSecurityName(new OctetString("MD5DES"));

        // Create PDU for V3
        ScopedPDU pdu = new ScopedPDU();
        pdu.setType(ScopedPDU.NOTIFICATION);
        pdu.add(new VariableBinding(SnmpConstants.sysUpTime));
        pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, SnmpConstants.linkDown));
        pdu.add(new VariableBinding(new OID("1.2.3.4.5"), new OctetString("Major")));

        // Send the PDU
        snmp.send(pdu, target);

        transport.close();
        snmp.close();
    } catch (Exception e) {
        System.err.println("Error in Sending Trap to (IP:Port)=> " + "127.0.0.1" + ":" + port);
        System.err.println("Exception Message = " + e.getMessage());
    }
}
 
Example #18
Source Project: onos   Author: opennetworkinglab   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 #19
Source Project: openhab1-addons   Author: openhab   File: SnmpGenericBindingProvider.java    License: Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @{inheritDoc
 */
@Override
public int getSnmpVersion(String itemName) {
    SnmpBindingConfig config = (SnmpBindingConfig) bindingConfigs.get(itemName);
    return config != null ? config.get(IN_BINDING_KEY).snmpVersion : SnmpConstants.version1;
}
 
Example #20
Source Project: openhab1-addons   Author: openhab   File: SnmpGenericBindingProvider.java    License: Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @{inheritDoc
 */
@Override
public int getSnmpVersion(String itemName, Command command) {
    SnmpBindingConfig config = (SnmpBindingConfig) bindingConfigs.get(itemName);
    return config != null ? config.get(command).snmpVersion : SnmpConstants.version1;
}
 
Example #21
Source Project: ingestion   Author: Stratio   File: SNMPUtils.java    License: Apache License 2.0 3 votes vote down vote up
public static void sendTrapV3Auth(String port) throws IOException {
    try {
        Address targetAddress = GenericAddress.parse("udp:127.0.0.1/" + port);
        TransportMapping<?> transport = new DefaultUdpTransportMapping();
        Snmp snmp = new Snmp(transport);
        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(
                MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
        transport.listen();

        snmp.getUSM().addUser(
                new OctetString("user"),
                new UsmUser(new OctetString("user"), AuthMD5.ID, new OctetString("12345678"),
                        null, null));

        // Create Target
        UserTarget target = new UserTarget();
        target.setAddress(targetAddress);
        target.setRetries(1);
        target.setTimeout(11500);
        target.setVersion(SnmpConstants.version3);
        target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);
        target.setSecurityName(new OctetString("user"));

        // Create PDU for V3
        ScopedPDU pdu = new ScopedPDU();
        pdu.setType(ScopedPDU.NOTIFICATION);
        pdu.add(new VariableBinding(SnmpConstants.sysUpTime));
        pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, SnmpConstants.linkDown));
        pdu.add(new VariableBinding(new OID("1.2.3.4.5"), new OctetString("Major")));

        // Send the PDU
        snmp.send(pdu, target);

        transport.close();
        snmp.close();
    } catch (Exception e) {
        System.err.println("Error in Sending Trap to (IP:Port)=> " + "127.0.0.1" + ":" + port);
        System.err.println("Exception Message = " + e.getMessage());
    }
}
 
Example #22
Source Project: ingestion   Author: Stratio   File: SNMPUtils.java    License: Apache License 2.0 3 votes vote down vote up
public static void sendTrapV3AuthPriv(String port) throws IOException {
    try {
        Address targetAddress = GenericAddress.parse("udp:127.0.0.1/" + port);
        TransportMapping<?> transport = new DefaultUdpTransportMapping();
        Snmp snmp = new Snmp(transport);
        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(
                MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
        transport.listen();

        snmp.getUSM().addUser(
                new OctetString("user"),
                new UsmUser(new OctetString("user"), AuthMD5.ID, new OctetString("12345678"),
                        PrivDES.ID, new OctetString("passphrase")));

        // Create Target
        UserTarget target = new UserTarget();
        target.setAddress(targetAddress);
        target.setRetries(1);
        target.setTimeout(11500);
        target.setVersion(SnmpConstants.version3);
        target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);
        target.setSecurityName(new OctetString("user"));

        // Create PDU for V3
        ScopedPDU pdu = new ScopedPDU();
        pdu.setType(ScopedPDU.NOTIFICATION);
        pdu.add(new VariableBinding(SnmpConstants.sysUpTime));
        pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID, SnmpConstants.linkDown));
        pdu.add(new VariableBinding(new OID("1.2.3.4.5"), new OctetString("Major")));

        // Send the PDU
        snmp.send(pdu, target);

        transport.close();
        snmp.close();
    } catch (Exception e) {
        System.err.println("Error in Sending Trap to (IP:Port)=> " + "127.0.0.1" + ":" + port);
        System.err.println("Exception Message = " + e.getMessage());
    }
}