Improved probe features.

This commit is contained in:
Christian P. MOMON 2021-12-15 13:02:00 +01:00
parent 614c064a24
commit a9e817f653
4 changed files with 76 additions and 96 deletions

View file

@ -22,6 +22,7 @@ import java.io.File;
import java.time.LocalDateTime;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -109,7 +110,8 @@ public final class StatoolInfosCLI
message.appendln(" statoolinfos clear <configurationfile> remove property files from conf");
message.appendln(" statoolinfos crawl <configurationfile> crawl all file from conf and input");
message.appendln(" statoolinfos htmlize <configurationfile> generate web pages from conf");
message.appendln(" statoolinfos probe [<directory>|<configurationfile>] generate metrics files from conf");
message.appendln(" statoolinfos probe OPTION [<directory>|<configurationfile>] generate metrics files from conf");
message.appendln(" OPTION = [-full|-today|-previousday|-NN] with NN a day count");
message.appendln(" statoolinfos uptime <configurationfile> update uptime journal");
message.appendln();
message.appendln(" statoolinfos format <fileordirectory> format property files in tiny way");
@ -383,13 +385,52 @@ public final class StatoolInfosCLI
StatoolInfos.listVisitors(source, filter);
}
else if (isMatching(args, "probe", "\\s*.+\\.conf\\s*"))
else if (isMatching(args, "probe", "\\S*(-full|-today|-previousday|-\\d+)\\s*", "\\s*.+\\s*"))
{
File configurationFile = new File(StringUtils.trim(args[1]));
File configurationFile = new File(StringUtils.trim(args[2]));
String filter = StringUtils.trim(args[1]);
int dayCountFilter;
if (filter.equals("-full"))
{
dayCountFilter = -1;
}
else if (filter.equals("-today"))
{
dayCountFilter = 0;
}
else if (filter.equals("-previousday"))
{
dayCountFilter = 1;
}
else
{
dayCountFilter = -1 * NumberUtils.toInt(filter);
}
try
{
StatoolInfos.probe(configurationFile);
Files inputs = FilesUtils.searchEndingWith(configurationFile, ".conf");
if (inputs.isEmpty())
{
System.out.println("No configuration file found.");
}
else
{
for (File input : inputs)
{
try
{
System.out.println("Probing [" + input + "] with day count filter " + dayCountFilter);
StatoolInfos.probe(input, dayCountFilter);
}
catch (Exception exception)
{
logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage());
exception.printStackTrace();
}
}
}
}
catch (Exception exception)
{
@ -397,31 +438,6 @@ public final class StatoolInfosCLI
exception.printStackTrace();
}
}
else if (isMatching(args, "probe", "\\s*.+/\\s*"))
{
File configurationFile = new File(StringUtils.trim(args[1]));
if (configurationFile.isDirectory())
{
Files inputs = FilesUtils.searchEndingWith(configurationFile, ".conf");
for (File input : inputs)
{
try
{
StatoolInfos.probe(input);
}
catch (Exception exception)
{
logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage());
exception.printStackTrace();
}
}
}
else
{
System.out.println("Parameter is not a directory.");
}
}
else if (isMatching(args, "stat", "ip", "\\s*\\S+\\s*"))
{
File source = new File(args[2]);

View file

@ -376,30 +376,6 @@ public class Configuration extends PathPropertyList
return result;
}
/**
* Gets the probe mode.
*
* @return the probe mode
*/
public ProbePeriod getProbeMode()
{
ProbePeriod result;
String value = get("conf.probe.mode");
if (StringUtils.isBlank(value))
{
result = null;
}
else
{
result = ProbePeriod.valueOf(StringUtils.upperCase(value));
}
//
return result;
}
/**
* Gets the probe target.
*

View file

@ -325,9 +325,9 @@ public class StatoolInfos
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static void probe(final File configurationFile) throws StatoolInfosException, IOException
public static void probe(final File configurationFile, final int dayCountFilter) throws StatoolInfosException, IOException
{
Prober.probe(configurationFile);
Prober.probe(configurationFile, dayCountFilter);
}
/**

View file

@ -35,7 +35,6 @@ import org.slf4j.LoggerFactory;
import fr.devinsy.statoolinfos.core.Configuration;
import fr.devinsy.statoolinfos.core.Factory;
import fr.devinsy.statoolinfos.core.ProbePeriod;
import fr.devinsy.statoolinfos.core.StatoolInfosException;
import fr.devinsy.statoolinfos.core.StatoolInfosUtils;
import fr.devinsy.statoolinfos.metrics.http.HttpAccessLogAnalyzer;
@ -86,18 +85,21 @@ public class Prober
*
* @param configuration
* the configuration
* @param dayCountFilter
* the day count filter, 0=today, 1=previous day, 7=previous
* week
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws StatoolInfosException
* the statool infos exception
*/
public static void probe(final Configuration configuration) throws IOException, StatoolInfosException
public static void probe(final Configuration configuration, final int dayCountFilter) throws IOException, StatoolInfosException
{
StringList types = configuration.getProbeTypes();
System.out.println("Targets=" + types.toStringWithBrackets());
if (configuration.getProbeMode() != null)
if (!types.isEmpty())
{
PathCounters counters = new PathCounters();
@ -148,21 +150,10 @@ public class Prober
}
// Filter.
logger.info("== Filtering");
if (configuration.getProbeMode() == ProbePeriod.TODAY)
logger.info("== Filtering with {}", dayCountFilter);
if (dayCountFilter >= 0)
{
logger.info("=== TODAY");
counters = counters.searchByPeriod(LocalDate.now(), LocalDate.now());
}
else if (configuration.getProbeMode() == ProbePeriod.PREVIOUS_DAY)
{
logger.info("=== PREVIOUS_DAY");
counters = counters.searchByPeriod(LocalDate.now().minusDays(1), LocalDate.now());
}
else if (configuration.getProbeMode() == ProbePeriod.PREVIOUS_DAYS_7)
{
logger.info("=== PREVIOUS_WEEK");
counters = counters.searchByPeriod(LocalDate.now().minusDays(7), LocalDate.now());
counters = counters.searchByPeriod(LocalDate.now().minusDays(dayCountFilter), LocalDate.now());
}
//
@ -177,33 +168,29 @@ public class Prober
{
if (target.exists())
{
logger.info("ModeProbe=" + configuration.getProbeMode());
if (configuration.getProbeMode() != ProbePeriod.FULL)
// Load.
logger.info("== Reading previous target file.");
PathCounters previousCounters = readMetrics(target);
logger.info("previous size={}", previousCounters.size());
// Merge.
logger.info("== Merging");
for (PathCounter counter : counters.values())
{
// Load.
logger.info("== Reading previous target file.");
PathCounters previousCounters = readMetrics(target);
logger.info("previous size={}", previousCounters.size());
// Merge.
logger.info("== Merging");
for (PathCounter counter : counters.values())
PathCounter previousCounter = previousCounters.get(counter.getPath(), counter.getTimeMark());
if (previousCounter == null)
{
PathCounter previousCounter = previousCounters.get(counter.getPath(), counter.getTimeMark());
if (previousCounter == null)
{
previousCounters.put(counter);
}
else
{
previousCounter.setCounter(counter.getCounter());
}
previousCounters.put(counter);
}
else
{
previousCounter.setCounter(counter.getCounter());
}
//
counters = previousCounters;
}
//
counters = previousCounters;
//
logger.info("== Backing previous target file.");
target.renameTo(new File(target.getParentFile(), target.getName() + ".bak"));
@ -216,6 +203,7 @@ public class Prober
logger.info("== Writing.");
logger.info("size={}", counters.size());
}
}
/**
@ -226,13 +214,13 @@ public class Prober
* @throws IOException
* @throws StatoolInfosException
*/
public static void probe(final File configurationFile) throws StatoolInfosException, IOException
public static void probe(final File configurationFile, final int dayCountFilter) throws StatoolInfosException, IOException
{
logger.info("Probe {}", configurationFile.getAbsolutePath());
Configuration configuration = Factory.loadConfiguration(configurationFile);
probe(configuration);
probe(configuration, dayCountFilter);
}
/**