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

SNMP编程

开发平台:

C/C++

  1. /* $Id: snmpv1trap.java,v 1.4 2002/09/09 05:41:20 parasuraman Exp $ */
  2. /*
  3.  * @(#)snmpv1trap.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.  * v1 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 snmpv1trap hostname enterprise-oid agent-addr generic-trap specific-trap 
  13.  *      timeticks [OID type value] ...
  14.  *
  15.  * java snmpv1trap [-d] [-c community] [-p port] host enterprise agent-addr generic-trap specific-trap timeticks [OID {INTEGER | STRING | GAUGE | TIMETICKS | OPAQUE | IPADDRESS | COUNTER} value] ...
  16.  * e.g. 
  17.  * java snmpv1trap -p 162 -c public adventnet 1.2.0 adventnet 0 6 1000 1.5.0 STRING advent
  18.  * 
  19.  * If the oid is not starting with a dot (.) it will be prefixed by .1.3.6.1.2.1 .
  20.  * So the entire OID of 1.1.0 will become .1.3.6.1.2.1.1.1.0 . You can also
  21.  * give the entire OID .
  22.  * * Options: 
  23.  * [-d] option     Debug output. By default off.
  24.  * [-c] option     community String. By default "public".
  25.  * [-p] option     remote port no. By default 162.
  26.  * enterprise      Object Identifier (sysObjectID for generic traps)
  27.  * agent-addr      the IP address of the agent sending the trap
  28.  * generic-trap    generic trap type INTEGER (0..6)
  29.  * specific-trap   specific code INTEGER(0..2147483647)
  30.  * timeticks       the value of object sysUpTime when the event occurred
  31.  * host mandatory  The RemoteHost (agent).Format (string without double qoutes/IpAddress).
  32.  * OID  option     Give multiple no. of Object Identifiers with type and value.
  33.  * type            Type of the object
  34.  * value           object-instance value
  35.  */
  36. import java.lang.*;
  37. import java.util.*;
  38. import java.net.*;
  39. import com.adventnet.snmp.snmp2.*;
  40. import ParseOptions;
  41. public class snmpv1trap {
  42.   public static void main(String args[]) {
  43.       SnmpAPI api;
  44.   // Take care of getting options
  45.       String usage = "snmpv1trap [-d] [-c community] [-p port] host enterprise agent-addr generic-trap specific-trap timeticks [OID {INTEGER | STRING | GAUGE | TIMETICKS | OPAQUE | IPADDRESS | COUNTER} value] ..."; 
  46.       String options[] = { "-d", "-c", "-p"};
  47.       String values[] = { "None", null, null};
  48.       
  49.       ParseOptions opt = new ParseOptions(args,options,values, usage);
  50.       if (opt.remArgs.length<6) opt.usage_error();
  51.       // Start SNMP API
  52.       api = new SnmpAPI();
  53.       if (values[0].equals("Set")) api.setDebug( true );
  54.   // Open session
  55.       SnmpSession session = new SnmpSession(api);
  56.   session.setProtocol(session.TRANSPORT_PROVIDER);   
  57.   
  58.   // set community
  59.   if (values[1] != null) session.setCommunity( values[1] );
  60.   // Options that will be used for communication. This can be modified by the user 
  61.   // according to his need.
  62.   ProtocolOptions params = null;
  63.   if(values[2] != null) {
  64. params = new TcpProtocolOptionsImpl(opt.remArgs[0], Integer.parseInt(values[2]), -1);
  65.   }
  66.   else {
  67. params = new TcpProtocolOptionsImpl(opt.remArgs[0], 162, -1);
  68.   }
  69.       session.setProtocolOptions(params);
  70.   // Build SNMPv1 Trap PDU
  71.       SnmpPDU pdu = new SnmpPDU();
  72.       pdu.setCommand( api.TRP_REQ_MSG );
  73.       // fill in v1 trap PDU fields
  74.   try { 
  75.   // set enterprise OID
  76.           pdu.setEnterprise(new SnmpOID(opt.remArgs[1]));
  77.   // set agent address
  78.           pdu.setAgentAddress(InetAddress.getByName(opt.remArgs[2]));
  79.   // set generic trap type
  80.           pdu.setTrapType(Integer.parseInt(opt.remArgs[3]));
  81.    // set specific code
  82.           if (opt.remArgs.length>4) 
  83.           pdu.setSpecificType(Integer.parseInt(opt.remArgs[4]));
  84.   // set time-stamp
  85.           if (opt.remArgs.length>5) 
  86.           pdu.setUpTime(Integer.parseInt(opt.remArgs[5]));
  87.       } catch (Exception ex) { 
  88.           System.err.println("error in one or more required fields: "+ex);
  89.           opt.usage_error();
  90.       }
  91.   // add Variable Bindings
  92.       for (int i=6;i<opt.remArgs.length;) { 
  93.           if (opt.remArgs.length < i+3) opt.usage_error(); //need "{OID type value}"
  94.           SnmpOID oid = new SnmpOID(opt.remArgs[i++]);
  95.   if (oid.toValue() == null) {
  96.   System.err.println("Invalid OID argument: " + opt.remArgs[i]);
  97.   System.exit(0);
  98.   }
  99.   else {
  100.   addVarBind(pdu, oid, opt.remArgs[i++], opt.remArgs[i++]);
  101.   }
  102.       } // end of add variable bindings
  103.       try { 
  104.   // open session
  105.   session.open(); 
  106.   // Send PDU
  107.   session.send(pdu);
  108. Thread.sleep(1000); 
  109.       } catch (Exception e) {
  110.       System.err.println("Sending PDU"+e.getMessage());
  111.             session.close();
  112.             api.close();       
  113. }
  114.       System.exit(0);
  115.   }
  116.   /** adds the varbind  with specified oid, type and value to the pdu */
  117.   static void addVarBind(SnmpPDU pdu, SnmpOID oid, String type, String value)
  118.   {
  119.   byte dataType ;
  120.   if (type.equals("INTEGER")) {
  121.   dataType = SnmpAPI.INTEGER;
  122.       } else if (type.equals("STRING")) {
  123.   dataType = SnmpAPI.STRING;
  124.       } else if (type.equals("GAUGE")) {
  125.   dataType = SnmpAPI.GAUGE;
  126.       } else if (type.equals("TIMETICKS")) {
  127.   dataType = SnmpAPI.TIMETICKS;
  128.       } else if (type.equals("OPAQUE")) {
  129.   dataType = SnmpAPI.OPAQUE;
  130.       } else if (type.equals("IPADDRESS")) {
  131.   dataType = SnmpAPI.IPADDRESS;
  132.       } else if (type.equals("COUNTER")) {
  133.   dataType = SnmpAPI.COUNTER;
  134.       } else if (type.equals("OID")) { 
  135.   dataType = SnmpAPI.OBJID;
  136.       } else { // unknown type
  137.   System.err.println("Invalid variable type: " + type);
  138.   return;
  139.       }
  140.   SnmpVar var = null;
  141.   try {
  142.   // create variable 
  143.   var = SnmpVar.createVariable( value, dataType );
  144.   }
  145.   catch(SnmpException e){
  146.   System.err.println("Cannot create variable: " + oid + " with value: " + value);
  147.   return;
  148.   }
  149.   // create varbind
  150.   SnmpVarBind varbind = new SnmpVarBind(oid, var);
  151.   // add variable binding
  152.   pdu.addVariableBinding(varbind);
  153. }
  154. }