A trap handler for the SNMP manager is an object that implements the SnmpTrapListener interface in the com.sun.management.snmp.manager package. When this object is bound as a listener of an SnmpEventReportDispatcher object, its methods are called to handle trap PDUs.
A trap listener is not a notification listener because the dispatcher is not a notification broadcaster. The listener has callback methods that work in the same manner, but they are given objects that represent traps, not instances of the Notification class.
The interface defines three methods, one for processing SNMPv1 traps, another for SNMPv2 traps and a third for SNMPv3 traps. Trap PDU packets have already been decoded by the dispatcher, and these methods handle an object representation of the trap: SnmpPduTrap objects for SNMPv1, SnmpPduRequest objects for SNMPv2 and SnmpScopedPduRequest for SNMPv3.
public class TrapListenerImpl implements SnmpTrapListener { public void processSnmpTrapV1(SnmpPduTrap trap) { println("NOTE: TrapListenerImpl received trap :"); println("\tGeneric " + trap.genericTrap); println("\tSpecific " + trap.specificTrap); println("\tTimeStamp " + trap.timeStamp); println("\tAgent adress " + trap.agentAddr.stringValue()); } public void processSnmpTrapV2(SnmpPduRequest trap) { println("NOTE: Trap V2 not of interest !!!"); } public void processSnmpTrapV3(SnmpScopedPduRequest trap) { println("NOTE: TrapListenerImpl received trap V3:"); println("\tContextEngineId : " + SnmpEngineId.createEngineId(trap.contextEngineId)); println("\tContextName : " + new String(trap.contextName)); println("\tVarBind list :"); for(int i = 0; i < trap.varBindList.length; i++) println("oid : " + trap.varBindList[i].oid + " val : " + trap.varBindList[i].value); } } |
In the examplesDir/current/Snmp/Manager directory, generate the OID table description of MIB-II that our manager will access.
$ mibgen -mo mib_II.txt |
Compile the example classes.
To set up your environment, see “Directories and Classpath” in the Preface.
$ javac -classpath classpath -d . *.java |
Make sure that no other agent is running on port 8085, and start the simple SNMP agent in examplesDir/current/Snmp/Agent.
See To Run the SNMPv1/v2 Agent Example if you have not already built and run this example.
Type the following command to start the Agent example:
$ cd examplesDir/current/Snmp/Agent $ java -classpath classpath Agent nbTraps |
If you are also running the asynchronous manager example with this agent, omit the nbTraps parameter. The agent then sends traps continuously and they can be seen in both managers. Otherwise, specify the number of traps to be sent to the manager. Wait until the manager is started to send the traps.
Start the manager application in another window to connect to this agent.
If you want to run the manager on a different host, replace localhost with the name of the host where you started the agent.
$ cd examplesDir/current/Snmp/Manager $ java -classpath classpath SyncManager localhost 8085 SyncManager::main: Send get request to SNMP agent on localhost at port 8085 Result: [Object ID : 1.3.6.1.2.1.1.1.0 (Syntax : String) Value : SunOS sparc 5.7] |
Here we see the output of the SNMP request, it is the value of the sysDescr variable on the agent.
Press Enter in the agent's window.
You should see the manager receiving the traps it sends. Leave the agent running if you are going on to the next example, otherwise remember to stop it by pressing Control-C.
In the examplesDir/current/Snmp/Manager directory, generate the OID table description of MIB-II that our manager will access.
$ mibgen -mo mib_II.txt |
Compile the example classes.
To set up your environment, see “Directories and Classpath” in the Preface.
$ javac -classpath classpath -d . *.java |
Make sure that no other agent is running on port 8085, and start the simple SNMPv3 agent, AgentV3, in examplesDir/current/Snmp/Agent.
See To Run the SMNPv3 AgentV3 Example if you have not already built and run this example.
Type the following commands to start the AgentV3 example:
$ cd examplesDir/current/Snmp/Agent $ java -classpath classpath -Djdmk.security.file=jdmk.security AgentV3 |
Start the manager application in another window to connect to this agent.
If you want to run the manager on a different host, replace localhost with the name of the host where you started the agent.
$ cd examplesDir/current/Snmp/Manager $ java -classpath classpath -Djdmk.security.file=jdmk.security SyncManagerV3 localhost 8085 |
Be sure to run the manager in its directory, otherwise it cannot find its jdmk.security file.
Press Enter in the agent's window.
You should see the manager receiving the traps it sends. Stop the manager by typing Control-C.