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 ;
}

}

}

Dynamic SQL from Java use any number of parameters


Following is the Code that uses Java and Oracle . Its an utility to do a dynamic search without defining the sql . SQL is generated on the fly ( dynamically ) at runtime . You may have to tweak the sql generated for your purpose , but you can resue most of the Code

Params can be changed to include a variety of search options
1.Quotes for Exact Search
2. % for wider search
- abc% means starts with "abc"
- %xyz means ends with "xyz"
3.Use () to specify OR condition e.g.,(abc , efg)
4.Search is Non-Case Sensitive

One limitation is that all the parameters are created by AND condition .However you can extend the utility class to include
that feature too .

Sql Utlity class

import java.util.StringTokenizer;

public class Sql {
private String sOn ; // column name to search
private String sParam; // exact search parameter

public Sql(String sOn , String sParam)
{
this.sOn = sOn;
this.sParam = sParam;
}
// Generate the sql
public String getSQL()
{
StringBuffer sSql = new StringBuffer();
String sToken = new String();
String sTemp = new String();

if(sParam.startsWith("\"")&& sParam.endsWith("\""))
{
sSql.append("AND UPPER(").append(sOn).append(") =").append("'").append((sParam.substring(sParam.indexOf("\"")+1,sParam.lastIndexOf("\""))).toUpperCase()).append("'");
}else if(sParam.startsWith("(")&& sParam.endsWith(")"))
{
sTemp = sParam.substring(sParam.indexOf("(")+1,sParam.indexOf(")")) ;
sSql.append("AND UPPER(").append(sOn).append(") IN (");
StringTokenizer st1 = new StringTokenizer(sTemp,",");
while ( st1.hasMoreTokens() ) {
sToken = st1.nextToken();
if(sToken !=null)
{
sToken = sToken.trim().toUpperCase();
sSql.append("'").append(sToken).append("',");
}
}
sSql = new StringBuffer(sSql.substring(0,sSql.length()-1)) ;
sSql.append(")");
}
else if(sParam.startsWith("%") || sParam.endsWith("%"))
{
sSql.append("AND UPPER(").append(sOn).append(") LIKE '").append(sParam.toUpperCase()).append("'");

}else if(sParam!=null&& !sParam.equals(""))
{
sSql.append("AND UPPER(").append(sOn).append(") LIKE '%").append(sParam.toUpperCase()).append("%'");

}
return sSql.toString();
}
}

Utlity method


public String getSearchSQL(Vector v)
{
StringBuffer sql = new StringBuffer();
for(Enumeration e=v.elements();e.hasMoreElements();)
{
sql.append(((Sql)e.nextElement()).getSQL() );

}
return sql.toString();
}


Application Logic


Vector v = new Vector();
v.add(new Sql("<name of table column name>", <value to search on use empty string if null> ));

.....repeat for all the parameters to be searched on

<sql string> = getSearchSQL(v);

Bingo !

Velocity templates to send email


Here is a code sample for creating email from Velocity

Template template = null;
StringWriter sw = new StringWriter();
try {
template = Velocity.getTemplate(