viewTrap.java
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:5k
- /* $Id: viewTrap.java,v 1.5.2.3 2009/01/28 12:55:03 prathika Exp $ */
- /*
- * viewTrap.java
- * Copyright (c) 1996-2009 AdventNet, Inc. All Rights Reserved.
- * Please read the associated COPYRIGHTS file for more details.
- */
- /** The import classes */
- import com.adventnet.management.transport.TransportException;
- 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.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("Startn");
- 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.append("Loading MIB file: " + mib + "n");
- // Loading MIBS - For addtional mibs to load please modify yourself.
- MibOperations mibOps = new MibOperations();
- try {
- mibOps.loadMibModules(this, mib);
- }
- catch(Exception e) {
- System.err.println(e.getMessage());
- }
- }
- // Open session
- SnmpSession session = new SnmpSession(api);
- //Start the callback on a separate thread
- 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"); }
-
- SASProtocolOptions sas_opt = new SASProtocolOptions();
- sas_opt.setApplet(this);
- session.setProtocolOptions(sas_opt);
-
- // Open the session
- try { session.open(); }
- catch (SnmpException e) {
- System.err.println(e);
- System.exit(1);
- }
-
- SASClient sasclient = sas_opt.getSASClient();
- // set the port to listen for traps
- if(sasclient != null) {
- try {
- sasclient.reqTraps(port);
- }
- catch(IOException io){
- System.err.println("Error "+io.getMessage());
- }
- catch(TransportException io){
- System.err.println("Error "+io.getMessage());
- }
- text.append("Listening for Traps on port: " + port + "nn");
- }
- else {
- text.append("SAServer not Connected n");
- }
-
- } // 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) {
- text.append("Trap received from: "
- +pdu.getProtocolOptions().getSessionId() + ", community: " + pdu.getCommunity() + "n");
- if(pdu.getEnterprise() != null){
- text.append("Enterprise: " + pdu.getEnterprise() + "n");
- }
- if(pdu.getVersion() == api.SNMP_VERSION_1) {
- String agent_ip_addr = pdu.getAgentAddress().getHostAddress();
- text.append("Agent: " + agent_ip_addr + "n");
-
- text.append("TRAP_TYPE: " + pdu.getTrapType() + "n");
- text.append("SPECIFIC NUMBER: " + pdu.getSpecificType()+ "n");
- text.append("Time: " + pdu.getUpTime() +"nVARBINDS:n");
- }
- for (Enumeration e = pdu.getVariableBindings().elements() ;
- e.hasMoreElements() ;)
- text.append(((SnmpVarBind)
- e.nextElement()).toTagString() + "n");
- text.append(""); // 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;
- }
- }