Developers guide
125
logger.log( Level.INFO, "Java SnmpTrapDaemon Started");
fireSnmpTrapDaemonStarted();
} catch (SocketException e) {
logger.log( Level.WARNING, "Could not initialise SnmpTrapSession");
logger.log( Level.WARNING, "Check the permissions for UDP port 162, you may have to run this code
as root",e);
fireSnmpTrapDaemonStopped();
}
}
/**
* Stop listening to SNMP Traps by closing the
* org.opennms.protocols.snmp.SnmpTrapSession
*/
public void stop() {
if( trapSession != null ) {
synchronized(trapSession) {
if( trapSession.isClosed() ) {
logger.log( Level.INFO,"Tried to stop SnmpTrapDaemon, but it is not running");
} else {
trapSession.close();
trapSession = null;
logger.log( Level.INFO, "Java SnmpTrapDaemon Stopped");
fireSnmpTrapDaemonStopped();
}
}
}
}
/**
* <P>This method is defined to handle SNMPv2 traps
* that are received by the session. The parameters
* allow teh handler to determine the host, port, and
* community string of the received PDU</P>
*
* @param session The SNMP session
* @param agent The remote sender
* @param port The remote senders port
* @param community The community string
* @param pdu The SNMP pdu
*
*/
public void snmpReceivedTrap(SnmpTrapSession session,
InetAddress agent,
int port,
SnmpOctetString community,
SnmpPduPacket pdu) {
logger.log( Level.WARNING, "Received SNMP V2 Trap from "+ agent + " on port "+ port );
logger.log( Level.FINE, "V2 Trap PDU command " + pdu.getCommand());
logger.log( Level.FINE, "V2 Trap PDU ID " + pdu.getRequestId());
logger.log( Level.FINE, "V2 Trap PDU Length " + pdu.getLength());
if(pdu instanceof SnmpPduRequest)
{
logger.log( Level.FINE, "V2 Trap PDU Error Status " +
((SnmpPduRequest)pdu).getErrorStatus());
logger.log( Level.FINE, "V2 Trap PDU Error Index " +
((SnmpPduRequest)pdu).getErrorIndex());
}
int k = pdu.getLength();
for (int i = 0; i < k ; i++ ) {
SnmpVarBind vb = pdu.getVarBindAt(i);
logger.log( Level.FINE, "Varbind[" + i + "] := " + vb.getName().toString());
logger.log( Level.FINE, " --> " + vb.getValue().toString());
}
}
/**
* <P>This method is define to handle SNMPv1 traps
* that are received by the session. The parameters










