Developers guide

126
* allow the handler to determine the host, port,
* and community string of the received PDU.</P>
*
* @param session The SNMP session
* @param agent The Trap sender
* @param port The port of the sender
* @param community The community string
* @param pdu The SNMP trap pdu
*
*/
public void snmpReceivedTrap(SnmpTrapSession session,
InetAddress agent,
int port,
SnmpOctetString community,
SnmpPduTrap pdu) {
logger.log( Level.INFO, "Received V1 Trap from " + agent + " on port " + port);
logger.log( Level.FINE, "Ip Address................. " + pdu.getAgentAddress() );
logger.log( Level.FINE, "Enterprise Id.............. " + pdu.getEnterprise() );
logger.log( Level.FINE, "Generic ................... " + pdu.getGeneric() );
logger.log( Level.FINE, "Specific .................. " + pdu.getSpecific() );
logger.log( Level.FINE, "TimeStamp ................. " + pdu.getTimeStamp() );
logger.log( Level.FINE, "Length..................... " + pdu.getLength() );
int k = pdu.getLength();
for (int i = 0; i < k ; i++ ) {
SnmpVarBind vb = pdu.getVarBindAt(i);
System.out.print("Varbind[" + i + "] := " + vb.getName().toString());
logger.log( Level.FINE," --> " + vb.getValue().toString());
}
// Do some stuff here to fire some events.
}
/**
* <P>This method is invoked if an error occurs in
* the trap session. The error code that represents
* the failure will be passed in the second parameter,
* 'error'. The error codes can be found in the class
* SnmpTrapSession class.</P>
*
* <P>If a particular PDU is part of the error condition
* it will be passed in the third parameter, 'pdu'. The
* pdu will be of the type SnmpPduRequest or SnmpPduTrap
* object. The handler should use the "instanceof" operator
* to determine which type the object is. Also, the object
* may be null if the error condition is not associated
* with a particular PDU.</P>
*
* @param session The SNMP Trap Session
* @param error The error condition value.
* @param ref The PDU reference, or potentially null.
* It may also be an exception.
*
*
*/
public void snmpTrapSessionError(SnmpTrapSession session,
int error,
Object ref) {
logger.log(Level.WARNING, "SNMP Trap Session Error");
// This error code is not actaully implemented at the moment
if( error == SnmpTrapSession.ERROR_INVALID_PDU ) {
logger.log(Level.WARNING, "\tError: "+error+" ERROR_INVALID_PDU: "+ref);
} else if( error == SnmpTrapSession.ERROR_EXCEPTION) {
logger.log(Level.WARNING, "\tError: "+error+" ERROR_EXCEPTION: "+ref);
stop(); // close the session
} else {
logger.log(Level.WARNING, "\tError: "+error+" ERROR_UNDEFINED: "+ref);
}
}
}