Sending Traps

 

Sending traps are normally the functions of the SNMP agents. The trap-sending functionality should be used only for testing and simulating purposes. A full-fledged trap functionality is the responsibility of the agent.

 

Using High-Level API explains the usage of high-level API in sending SNMP traps.

 

Using Low-Level API explains the usage of low-level API in sending SNMP traps.

 

Using High-Level API

 

The SnmpTarget bean has API methods to send traps. To send SNMPv1 traps, use the snmpSendTrap() method of SnmpTarget class. Following are the steps involved in sending SNMPv1 trap using the SnmpTarget.

 

//instantiate the SnmpTarget bean

SnmpTarget target = new SnmpTarget();

target.settargetHost("hostname");

 

//set the Target Port on which the target host would be listening

target.setTargetPort(162);

 

//Set the SnmpOID

target.setObjectID("1.5.0");

 

//Send v1 trap

String values[] = {"testing"};

target.snmpSendTrap("1.2.0", "localhost", 0, 6, 1000, values);

 

View the complete example present in <tutorials/highlevelapi/SendTrap.java>.

 

Using Low-Level API

 

Following are the steps involved in sending a SNMPv1 trap using the low-level API.

 

//Instantiate an SnmpPDU class

SnmpPDU pdu = new SnmpPDU();

 

//Set the SNMP command on the SnmpPDU class as v1 trap

SnmpAPI api = new SnmpAPI();

UDPProtocolOptions option = new UDPProtocolOptions(remoteHost);

pdu.setProtocolOptions(option);

 

//Build SNMPv1 trap

pdu.setCommand(SnmpAPI.TRP_REQ_MSG);

 

//Set the Enterprise OID.

SnmpOID enterpriseOID = new SnmpOID(".1.3.6.1.2.1.11");

pdu.setEnterprise(enterpriseOID);

 

//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;

dataType = SnmpAPI.STRING;

 

//create SnmpVar instance for the value and the type

SnmpVar var = SnmpVar.createVariable(value, dataType);

 

//create varbind

SnmpVarBind varbind = new SnmpVarBind(oid, var);

 

// add variable binding

pdu.addVariableBinding(varbind);

 

//Send the Trap PDU

SnmpSession session = new SnmpSession(api);

session.open();

session.send(pdu);

 

View the complete example present in <tutorials/lowlevelapi/Sendv1Trap.java>.

 

 



Copyright © 1996-2006, AdventNet Inc. All Rights Reserved.