- /* $Id: Sendv2cTrap.java,v 1.1 2002/06/15 14:42:50 ram Exp $ */
- /*
- * @(#)Sendv2cTrap.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 send a
- * v2c trap message using com.adventnet.snmp.snmp2 package of AdventNetSNMP API.
- *
- * The user could run this application by giving the following usage.
- *
- * java Sendv2cTrap hostname
- *
- * where
- *
- * hostname is the RemoteHost (agent).The Format is string without double quotes/IpAddress.
- *
- *
- */
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import com.adventnet.snmp.snmp2.*;
- public class Sendv2cTrap
- {
- public static void main(String args[])
- {
- if( args.length == 0)
- {
- System.out.println("Usage : java SnmpSendTrap hostname");
- System.exit(0);
- }
- // get the parameter
- String remoteHost = args[0];
- // Start SNMP API
- SnmpAPI api = new SnmpAPI();
- api.setDebug(true);
- SnmpSession session = new SnmpSession(api);
- // set remote Host
- UDPProtocolOptions option = new UDPProtocolOptions(hostname);
- SnmpPDU pdu = new SnmpPDU();
- pdu.setProtocolOptions(option);
- // set remote port
- option.setRemotePort(8001);
- //open the session
- try
- {
- session.open();
- }
- catch (SnmpException e )
- {
- System.err.println("Error opening socket: "+e);
- }
- // Build SNMPv2c Trap PDU
- pdu.setCommand(SnmpAPI.TRP2_REQ_MSG );
- //Add the Up time variable binding - mandatory
- SnmpVar var = null;
- SnmpOID oid = new SnmpOID(".1.3.6.1.2.1.1.3.0");
- try
- {
- //create SnmpVar instance
- var = SnmpVar.createVariable(systemuptime, SnmpAPI.TIMETICKS);
- }
- catch(SnmpException e)
- {
- System.err.println("Cannot create variable: " + oid + " with value: " + systemuptime);
- return;
- }
- //create varbind
- SnmpVarBind varbind = new SnmpVarBind(oid,var);
- // add variable binding
- pdu.addVariableBinding(varbind);
- //Add the Trap OID variable binding - mandatory
- oid = new SnmpOID(".1.3.6.1.6.3.1.1.4.1.0");
- try
- {
- //create SnmpVar instance
- var = SnmpVar.createVariable(trapoidvalue,SnmpAPI.OBJID);
- }
- catch(SnmpException e)
- {
- System.err.println("Cannot create variable: " + oid + " with value: " + trapoidvalue);
- 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);
- }
- }