On Wednesday, 6 April 2016 15:20:32 UTC+5:30, rajarshi bhattacharjee wrote: > > I am actually new to graylog and frankly not quite aware of how graylog > is triggering the drools rule engine so it might be a very simple question > with an obvious solution which is somehow escaping me . > > > I am trying to improve an already existing (drl code) by invoking > another java class , which is residing within a different directory as > compared to my drl file. Understood that graylog doesnt allow adding other > packages definitions( > https://github.com/Graylog2/graylog2-server/issues/1213) , so tried with > a direct import by putting the java class in a different directory, but > when i try to invoke this java class my drl fails with the following > exception `Rule Compilation error : [Rule name='BPTM logs filter'] > org/graylog2/rules/Rule_BPTM_logs_filter508671689.java (42:2371) : > *PropertiesCache > cannot be resolved* > > 2016-04-06 09:11:09,601 WARN : org.graylog2.rules.DroolsEngine - Unable to > add rules due to compilation errors. > org.graylog2.rules.RulesCompilationException: Message [id=1, level=ERROR, > path=r1.drl, line=30, column=0 > text=Rule Compilation error PropertiesCache cannot be resolved] > > Note :PropertiesCache is my java class and my drl file exists in > different directories. Pretty much its not able to resolve the import and > fails with compilation error > > Any insights would be valuable , >
Attached DRL file , along with PropertiesCache.java file (external class i am trying to invoke) Please advice , i am not sure whether this error is due to environment/drools/or graylog restrictions as i m new to both drools and graylog and just trying to improve this code from java perspective -- You received this message because you are subscribed to the Google Groups "Graylog Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/graylog2/ea4946a4-f7fe-4163-9986-57e3097f0d74%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
GRAYLOG.DRL (Available in directory
/wls_domains/Graylog/GraylogServer/graylog-1.2.1/etc)
*******************************************
import org.graylog2.plugin.Message;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Enumeration;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Arrays;
import etc.code.PropertiesCache;
rule "BPTM logs filter"
when
m:Message(getField("type")!=null && getField("type")!="" &&
getField("type")=="BPTM" && getField("message")!="" &&
getField("message")!=null)
then
String patternString=null;
String pathOfMetaFile=null;
String metaFileName=null;
String dontIndexInGraylogFlag=null;
String dontFilterFlag=null;
String messageId=null;
String messageIdVal=null;
String msgIdToBeFilter=null;
String capabilityName=null;
String configFileName=null;
String notApplicableConstant=null;
String filterMsgBasedOnMessageIdFlag=null;
String msgIdSplitFlag=null;
String msgIdSpliterVal=null;
String capabilityrequiredFlag=null;
String matchedCapability=null;
String foundError="No";
int errorLoopCount=0;
int capabilityLoopCount=0;
boolean foundCapability=false;
boolean containsError=false;
StringBuilder sb=new StringBuilder();
Properties properties;
configFileName=(m.getField("configFileName")).toString();
configFileName=configFileName+"_metaFileFields.properties";
capabilityName=(m.getField("capability")).toString();
metaFileName=(m.getField("source_file")).toString();
try {
// this is where trying to invoke external
singleton class and getting unresolved errors
properties=PropertiesCache.getInstance().getProperty();
String workingDir =
System.getProperty("user.dir");
File file = new
File(workingDir+"/properties/"+configFileName);
FileInputStream fileInput = new
FileInputStream(file);
properties.load(fileInput);
fileInput.close();
pathOfMetaFile=properties.getProperty("_pathOfMetaFile");
dontIndexInGraylogFlag=properties.getProperty("_dontIndexInGraylog");
dontFilterFlag=properties.getProperty("_dontFilter");
msgIdToBeFilter=properties.getProperty("_msgIdToBeFilter");
notApplicableConstant=properties.getProperty("_notApplicableConstant");
errorLoopCount=Integer.valueOf(properties.getProperty("_error_loop_count"));
capabilityLoopCount=Integer.valueOf(properties.getProperty("_capability_loop_count"));
capabilityrequiredFlag=properties.getProperty("_Capability_required");
filterMsgBasedOnMessageIdFlag=properties.getProperty("_filterMsgBasedOnMessageId");
msgIdSplitFlag=properties.getProperty("_Mes_Id_Split");
msgIdSpliterVal=properties.getProperty("_Mes_Id_Spliter");
// Code changes to extract the capabilityName
Starts //
if(!(foundCapability))
{
for(int c=0;c<capabilityLoopCount;c++)
{
Matcher capabilityMatcher =
(Pattern.compile(properties.getProperty("_matchedCapability"+c),Pattern.CASE_INSENSITIVE)).matcher(metaFileName);
if
(capabilityrequiredFlag.equalsIgnoreCase("true") && (capabilityMatcher.find()))
{
matchedCapability=(capabilityMatcher.group()).toString();
foundCapability=true;
}
if(foundCapability)
{
break;
}
}
}
// Code changes to extract the capability
NameEnds
Matcher messageIdMatcher =
(Pattern.compile(properties.getProperty("_Mes_Id"),Pattern.CASE_INSENSITIVE)).matcher(m.getMessage());
if (messageIdMatcher.find())
{
messageId=(messageIdMatcher.group(1)).toString();
if(msgIdSplitFlag.equalsIgnoreCase("true") &&
messageId.contains(msgIdSpliterVal)){
messageId=messageId.substring(messageId.lastIndexOf(msgIdSpliterVal)+1,messageId.length());}
}
if(dontIndexInGraylogFlag.equalsIgnoreCase("true")){
m.setFilterOut(true);}
if(filterMsgBasedOnMessageIdFlag.equalsIgnoreCase("false"))
messageIdVal="*";
else
messageIdVal=messageId;
if(msgIdToBeFilter.contains(messageIdVal) &&
dontFilterFlag.equalsIgnoreCase("false"))
{
String wholeMessage=null;
wholeMessage=(m.getMessage()).toString();
wholeMessage=wholeMessage.replace("\n","").replace("\r","").replace("&gt;",">").replace(">",">").replace("&lt;","<").replace("<","<").replace(""","\"").replace("'","\'");
Matcher dateMatcher =
(Pattern.compile(properties.getProperty("_Date"),Pattern.CASE_INSENSITIVE)).matcher(wholeMessage);
if (dateMatcher.find())
{
sb.append(properties.getProperty("_Date_Key"));
sb.append("=");
sb.append(((dateMatcher.group(1)).toString()).replace("-","/"));
Matcher timeMatcher =
(Pattern.compile(properties.getProperty("_Time"),Pattern.CASE_INSENSITIVE)).matcher(wholeMessage);
if (timeMatcher.find())
{
sb.append(" ");
sb.append(timeMatcher.group(1));
}
sb.append(",");
}
sb.append(properties.getProperty("_Mes_Id_key"));
sb.append("=");
sb.append(messageId);
sb.append(",");
if((properties.getProperty("_Mes_Type_Required")).equalsIgnoreCase("true"))
{
sb.append(properties.getProperty("_Mes_Type_key"));
sb.append("=");
if(properties.getProperty("_"+messageId)!=null)
sb.append(properties.getProperty("_"+messageId));
else
sb.append(messageId);
sb.append(",");
}
Enumeration enuKeys = properties.keys();
while (enuKeys.hasMoreElements())
{
String key = (String)
enuKeys.nextElement();
if(!key.startsWith("_"))
{
if(key.equalsIgnoreCase("FT"))
{
Matcher
textMatcher =
(Pattern.compile(properties.getProperty(key),Pattern.CASE_INSENSITIVE)).matcher(wholeMessage);
if
(textMatcher.find())
{
sb.append(properties.getProperty("_size_key"));
sb.append("=");
String
textValue=null;
textValue=(textMatcher.group(1)).toString();
if(textValue!="")
{
sb.append(textValue.length());
//Checking the error string in text value
String[]
errorKeywords=(properties.getProperty("_Error_Keyword")).split(",");
for(int
p=0;p<errorKeywords.length;p++)
{
if(textValue.contains(errorKeywords[p]))
{
containsError=true;
break;
}
}
}
else
{
sb.append(notApplicableConstant);
}
sb.append(",");
if(containsError)
{
for(int
g=0;g<errorLoopCount;g++)
{
Matcher
errorCodeMatcher =
(Pattern.compile(properties.getProperty("_errorCode"+g),Pattern.CASE_INSENSITIVE)).matcher(textValue);
if
(errorCodeMatcher.find())
{
sb.append(properties.getProperty("_error_key"));
sb.append("=");
sb.append(errorCodeMatcher.group(1));
Matcher errorDescMatcher =
(Pattern.compile(properties.getProperty("_errorDesc"+g),Pattern.CASE_INSENSITIVE)).matcher(textValue);
if
(errorDescMatcher.find())
{
sb.append("-");
sb.append(((errorDescMatcher.group(1)).toString()).trim());
}
sb.append(",");
foundError="Yes";
break;
}
}
sb.append(properties.getProperty("_Error_Flag"));
sb.append("=");
sb.append("1");
sb.append(",");
}
if(foundError.equalsIgnoreCase("No"))
{
sb.append(properties.getProperty("_error_key"));
sb.append("=");
sb.append(notApplicableConstant);
sb.append(",");
}
}
else
{
sb.append(properties.getProperty("_size_key"));
sb.append("=");
sb.append(notApplicableConstant);
sb.append(",");
sb.append(properties.getProperty("_error_key"));
sb.append("=");
sb.append(notApplicableConstant);
sb.append(",");
}
}
else
if(key.equalsIgnoreCase("SE"))
{
Matcher
serviceMatcher =
(Pattern.compile(properties.getProperty(key),Pattern.CASE_INSENSITIVE)).matcher(wholeMessage);
if
(serviceMatcher.find())
{
String
serviceValue=(serviceMatcher.group(1)).toString();
if(serviceValue.contains("_"))
{
String[] serviceValueSplits = serviceValue.split("_");
sb.append(key);
sb.append("=");
sb.append(serviceValueSplits[0]);
sb.append(",");
sb.append(properties.getProperty("_Version_Key"));
sb.append("=");
sb.append(serviceValueSplits[1]);
sb.append(",");
}
else
{
sb.append(key);
sb.append("=");
sb.append(serviceValue);
sb.append(",");
sb.append(properties.getProperty("_Version_Key"));
sb.append("=");
sb.append(notApplicableConstant);
sb.append(",");
}
}
}
// Code added for
capability extractions starts //
else
if(key.equalsIgnoreCase("capabilityKey")){
if(capabilityrequiredFlag.equalsIgnoreCase("true")){
if(properties.getProperty("capabilityKey")!=null){
sb.append(properties.getProperty("capabilityKey"));
sb.append("=");
sb.append(matchedCapability);
sb.append(",");
}
}
}
// Code added for
capability extraction ends
else
{
Matcher
allMatcher =
(Pattern.compile(properties.getProperty(key),Pattern.CASE_INSENSITIVE)).matcher(wholeMessage);
if
(allMatcher.find())
{
sb.append(key);
sb.append("=");
sb.append(allMatcher.group(1));
sb.append(",");
}
else
{
sb.append(key);
sb.append("=");
sb.append(notApplicableConstant);
sb.append(",");
}
}
}
}
if (sb.length() > 0){
sb.setLength(sb.length() - 1);}
}
else if(dontFilterFlag.equalsIgnoreCase("true"))
{
sb.append(m.getMessage());
}
metaFileName=metaFileName.substring(metaFileName.lastIndexOf("/")+1,metaFileName.indexOf("."));
File dir = new
File(pathOfMetaFile+"/"+capabilityName);
if (!dir.exists()){
dir.mkdir();}
String file1Name=null;
file1Name=pathOfMetaFile+"/"+capabilityName+"/"+metaFileName+"_META.txt";
File file1 = new File(file1Name);
if (!file1.exists()){
file1.createNewFile();}
BufferedWriter bw1 = new BufferedWriter(new
FileWriter(file1,true));
bw1.write(sb.toString());
bw1.newLine();
bw1.close();
} catch (FileNotFoundException e) {
if(dontIndexInGraylogFlag.equalsIgnoreCase("true")){
m.setFilterOut(true);}
System.out.println("Not able to find property
file:"+e);
} catch (IOException e) {
if(dontIndexInGraylogFlag.equalsIgnoreCase("true")){
m.setFilterOut(true);}
System.out.println("IOException occured:"+e);
File dir1 = new
File(pathOfMetaFile+"/"+capabilityName);
if (!dir1.exists()){
dir1.mkdir();}
String file2Name=null;
file2Name=pathOfMetaFile+"/"+capabilityName+"/error.txt";
File file2 = new File(file2Name);
if (!file2.exists()){
file2.createNewFile();}
BufferedWriter bw2 = new BufferedWriter(new
FileWriter(file2,true));
bw2.write(m.getMessage());
bw2.newLine();
bw2.close();
}
end
***************************************************************************************************
PropertiesCache.java (Available in directory
/wls_domains/Graylog/GraylogServer/graylog-1.2.1/code)
package etc.code;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
public class PropertiesCache
{
private final Properties configProp = new Properties();
private PropertiesCache()
{
//Private constructor to restrict new instances
InputStream in =
this.getClass().getClassLoader().getResourceAsStream("RoBT_CMPS_metaFileFields.properties");
System.out.println("Read all properties from file");
try {
configProp.load(in);
} catch (IOException e) {
e.printStackTrace();
}
}
private static class LazyHolder
{
private static final PropertiesCache INSTANCE = new PropertiesCache();
}
public static PropertiesCache getInstance()
{
return LazyHolder.INSTANCE;
}
public String getProperty(String key){
return configProp.getProperty(key);
}
public Set<String> getAllPropertyNames(){
return configProp.stringPropertyNames();
}
public boolean containsKey(String key){
return configProp.containsKey(key);
}
public Properties getProperty(){
return configProp;
}
}
***************************************************************************************************
below is my classpath details (so that i have the directory and java class i am
invoking is available in the classpath)
echo $CLASSPATH
/wls_domains/Graylog/GraylogServer/graylog-1.2.1/etc:/wls_domains/Graylog/GraylogServer/graylog-1.2.1/etc/code:/wls_domains/Graylog/GraylogServer/graylog-1.2.1/etc/code/PropertiesCache.java
