Add read from start opton
This commit is contained in:
@@ -26,6 +26,7 @@ public class Cli {
|
||||
public static Option fileParam = new Option( "paramfile", true , "Input a param file" );
|
||||
public static Option debugOption = new Option( "debug", "Active debug output message" );
|
||||
public static Option infoOption = new Option( "info", "Active info output message" );
|
||||
public static Option once = new Option( "once", "Read the file once time" );
|
||||
private static long lastModifiedTime = 0L;
|
||||
public Cli() {
|
||||
// TODO Auto-generated constructor stub
|
||||
@@ -52,6 +53,7 @@ public class Cli {
|
||||
|
||||
HelpFormatter formatter = new HelpFormatter();
|
||||
boolean readFromStart = false;
|
||||
boolean onceTime = false;
|
||||
CommandLineParser parser = new DefaultParser();
|
||||
CommandLine cmd = null;
|
||||
try {
|
||||
@@ -91,15 +93,20 @@ public class Cli {
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
if(cmd.hasOption("once")) {
|
||||
onceTime = true;
|
||||
}
|
||||
|
||||
if(cmd.hasOption("paramfile")) {
|
||||
String fileParamName = cmd.getOptionValue("paramfile");
|
||||
File param = new File(fileParamName);
|
||||
ParserEngine engine = new ParserEngine(patternPath);
|
||||
Log log = Log.getLogger(Cli.class.getName());
|
||||
try {
|
||||
startParserFromFile(param, engine );
|
||||
startParserFromFile(param, engine , onceTime);
|
||||
lastModifiedTime = param.lastModified();
|
||||
log.info("All parsers started");
|
||||
// Check for reload the param file
|
||||
while (true) {
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
@@ -108,7 +115,7 @@ public class Cli {
|
||||
if ( param.lastModified() != lastModifiedTime )
|
||||
{
|
||||
engine.stopAllParser();
|
||||
startParserFromFile(param, engine );
|
||||
startParserFromFile(param, engine , onceTime);
|
||||
lastModifiedTime = param.lastModified();
|
||||
}
|
||||
}
|
||||
@@ -154,7 +161,7 @@ public class Cli {
|
||||
String[] filesName = cmd.getOptionValues("logfile");
|
||||
for (int i = 0; i < filesName.length ; i++) {
|
||||
File f = new File (filesName[i]);
|
||||
engine.addNewParser(f, regexName[i], applicationName, readFromStart);
|
||||
engine.addNewParser(f, regexName[i], applicationName, readFromStart, onceTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -162,7 +169,7 @@ public class Cli {
|
||||
}
|
||||
|
||||
|
||||
static void startParserFromFile(File file, ParserEngine engine) throws FileNotFoundException {
|
||||
static void startParserFromFile(File file, ParserEngine engine, boolean onceTime) throws FileNotFoundException {
|
||||
|
||||
Scanner scanner = new Scanner(file);
|
||||
|
||||
@@ -175,10 +182,14 @@ public class Cli {
|
||||
String applicationName = arguments[0].toLowerCase();
|
||||
Paths paths = new Paths("/", arguments[1]);
|
||||
String regexName = arguments[2];
|
||||
if (arguments[3] != null)
|
||||
readFromStart = true;
|
||||
if (arguments.length == 4) {
|
||||
if ( arguments[3].equalsIgnoreCase("true") ){
|
||||
readFromStart = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (File f : paths.getFiles()) {
|
||||
engine.addNewParser(f, regexName, applicationName, readFromStart);
|
||||
engine.addNewParser(f, regexName, applicationName, readFromStart, onceTime);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ public class ParserEngine implements Runnable{
|
||||
final private String database = "qualif";
|
||||
final private String urlInfluxdb = "http://diqmqs.airfrance.fr/influxdb_query/write?rp=one_week&db=" + database;
|
||||
final long delay = 200;
|
||||
private boolean onceTime = false;
|
||||
private ArrayList<Thread> threadsParser = null;
|
||||
static BlockingDeque<String> queue = new LinkedBlockingDeque<String>(2000);
|
||||
private String patternPath = "";
|
||||
@@ -50,7 +51,7 @@ public class ParserEngine implements Runnable{
|
||||
log.info("Schedule sender is set to " + 1 + " secs" );
|
||||
}
|
||||
|
||||
void addNewParser(File f , String regexName, String application , boolean readFromStart) {
|
||||
void addNewParser(File f , String regexName, String application , boolean readFromStart, boolean onceTime) {
|
||||
TailerListener listener = new Parser(application, regexName, this);
|
||||
Tailer tailer = new Tailer(f, listener, delay, !readFromStart, true, 8192);
|
||||
Thread thread = new Thread(tailer);
|
||||
@@ -58,6 +59,7 @@ public class ParserEngine implements Runnable{
|
||||
thread.setName("Parser - " + threadsParser.size() );
|
||||
thread.start();
|
||||
threadsParser.add(thread);
|
||||
this.onceTime = onceTime;
|
||||
log.info("Thread Parser - " + threadsParser.size() + " started on file : " + f.getName());
|
||||
}
|
||||
|
||||
@@ -80,6 +82,14 @@ public class ParserEngine implements Runnable{
|
||||
postData.append( s + "\n" );
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (onceTime)
|
||||
{
|
||||
stopAllParser();
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
}
|
||||
try {
|
||||
if (postData.length() > 0) {
|
||||
if (sendMetricToInfluxdb(postData.toString()))
|
||||
|
||||
Reference in New Issue
Block a user