Advent SNMP API Summary
Advent SNMP API Summary
This document explains how to create SNMP applications
which send and receive various SNMP requests using
synchronous and asynchronous communication between the Application
and the Agent being managed.
Follow these links for quick information.
Starting the SNMP session to send and receive
PDUs
Forming the PDU to be sent to the agent
Sending and Receiving SNMP messages
Synchronously
Sending and Receiving SNMP messages
Asynchronously
Receiving SNMP trap messages
Starting the SNMP session to send and receive PDUs:
Any application which has to start a session to send
requests and receive responses from an agent has to first
instantiate the SnmpAPI class and start the api thread which
manages and monitors snmp sessions through a separate thread.
Following are the steps involved in starting an snmp session
to send and receive snmp messages.
- Instantiate the SnmpAPI class (api = SnmpAPI())
- Start an API thread to monitor SNMP sessions
(api.start())
- Instantiate an SnmpSession for the api object
(SnmpSession
session = new SnmpSession(api))
- Initialize the peername, community, local_port, remote_port,
retries, timeout elements of
the session object. If the community (public),
local_port (0 - system assigned), remote_port (161),
retries (0), timeout(5000ms) are not
initialized they take default values (as indicated).
Forming the PDU to be sent to the agent:
To send an SNMP request a PDU needs to be instantiated and filled in.
The command element has to be filled in with the appropriate SNMP
command
eg. SNM_GET_REQ for get etc. The address of the remoteHost element has
to be instantiated
if the peername is not already set for SNMP session. The PDU should also
be filled with
the OID. The following are the steps involved in creating a PDU to be
sent to an agent.
- Instantiate the SnmpPDU class (SnmpPDU pdu = new
SnmpPDU(api))
- Initialize the command element of the PDU object (pdu.command =
GET_REQ_MSG)
- Instantiate the SnmpOID object (SnmpOID oid = new
SnmpOID("OID",api))
- Add the oid to the PDU list (pdu.addNull(oid))
Sending and Receiving SNMP messages Synchronously
To send and receive messages synchronously, first
open the SNMP session (session.open()). Then use (rpdu =
session.syncSend(pdu))
and it waits till a response is received or timed out. rpdu will have
the response received by the remote host.
Sending and Receiving SNMP messages Asynchronously
There are two ways by which the sending and receiving SNMP message
can be made asynchronous. One is using session.send() and
session.receive()
and another using the callback method in the snmpClient.
Using Send and Receive
Using Callback
Using Send and Receive
session.send() is similar to session.syncSend() except that it
returns immediately, instead of waiting for a response. Using
session.receive is analogous to polling the socket for any
response. The following steps explains how to implement the
session.send() and session.receive() using polling.
- Open the SNMP session (session.open())
- Check for any responses (responses[] =
session.checkResponses())
- If a message has been received responses[] will be non null
- Check if any element of the responses array for matching
pdu.reqid
- If a match is there, that means a response has been received for
what we sent
- If a match is not there go back to step 3.
- Use session.receive() to receive the pdu (pdu =
session.receive(pdu.reqid))
Using Callback
Callback is used when we want to avoid the polling done to check for
responses when we used session.receive to receive the responses.
If we use callback the session.callback() method will be called
automatically when a response arrives. Any application which
wants to use callbacks should implement the SnmpClient Interface class
and define the methods callback, authenticate and debugprint.
The following explains the steps involved in using callbacks.
- Implement the Interface class SnmpClient (class myClass
implements snmpClient)
- Open the SNMP session (session.open())
- Define the method "callback". This method should handle the pdu
received for a particular session. The "SnmpSession session" argument is the session for which the
pdu is received. The "SnmpPDU pdu" argument is the pdu received. The "int requestID" is the
requestID of the sent pdu for which the response is received.
- Define the method "authenticate()". This method should check the
community received in the response pdu with the community of the particular session.
The "String community" argument in the authenticate() method is same as the
session.community and the "SnmpPDU pdu" argument is the received pdu.
- Define the method debugprint. It is used for printing any debug
information.
Receiving SNMP trap messages
There are two ways by which an application can receive an SNMP trap message
One is session.receive() and another using the callback method in the
snmpClient.
Receiving SNMP trap messages using session.receive()
Receiving SNMP trap messages using callback
Receiving SNMP trap messages using session.receive()
Using session.receive() is analogous to polling the socket for any
response. The following steps explains how to receive trap messages
using session.receive().
- Instantiate the SnmpSession class (SnmpSession session = new SnmpSession(api)
- Initialize the session.community(null), session.local_port(162). If they
are not initialized they take default values of "public" and "162" respectively.
If the community of the trap message does not match the session.community then
the trap message will be dropped unless the session.community is set to null.
- Open the SNMP session (session.open())
- Use session.receive()to receive forvever till we receive a non null pdu.
session.receive() returns null if there is no received pdu.(while((pdu = session.receive(0))!= NULL))
- If the pdu.command is equal to TRAP_REQ_MSG, then an SNMP trap message
has been received.
Receiving SNMP trap messages using callback
Callback is used when we want to avoid the polling done to check for
responses when we used session.receive() to receive the responses.
If we use callback the session.callback() method will be called
automatically when a response arrives. Any application which
wants to use callbacks should implement the SnmpClient Interface class
and define the methods callback, authenticate and debugprint.
The following explains the steps involved in using callbacks.
- Implement the Interface class SnmpClient (class myClass
implements snmpClient)
- Open the SNMP session (session.open())
- Define the method "callback()". This method should handle the trap pdu
received. The "SnmpSession session" argument is the session for which the
pdu is received. The "SnmpPDU pdu" argument is the pdu received. The "int requestID"
argument is the requestID of the sent pdu for which the response is received. The requestID
for trap messages is zero.
- Define the method "authenticate()". This method should check the
community received in the response pdu with the community of the particular
session. The "String community" argument in the authenticate() method is same as the
session.community and the "SnmpPDU pdu" argument is the received pdu.
- Define the method debugprint. It is used for printing any debug
information.
