简体   繁体   中英

snmp4j Unknown security name

I have a problem about snmp4j. The exception is about Unknown security name. I use the snmp4j to monitoring devices and I run it in multi-thread.My code is below. Who can tell me how to fix this problem.

public static JSONObject snmpV3Check(Integer ida, String contextName, String ip, String userName, Integer authProtocol, String authPass, Integer privProtocol, String privPass, Integer level) {
    JSONObject json = new JSONObject();
    int isSnmpConn = -1;
    UserTarget target = null;
    if (ida != null) {
        Integer snmpTimeOut = StrategyContainer.getStrategy(ida).getSnmpTimeOut();
        Integer snmpRetryCount = StrategyContainer.getStrategy(ida).getSnmpRetryCount();
        target = creatV3Default(ip, userName, level, snmpTimeOut, snmpRetryCount);
    } else {
        target = creatV3Default(ip, userName, level, DEFAULT_TIMEOUT, DEFAULT_RETRY);
    }
    ScopedPDU request = new ScopedPDU();
    request.setType(PDU.GET);
    request.setContextName(new OctetString(contextName));
    Snmp snmp = null;
    TransportMapping<?> transport = null;
    try {
        transport = new DefaultUdpTransportMapping();
        snmp = new Snmp(transport);
        USM usm = new USM(SecurityProtocols.getInstance().addDefaultProtocols(), new OctetString(MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
        //USM usm = USMFactory.getInstance();
        SecurityModels.getInstance().addSecurityModel(usm);
        transport.listen();
        snmp.getUSM().addUser(new OctetString(userName), UsmUserUtil.creatUsmUser(userName, authProtocol, authPass, privProtocol, privPass));
        ResponseEvent respEvt = snmp.send(request, target);
        if (respEvt != null && respEvt.getResponse() != null) {
            if (respEvt.getResponse().getType() == PDU.RESPONSE && respEvt.getResponse().getErrorStatus() == PDU.noError) {
                isSnmpConn = 1;
            } else {
                logger.error(">>>>>>>>>snmp(v3) Error Code About Response:{ip:{}, type:{}, ErrorStatus:{}}<<<<<<<<<", ip, respEvt.getResponse().getType(), respEvt.getResponse().getErrorStatus());
            }
        } 
    } catch (IOException e) {
        logger.error("V3snmp's Exception:’", e);

    } 

    json.put("conn", isSnmpConn);
    return json;
}

Using the snmp v3 you must provide a security name by a design specification.
The security name should be specified through a setter of UserTarget or provided directly by a constructor . Example:

UserTarget target = new UserTarget();
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setSecurityName(userName);

How to add a security name in the snmp v3 with details look at:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM