Hi,

I am sending the code with the error line no.
//import java.net.InetAddress;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;
//import java.lang.NullPointerException;
public class SNMP4JHelper
{
    public static final int SNMP_PORT = 161;
    public static final String READ_COMMUNITY = "public";
    public static final String WRITE_COMMUNITY= "private";
    public static final int mSNMPVersion =0; // 0 represents SNMP version=1
    public static final String OID_Hard_disk = "1.3.6.1.2.1.1.3.0";
//     "1.3.6.1.4.1.2021.9.1.7.1";
//        ".1.3.6.1.4.1.2021.4.4.0";
//        "1.3.6.1.4.1.311.1.1.3.1.1.5.1.3";
        //"1.3.6.1.4.1.318.1.1.1.12.3.2.1.3.1";
    //public static final String 
OID_UPS_BATTERY_CAPACITY="1.3.6.1.4.1.318.1.1.1.2.2.1.0";
    public static void main(String[] args)
    {
        try
        {
//            "172.20.1.150"
            String strIPAddress = "127.0.0.1";
            SNMP4JHelper objSNMP = new SNMP4JHelper();
            
        //objSNMP.snmpSet();
        int Value = 1;
            ///////////////////////////////////////////
            //Set Value=2 to trun OFF UPS OUTLET Group1
            //Value=1 to trun ON UPS OUTLET Group1
            //////////////////////////////////////////
            
        //    "strIPAddress, WRITE_COMMUNITY,OID_Hard_disk, Value"
        //    UPS_OUTLET_GROUP1
            objSNMP.snmpSet(strIPAddress, WRITE_COMMUNITY,OID_Hard_disk, Value);
            //////////////////////////////////////////////////////////
            //Get Basic state of UPS
            /////////////////////////////////////////////////////////
//line 45//
String batteryCap =objSNMP.snmpGet(strIPAddress,READ_COMMUNITY,OID_Hard_disk);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    /*
     * The following code valid only SNMP version1. This
     * method is very useful to set a parameter on remote device.
     * */
     
    public void snmpSet(String strAddress, String community, String strOID, int 
Value)
    {
        strAddress= strAddress+"/"+SNMP_PORT;
        Address targetAddress = GenericAddress.parse(strAddress);
        Snmp snmp;
        try
        {
            TransportMapping transport = new DefaultUdpTransportMapping();
            snmp = new Snmp(transport);
            transport.listen();
            CommunityTarget target = new CommunityTarget();
            target.setCommunity(new OctetString(community));
            target.setAddress(targetAddress);
            target.setRetries(2);
            target.setTimeout(5000);
            target.setVersion(SnmpConstants.version1);
            PDU pdu = new PDU();
//            pdu.add(new VariableBinding(new OID(strOID), new 
Integer32(Value)));
            pdu.setType(PDU.SET);
            ResponseListener listener = new ResponseListener() 
            {
                public void onResponse(ResponseEvent event) 
                {
                    // Always cancel async request when response has been 
received
                    // otherwise a memory leak is created! Not canceling a 
request
                    // immediately can be useful when sending a request to a 
broadcast
                    // address
                    ((Snmp)event.getSource()).cancel(event.getRequest(), this);
                    if ( event.getResponse() != null )
                    {
                        System.out.println("Set Status 
is:"+event.getResponse().getErrorStatusText());
                    }
                }
            };
            snmp.send(pdu, target, null, listener);
            
            snmp.close();
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    /*
     * The code is valid only SNMP version1. SnmpGet method
     * return Response for given OID from the Device.
     */
     
    public String snmpGet(String strAddress, String community, String strOID)
    {
        String str="";
        try
        {
            OctetString comm1 = new OctetString(community);
            strAddress= strAddress+"/"+ SNMP_PORT;
            Address targetaddress = new UdpAddress(strAddress);
            TransportMapping transport = new DefaultUdpTransportMapping();
            transport.listen();
            CommunityTarget comtarget = new CommunityTarget();
            comtarget.setCommunity(comm1);
            comtarget.setVersion(SnmpConstants.version1);
            comtarget.setAddress(targetaddress);
            comtarget.setRetries(2);
            comtarget.setTimeout(5000);
            PDU pdu = new PDU();
            ResponseEvent response;
            Snmp snmp;
            pdu.add(new VariableBinding(new OID(strOID)));
            pdu.setType(PDU.GET);
            snmp = new Snmp(transport);
            response = snmp.get(pdu,comtarget);
            if(response != null)
            {
//line 131//    
System.out.println( "Error msg : " + response.getError().toString() );
                
if(response.getResponse().getErrorStatusText().equalsIgnoreCase("Success"))
                {
                    PDU pduresponse=response.getResponse();
                    
str=pduresponse.getVariableBindings().firstElement().toString();
                    if(str.contains("="))
                    {
                        int len = str.indexOf("=");
                        str=str.substring(len+1, str.length());
                    }
                }
            }
            else
            {
                System.out.println("Feeling like a TimeOut occured ");
            }
            snmp.close();
        } catch(Exception e) 
        { e.printStackTrace(); }
        System.out.println("Response="+str);
        return str;
    }
}




_______________________________________________
SNMP4J mailing list
SNMP4J@agentpp.org
http://lists.agentpp.org/mailman/listinfo/snmp4j

Reply via email to