Created Parameters, ParametersManager, InputWatcher, Watcher.

This commit is contained in:
didfet
2016-08-27 10:46:45 +02:00
parent adb969518d
commit d362cbb675
4 changed files with 351 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
package info.fetter.logstashforwarder;
import info.fetter.logstashforwarder.config.FilesSection;
import info.fetter.logstashforwarder.config.Parameters;
import info.fetter.logstashforwarder.util.AdapterException;
import java.io.IOException;
import org.apache.log4j.Logger;
public class InputWatcher implements Watcher {
private static final Logger logger = Logger.getLogger(InputWatcher.class);
private Event stdinFields;
private boolean stdinConfigured = false;
private InputReader reader;
public void addFilesToWatch(FilesSection files) {
for(String path : files.getPaths()) {
if(path.equals("-")) {
logger.error("Watching stdin");
stdinFields = new Event(files.getFields());
stdinConfigured = true;
}
}
}
private int readStdin(InputReader reader) throws AdapterException, IOException {
if(stdinConfigured) {
logger.debug("Reading stdin");
reader.setFields(stdinFields);
int numberOfLinesRead = reader.readInput();
return numberOfLinesRead;
} else {
return 0;
}
}
public Reader getReader() {
return reader;
}
public int readFiles() throws AdapterException, IOException {
return readStdin(reader);
}
public void setParameters(Parameters parameters) {
reader = new InputReader(parameters.getSpoolSize(), System.in);
}
}

View File

@@ -0,0 +1,14 @@
package info.fetter.logstashforwarder;
import java.io.IOException;
import info.fetter.logstashforwarder.config.FilesSection;
import info.fetter.logstashforwarder.config.Parameters;
import info.fetter.logstashforwarder.util.AdapterException;
public interface Watcher {
public void addFilesToWatch(FilesSection files);
public Reader getReader();
public int readFiles() throws AdapterException, IOException;
public void setParameters(Parameters parameters);
}

View File

@@ -0,0 +1,154 @@
package info.fetter.logstashforwarder.config;
import static org.apache.log4j.Level.INFO;
import org.apache.log4j.Level;
public class Parameters {
private final String SINCEDB = ".logstash-forwarder-java";
private int spoolSize = 1024;
private int idleTimeout = 5000;
private Level logLevel = INFO;
private int signatureLength = 4096;
private boolean tailSelected = false;
private String logfile = null;
private String logfileSize = "10MB";
private int logfileNumber = 5;
private String sincedbFile = SINCEDB;
private String configFile = null;
private boolean debugWatcherSelected = false;
/**
* @return the spoolSize
*/
public int getSpoolSize() {
return spoolSize;
}
/**
* @param spoolSize the spoolSize to set
*/
public void setSpoolSize(int spoolSize) {
this.spoolSize = spoolSize;
}
/**
* @return the idleTimeout
*/
public int getIdleTimeout() {
return idleTimeout;
}
/**
* @param idleTimeout the idleTimeout to set
*/
public void setIdleTimeout(int idleTimeout) {
this.idleTimeout = idleTimeout;
}
/**
* @return the logLevel
*/
public Level getLogLevel() {
return logLevel;
}
/**
* @param logLevel the logLevel to set
*/
public void setLogLevel(Level logLevel) {
this.logLevel = logLevel;
}
/**
* @return the signatureLength
*/
public int getSignatureLength() {
return signatureLength;
}
/**
* @param signatureLength the signatureLength to set
*/
public void setSignatureLength(int signatureLength) {
this.signatureLength = signatureLength;
}
/**
* @return the tailSelected
*/
public boolean isTailSelected() {
return tailSelected;
}
/**
* @param tailSelected the tailSelected to set
*/
public void setTailSelected(boolean tailSelected) {
this.tailSelected = tailSelected;
}
/**
* @return the logfile
*/
public String getLogfile() {
return logfile;
}
/**
* @param logfile the logfile to set
*/
public void setLogfile(String logfile) {
this.logfile = logfile;
}
/**
* @return the logfileSize
*/
public String getLogfileSize() {
return logfileSize;
}
/**
* @param logfileSize the logfileSize to set
*/
public void setLogfileSize(String logfileSize) {
this.logfileSize = logfileSize;
}
/**
* @return the logfileNumber
*/
public int getLogfileNumber() {
return logfileNumber;
}
/**
* @param logfileNumber the logfileNumber to set
*/
public void setLogfileNumber(int logfileNumber) {
this.logfileNumber = logfileNumber;
}
/**
* @return the sincedbFile
*/
public String getSincedbFile() {
return sincedbFile;
}
/**
* @param sincedbFile the sincedbFile to set
*/
public void setSincedbFile(String sincedbFile) {
this.sincedbFile = sincedbFile;
}
/**
* @return the configFile
*/
public String getConfigFile() {
return configFile;
}
/**
* @param configFile the configFile to set
*/
public void setConfigFile(String configFile) {
this.configFile = configFile;
}
/**
* @return the debugWatcherSelected
*/
public boolean isDebugWatcherSelected() {
return debugWatcherSelected;
}
/**
* @param debugWatcherSelected the debugWatcherSelected to set
*/
public void setDebugWatcherSelected(boolean debugWatcherSelected) {
this.debugWatcherSelected = debugWatcherSelected;
}
}

