viewTrap.java
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:5k
- /* $Id: viewTrap.java,v 1.4 2002/09/09 05:45:08 parasuraman Exp $ */
- /*
- * viewTrap.java
- * Copyright (c) 1996-2002 AdventNet, Inc. All Rights Reserved.
- * Please read the associated COPYRIGHTS file for more details.
- */
- /** The import clauses */
- import java.lang.*;
- import java.io.*;
- import java.util.*;
- import java.awt.*;
- import java.applet.*;
- import java.net.URL;
- import java.io.InputStream;
- import com.adventnet.snmp.snmp2.*;
- import com.adventnet.management.transport.TransportException;
- import com.adventnet.snmp.mibs.*;
- public class viewTrap extends Applet implements SnmpClient {
- /** Some of the widgets in the main applet window. */
- TextArea text;
- Label header;
- /** The SNMP API instance */
- SnmpAPI api;
-
- /** The applets init method sets up the GUI */
- public void init() {
-
- /** The font to be used */
- Font fontb = new Font("Helvetica",Font.BOLD,16);
- // Now the GUI elements
- header = new Label("INCOMING TRAPS");
- header.setFont(fontb);
- text = new TextArea("Startnn");
- text.setEditable(false);
- text.setFont(fontb);
- setLayout(new BorderLayout());
- add("North", header);
- add("Center", text);
-
-
- } // end of init
- /** The start method, used when applet is started */
- public void start() {
- // Start SNMP API
- api = new SnmpAPI();
- api.setDebug(true);
- String mib = getParameter("mib");
- if (mib != null){
- text.appendText("Loading MIB file: " + mib + "n");
- // Loading MIBS - For addtional mibs to load please modify yourself.
- MibOperations mibOps = new MibOperations();
- try {
- mibOps.loadMibModules(mib);
- }
- catch(Exception e) {
- System.err.println(e.getMessage());
- }
- }
- // Open session
- SnmpSession session = new SnmpSession(api);
- session.setSASProtocol(2);
- session.setCallbackthread(true);
- session.addSnmpClient( this);
- // set port to listen for traps
- int port = 162;
- if (getParameter("port") != null)
- try {
- port = Integer.parseInt(getParameter("port"));
- }
- catch (NumberFormatException ex) {
- System.err.println("Invalid Integer Arg"); }
-
-
- // Open the session
- try { session.open(this); }
- catch (SnmpException e) {
- System.err.println(e);
- System.exit(1);
- }
- // set the port to listen for traps
- if(session.getSASClient() != null)
- try {
- session.getSASClient().reqTraps(port);
- }
- catch(IOException io){
- //System.err.println("Error "+io.getMessage());
- }
- catch(TransportException io) {
- System.err.println("Error "+io.getMessage()); }
-
- text.appendText("Listening for Traps on port " + port + "nn");
-
- } // end of start()
- /** The stop method, used when applet is no longer on
- screen */
- public void stop() {
- if (api == null) return;
- api.close();
- api = null;
-
- }
- public boolean callback(SnmpSession session, SnmpPDU pdu,
- int reqid) {
- if (pdu.getCommand() != api.TRP_REQ_MSG)
- System.err.println("Non trap PDU received, printing anyway.");
- text.appendText("Trap received from: "
- +pdu.getRemoteHost() + ", community: " + pdu.getCommunity() + "n");
- if(pdu.getEnterprise() != null){
- text.appendText("Enterprise: " + pdu.getEnterprise() + "n");
- }
-
- String agent_ip_addr = SnmpIpAddress.netbToString(pdu.getAgentAddress().getAddress()) ;
- text.appendText("Agent: " + agent_ip_addr + "n");
-
- int host_timeout=26000;
-
-
-
- text.appendText("TRAP_TYPE: " + pdu.getTrapType() + "n");
- text.appendText("SPECIFIC NUMBER: " + pdu.getSpecificType()+ "n");
- text.appendText("Time: " + pdu.getUpTime() +"nVARBINDS:n");
- for (Enumeration e = pdu.getVariableBindings().elements() ;
- e.hasMoreElements() ;)
- text.appendText(((SnmpVarBind)
- e.nextElement()).toTagString() + "n");
- text.appendText("nn"); // a blank line between traps
- return true; // need no more processing
- } // end of callback()
- /** We need to implement the other methods in the
- SnmpClient interface */
- public void debugPrint(String s) {
- System.err.println(s);
- }
- public boolean authenticate(SnmpPDU pdu, String community)
- {
- return true;
- }
- }