- /* $Id: Sendv1Trap.java,v 1.1 2002/06/15 14:42:50 ram Exp $ */
- /*
- * @(#)Sendv1Trap.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
- * v1 trap message using com.adventnet.snmp.snmp2 package of AdventNetSNMP API.
- *
- * The user could run this application by giving the following usage.
- *
- * java Sendv1Trap 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 Sendv1Trap
- {
- 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 SNMPv1 Trap PDU
- pdu.setCommand(SnmpAPI.TRP_REQ_MSG );
- // set the enterprise OID
- SnmpOID enterpriseOID = new SnmpOID(".1.3.6.1.2.1.11");
- //Set the Agent Address.
- InetAddress hostName = InetAddress.getByName("xyz");
- pdu.setAgentAddress(hostName);
- //Set Trap Type.
- pdu.setTrapType(1);
- //Set Specific Trap Type
- pdu.setSpecificType(0);
- //Set the up time value
- pdu.setUpTime(100);
- // 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 trap PDU
- try
- {
- session.send(pdu);
- }
- catch (SnmpException e)
- {
- System.err.println("Sending PDU "+e.getMessage());
- }
- System.exit(0);
- }
- }