Sunday, February 6, 2011

SNMP4J Introduction

SNMP (Simple Network Management Protocol) is a protocol which is used to manage and monitor the network of elements. Elements can be any of kind such as routers, switches, printers, workstations, modems, hubs and many more. SNMP4J is an open source API implemented in java. It supports different versions of SNMP such v1/v2c and v3.

To see the SNMP4J API and license details click on snmp4j.

The following section will describe how to retrieve data from a managed object.


import java.util.Vector;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;


public class Snmp4jExample {

/**
* @param args
*/
public static void main(String[] args) {
try {
Address targetAddress = GenericAddress.parse("udp:10.31.88.93/161");
TransportMapping transport = new DefaultUdpTransportMapping();
PDU pdu = new PDU();
Snmp snmp = new Snmp(transport);
CommunityTarget target = new CommunityTarget();
target.setVersion(SnmpConstants.version2c);
target.setAddress(targetAddress);

target.setCommunity(new OctetString("public"));
transport.listen();

pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.2.0")));
pdu.setType(PDU.GET);

ResponseEvent response = snmp.get(pdu, target);
PDU pduResponse = response.getResponse();
System.out.println("pdu response:::"+pduResponse);
Vector vbs = pduResponse.getVariableBindings();
System.out.println("Variable Bindings:::"+vbs);
}catch(Exception e) {
e.printStackTrace();
}

}
}

This example uses snmpv2c to sysobjectid from the switch. The following steps describes in detail.

1) Create an address object with the ip address.
2)

No comments: