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)

Show process id in windows

Many people might have faced issues with the processes and first thing comes to your mind to solve the issue is to know about the process identifier (simply PID). The procedure to find it is different for different Operating systems. In linux ps -ef |grep process_name. But when it comes to windows it is different. To view the PID in windows follow the below steps.

1) Right click on the taskbar -> select Task Manager.
2) Click on View -> Select Columns.
3) Select the PID (Process Identifier) and click OK.
4) You can choose many other options from Select Columns.

The screenshot shows the process id.