Hi
I have to send custom messages in trap to Opennms Server.
I tried with JoeSNMP and SNMP4j to send traps to server, but i cant see traps
in Server side.
I have pasted the code below for both JoeSNMP and SNMP4j.
How to configure OID in server side to monitor ?
Do i need to create custom OID ?
Can any one say where im wrong.
I have configured a custom OID as per the following links
http://opennms.org/wiki/SNMP_Reports_How-To
http://conf.se/?p=112
----------------------------------------------------------------------
code for JoeSNMP
----------------------------------------------------------------------
public class snmpagent implements SnmpHandler {
/** SNMP parameter for community */
private static final String PARAM_COMMUNITY = "public";
/** SNMP parameter for number of retries */
private static final int PARAM_RETRIES = 10;
/** SNMP parameter for timeout */
private static final int PARAM_TIMEOUT = 5000;
/** SNMP parameter for generic type (V1 only) */
private static final int PARAM_GENERIC_TYPE = 0;
/** SNMP parameter for specific type (V1 only) */
private static final int PARAM_SPECIFIC_TYPE = 0;
/** SNMP OID of the sent trap */
private String m_trapOid;
/** Holds the reference to the SNMP session */
private SnmpSession m_session;
public snmpagent(String managerAddress, int managerPort,
String trapOid) throws Exception {
// Memorize attributes
m_trapOid = trapOid;
// Build the SNMP session
try {
// Create the peer object
SnmpPeer peer = new SnmpPeer(InetAddress.getByName(managerAddress));
peer.setPort(managerPort);
peer.setRetries(PARAM_RETRIES);
peer.setTimeout(PARAM_TIMEOUT);
SnmpParameters parameters = peer.getParameters();
parameters.setReadCommunity(PARAM_COMMUNITY);
System.out.println(managerAddress+"~~~~~~~~"+managerPort+"~~~~~~~~"+trapOid);
// Create the session object
m_session = new SnmpSession(peer);
m_session.setDefaultHandler(this);
} catch (Exception e) {
System.out.println("SNMP agent creation error");
}
}
public void close() {
m_session.close();
System.out.println("Session closed :"+m_session.isClosed());
}
public static void main(String[] args) throws Exception {
snmpagent s=new snmpagent("192.168.1.151", 162,
".1.3.6.1.4.1.2021.8.2.101.2");
s.sendV2Trap("Test");
s.close();
}
public void sendV2Trap(String message) {
try {
// Set SNMP version as V2
SnmpPeer peer = m_session.getPeer();
System.out.println("Peeer port "+peer.getPort());
System.out.println("Peeer IP "+peer.getPeer());
SnmpParameters parameters = peer.getParameters();
parameters.setVersion(SnmpSMI.SNMPV2);
// Create trap
SnmpPduRequest trapPdu = new SnmpPduRequest(SnmpPduPacket.V2TRAP);
trapPdu.addVarBind(new SnmpVarBind(new SnmpObjectId(m_trapOid),
new SnmpOctetString(message.getBytes())));
// Send trap
System.out.println("Sending trap");
m_session.send(trapPdu);
System.out.println("Finished sending trap
:"+m_session.getOutstandingCount());
} catch (Exception e) {
System.out.println("SNMP send error");
}
}
public void snmpInternalError(SnmpSession session, int err, SnmpSyntax pdu) {
System.out.println("SNMP internal error, code: " + err);
}
public void snmpTimeoutError(SnmpSession session, SnmpSyntax pdu) {
System.out.println("SNMP timeout");
}
public void snmpReceivedPdu(SnmpSession session, int cmd, SnmpPduPacket pdu) {
// Do nothing (we're not interested in incoming SNMP messages)
}
}
-------------------------------------------------------------
Code for SNMP4J
-------------------------------------------------------------
public class TestSnmp4j {
public static void main(String[] args) throws IOException {
TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
snmp.listen();
CommunityTarget target1 = new CommunityTarget();
target1.setAddress(new UdpAddress("192.168.1.151/162"));
target1.setCommunity(new OctetString("public"));
target1.setVersion(SnmpConstants.version2c);
PDU request = new PDU();
request.setType(PDU.TRAP);
request.add(new VariableBinding(new
OID(".1.3.6.1.4.1.2021.8.2.101.2"), new OctetString("Test Trap")));
ResponseEvent responseEvent = snmp.send(request, target1);
System.out.println("~~~~~~~~"+request);
ResponseEvent response = snmp.send(request, target1);
}
}
Thanks & Regards
P. Kaviarasu |Senior Software Engineer - CDO
T: +91 44 6108 4100 | Ext:4553 | F: +91 44 6108 4101
________________________________________
The information contained in this e-mail message or messages (which includes
any attachments) is confidential and may be legally privileged. It is intended
only for the use of the person or entity to which it is addressed.
------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:
Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
Please read the OpenNMS Mailing List FAQ:
http://www.opennms.org/index.php/Mailing_List_FAQ
opennms-devel mailing list
To *unsubscribe* or change your subscription options, see the bottom of this
page:
https://lists.sourceforge.net/lists/listinfo/opennms-devel