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

SNMP编程

开发平台:

C/C++

  1. /*$Id: walk_indexes.src,v 1.3 2002/09/09 05:36:28 tonyjpaul Exp $*/
  2. /*
  3.  * @(#)walk_indexes.java
  4.  * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
  5.  * Please read the COPYRIGHTS file for more details.
  6.  */
  7. /** 
  8.  *  Get SNMP data based on command line arguments.  Loads MIBs 
  9.  *  as specified, and converts to/from names for loaded MIB data. 
  10.  *
  11.  * [-d]                - Debug output. By default off.
  12.  * [-c] <community>    - community String. By default "public".
  13.  * [-p] <port>         - remote port no. By default 161.
  14.  * [-t] <Timeout>      - Timeout. By default 5000ms.
  15.  * [-r] <Retries>      - Retries. By default 0.      
  16.  *<img SRC="images/v3only.jpg" ALT="v3 only"> [-v] <version>      - version(v1 / v2 / v3). By default v1.
  17.  * [-u] <username>     - The v3 principal/userName
  18.  * [-a] <autProtocol>  - The authProtocol(MD5/SHA). Mandatory if authPassword is specified
  19.  * [-w] <authPassword> - The authentication password.
  20.  * [-s] <privPassword> - The privacy protocol password. Must be accompanied with auth password and authProtocol fields.
  21.  * [-n] <contextName>  - The snmpv3 contextName to be used.
  22.  * [-i] <contextID>    - The snmpv3 contextID to be used.
  23.  * host Mandatory      - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
  24.  * OID  Mandatory      - Give multiple no. of Object Identifiers.
  25.  * mib  Mandatory      - The mibs to be loaded.  
  26.  */
  27. import com.adventnet.snmp.beans.*;
  28. import com.adventnet.snmp.snmp2.*;
  29. import com.adventnet.snmp.mibs.*;
  30. import java.util.*;
  31. public class walk_indexes {
  32.     private static final int COMMUNITY = 1;
  33.     private static final int PORT = 2;
  34.     private static final int RETRIES = 3;
  35.     private static final int TIMEOUT = 4;
  36.     private static final int VERSION = 0;
  37.     private static final int USER_NAME = 5;
  38.     private static final int AUTH_PROTOCOL = 6;
  39.     private static final int AUTH_PASSWORD = 7;
  40.     private static final int PRIV_PASSWORD = 8;
  41. private static final int CONTEXT_NAME = 9;
  42. private static final int CONTEXT_ID = 10;
  43.     private static final int DEBUG = 11;
  44.     public static void main(String args[]) {
  45.     // Take care of getting options
  46.     String usage = "walk_indexes [-v version(v1,v2,v3)] [-c community] [-p port] [-t timeout] [-r retries] [-u user] [-a auth_protocol(MD5/SHA)] [-w auth_password] [-s priv_password] [-n contextName] [-i contextID] [-d debug] host OID MIB_File";
  47.     String options[] = { "-v", "-c", "-p", "-r", "-t", "-u", "-a", "-w", "-s", "-n", "-i", "-d" };
  48.     String values[] = { null, null, null, null, null, null, null, null, null, null, null, "None"};
  49.     String authProtocol = new String("NO_AUTH");
  50.     ParseOptions opt = new ParseOptions(args,options,values, usage);
  51.     if ((values[VERSION] == "v3") && (values[USER_NAME] == null)) opt.usage_error();
  52.     // check for hostname, index, and one OID in remaining arguments
  53.     if (opt.remArgs.length<3) opt.usage_error();
  54.     // Use an SNMP target bean to perform SNMP operations
  55.     SnmpTarget target = new SnmpTarget();
  56.  //To load MIBs from compiled file
  57.  target.setLoadFromCompiledMibs(true);
  58.     if (values[DEBUG].equals("Set")) target.setDebug(true);
  59.         if(values[VERSION] != null) {  // if SNMP version is specified, set it
  60.             if(values[VERSION].equals("v2"))
  61.                 target.setSnmpVersion( SnmpTarget.VERSION2C ) ;
  62.             else if(values[VERSION].equals("v1"))
  63.                 target.setSnmpVersion( SnmpTarget.VERSION1 );
  64.             else if(values[VERSION].equals("v3"))
  65.                 target.setSnmpVersion( SnmpTarget.VERSION3 );
  66.             else {
  67.                 System.out.println("Invalid Version Number"); 
  68.                 System.exit(1);
  69.             }
  70.         }
  71.     target.setTargetHost( opt.remArgs[0] );  // set the agent hostname
  72.     if (values[COMMUNITY] != null) // set the community if specified
  73.         target.setCommunity( values[COMMUNITY] );
  74.     try { // set the timeout/retries/port parameters, if specified
  75.         if (values[PORT] != null) 
  76.             target.setTargetPort( Integer.parseInt(values[PORT]) );
  77.         if (values[RETRIES] != null) 
  78.             target.setRetries( Integer.parseInt(values[RETRIES]) );
  79.         if (values[TIMEOUT] != null) 
  80.             target.setTimeout( Integer.parseInt(values[TIMEOUT]) );
  81.     } catch (NumberFormatException ex) {
  82.         System.err.println("Invalid Integer Argument "+ex);
  83.     }
  84.     if(target.getSnmpVersion() == target.VERSION3) {
  85.         if (values[USER_NAME] != null) 
  86.             target.setPrincipal(values[USER_NAME]);
  87.         if (values[AUTH_PROTOCOL] != null) {
  88.             //System.out.println("authProtocol = " + authProtocol);
  89.             authProtocol = values[AUTH_PROTOCOL];
  90.         }
  91.         if(authProtocol.equals("SHA"))
  92.             target.setAuthProtocol(target.SHA_AUTH);
  93.         else if(authProtocol.equals("MD5"))
  94.             target.setAuthProtocol(target.MD5_AUTH);
  95.         else
  96.             target.setAuthProtocol(target.NO_AUTH);
  97.         if (values[AUTH_PASSWORD] != null) 
  98.             target.setAuthPassword(values[AUTH_PASSWORD]);
  99.         if (values[PRIV_PASSWORD] != null) 
  100.             target.setPrivPassword(values[PRIV_PASSWORD]);
  101. if(values[CONTEXT_NAME]!= null)
  102. target.setContextName(values[CONTEXT_NAME]);
  103. if(values[CONTEXT_ID]!= null)
  104. target.setContextID(values[CONTEXT_ID]);
  105.     }
  106.     if (opt.remArgs[2] != null) try { // Load the MIB files 
  107.         System.err.println("Loading MIBs: "+opt.remArgs[2]);
  108.         target.loadMibs(opt.remArgs[2]);
  109.     } catch (Exception ex) {
  110.         System.err.println("Error loading MIBs: "+ex);
  111.     }
  112. if(target.getSnmpVersion() == target.VERSION3){
  113. target.create_v3_tables();
  114. }
  115.     try { 
  116.       // create OID array from command line arguments
  117.       String oid = opt.remArgs[1];
  118.       MibOperations mibOps = target.getMibOperations();
  119.       SnmpOID rootoid = mibOps.getSnmpOID(oid);
  120.       MibNode node = mibOps.getMibNode(rootoid);
  121.       if ((node == null) || (node.getSyntax()==null)) {
  122.          System.err.println("Need the MIB and leaf node for this. n"+oid);
  123.          System.exit(1);
  124.       }
  125.       if(!node.isTableColumn()){
  126.           System.err.println("Need the MIB and leaf node of a table for this. n"+oid);
  127.           System.exit(1);
  128.       }
  129.       // Set the OID List on the SnmpTarget instance
  130.       target.setSnmpOID(rootoid);
  131.       int maxtry = 0;
  132.       while ( maxtry++ < 1000) {  // limit the max getnexts to 1000
  133.         SnmpVarBind vb = target.snmpGetNextVariableBinding();
  134.         if (vb == null) break;
  135.         if ( !SnmpTarget.isInSubTree(rootoid,target.getSnmpOIDList()[0]) )
  136.           break;  // check first column
  137.         if (maxtry == 1) System.out.println("Response received walking: "+
  138.                         mibOps.toString(rootoid));
  139.         SnmpOID roid = vb.getObjectID();
  140.         String inst = mibOps.getInstanceString(roid);
  141.         Vector indexNodes = node.getIndexes(mibOps);
  142.         Vector v = 
  143.           node.getSyntax().decodeInstanceString(inst, indexNodes);
  144.         StringBuffer sb = new StringBuffer();
  145.         for (int i=0;i<v.size();i++) {
  146.           MibNode indexNode = (MibNode)indexNodes.elementAt(i);
  147.           SnmpVar var = (SnmpVar)v.elementAt(i);
  148.           String s = mibOps.toString( var, mibOps
  149.                       .getSnmpOID(indexNode.getLabel()) );
  150.           if (var instanceof SnmpString) {
  151.         if (indexNode.getLabel().indexOf("Address") != -1)
  152.           s = (new SnmpIpAddress(var.toBytes())).toString();
  153.           }  
  154.           sb.append(" Index"+i+": "+s);
  155.         }
  156.         System.out.println("Indexes: "+sb+"nValue: "+vb.getVariable());
  157.       }
  158.       if (maxtry == 1) {  // we did not get a valid row
  159.         System.err.println("Request failed, timed out or no available data. n"+target.getErrorString());
  160.       } 
  161.     } catch (Exception ex) {
  162.       System.err.println("Exception. "+ex);
  163.     }
  164.     System.exit(0);
  165.     
  166.     }
  167. }