嵌入式设备中的SNMP4j日志访问失败与成功

7
我正在使用SNMP4j编写Java代理,进展顺利。我可以获取和设置值(目前仅支持SNMPv1,但v3即将推出)。
我的下一个要求是在我的应用程序日志中记录以下三个内容:
1. 新用户从何处登录 2. 用户尝试新的失败SNMP连接来自 3. SNMP SET被用于通过 写入值。
我已经使用org.snmp4j.log.LogAdapter将SNMP4j日志传输到我的调试日志中,但那不是我想要的特定日志记录。
我已经尝试了org.snmp4j.event.AuthenticationFailureListener来记录身份验证失败时发生的情况。这似乎仅适用于SNMPv3,并且它没有给我失败的用户名。
有人知道如何做到这一点吗?侦听器架构部分地已经就位,还有我找不到的其他部分吗?我可以使用源代码并在需要时添加自己的日志记录,但是这样做的许可证影响是什么?SNMP使用Apache 2.0许可证。
1个回答

1
我做了以下事情,以完全访问请求和响应PDU:
对于“身份验证失败”记录:
  1. Extend the AuthenticationFailureEvent.class. In my case I added the security name and status info to the constructor of the event class.
  2. Extend the MessageDispatcherImpl.class and override the method dispatchMessage():

    switch (status) {
      case SnmpConstants.SNMP_MP_UNSUPPORTED_SECURITY_MODEL :
      case SnmpConstants.SNMPv3_USM_AUTHENTICATION_FAILURE :
      case SnmpConstants.SNMPv3_USM_UNSUPPORTED_SECURITY_LEVEL :
      case SnmpConstants.SNMPv3_USM_UNKNOWN_SECURITY_NAME :
      case SnmpConstants.SNMPv3_USM_AUTHENTICATION_ERROR :
      case SnmpConstants.SNMPv3_USM_NOT_IN_TIME_WINDOW :
      case SnmpConstants.SNMPv3_USM_UNSUPPORTED_AUTHPROTOCOL :
      case SnmpConstants.SNMPv3_USM_UNKNOWN_ENGINEID :
      case SnmpConstants.SNMP_MP_WRONG_USER_NAME :
      case SnmpConstants.SNMPv3_TSM_INADEQUATE_SECURITY_LEVELS :
      case SnmpConstants.SNMP_MP_USM_ERROR : {
        // create an extended version of the failure event
        AuthenticationFailureEvent event = new ExtendedAuthenticationFailureEvent(this,incomingAddress,securityName.getValue(),sourceTransport, status, statusInfo, wholeMessage);
        fireAuthenticationFailure(event);
        break;
      }
    }
    
  3. In your agent class override the initMessageDispatcher() method:

    protected void initMessageDispatcher() {
      ...
      dispatcher = new ExtendedMessageDispatcherImpl();
      ...
    }
    
  4. Add your logging class as listener to this dispatcher (for example in the finishInit() method of your agent):

    dispatcher.addAuthenticationFailureListener(loggingHandler);
    

对于请求日志记录:

只需在您的日志记录类中实现CommandResponder接口并将其添加到会话中即可:

getSession().addCommandResponder(loggingHandler);

针对“响应”日志记录:
  1. Create a method e.g. logResponsePdu(PDU pdu) in your logging class.

  2. Extend MessageDispatcherImpl.class and override the method returnResponsePdu().

    public int returnResponsePdu(int messageProcessingModel, int securityModel, byte[] securityName, int securityLevel, PDU pdu, int maxSizeResponseScopedPDU, StateReference stateReference, StatusInformation statusInformation) throws MessageException {
      int result = super.returnResponsePdu(messageProcessingModel, securityModel, securityName, securityLevel, pdu, maxSizeResponseScopedPDU, stateReference, statusInformation);
      // log response message
      loggingHandler.logResponsePdu(pdu);
      return result;
    }
    
在我的情况下,结果以以下形式记录:

收到请求!来自:(已删除的ip),安全名称:(登录名),PDU类型:SET,OID:1.3.6.1.2.1.1.5.0 ='测试名称'

请求PDU: SET[{contextEngineID=(已删除的数据), contextName=private}, requestID=(已删除的数据), errorStatus=0, errorIndex=0, VBS[1.3.6.1.2.1.1.5.0 = 测试名称]]

发送响应!错误状态:成功,PDU类型:RESPONSE,OID: 1.3.6.1.2.1.1.5.0 ='测试名称'

响应PDU!PDU: RESPONSE[{contextEngineID=(已删除的数据), contextName=private}, requestID=(已删除的数据), errorStatus=0, errorIndex=0, VBS[1.3.6.1.2.1.1.5.0 = 测试名称]]

也许这不是最好的方法,但它起作用。我希望我能帮助你。

很酷,这似乎可以工作!我已经让AuthFailure位工作了,现在要实现其他的。 - Jon
我现在已经解决了这个问题。我在日志中看到一个神秘的用户“initial”出现了。你有看到过吗?我猜测可能是SNMP4j在做某些事情,或者我的MIB浏览器可能在使用它。 - Jon
1
不,我没有那个用户。也许要检查发件人地址。 - Blakhar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接