- /* $Id: SnmpSendTrap.java,v 1.1 2002/06/15 14:42:11 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.snmp2 package of AdventNetSNMP2 api.
- *
- * The user could run this application by giving the following usage.
- *
- * java SnmpSendTrap hostname enterprise agent-addr generic-trap specific-trap timeticks OID type value
- *
- * where
- *
- * 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
- *
- * type is the Object type (STRING, INTEGER, COUNTER etc)
- *
- * value is the object instance value to be set *
- *
- *
- *
- */
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import com.adventnet.snmp.snmp2.*;
- public class SnmpSendTrap {
- public static void main(String args[]) {
- if( args.length < 8)
- {
- System.out.println("Usage : java SnmpSendTrap hostname enterprise agent-addr generic-trap specific-trap timeticks OID type value");
- System.exit(0);
- }
- // Take care of getting all the parameters
- String remoteHost = args[0];
- String enterprise = args[1];
- String agentaddr = args[2];
- String generictrap = args[3];
- String specifictrap = args[4];
- String timeticks = args[5];
- String OID = args[6];
- String type = 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);
- // set remote Host
- session.setPeername(remoteHost);
- // set remote port
- session.setRemotePort(8001);
- //open the session
- try {
- session.open();
- } catch (SnmpException e ) {
- System.err.println("Error opening socket: "+e);
- }
- // 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 = new SnmpOID(OID);
- // get the type
- byte dataType;
- if (type.equals("STRING")) {
- dataType = SnmpAPI.STRING;
- }
- else {
- System.err.println("Invalid variable type: " + type);
- return;
- }
- // create SnmpVar instance for the value and the type
- SnmpVar var = null;
- try {
- var = SnmpVar.createVariable( value, dataType );
- }
- catch(SnmpException e){
- System.err.println("Cannot create variable: " + oid + " with value: " + value);
- return;
- }
- //create varbind
- SnmpVarBind varbind = new SnmpVarBind(oid, var);
- // add variable binding
- pdu.addVariableBinding(varbind);
- // send PDU
- try {
- session.send(pdu);
- } catch (SnmpException e) {
- System.err.println("Sending PDU"+e.getMessage());
- }
- System.exit(0);
- }
- }