rmisendtrap.java.txt
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:6k
- /* $Id: rmisendtrap.java,v 1.3 2002/09/09 05:41:24 tonyjpaul Exp $ */
- /*
- * @(#)rmisendtrap.java
- * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
- * Please read the COPYRIGHTS file for more details.
- */
- /**
- * Use RMI to send SNMP trap based on command line arguments. Loads MIBs
- * as specified, and converts to/from names for loaded MIB data.
- * The RMI server is specified with the -SnmpServer option
- * and defaults to localhost if not specified.
- * Since variable types are not input, MIBs have to be loaded for
- * any variables being used in trap PDU.
- * Once a MIB is loaded on the server, susequent uses of
- * This program will have the MIB available for SNMP operations.
- *
- * java rmisendtrap [options ] -m MIB_files host enterprise agent-addr generic-trap specific-trap timeticks OID value
- *
- * java rmisendtrap [-SnmpServer hostName] [-c community] [-p port] -m MIB_files host enterprise agent-addr generic-trap specific-trap timeticks OID value
- * e.g.
- * java rmisendtrap -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 RMI server host.
- * [-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 java.lang.*;
- import java.util.*;
- import java.net.*;
- import java.rmi.*;
- public class rmisendtrap {
- public static void main(String args[]) {
- // Take care of getting options
- String usage = "rmisendtrap [-SnmpServer hostName] [-c community] [-p port] -m MIB_files host enterprise agent-addr generic-trap specific-trap timeticks [OID value] ...";
- String options[] = { "-c", "-m", "-p", "-SnmpServer" };
- String values[] = { null,null, null, null};
-
- ParseOptions opt = new ParseOptions(args,options,values, usage);
- if (opt.remArgs.length<6) opt.usage_error();
- com.adventnet.snmp.rmi.SnmpFactory factory = null;
- com.adventnet.snmp.rmi.SnmpTarget target = null;
- try {
- System.out.println( "Performing lookup with RMI Registry ... " );
- if(values[3] != null)
- factory = (com.adventnet.snmp.rmi.SnmpFactory)
- Naming.lookup( "rmi://" + values[3] + "/AdventnetSnmpFactory" );
- else
- factory = (com.adventnet.snmp.rmi.SnmpFactory)
- Naming.lookup( "rmi:///AdventnetSnmpFactory" );
- System.out.println( "Lookup succeeded " );
- // Get a Target object
- target = factory.createTarget();
- target.setTargetHost( opt.remArgs[0] ); // set destination hostname
- target.setTargetPort( 162 ); // set destination port
- if (values[0] != null) // set the community if specified
- target.setCommunity( values[0] );
- int trap_type=-1,specific_type=-1;
- long time=0;
- try { // set the port parameter, if specified, and trap parameters
- if (values[2] != null)
- target.setTargetPort( Integer.parseInt(values[2]) );
- 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[1] != null) try { // Load the MIB files
- System.err.println("Loading MIBs: "+values[1]);
- target.loadMibs(values[1]);
- } catch (Exception ex) {
- System.err.println("Error loading MIBs: "+ex);
- }
- else
- opt.usage_error();
- // Put together OID and variable value lists from command line
- String oids[] = null, var_values[] = null; // trap oids and values
- 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];
- }
- if(oids != null)
- // use SnmpTarget methods to send trap w/ specified OIDs/values
- target.setObjectIDList(oids);
-
- if(var_values != null && var_values.length > 0)
- System.err.println("Sending Trap: "+opt.remArgs[1]+ opt.remArgs[2]+ trap_type+
- specific_type+ time+ var_values[0]);
- else
- System.err.println("Sending Trap: "+opt.remArgs[1]+ opt.remArgs[2]+ trap_type+
- specific_type+ time);
- target.snmpSendTrap(opt.remArgs[1], opt.remArgs[2], trap_type,
- specific_type, time, var_values);
- // allow time to send trap before exiting
- Thread.sleep(500);
- } catch (Exception e) {
- System.err.println("Error Sending Trap: "+e.getMessage());
- e.printStackTrace();
- }
- System.exit(0);
-
- }
- }