org.snmp4j.mp.MPv3 Java Examples

The following examples show how to use org.snmp4j.mp.MPv3. 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: SnmpService.java    From SkaETL with Apache License 2.0 6 votes vote down vote up
public void init() {

        try {
            targetAddress = GenericAddress.parse("udp:" + snmpConfiguration.getIpAddress() + "/" + snmpConfiguration.getPort());
            TransportMapping<?> transport = new DefaultUdpTransportMapping();
            snmp = new Snmp(transport);
            USM usm = new USM(SecurityProtocols.getInstance()
                    .addDefaultProtocols(), new OctetString(
                    MPv3.createLocalEngineID()), 0);
            SecurityProtocols.getInstance()
                    .addPrivacyProtocol(new PrivAES192());
            SecurityModels.getInstance().addSecurityModel(usm);
            transport.listen();

            snmp.getUSM().addUser(
                    new OctetString("MD5DES"),
                    new UsmUser(new OctetString("MD5DES"), AuthMD5.ID,
                            new OctetString("UserName"), PrivAES128.ID,
                            new OctetString("UserName")));


        } catch (Exception ex) {
            log.error("Error in Sending Trap to ({}:{})", snmpConfiguration.getIpAddress(), snmpConfiguration.getPort());
            ex.printStackTrace();
        }
    }
 
Example #2
Source File: SnmpH3C.java    From yuzhouwan with Apache License 2.0 6 votes vote down vote up
/**
 * Create SNMP.
 */
private void createSNMP() {

    LOG.info("Creating SNMP...");
    try {
        TransportMapping transport = new DefaultUdpTransportMapping();
        transport.listen();
        snmp = new Snmp(transport);

        // SNMP V3
        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);

        LOG.debug("Snmp created.");
        snmp.listen();
        LOG.debug("Snmp listening...");
    } catch (IOException e) {
        throw new RuntimeException("Cannot create snmp!!!", e);
    }
}
 
Example #3
Source File: AbstractSNMPProcessor.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Builds target resource.
 * @param context Process context
 */
private void buildTargetResource(ProcessContext context) {
    if((this.transportMapping == null) || !this.transportMapping.isListening() || (this.snmp == null)) {
        try {
            this.transportMapping = new DefaultUdpTransportMapping();
            this.snmp = new Snmp(this.transportMapping);

            if("SNMPv3".equals(context.getProperty(SNMP_VERSION).getValue())) {
                USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
                SecurityModels.getInstance().addSecurityModel(usm);
            }

            this.transportMapping.listen();
        } catch (Exception e) {
            throw new IllegalStateException("Failed to initialize UDP transport mapping", e);
        }
    }
    if (this.snmpTarget == null) {
        this.snmpTarget = this.createSnmpTarget(context);
    }
    if (this.targetResource == null) {
        this.targetResource = this.finishBuildingTargetResource(context);
    }
}
 
Example #4
Source File: AbstractSNMPProcessor.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Builds target resource.
 * @param context Process context
 */
private void buildTargetResource(ProcessContext context) {
    if((this.transportMapping == null) || !this.transportMapping.isListening() || (this.snmp == null)) {
        try {
            this.transportMapping = new DefaultUdpTransportMapping();
            this.snmp = new Snmp(this.transportMapping);

            if("SNMPv3".equals(context.getProperty(SNMP_VERSION).getValue())) {
                USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
                SecurityModels.getInstance().addSecurityModel(usm);
            }

            this.transportMapping.listen();
        } catch (Exception e) {
            throw new IllegalStateException("Failed to initialize UDP transport mapping", e);
        }
    }
    if (this.snmpTarget == null) {
        this.snmpTarget = this.createSnmpTarget(context);
    }
    if (this.targetResource == null) {
        this.targetResource = this.finishBuildingTargetResource(context);
    }
}
 
Example #5
Source File: SNMPV3Session.java    From SuitAgent with 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 #6
Source File: ListenpointSnmp.java    From mts with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean create(String protocol) throws Exception
{
    processMessage = new ProcessMessage(this);
    snmp = new Snmp(new DefaultUdpTransportMapping(new UdpAddress(getHost() + "/" + getPort())));
    snmp.addCommandResponder(processMessage);

    // TEST CODE : initialize for receiving SNMPV3
    {
        String authPassword = stack.getConfig().getString("protocol.authPassword");
        String encryptPassword = stack.getConfig().getString("protocol.encryptPassword");

        snmp.getMessageDispatcher().removeMessageProcessingModel(new MPv3());
        snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3(MPv3.createLocalEngineID(new OctetString(this.getUID()))));

        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(this.getUID().getBytes()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);

        // need to get a list of users and their params from config
        snmp.getUSM().addUser(new OctetString("MD5DES"),
                new UsmUser(new OctetString("MD5DES"),
                            AuthMD5.ID, new OctetString("protocol.authPassword"),
                            PrivDES.ID, new OctetString("protocol.encryptPassword")));
    }

    snmp.listen();

    return true;
}
 
Example #7
Source File: SNMPV3Session.java    From OpenFalcon-SuitAgent with 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 #8
Source File: SNMPClient.java    From mysql_perf_analyzer with Apache License 2.0 4 votes vote down vote up
/**
 * Start the Snmp session. If you forget the listen() method you will not
 * get any answers because the communication is asynchronous
 * and the listen() method listens for answers.
 * @throws IOException
 */
public void start() throws IOException 
{
	TransportMapping transport = new DefaultUdpTransportMapping();
	snmp = new Snmp(transport);
	if("3".equals(this.version))//add v3 support
	{
		USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);  
		SecurityModels.getInstance().addSecurityModel(usm);  
	}
	// Do not forget this line!
	transport.listen();
}
 
Example #9
Source File: SNMPUtils.java    From ingestion with 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 #10
Source File: SnmpmanAgent.java    From snmpman with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes a new instance of an SNMP agent.
 *
 * @param configuration the configuration for this agent
 */
public SnmpmanAgent(final AgentConfiguration configuration) {
    super(SnmpmanAgent.getBootCounterFile(configuration), SnmpmanAgent.getConfigurationFile(configuration), new CommandProcessor(new OctetString(MPv3.createLocalEngineID())));
    this.agent.setWorkerPool(ThreadPool.create("RequestPool", 3));
    this.configuration = configuration;
}
 
Example #11
Source File: SNMPUtils.java    From ingestion with 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 #12
Source File: SNMPUtils.java    From ingestion with 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());
    }
}