Managers receive inform requests as they do traps: they are unsolicited events that must be received by a dispatcher object. Unlike traps, an inform request requires a response PDU which, according to the SNMP specification, must contain the same variable bindings. Therefore, immediately after an inform request is successfully received and decoded, the SnmpEventReportDispatcher class automatically constructs and sends the inform response back to the originating host.
The manager application then retrieves the data in the inform request through a listener on the dispatcher. Inform request listeners are registered with the dispatcher object in the same way as trap listeners. The receiving manager in our example is very simple, since its only function is to create the dispatcher and the listener for inform requests.
// Initialize the port number to listen for incoming inform PDUs on port 8085. // int port = 8085; try { // Create a dispatcher for SNMP event reports (SnmpEventReportDispatcher). // SnmpEventReportDispatcher is run as a thread and listens for informs // on the specified port. // Add our InformListenerImpl class as an SnmpInformListener. // InformListenerImpl will receive a callback when a valid trap // PDU is received. // SnmpEventReportDispatcher informDispatcher = new SnmpEventReportDispatcher(port); informDispatcher.addInformListener(new InformListenerImpl()); new Thread(informDispatcher).start(); // Note that you can use the same SnmpEventReportDispatcher object // for both incoming traps and informs. // Just add your trap listener to the same dispatcher, for example: // informDispatcher.addTrapListener(new TrapListenerImpl()); // Here we are just going to wait for inform PDUs. // java.lang.System.out.println("\nNOTE: Event report listener initialized"); java.lang.System.out.println( " and listening for incoming inform PDUs on port " + port + "..."); } catch(Exception e) { java.lang.System.err.println( "SimpleManager2::main: Exception occurred:" + e ); e.printStackTrace(); } |
The remaining step is to program the behavior we want upon receiving an inform request. To do this, we must write the InformListenerImpl class that we registered as an inform request listener in the previous code sample. This class implements the SnmpInformListener interface and its processSnmpInform method handles the incoming inform request.
Because the dispatcher automatically sends the inform response back to the originating host, the SnmpInformListener implementation does not need to do this. Usually this method will extract the variable bindings and take whatever action is necessary upon receiving an inform request. In our example, we simply print out the source and the contents of the inform request.
import java.io.IOException; import javax.management.snmp.SnmpPduRequest; import javax.management.snmp.manager.SnmpInformListener; /** * This class implements the SnmpInformListener interface. * The callback method processSnmpInform is called when a * valid inform PDU is received. */ public class InformListenerImpl implements SnmpInformListener { public void processSnmpInform(SnmpPduRequest inform) { // Display the received PDU. // java.lang.System.out.println("\nNOTE: Inform request received:\n"); java.lang.System.out.println("\tType = " + inform.pduTypeToString(inform.type)); java.lang.System.out.println("\tVersion = " + inform.version); java.lang.System.out.println("\tRequestId = " + inform.requestId); java.lang.System.out.println("\tAddress = " + inform.address); java.lang.System.out.println("\tPort = " + inform.port); java.lang.System.out.println("\tCommunity = " + new String(inform.community)); java.lang.System.out.println("\tVB list = "); for (int i = 0; i < inform.varBindList.length; i++) { java.lang.System.out.println("\t\t" + inform.varBindList[i]); } // Our listener stop the manager after receiving its first // inform request java.lang.System.out.println( "\nNOTE: SNMP simple manager 2 stopped..."); java.lang.System.exit(0); } } |