corbasendtrap.java.txt
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:6k
- /* $Id: corbasendtrap.java,v 1.3 2002/09/09 05:45:01 tonyjpaul Exp $ */
- /*
- * @(#)corbasendtrap.java
- * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
- * Please read the associated COPYRIGHTS file for more details.
- */
- /**
- * This is an example program to explain how to write an application to do
- * send trap using com.adventnet.snmp.corba of AdventNetSNMP2 api.
- * The user could run this application by giving any one of the following usage.
- *
- * java corbasendtrap [options ] -m MIB_files host enterprise agent-addr generic-trap specific-trap timeticks OID value
- *
- * java corbasendtrap [-SnmpServer hostName] [-ORBInitialPort orbPort] [-ORBInitialHost orbHost] [-c community] [-p port] -m MIB_files host enterprise agent-addr generic-trap specific-trap timeticks OID value
- * e.g.
- * java corbasendtrap -c public -p 2000 -m ....mibsrfc1213-MIB local-host 1.2.0 local-host 1 2 100 1.5.0 trap
- *
- * Options:
- * [-SnmpServer ] <hostName> - SnmpServer SERVER option to specify the CORBA server host.
- * [-ORBInitialPort ] <orbPort> - port on which transient name server is running
- * [-ORBInitialHost ] <orbHost> - host name of transient name server
- * [-c ] <community> - community String. By default "public".
- * [-p ] <port> - remote port no. By default 161.
- * enterprise Mandatory - enterprise oid
- * generic-trap Mandatory - generic trap value
- * specific-trap Mandatory - specific-trap value
- * agent-addr Mandatory - agent address
- * -m Mandatory - MIB files.To load multiple mibs give within double quotes files seperated by a blank space.
- * host Mandatory - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
- * OID Mandatory - Give multiple no. of Object Identifiers.
- */
- import org.omg.CosNaming.*;
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import org.omg.CORBA.*;
- import com.adventnet.snmp.corba.*;
- import ParseOptions;
- public class corbasendtrap
- {
- public static void main( String[] args )
- {
- // Take care of getting options
- String usage = "corbasendtrap [-SnmpServer hostName] [-ORBInitialPort orbPort] [-ORBInitialHost orbHost] [-c community] [-p port] -m MIB_files host enterprise agent-addr generic-trap specific-trap timeticks OID value ";
- String options[] = { "-SnmpServer", "-ORBInitialPort", "-ORBInitialHost",
- "-m", "-c", "-p" };
- String values[] = { null, null, null, null, null, null };
- ParseOptions opt = new ParseOptions(args,options,values, usage);
- // check for at least hostname and one OID in remaining arguments
- if (opt.remArgs.length<8) opt.usage_error();
- try
- {
- // Initialise the ORB
- System.out.println( "Initialising the ORB" );
- org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init( args, null );
- // Use Naming Service
- org.omg.CORBA.Object objRef = orb.resolve_initial_references(
- "NameService" );
- NamingContext ncRef = NamingContextHelper.narrow( objRef );
- // bind the Object Reference in Naming
- NameComponent nc = new NameComponent( "AdventnetSnmpFactory" , "" );
- NameComponent[] path = { nc };
-
- // Bind to the SnmpTarget object
- //System.out.println( "Binding to the Factory object" );
- //CORBAsnmp.SnmpTargetFactory factory = CORBAsnmp.SnmpTargetFactoryHelper.
- //bind( orb, "AdventnetSnmpTargetFactory" );
- com.adventnet.snmp.corba.SnmpFactory factory = com.adventnet.snmp.corba.SnmpFactoryHelper.narrow( ncRef.resolve( path ) );
- System.out.println( "Bound to the Factory object" );
- // Get a Target object
- com.adventnet.snmp.corba.SnmpTarget target = factory.createTarget();
- System.out.println( "Got the SnmpTarget object" );
- target.setTargetHost( opt.remArgs[0] );
- target.setTargetPort( 162 );
- if ( values[4] != null )
- target.setCommunity( values[4] );
- int trap_type = 1, specific_type = -1;
- long time=0;
- try
- {
- if ( values[5] != null )
- target.setTargetPort( Integer.parseInt( values[5] ));
- trap_type = Integer.parseInt( opt.remArgs[3] );
- specific_type = Integer.parseInt( opt.remArgs[4] );
- time = (long) Integer.parseInt( opt.remArgs[5] );
- } catch( NumberFormatException ex ){
- System.err.println( "Invalid Integer Argument " + ex);
- opt.usage_error();
- }
- if (values[3] != null)
- try
- { // Load the MIB files
- System.err.println("Loading MIBs: "+values[3]);
- target.loadMibs(values[3]);
- } catch (com.adventnet.snmp.corba.SnmpException ex)
- {
- System.err.println("Error loading MIBs: " + ex.reason );
- }
- else
- opt.usage_error();
- // create OID and variable value lists from command line
- String oids[] = null, var_values[] = null;
- int num_varbinds = 0;
- for (int i=6;i<opt.remArgs.length;i+=2) { // add Variable Bindings
- if (opt.remArgs.length < i+2) //need "{OID type value}"
- opt.usage_error();
- num_varbinds++;
- }
- oids = new String[num_varbinds];
- var_values = new String[num_varbinds];
- for (int i=0;i<num_varbinds;i++) { // add Variable Bindings
- oids[i] = opt.remArgs[(2*i)+6];
- var_values[i] = opt.remArgs[(2*i)+7];
- }
- // use SnmpTarget methods to send trap w/ specified OIDs/values
- target.setObjectIDList(oids);
- System.err.println("Sending Trap: "+opt.remArgs[1]+ opt.remArgs[2]+ trap_type+
- specific_type+ time+ var_values[0]);
- target.snmpSendTrap(opt.remArgs[1], opt.remArgs[2], trap_type,
- specific_type, (int) time, var_values);
- // allow time to send trap before exiting
- Thread.sleep(500);
- // All done; destroy the server-side Target object
- factory.destroyTarget( target.getName() );
- } catch (Exception e) {
- System.err.println("Error Sending Trap: "+e.getMessage());
- e.printStackTrace();
- }
- System.exit(0);
-
- }
- }