- /* $Id: SnmpSendTrap.java,v 1.1 2002/06/15 14:43:37 ram Exp $ */
- /*
- * @(#)SnmpSendTrap.java
- * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
- * Please read the associated COPYRIGHTS file for more details.
- */
- /**
- * This is a tutorial example program to explain how to write an application to send a
- * v1 Trap message using com.adventnet.snmp.mibs package of AdventNetSNMP2 api.
- *
- *
- *
- * The user could run this application by giving the following usage.
- *
- * java SnmpSendTrap mibfile hostname enterprise agent-addr generic-trap specific-trap timeticks OID value
- *
- * where
- *
- * mibfile is the name of the MIB file that is loaded. To load multiple mibs give within double quotes files seperated
- * by a blank space.
- *
- * hostname is the RemoteHost (agent).The Format is string without double qoutes/IpAddress.
- *
- * OID is the Object Identifier. Multiple OIDs can also be given.
- * The entire OID can be given or it can be given in the form of 1.1.0.
- * If the oid is not starting with a dot (.) it will be prefixed by .1.3.6.1.2.1 .
- * So the entire OID of 1.1.0 will become .1.3.6.1.2.1.1.1.0 .
- *
- * enterprise is the Object Identifier (sysObjectID for generic traps)
- *
- * agent-addr is the IP address of the agent sending the trap
- *
- * generic-trap is the generic trap type INTEGER (0..6)
- *
- * specific-trap is the specific trap code INTEGER(0..2147483647)
- *
- * timeticks is the value of object sysUpTime when the event occurred
- *
- * value is the object instance value to be set
- *
- *
- *
- *
- */
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import com.adventnet.snmp.snmp2.*;
- import com.adventnet.snmp.mibs.*;
- public class SnmpSendTrap {
- public static void main(String args[]) {
- if( args.length < 8)
- {
- System.out.println("Usage : java SnmpSendTrap mibfile hostname enterprise agent-addr generic-trap specific-trap timeticks OID value ");
- System.exit(0);
- }
- // Take care of getting all the parameters
- String mibfile = args[0];
- String remoteHost = args[1];
- String enterprise = args[2];
- String agentaddr = args[3];
- String generictrap = args[4];
- String specifictrap = args[5];
- String timeticks = args[6];
- String OID = args[7];
- String value = args [8];
- // Start SNMP API
- SnmpAPI api;
- api = new SnmpAPI();
- api.start();
- api.setDebug( true );
- // Open session
- SnmpSession session = new SnmpSession(api);
- try {
- session.open();
- } catch (SnmpException e ) {
- System.err.println("Error opening socket: "+e);
- }
- // set remote Host
- session.setPeername(remoteHost);
- // set remote port
- session.setRemotePort(8001);
- //load the MIB file
- MibOperations mibops = new MibOperations();
- try {
- mibops.loadMibModules(mibfile);
- } catch (Exception ex){
- System.err.println("Error loading MIBs: "+ex);
- }
- // Build SNMPv1 Trap PDU
- SnmpPDU pdu = new SnmpPDU();
- pdu.setCommand( api.TRP_REQ_MSG );
- // fill in v1 trap PDU fields
- SnmpOID oids = new SnmpOID(enterprise);
- try {
- pdu.setEnterprise(oids);
- pdu.setAgentAddress(InetAddress.getByName(agentaddr));
- pdu.setTrapType(Integer.parseInt(generictrap));
- pdu.setSpecificType(Integer.parseInt(specifictrap));
- pdu.setUpTime(Integer.parseInt(timeticks));
- }
- catch (Exception ex) {
- System.err.println("error in one or more required fields: "+ex);
- }
- // add OID
- SnmpOID oid = mibops.getSnmpOID(OID);
- MibNode node = mibops.getMibNode(oid);
- // create varbinds
- SnmpVarBind varbind = null;
- try {
- SnmpVar var = node.getSyntax().createVariable(value);
- varbind = new SnmpVarBind(oid,var);
- } catch (SnmpException ex) {
- System.err.println("Error creating variable.");
- }
- pdu.addVariableBinding(varbind);
- try {
- // Send PDU and receive response PDU
- session.send(pdu);
- } catch (SnmpException e) {
- System.err.println("Error sending PDU: "+e);
- }
- // print the response pdu varbinds
- System.out.println(mibops.toString(pdu));
- // close session
- session.close();
- // stop api thread
- api.close();
- }
- }