- /* $Id: ReceiveTrap.java,v 1.1.2.1 2004/02/21 07:21:07 ramkumar Exp $ */
- /*
- * @(#)ReceiveTrap.java
- * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
- * Please read the associated COPYRIGHTS file for more details.
- */
- /**
- * This is a tutorial example program for receiving traps
- * at the port 8001 by using the
- * com.adventnet.snmp.snmp2 package of AdventNetSNMP api.
- *
- * The user could run this application by giving the following usage.
- *
- * java ReceiveTrap
- * the application will start receiving traps in the port 8001.
- *
- *
- *
- */
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import com.adventnet.snmp.snmp2.*;
- public class ReceiveTrap implements SnmpClient {
- public static void main(String args[]) {
- // Start SNMP API
- SnmpAPI api = new SnmpAPI();
- SnmpSession session = new SnmpSession(api);
- session.addSnmpClient(new ReceiveTrap());
- // set local port
- UDPProtocolOptions option = new UDPProtocolOptions();
- option.setLocalPort(8001);
- session.setProtocolOptions(option);
- // Open the session
- try
- {
- session.open();
- }
- catch (SnmpException e )
- {
- System.err.println("Error opening socket: "+e);
- }
- System.out.println("Waiting to receive traps in the port "+option.getLocalPort() +".......");
- }
- public boolean authenticate(SnmpPDU pdu, String community)
- {
- return (pdu.getCommunity().equals(community));
- }
- public boolean callback(SnmpSession session, SnmpPDU pdu, int requestID)
- {
- if(pdu == null)
- {
- return false;
- }
- if (pdu.getCommand() == SnmpAPI.TRP_REQ_MSG)
- {
- System.out.print("Trap received from: ");
- System.out.print(pdu.getProtocolOptions().getSessionId());
- System.out.println(", community: " + pdu.getCommunity());
- System.out.println("Enterprise: " + pdu.getEnterprise());
- System.out.println("Agent: " + pdu.getAgentAddress().getHostAddress());
- System.out.println("TRAP_TYPE: " + pdu.getTrapType());
- System.out.println("SPECIFIC NUMBER: " + pdu.getSpecificType());
- System.out.println("Time: " + pdu.getUpTime()+"nVARBINDS:");
- // print varbinds
- Enumeration e = pdu.getVariableBindings().elements();
- while(e.hasMoreElements())
- {
- System.out.println(((SnmpVarBind) e.nextElement()).toTagString());
- }
- }
- else if(pdu.getCommand() == SnmpAPI.TRP2_REQ_MSG)
- {
- System.out.print("Trap received from: ");
- System.out.print(pdu.getProtocolOptions().getSessionId());
- System.out.println(", community: " + pdu.getCommunity());
- Enumeration e = pdu.getVariableBindings().elements();
- while(e.hasMoreElements())
- {
- System.out.println(((SnmpVarBind) e.nextElement()).toTagString());
- }
- }
- else
- {
- System.err.println("Non trap PDU received.");
- }
- System.out.println("Continues waiting to receive traps .......");
- return true;
- }
- public void debugPrint(String debugOutput)
- {
- System.out.println(debugOutput);
- return;
- }
- }