【问题标题】:Embedded SNMP4j log access fail and success嵌入式 SNMP4j 日志访问失败和成功
【发布时间】:2016-01-19 11:39:00
【问题描述】:

我正在使用 SNMP4j 在 java 中编写代理,并且进展顺利。我可以获取和设置值(目前仅支持 SNMPv1,但 v3 即将推出)。

我的下一个要求是登录我的应用程序日志(不是 SNMP4J 日志),特别是这三件事:

  1. 新用户从
  2. 登录
  3. 来自
  4. 的用户尝试新建失败的 SNMP 连接
  5. SNMP SET 用于通过 from 写入值。

我已经使用 org.snmp4j.log.LogAdapter 将 SNMP4j 日志记录到我的调试日志中,但这不是我想要的特定日志记录。

我已经使用 org.snmp4j.event.AuthenticationFailureListener 来记录身份验证失败的发生情况。这似乎只是 SNMPv3,它没有给我失败的用户名。

有人知道怎么做吗?侦听器架构似乎部分到位,还有更多我找不到的吗?我可以使用源代码并在需要的地方添加我自己的日志记录,但这对许可证有什么影响? SNMP 使用 Apache 2.0 许可证

【问题讨论】:

    标签: logging snmp4j


    【解决方案1】:

    我执行以下操作以获得对请求和响应 PDU 的完全访问权限:

    对于身份验证失败日志记录:

    1. 扩展AuthenticationFailureEvent.class。在我的例子中,我将安全名称和状态信息添加到事件类的构造函数中。
    2. 扩展MessageDispatcherImpl.class并覆盖方法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. 在您的代理类中覆盖initMessageDispatcher() 方法:

      protected void initMessageDispatcher() {
        ...
        dispatcher = new ExtendedMessageDispatcherImpl();
        ...
      }
      
    4. 将您的日志记录类添加为该调度程序的侦听器(例如在您的代理的 finishInit() 方法中):

      dispatcher.addAuthenticationFailureListener(loggingHandler);
      

    对于请求日志记录:

    只需在您的日志记录类中实现CommandResponderinterface 并将其添加到您的会话中:

    getSession().addCommandResponder(loggingHandler);
    

    对于响应日志记录:

    1. 创建一个方法,例如logResponsePdu(PDU pdu) 在您的日志记录类中。

    2. 扩展MessageDispatcherImpl.class并覆盖方法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;
      }
      

    在我的情况下,结果是以以下形式登录:

    请求已收到! From: (ip 已移除), security name: (login name), 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!配电单元: RESPONSE[{contextEngineID=(数据已删除), contextName=private},requestID=(数据已删除),errorStatus=0,errorIndex=0, VBS[1.3.6.1.2.1.1.5.0 = 测试名称]]

    也许这不是最好的方法,但它确实有效。我希望我能帮助你。

    【讨论】:

    • 酷,这似乎有效!无论如何,我得到了 AuthFailure 位,现在来实现其他位。
    • 我现在有这个工作。我在我的日志中看到一个神秘的用户“初始”。你看到了吗?我猜是 SNMP4j 在做某事,或者我的 MIB 浏览器可能正在使用它。
    • 不,我没有那个用户。也许检查发件人地址。
    猜你喜欢
    • 2017-03-17
    • 2021-10-12
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多