Thursday, February 28, 2013

Create SNMP Client in Java using SNMP4j

Please follow the below mentioned steps to write a very simple SNMP4J application using Java and eclipse IDE.

First you need to download a copy of SNMP4J.jar. You can get it from http://www.snmp4j.org/html/download.html

Then you need to decide how you are going to test your application. Check whether you have access to any real network devices like Cisco routers or Juniper routers or whether you are aware of any SNMP agent running on any of your servers.There is nothing to worry if you cannot find one , you can start SNMP agent on your desktop machine as well. Here I am giving an example of how you can use your Windows machine to test your application by starting the SNMP service on the machine.

I am using a Windows XP machine and below are the steps needed to start SNMP agent on the machine.

1. Go to Start->Control Panel -> Add or Remove Programs -> Add or Remove Windows Components
     Select or Check "Management and Monitoring Tools".


    This will start the SNMP Agent running on your windows machine.

2. Verify SNMP agent running on your machine
    Go to Start->Control Panel -> Administrative Tools->Services and verify the service. You can also add 
    new community strings for additional security here.


3. Now come to Java coding. Open a Java application project in Eclipse and add the SNMP4J.jar to the
    library path.

Below code is almost self explanatory. Please refer to http://www.snmp4j.org/doc/index.html for API reference. You are welcome with questions or comments.

Please note that I have tried snmpget on 'sysDescr' MIB on my Windows machine and got the output "
Hardware: x86 Family 6 Model 23 Stepping 10 AT/AT COMPATIBLE - Software: Windows 2000 Version 5.1 (Build 2600 Multiprocessor Free)". This is Cool.....




import java.io.IOException;
import java.util.Vector;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.smi.*;
import org.snmp4j.mp.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.util.*;
 

public class Collector
{
 
    public static void main(String[] args) {
        try {
            Snmp snmp4j =  new Snmp(new DefaultUdpTransportMapping());
            snmp4j.listen();
            Address add = new UdpAddress("192.168.1.132"+"/"+"161");
            CommunityTarget target = new CommunityTarget();
            target.setAddress(add);
            target.setTimeout(500);
             
            target.setRetries(3);

            target.setCommunity(new OctetString("public"));
            target.setVersion(SnmpConstants.version2c);
            
            PDU request = new PDU();
            request.setType(PDU.GET);
            OID oid= new OID(".1.3.6.1.2.1.1.1.0");
            request.add(new VariableBinding(oid));

            PDU responsePDU=null;
            ResponseEvent responseEvent;
            responseEvent = snmp4j.send(request, target);

            if (responseEvent != null)
            {
                responsePDU = responseEvent.getResponse();
                if ( responsePDU != null)
                {
                                
                    Vector <VariableBinding> tmpv = responsePDU.getVariableBindings();
                    if(tmpv != null)
                    {
                        for(int k=0; k <tmpv.size();k++)
                        {
                            VariableBinding vb = (VariableBinding) tmpv.get(k);
                            String output = null;
                            if ( vb.isException())
                            {

                                String errorstring = vb.getVariable().getSyntaxString();
                                System.out.println("Error:"+errorstring);
                            }
                            else
                            {
                                String sOid = vb.getOid().toString();
                                Variable var = vb.getVariable();
                                OctetString oct = new OctetString((OctetString)var);
                                String sVar =  oct.toString();

                                System.out.println("success:"+sVar);
                            }

                        
                    }
                    
                }

            }
            }

        } catch (IOException e) {
             
            e.printStackTrace();
        }

        

    }

}