ejbtable.java
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:9k
源码类别:

SNMP编程

开发平台:

C/C++

  1. /* $Id: ejbtable.src,v 1.2 2001/10/04 12:41:43 ram Exp $ */
  2. /*
  3.  * @(#)ejbtable.java
  4.  * Copyright (c) 1998 AdventNet, Inc. All Rights Reserved.
  5.  * Please read the COPYRIGHTS file for more details.
  6.  */
  7. /** 
  8.  *  This is an example of using the SnmpTable class.
  9.  *  This is a command line application that does not require JFC/swing
  10.  *  components.  See the swingapps directory for examples that need
  11.  *  and use swing components.
  12.  *
  13.  * java ejbtable [options] host tableoid ...
  14.  *<img SRC="images/v3only.jpg" ALT="v3 only">
  15.  * v3 request:
  16.  * java ejbtable [-SnmpServer hostName] [-v version(v1,v2,v3)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] [-u user] [-a auth_protocol] [-w auth_password] [-s priv_password] [-i contextID] [-n contextName] host tableOID ...
  17.  * e.g. 
  18.  * java ejbtable -v v3 -m ....mibsrfc1213-MIB -u user -a MD5 -w passwd localhost tcpConnTable
  19.  *
  20.  *<img SRC="images/v2candv3only.jpg" ALT="v2c and v3 only"> v2c request:
  21.  * java ejbtable [-SnmpServer hostName] [-v version(v1,v2)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] host OID [OID] ...
  22.  * e.g. 
  23.  * java ejbtable -v v2 -m ....mibsrfc1213-MIB localhost tcpConnTable
  24.  *
  25.  * v1 request:
  26.  * java ejbtable [-SnmpServer hostName] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] host OID [OID] ...
  27.  * e.g. 
  28.  * java ejbtable -m ....mibsrfc1213-MIB localhost tcpConnTable
  29.  *
  30.  * Options:
  31.  * [-SnmpServer ] <hostName>      - SnmpServer SERVER option to specify the ejb server host.
  32.  * [-c] <community>    - community String. By default "public".
  33.  * [-p] <port>         - remote port no. By default 161.
  34.  * [-t] <Timeout>      - Timeout. By default 5000ms.
  35.  * [-r] <Retries>      - Retries. By default 0. 
  36.  * [-m] <MIBfile>      - MIB files.To load multiple mibs give within double quotes files seperated by a blank space.       
  37.  *<img SRC="images/v3only.jpg" ALT="v3 only"> [-v] <version>      - version(v1 / v2 / v3). By default v1.
  38.  * [-u] <username>     - The v3 principal/userName
  39.  * [-a] <autProtocol>  - The authProtocol(MD5/SHA). Mandatory if authPassword is specified
  40.  * [-w] <authPassword> - The authentication password.
  41.  * [-s] <privPassword> - The privacy protocol password. Must be accompanied with auth password and authProtocol fields.
  42.  * [-n] <contextName>  - The context to be used for the v3 pdu.
  43.  * [-i] <contextID>    - The contextID to be used for the v3 pdu.
  44.  * host Mandatory      - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
  45.  * tableOID  Mandatory      - Give multiple no. of table Object Identifiers.
  46.  **/
  47. import com.adventnet.snmp.ejb.*;
  48. import com.adventnet.snmp.snmp2.*;
  49. import com.adventnet.snmp.mibs.*;
  50. import javax.naming.*;
  51. import weblogic.jndi.*;
  52. public class ejbtable implements com.adventnet.snmp.rmi.SnmpTableListener {
  53. static final int USM_SECURITY_MODEL = 3;
  54. static SnmpTable table;
  55. public static void main(String args[]) {
  56. // Take care of getting options
  57.         String usage = "ejbtable [-SnmpEJBServer hostName] [-v version(v1,v2,v3)] [-m MIB_files] [-c community] [-p port] [-t timeout] [-r retries] [-u user] [-a auth_protocol] [-w auth_password] [-s priv_password] [-i contextID] [-n contextName] host OID [OID] ...";
  58.         String options[] = { "-SnmpEJBServer", "-c", "-p", "-r", "-t", "-m", "-v", "-u", "-a", "-w", "-s" ,"-i" , "-n"};
  59.         String values[] = { null, null, null, null, null, null, null, null,null,null,null,null,null};
  60.         String userName = new String("");
  61.         String authProtocol = new String("NO_AUTH");
  62.         String authPassword = new String ("");
  63.         String privPassword = new String ("");
  64.         String contextName = new String ("");
  65. String contextID = new String ("");
  66.     ParseOptions opt = new ParseOptions(args,options,values, usage);
  67.     // check for at least hostname and one OID in remaining arguments
  68.     if (opt.remArgs.length<2) opt.usage_error();
  69. //This block performs a lookup on the EJBComponent.
  70. //The lookup is specific to the ApplicationServer used.
  71. try {
  72.     Environment env = new Environment();
  73. if(values[0]!=null)
  74.   env.setProviderUrl(values[0]+"/SnmpTableEJB");
  75. else env.setProviderUrl("t3://localhost:7001/SnmpTableEJB");
  76.     Context ctx = env.getInitialContext();
  77.     SnmpTableHome home = (SnmpTableHome) ctx.lookup("SnmpTableEJB");
  78. table = (SnmpTable) home.create();
  79.     if(values[6] != null) {  // if SNMP version is specified, set it
  80.         if(values[6].equals("v1"))
  81.       table.setSnmpVersion( com.adventnet.snmp.ejb.SnmpTable.VERSION1 ) ;
  82.         else if(values[6].equals("v2"))
  83.             table.setSnmpVersion( com.adventnet.snmp.ejb.SnmpTable.VERSION2C );
  84.         else if(values[6].equals("v3"))
  85.             table.setSnmpVersion( com.adventnet.snmp.ejb.SnmpTable.VERSION3 );
  86.         else {
  87.             System.out.println("Invalid Version Number");
  88.             System.exit(1);
  89.         }
  90.         }
  91.         table.setTargetHost( opt.remArgs[0] );  // set the agent hostname
  92.         if (values[1] != null) // set the community if specified
  93.         table.setCommunity( values[1] );
  94.         try { // set the timeout/retries/port parameters, if specified
  95.         if (values[2] != null)
  96.             table.setTargetPort( Integer.parseInt(values[2]) );
  97.         if (values[3] != null)
  98.             table.setRetries( Integer.parseInt(values[3]) );
  99.         if (values[4] != null)
  100.             table.setTimeout( Integer.parseInt(values[4]) );
  101.         if (values[7] != null)
  102.             userName = values[7];
  103.         if (values[8] != null) 
  104.             authProtocol = values[8];
  105.         if (values[9] != null)
  106.         authPassword = values[9];
  107.         if (values[10] != null) 
  108.         privPassword = values[10];
  109.         if (values[11] != null) 
  110.         contextID  = values[11];
  111.         if (values[12] != null) 
  112.         contextName  = values[12];
  113.                 } catch (NumberFormatException ex) {
  114.         System.err.println("Invalid Integer Argument "+ex);
  115.         }
  116.         if(table.getSnmpVersion() == com.adventnet.snmp.ejb.SnmpTarget.VERSION3) {        
  117.         table.setPrincipal(userName);
  118.         table.setAuthPassword(authPassword);
  119.         table.setPrivPassword(privPassword);
  120.         if(authProtocol.equals("SHA"))
  121.         table.setAuthProtocol(com.adventnet.snmp.ejb.SnmpTarget.SHA_AUTH);
  122.         else
  123.         table.setAuthProtocol(com.adventnet.snmp.ejb.SnmpTarget.MD5_AUTH);
  124. table.setContextName(contextName);
  125. table.setContextID(contextID); // Create the SNMPv3 tables.
  126. table.create_v3_tables();
  127. }
  128.         if (values[5] != null)     try { // Load the MIB files 
  129.         System.out.println("Loading MIBs: "+values[5]);
  130.         table.loadMibs(values[5]);
  131.         System.out.println("Done.");
  132.         } catch (Exception ex) {
  133.         System.err.println("Error loading MIBs: "+ex);
  134.         }
  135.    
  136.  
  137.         // instantiate this class to receive table events, and register it
  138.         com.adventnet.snmp.rmi.SnmpTableListener gettable = new ejbtable();
  139.         weblogic.rmi.server.UnicastRemoteObject.exportObject(gettable);
  140.         table.addSnmpTableListener(gettable);
  141.         // Set the table OID in SnmpTable.  Once specified,
  142.         // SnmpTable will automatically get the data and send table events 
  143. while(true){
  144.         try { 
  145.         table.setTableOID( opt.remArgs[1] );
  146. table.setPollInterval(30);
  147.         } catch (Exception ex) {
  148.         System.err.println("Invalid table OID: "+ex);
  149.         System.exit(1);
  150.         }
  151.         System.out.println("Getting table.  Table items:");
  152.         try { Thread.sleep(table.getPollInterval()*1000); }  // allow some time to get all rows
  153.         catch (InterruptedException ex) {}  
  154. }
  155.     } catch (Exception ex) {
  156.         System.err.println("Failed: "+ ex);
  157.         ex.printStackTrace();
  158.     }
  159.     System.exit(0);
  160.     }
  161.     /* This is the listener method which is notified of table changes **/
  162.     public void tableChanged(com.adventnet.snmp.beans.SnmpTableEvent e) {
  163.    
  164.       
  165.     try {
  166.         StringBuffer sb = new StringBuffer();
  167.         
  168.         if (e.getFirstRow() == 0) {  // we're being notified of first row
  169.         for (int i=0;i<table.getColumnCount();i++)  // print column names
  170.             sb.append(table.getColumnName(i)+" t");
  171.         System.out.println(sb.toString());
  172.         }
  173.         if (table.getRowCount() == 0) { // no rows and we're being notified
  174.         System.err.println("Request failed or timed out. n"+
  175.                    table.getErrorString());
  176.         } else { // print the rows we're getting
  177.         sb = new StringBuffer();
  178.         for (int j=e.getFirstRow();j<=e.getLastRow();j++) { 
  179.             for (int i=0;i<table.getColumnCount();i++) 
  180.             sb.append(table.getValueAt(j,i)+" t");
  181.         }
  182.         System.out.println(sb.toString());
  183.         }
  184.     } catch (Exception ex) {
  185.         System.err.println("Failed: "+ ex);
  186.         ex.printStackTrace();
  187.     }
  188.     }
  189. }