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

SNMP编程

开发平台:

C/C++

  1. /* $Id: snmpv2ctrap.src,v 1.5 2002/09/09 05:36:52 parasuraman Exp $ */
  2. /*
  3.  * @(#)snmpv2ctrap.java
  4.  * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
  5.  * Please read the associated COPYRIGHTS file for more details.
  6.  */
  7. /**
  8.  * This is an example program to explain how to write an application to send a
  9.  * v2c Trap message using com.adventnet.snmp.snmp2 package of AdventNetSNMP2 api.
  10.  * The user could run this application by giving any one of the following usage.
  11.  *  
  12.  * java snmpv2ctrap [options] hostname timeTicks snmpTrapOid-value [OID type value] ...
  13.  *
  14.  * java snmpv2ctrap [-d] [-c community] [-p port] host TimeTicksvalue TrapOIDvalue [OID {INTEGER | STRING | GAUGE | TIMETICKS | OPAQUE | IPADDRESS | COUNTER | COUNTER64 | UNSIGNED32} value] ...
  15.  * e.g. 
  16.  * java snmpv2ctrap -p 162 -c public adventnet 1000 1.2.0 1.5.0 STRING advent
  17.  * 
  18.  * If the oid is not starting with a dot (.) it will be prefixed by .1.3.6.1.2.1 .
  19.  * So the entire OID of 1.1.0 will become .1.3.6.1.2.1.1.1.0 . You can also
  20.  * give the entire OID .
  21.  *
  22.  * Options: 
  23.  * [-d]                  - Debug output. By default off.
  24.  * [-c] <community>      - community String. By default "public".
  25.  * [-p] <port>           - remote port no. By default 162.
  26.  * <host>      Mandatory - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
  27.  * <timeticks> Mandatory - the value of object sysUpTime when the event occurred
  28.  * <OID-value> Mandatory - Object Identifier  
  29.  * <OID>                 - Give multiple no. of Object Identifiers with value.
  30.  * <type>                - Type of the object
  31.  * <value>               - object-instance value
  32.  */
  33. import java.lang.*;
  34. import java.util.*;
  35. import java.net.*;
  36. import com.adventnet.snmp.snmp2.*;
  37. public class snmpv2ctrap {
  38.   public static void main(String args[]) {
  39.     SnmpAPI api;
  40.             
  41.     // Take care of getting options
  42.     String usage = "snmpv2ctrap [-d] [-c community] [-p port] [-g agent-address]  nhost TimeTicksvalue TrapOIDvalue [OID {INTEGER | STRING | GAUGE | TIMETICKS | OPAQUE | IPADDRESS | COUNTER | COUNTER64 | UNSIGNED32} value] ...";
  43.     String options[] = { "-d", "-c", "-p","-g"};
  44.     String values[] = { "None", null, null,null};
  45.     ParseOptions opt = new ParseOptions(args,options,values, usage);
  46.     if (opt.remArgs.length<3) opt.usage_error();
  47.                      
  48.     // Start SNMP API
  49.     api = new SnmpAPI();
  50.     if (values[0].equals("Set")) api.setDebug( true );
  51.                 
  52.     // Open session 
  53.     SnmpSession session = new SnmpSession(api);
  54.     
  55.     // set v2c version
  56.     session.setVersion( SnmpAPI.SNMP_VERSION_2C ) ;
  57.     // set remote Host
  58.     session.setPeername( opt.remArgs[0] );
  59.     // set community
  60.     if (values[1] != null) session.setCommunity( values[1] );
  61.     // set remote Port
  62.     try {
  63.         if (values[2] != null) 
  64.             session.setRemotePort( Integer.parseInt(values[2]) );        
  65.         else 
  66.             session.setRemotePort(162);
  67.     }
  68.     catch (NumberFormatException ex) {
  69.         System.err.println("Invalid Integer Arg");
  70.     }
  71.     // Build v2 trap PDU
  72.     SnmpPDU pdu = new SnmpPDU();
  73.     pdu.setCommand( api.TRP2_REQ_MSG );
  74.     
  75.     // Add the sysUpTime variable binding
  76.     SnmpOID oid = new SnmpOID(".1.3.6.1.2.1.1.3.0");
  77.     if (oid.toValue() == null) 
  78.         System.err.println("Invalid OID argument: .1.3.6.1.2.1.1.3.0");
  79.     else {
  80.         SnmpVar var = null ; 
  81.         try {
  82.             var = SnmpVar.createVariable(opt.remArgs[1], SnmpAPI.TIMETICKS);
  83.         }
  84.         catch (SnmpException e) {
  85.             System.err.println("Cannot create variable: " +oid+" with value: "+opt.remArgs[1]);
  86.         }
  87.         SnmpVarBind varbind = new SnmpVarBind(oid, var);
  88.         pdu.addVariableBinding(varbind);
  89.         
  90.     }        
  91.     //Add snmpTrapOid variable bindings
  92.      oid = new SnmpOID(".1.3.6.1.6.3.1.1.4.1.0");
  93.     if (oid.toValue() == null) System.err.println("Invalid OID argument: .1.3.6.1.6.3.1.1.4.1.0");
  94.     else {
  95.         SnmpVar var = null ;
  96.         try {
  97.             var = SnmpVar.createVariable(opt.remArgs[2], SnmpAPI.OBJID);
  98.         }
  99.         catch (SnmpException e) {
  100.             System.err.println("Cannot create variable: " +oid+" with value: "+opt.remArgs[2]);
  101.         }
  102.         SnmpVarBind varbind = new SnmpVarBind(oid, var);
  103.         pdu.addVariableBinding(varbind);
  104.         
  105.     }    
  106. String agentAddress="";
  107. String community="";
  108. int otherVarBinds=0;
  109.     
  110.     // add Variable Bindings
  111.     for (int i=3;i<opt.remArgs.length;) {
  112.       if (opt.remArgs.length < i+3) opt.usage_error(); //need "{OID type value}"
  113.      
  114.         oid = new SnmpOID(opt.remArgs[i++]);
  115.       if (oid.toValue() == null) 
  116.         System.err.println("Invalid OID argument: " + opt.remArgs[i]);
  117.       else 
  118.         addVarBind(pdu, oid, opt.remArgs[i++], opt.remArgs[i++]);
  119.     } // end of add variable bindings
  120. if(values[3]!=null)
  121. {
  122.         oid = new SnmpOID(".1.3.6.1.6.3.18.1.3.0");
  123.         addVarBind(pdu, oid,"STRING" , values[3]);
  124. }
  125. if(values[1]!=null)
  126. {
  127.         oid = new SnmpOID(".1.3.6.1.6.3.18.1.4.0");
  128.         addVarBind(pdu, oid,"STRING", values[1]);
  129. }
  130.     try {
  131.         // open session
  132.         session.open();
  133.         // Send PDU
  134.          session.send(pdu);
  135.     
  136.     } 
  137.     catch (SnmpException e) {
  138.       System.err.println("Sending PDU"+e.getMessage());
  139.     }
  140.     // close session    
  141.     session.close();
  142.     // stop api thread
  143.     api.close();
  144.     System.exit(0);
  145.   }
  146.  /** <img SRC="images/v2candv3only.jpg" ALT="v3 only"> adds the varbind with specified oid, type and value to the pdu */
  147.   static void addVarBind(SnmpPDU pdu, SnmpOID oid, String type, String value)
  148.     {        
  149.       byte dataType ;
  150.       if (type.equals("INTEGER")) {
  151.           dataType = SnmpAPI.INTEGER;
  152.       } else if (type.equals("STRING")) {
  153.           dataType = SnmpAPI.STRING;
  154.       } else if (type.equals("GAUGE")) {
  155.           dataType = SnmpAPI.GAUGE;
  156.       } else if (type.equals("TIMETICKS")) {
  157.           dataType = SnmpAPI.TIMETICKS;
  158.       } else if (type.equals("OPAQUE")) {
  159.           dataType = SnmpAPI.OPAQUE;
  160.       } else if (type.equals("IPADDRESS")) {
  161.           dataType = SnmpAPI.IPADDRESS;
  162.       } else if (type.equals("COUNTER")) {
  163.           dataType = SnmpAPI.COUNTER;
  164.       } else if (type.equals("OID")) { 
  165.           dataType = SnmpAPI.OBJID;
  166.       } else { // unknown type
  167.           System.err.println("Invalid variable type: " + type);
  168.           return;
  169.       }
  170.       SnmpVar var = null;
  171.       try {
  172.           // create variable
  173.           var = SnmpVar.createVariable( value, dataType );
  174.       }
  175.       catch(SnmpException e){
  176.           System.err.println("Cannot create variable: " + oid + " with value: " + value);
  177.           return;
  178.       }
  179.       // create varbind
  180.       SnmpVarBind varbind = new SnmpVarBind(oid, var);
  181.       // add variable binding
  182.       pdu.addVariableBinding(varbind);
  183.     }
  184.   
  185. }