Java Dynamic Management Kit 5.0 Tutorial

Sending an Inform Request (SNMPv2)

Like the other types of requests, the manager sends an inform request through a session. The only difference is that the peer object associated with the request should be an SNMP manager able to receive and reply to InformRequest PDUs.

You can associate a peer with a session by making it the default peer object. This is how we do it in this example. This means that if we do not specify a peer when sending requests, they are automatically addressed to our manager peer. Because sessions often have agent peers as a default, you can specify the manager peer as a parameter to the snmpInform method of the session object.

Example 19–9 Sending an SNMPv2 Inform Request in SimpleManager1

// When calling the program, you must specify the hostname
// of the SNMP manager you want to send the inform to.
String host = argv[0];
// Initialize the port number to send inform PDUs on port 8085.
int port = 8085;
try {
    // Create an SnmpPeer object that represents the entity to
    // communicate with. 
    SnmpPeer peer = new SnmpPeer(host, port);
    // Create parameters to associate to the peer.
    // When creating the parameter object, you can specify the
    // read and write community to be used when sending an
    // inform request.
    SnmpParameters params = new SnmpParameters(
        "public", "private", "public");
    // The newly created parameter must be associated to the peer.
    // Build the session. A session creates, controls and manages
    // one or more requests.
    SnmpSession session = new SnmpSession("SimpleManager1 session");
    // Make the SNMP inform request and wait for the result.
    SnmpRequest request = session.snmpInform(
        null, new SnmpOid(""), null);
        "NOTE: Inform request sent to SNMP manager on " +
        host + " at port " + port);
    boolean completed = request.waitForCompletion(10000);
    // Check for a timeout of the request.
    if (completed == false) {
            "\nSimpleManager1::main: Request timed out. " +
            "Check if agent can be reached");
        // Print request.
        java.lang.System.out.println("Request: " + request.toString());
    // Now we have a response. Check if the response contains an error.
    int errorStatus = request.getErrorStatus();
    if (errorStatus != SnmpDefinitions.snmpRspNoError) {
        java.lang.System.out.println("Error status = " +
        java.lang.System.out.println("Error index = " +
    // Now we shall display the content of the result.
    SnmpVarbindList result = request.getResponseVbList();
    java.lang.System.out.println("\nNOTE: Response received:\n" + result);
    // Stop the session properly before exiting
} catch(Exception e) {
        "SimpleManager1::main: Exception occurred:" + e );

Before sending the request, the snmpInform method automatically adds two variables to the head of the varbind list that is passed in as the last parameter. These are sysUpTime.0 and snmpTrapOid.0, in the order they appear in the list. These variables are mandated by RFC 1905 and added systematically so that the calling process does not need to add them.

Like all other requests in a session, inform requests can be handled either synchronously or asynchronously in the sender. In our example, we process the inform request synchronously: the manager blocks the session while waiting for the completion of the request. In an asynchronous manager, you would need to implement a response handler as explained in Response Handler, and then use it to process responses, as shown in Example 19–7.