嵌入式SNMP4j日志访问失败并成功

Jon*_*Jon 7 logging snmp4j

我正在使用SNMP4j在java中编写代理,并且进展顺利.我可以获得并设置有价值(仅限SNMPv1,但v3即将推出).

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

  1. 新用户登录
  2. 用户从中尝试新的失败SNMP连接
  3. 用于通过from写入值的SNMP SET.

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

我使用org.snmp4j.event.AuthenticationFailureListener来记录验证失败的时间.这似乎只是SNMPv3,并没有给我失败的用户名.

有谁知道如何做到这一点?听众的建议似乎部分到位,还有更多我找不到的东西吗?我可以使用源代码并在需要的地方添加我自己的日志记录,但这有什么许可证含义?SNMP使用Apache 2.0许可证

Bla*_*har 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;
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在您的代理类中重写该initMessageDispatcher()方法:

    protected void initMessageDispatcher() {
      ...
      dispatcher = new ExtendedMessageDispatcherImpl();
      ...
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 将您的日志类作为侦听器添加到此调度程序(例如在finishInit()代理的方法中):

    dispatcher.addAuthenticationFailureListener(loggingHandler);
    
    Run Code Online (Sandbox Code Playgroud)

对于请求日志记录:

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

getSession().addCommandResponder(loggingHandler);
Run Code Online (Sandbox Code Playgroud)

对于响应日志记录:

  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;
    }
    
    Run Code Online (Sandbox Code Playgroud)

在我的例子中,结果以以下形式记录:

请求已收到!来自:(已删除 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 = 测试名称]]

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