AdventNet Web NMS 4 API Specification

com.adventnet.nms.trap
Interface SocketListener

All Superinterfaces:
java.rmi.Remote

public interface SocketListener
extends java.rmi.Remote

This is the interface that needs to be implemented by Applications which intend to register with WebNMS for trap notification. Applications can use TrapAPI methods to register and deregister for Trap notification. Traps will be encapsulated in the com.adventnet.snmp.sas.ProtocolDataUnit class. A single Application can register for multiple ports which may or may not be same as that of port where WebNMS is listening for traps. For example consider WebNMS listenes for traps at port 5000 and other application implemeting this interface (i.e TrapListener) is registered at port 6000. If any trap comes for port 5000, WebNMS will be notified and Trap Listener won't be notified. Like that if any trap comes to port 6000, Trap Listener alone will be notified. TrapListener and WebNMS can register for the same trap port. Then it that case received trap will be notified to WebNMS and Trap Listener.

Following are the two steps need to be followed to create a successful Trap Listener.

  1. The user written class should implement this interface. receivedData(ProtocolDataUnit) method of this interface will be invoked whenever a trap is received in the port in which the application is registered for notification. com.adventnet.snmp.sas.ProtocolDataUnit object which encapsulates the details of the received trap, will be passed as an argument to that method.
  2. Instances of the user written class (implementing this interface) should register themselves as a listener, by invoking TrapAPI.registerForTrap(int,SocketListener) method. A single Application can register for multiple trap ports using TrapAPI.registerForTraps(int[],SocketListener) method. Once the instance is registered as a Trap Listener, then it will be intimated for traps in those registered ports through the invocation of the receivedData(ProtocolDataUnit) method.

    As TrapAPI could be accessed via RMI (Remote Method Invocation), this allows the flexibility to the Trap Listeners to function from a seperate (or Remote) JVM other than that of the Web NMS Server.

Following code snippet can be used for a Trap Listener which can run in a separate JVM. This example gets TrapAPI handle in secured and authenticated mode. This code registers a Trap Listener at port 4000.

 
 import com.adventnet.nms.trap.SocketListener;
 import com.adventnet.nms.eventdb.TrapAPI;
 import com.adventnet.snmp.sas.ProtocolDataUnit;
 import com.adventnet.security.authentication.RMIAccessAPI;
 import java.rmi.Naming; 
 import java.rmi.server.UnicastRemoteObject;
 public class TestTrapListener extends UnicastRemoteObject implements SocketListener
 {
     public TestTrapListener() throws java.rmi.RemoteException
     {
     }
 
     public void receivedData(ProtocolDataUnit ppdu)
     {
        System.out.println("Notification Received for Trap");
     }
 
     void start()
     {
         try
         {
             RMIAccessAPI rmiapi = (RMIAccessAPI)Naming.lookup("//hostName/RMIAccessAPI");
             TrapAPI api = (TrapAPI)rmiapi.getAPI("userName","password","TrapAPI");
             api.registerForTrap(4000,this);
             System.out.println("Successfully registered as Trap Listener-->");
         }
         catch(Exception e)
         {
             System.out.println("Error in registering ");
             e.printStackTrace();
         }    
      }
      public static void main(String args[])
      {
         try
          {
             TestTrapListener listener = new TestTrapListener();
             listener.start();
          }
          catch(Exception e)
          {
             System.out.println("Error in initailizing TestTrapListener");
             e.printStackTrace();
          }
      }
 }
 
 

While running Trap Listener in the same JVM as that of WebNMS server, NmsUtil.getAPI(String) can be used to get the TrapAPI handle.

See TrapAPI to get TrapAPI handle with out RMI Security mechanism of NMS.

Registered TrapListeners can be deregistered using TrapAPI.deRegisterForTrap(int,SocketListener) method. TrapListeners which are registered for more than one trap port can deregister using TrapAPI.deRegisterForTraps(int[],SocketListener) method. TrapAPI.deRegister(SocketListener) can be directly used to completely deregister from all registered ports.

Note: TrapObserver differs from TrapListener since Trap Observers are getting notification about the Traps which are processed only by WebNMS.


Method Summary
 void receivedData(com.adventnet.nms.trap.ProtocolDataUnit protocoldataunit)
          This method will be called when traps are received in registered ports.
 

Method Detail

receivedData

public void receivedData(com.adventnet.nms.trap.ProtocolDataUnit protocoldataunit)
                  throws java.rmi.RemoteException
This method will be called when traps are received in registered ports. ProtocolDataUnit class provides methods to fetch Port number in which Trap has been received, Agent from where the Trap has been received.

Users can use the following code snippet to obtain the SnmpPDU object from com.adventnet.snmp.sas.ProtocolDataUnit

  
  SnmpMessage msg = new Snmp3Message(ppdu.getData(),(new SnmpAPI()).getSecurityProvider());
  TrapPDUDecode decoder = new TrapPDUDecode();
  SnmpPDU pdu = msg.getPDU();
  decoder.decode(pdu);
  pdu.setAddress(java.net.InetAddress.getByName(ppdu.getHostAddress()));
  pdu.setRemotePort(ppdu.getRemotePort());
  

Note: Trap processing thread waits until invocation of this method gets completed. Hence users are suggested not to overload this method.

Parameters:
protocoldataunit - The wrapper class containing Trap details.
Throws:
java.rmi.RemoteException - if excetions any in remote machine

AdventNet Web NMS 4 API Specification