viewTrap.java.txt
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:5k
- /* $Id: viewTrap.java,v 1.1 2002/06/15 14:43:56 ram Exp $ */
- /*
- * viewTrap.java
- * Copyright (c) 1996-2003 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.start();
- 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"); }
-
- // 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.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) {
- if (pdu.getCommand() != api.TRP_REQ_MSG)
- System.err.println("Non trap PDU received, printing anyway.");
- text.append("Trap received from: "
- +pdu.getRemoteHost() + ", community: " + pdu.getCommunity() + "n");
- if(pdu.getEnterprise() != null){
- text.append("Enterprise: " + pdu.getEnterprise() + "n");
- }
-
- String agent_ip_addr = SnmpIpAddress.netbToString(pdu.getAgentAddress().getAddress()) ;
- text.append("Agent: " + agent_ip_addr + "n");
-
- int host_timeout=26000;
- // Usage of getHostName to get the agent host name since applets can't perform DNS
- String hostess=null;
- try {
- hostess = session.getSASClient().getHostName(agent_ip_addr,host_timeout);
- text.append("Agent Host : " + hostess + "n");
- }
- catch(Exception e) {
- }
- try {
- text.append("Agent Rev : " + session.getSASClient().getHostAddress(hostess,host_timeout) + "n");
- }
- catch(Exception e) {
- System.err.println(e.getMessage());
- }
-
- 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;
- }
- }