Java Code Examples for org.snmp4j.Snmp#listen()

The following examples show how to use org.snmp4j.Snmp#listen() . 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: 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 2
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 3
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 4
Source File: SnmpmanSetTest.java    From snmpman with Apache License 2.0 4 votes vote down vote up
@BeforeMethod
public void initSnmpSession() throws IOException {
    snmp = new Snmp(new DefaultUdpTransportMapping());
    snmp.listen();
}
 
Example 5
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);
}