I did the following to have full access to the request and response PDUs:
For the authentication failure logging:
- Extend the
AuthenticationFailureEvent.class
. In my case I added the security name and status info to the constructor of the event class.
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;
}
}
In your agent class override the initMessageDispatcher()
method:
protected void initMessageDispatcher() {
...
dispatcher = new ExtendedMessageDispatcherImpl();
...
}
Add your logging class as listener to this dispatcher (for example in the finishInit()
method of your agent):
dispatcher.addAuthenticationFailureListener(loggingHandler);
For the request logging:
Just implement the CommandResponder
interface in your logging class and add it to your session:
getSession().addCommandResponder(loggingHandler);
For the response logging:
Create a method e.g. logResponsePdu(PDU pdu)
in your logging class.
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;
}
The result in my case is logging in form of:
Request received! From: (ip removed), security name: (login name), PDU
type: SET, OID: 1.3.6.1.2.1.1.5.0 = 'Test Name'
Request PDU:
SET[{contextEngineID=(data removed),
contextName=private}, requestID=(data removed), errorStatus=0, errorIndex=0,
VBS[1.3.6.1.2.1.1.5.0 = Test Name]]
Response sent! Error status: Success, PDU type: RESPONSE, OID:
1.3.6.1.2.1.1.5.0 = 'Test Name'
Response PDU! PDU:
RESPONSE[{contextEngineID=(data removed),
contextName=private}, requestID=(data removed), errorStatus=0, errorIndex=0,
VBS[1.3.6.1.2.1.1.5.0 = Test Name]]
Maybe it is not the best way to do it, but it works.I hope I could help you with this.