View File

@@ -0,0 +1,134 @@
package info.fetter.logstashforwarder.config;
import static org.apache.log4j.Level.DEBUG;
import static org.apache.log4j.Level.ERROR;
import static org.apache.log4j.Level.TRACE;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
public class ParametersManager {
@SuppressWarnings("static-access")
public static Parameters parseOptions(String[] args) {
Options options = new Options();
Option helpOption = new Option("help", "print this message");
Option quietOption = new Option("quiet", "operate in quiet mode - only emit errors to log");
Option debugOption = new Option("debug", "operate in debug mode");
Option debugWatcherOption = new Option("debugwatcher", "operate watcher in debug mode");
Option traceOption = new Option("trace", "operate in trace mode");
Option tailOption = new Option("tail", "read new files from the end");
Option spoolSizeOption = OptionBuilder.withArgName("number of events")
.hasArg()
.withDescription("event count spool threshold - forces network flush")
.create("spoolsize");
Option idleTimeoutOption = OptionBuilder.withArgName("")
.hasArg()
.withDescription("time between file reads in seconds")
.create("idletimeout");
Option configOption = OptionBuilder.withArgName("config file")
.hasArg()
.isRequired()
.withDescription("path to logstash-forwarder configuration file")
.create("config");
Option signatureLengthOption = OptionBuilder.withArgName("signature length")
.hasArg()
.withDescription("Maximum length of file signature")
.create("signaturelength");
Option logfileOption = OptionBuilder.withArgName("logfile name")
.hasArg()
.withDescription("Logfile name")
.create("logfile");
Option logfileSizeOption = OptionBuilder.withArgName("logfile size")
.hasArg()
.withDescription("Logfile size (default 10M)")
.create("logfilesize");
Option logfileNumberOption = OptionBuilder.withArgName("number of logfiles")
.hasArg()
.withDescription("Number of logfiles (default 5)")
.create("logfilenumber");
Option sincedbOption = OptionBuilder.withArgName("sincedb file")
.hasArg()
.withDescription("Sincedb file name")
.create("sincedb");
options.addOption(helpOption)
.addOption(idleTimeoutOption)
.addOption(spoolSizeOption)
.addOption(quietOption)
.addOption(debugOption)
.addOption(debugWatcherOption)
.addOption(traceOption)
.addOption(tailOption)
.addOption(signatureLengthOption)
.addOption(configOption)
.addOption(logfileOption)
.addOption(logfileNumberOption)
.addOption(logfileSizeOption)
.addOption(sincedbOption);
CommandLineParser parser = new GnuParser();
Parameters parameters = new Parameters();
try {
CommandLine line = parser.parse(options, args);
if(line.hasOption("spoolsize")) {
parameters.setSpoolSize(Integer.parseInt(line.getOptionValue("spoolsize")));
}
if(line.hasOption("idletimeout")) {
parameters.setIdleTimeout(Integer.parseInt(line.getOptionValue("idletimeout")));
}
if(line.hasOption("config")) {
parameters.setConfigFile(line.getOptionValue("config"));
}
if(line.hasOption("signaturelength")) {
parameters.setSignatureLength(Integer.parseInt(line.getOptionValue("signaturelength")));
}
if(line.hasOption("quiet")) {
parameters.setLogLevel(ERROR);
}
if(line.hasOption("debug")) {
parameters.setLogLevel(DEBUG);
}
if(line.hasOption("trace")) {
parameters.setLogLevel(TRACE);
}
if(line.hasOption("debugwatcher")) {
parameters.setDebugWatcherSelected(true);
}
if(line.hasOption("tail")) {
parameters.setTailSelected(true);
}
if(line.hasOption("logfile")) {
parameters.setLogfile(line.getOptionValue("logfile"));
}
if(line.hasOption("logfilesize")) {
parameters.setLogfileSize(line.getOptionValue("logfilesize"));
}
if(line.hasOption("logfilenumber")) {
parameters.setLogfileNumber(Integer.parseInt(line.getOptionValue("logfilenumber")));
}
if(line.hasOption("sincedb")) {
parameters.setSincedbFile(line.getOptionValue("sincedb"));
}
} catch(ParseException e) {
printHelp(options);
System.exit(1);;
} catch(NumberFormatException e) {
System.err.println("Value must be an integer");
printHelp(options);
System.exit(2);;
}
return parameters;
}
private static void printHelp(Options options) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("logstash-forwarder", options);
}
}