Showing posts with label snmp call from java. Show all posts
Showing posts with label snmp call from java. Show all posts

Wednesday, October 10, 2007

SNMP Call from Java

SNMP code for clearning a port using SNMPJ , I have implemented this in Turbine/Velocity framework as an ajax call .

package org.mymodule;

import org.apache.turbine.util.RunData;
import org.apache.velocity.context.Context;
import org.mymodule.modules.util.LMLab;
import org.netsnmp.* ;
import org.netsnmp.ASN.INTEGER;
import org.netsnmp.ASN.OCTET_STR ;

public class Snmp extends SecureScreen {

public void doBuildTemplate(RunData data, Context context) throws Exception
{
data.setContentType("text/xml");
LMLab lab = (LMLab)data.getSession().getAttribute("sellab");
String community = lab.getCommunitystr();
if ((community == null)||(community.equals("")))
throw new Exception("ERROR-NO COMMUNITY STRING");

NetSNMPSession session ;
MyListener listener ;
PDU pdu ;
OID sysLocationOID ;
INTEGER lineno;
int lnlength;

String host = data.getParameters().getString("ip",null);
//String community = "private";
String newLocation = data.getParameters().getString("port",null);

if (host == null)
throw new Exception("ERROR-NO HOST");

if (newLocation == null)
throw new Exception("ERROR-NO PORT");
lnlength =newLocation.length() ;
if (lnlength >2)
lineno = new INTEGER(Integer.valueOf(newLocation.substring(lnlength-2,lnlength)).intValue());
else
lineno = new INTEGER(Integer.valueOf(newLocation).intValue());
/*;
* Open a session to the remote host
*/
session = new NetSNMPSession(host, community) ;

/*
* Create a PDU that will perform a 'SET' operation
*/
pdu = new PDU(NetSNMP.MSG_SET) ;

/*
* Create an OID for the systemLocation
*/
try {
//sysLocationOID = new DefaultOID("SNMPv2-MIB::sysLocation.0") ;
//sysLocationOID = new DefaultOID("SNMPv2-MIB::tsClrTtyLine.0") ;

sysLocationOID = new DefaultOID(".1.3.6.1.4.1.9.2.9.10.0") ;

/*
* Add an OCTET_STR value to the PDU that will contain the new
* system location.
*/
//pdu.addEntry(sysLocationOID, new OCTET_STR(" integer "+newLocation)) ;
// pdu.append()
PDU.entry pe = new PDU.entry(sysLocationOID,lineno);
PDU.entry[] peArray = {pe};
pdu.setEntries(peArray);

/*
* create a new listener instance and add it to the listeners for the session
*/
listener = new MyListener() ;
session.addListener(listener) ;

synchronized ( listener ) {
session.send(pdu, null) ;
listener.wait() ;

if( listener.success )
context.put("msg","SUCCESS");
else {
//System.out.println("operation failed") ;
context.put("msg","ERROR-TIMEDOUT");
}
}


}
catch( MIBItemNotFound e ) {
/*System.out.println("The sysLocation Object was not found.") ;
System.out.println("Please ensure that the MIBDIRS and MIBS") ;
System.out.println("environmental variables are set properly");*/
context.put("msg","ERROR-SYSLOCATION");
return ; // NOT REACHED
}
catch (NetSNMPSendError e) {
//System.out.println("An error occurred sending the pdu") ;
context.put("msg","ERROR-NETSNMP");
}
catch (InterruptedException e) {
//System.out.println("the wait operation was interrupted") ;
context.put("msg","ERROR-INTERRUPTED");
}
catch(Exception e)
{
context.put("msg",e.getMessage());
}



/*
* The actionPerformed method will be called on a different thread. Synchronize
* this thread with the thread that it will be called upon with a 'synchronized' block.
*/

/*synchronized ( listener ) {
try {*/
/*
* Send the pdu to the remote agent
*/
//session.send(pdu, null) ;


/*
* wait for the listener actionPerformed method to notify that
* operation has been completed
*/
/*listener.wait() ;

if( !listener.success ) {
System.out.println("operation failed") ;

}
} // try
catch (NetSNMPSendError e) {
System.out.println("An error occurred sending the pdu") ;
}
catch (InterruptedException e) {
System.out.println("the wait operation was interrupted") ;
}

} // synchronized
*/
}

/**
* Class that will receive and process the response from the remote agent.
*/
public static class MyListener implements NetSNMPAction {

boolean success = true ;

/**
* @see org.netsnmp.NetSNMPAction#actionPerformed(int, NetSNMPSession, PDU, Object)
*/
public synchronized boolean actionPerformed(int result, NetSNMPSession session, PDU pdu, Object o) throws Throwable {
if( result == NetSNMP.STAT_TIMEOUT ) {
//System.out.println("Operation timed out. Ensure that your remote agent is running and that the community parameter is correct") ;
success = false ;
this.notify() ; // notify that the operation has completed
return true ; // keep calling other registerred listeners, if any
} // if

if( pdu.errStatus != 0 ) {
//System.out.println("Set operation failed. Ensure that the security info you specified provides write access to the sysLocation Object") ;
success = false ;
this.notify() ; // notify that the operation has completed
return true ;
}

this.notify() ; // notify that the operation has completed
return true ;
}

}

}