From 8fdafcdf1d78717951a01adeaa174c32b142ced8 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sun, 26 Feb 2023 02:02:24 +0100 Subject: [PATCH] Added CLI commands to test HttpAccessLog files and lines. --- .../statoolinfos/cli/StatoolInfosCLI.java | 31 +++++++- .../statoolinfos/core/StatoolInfos.java | 73 ++++++++++++++++++- 2 files changed, 102 insertions(+), 2 deletions(-) diff --git a/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java b/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java index a307840..0226ec2 100644 --- a/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java +++ b/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2022 Christian Pierre MOMON + * Copyright (C) 2020-2023 Christian Pierre MOMON * * This file is part of StatoolInfos, simple service statistics tool. * @@ -122,6 +122,8 @@ public final class StatoolInfosCLI message.appendln(" statoolinfos stat ip [-bot|-nobot] generate stats about ip from http log file"); message.appendln(" statoolinfos stat ua [-bot|-nobot] generate stats about user agent from http log file"); message.appendln(" statoolinfos stat visitors [-bot|-nobot] generate stats about visitors (ip+ua) from http log file"); + message.appendln(" statoolinfos test HttpAccessLog -files display log files from the configuration file"); + message.appendln(" statoolinfos test HttpAccessLog -lines display log lines from the configuration file"); System.out.print(message.toString()); } @@ -549,6 +551,33 @@ public final class StatoolInfosCLI exception.printStackTrace(); } } + else if (isMatching(args, "test", "HttpAccessLog", "-files", "\\s*.+\\.conf\\s*")) + { + File configurationFile = new File(StringUtils.trim(args[3])); + try + { + StatoolInfos.testHttpAccessLogFiles(configurationFile); + } + catch (Exception exception) + { + logger.error("Error with [{}]: {}", configurationFile.getAbsoluteFile(), exception.getMessage()); + exception.printStackTrace(); + } + } + else if (isMatching(args, "test", "HttpAccessLog", "-lines", "\\s*.+\\.conf\\s*")) + { + File configurationFile = new File(StringUtils.trim(args[3])); + + try + { + StatoolInfos.testHttpAccessLogLines(configurationFile); + } + catch (Exception exception) + { + logger.error("Error with [{}]: {}", configurationFile.getAbsoluteFile(), exception.getMessage()); + exception.printStackTrace(); + } + } else { System.out.println("Bad usage."); diff --git a/src/fr/devinsy/statoolinfos/core/StatoolInfos.java b/src/fr/devinsy/statoolinfos/core/StatoolInfos.java index abfa81c..34d25ec 100644 --- a/src/fr/devinsy/statoolinfos/core/StatoolInfos.java +++ b/src/fr/devinsy/statoolinfos/core/StatoolInfos.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2022 Christian Pierre MOMON + * Copyright (C) 2020-2023 Christian Pierre MOMON * * This file is part of StatoolInfos, simple service statistics tool. * @@ -36,6 +36,7 @@ import fr.devinsy.statoolinfos.metrics.Prober; import fr.devinsy.statoolinfos.metrics.http.HttpAccessLog; import fr.devinsy.statoolinfos.metrics.http.HttpAccessLogIterator; import fr.devinsy.statoolinfos.metrics.http.HttpAccessLogParser; +import fr.devinsy.statoolinfos.metrics.http.HttpAccessLogs; import fr.devinsy.statoolinfos.properties.PathProperties; import fr.devinsy.statoolinfos.properties.PathPropertyUtils; import fr.devinsy.statoolinfos.stats.ip.IpStat; @@ -48,6 +49,7 @@ import fr.devinsy.statoolinfos.uptime.UptimeJournal; import fr.devinsy.statoolinfos.uptime.UptimeSurveyor; import fr.devinsy.statoolinfos.util.Chrono; import fr.devinsy.statoolinfos.util.Files; +import fr.devinsy.statoolinfos.util.FilesUtils; import fr.devinsy.statoolinfos.util.LineIterator; import fr.devinsy.strings.StringList; import fr.devinsy.strings.StringsUtils; @@ -549,6 +551,75 @@ public class StatoolInfos } } + /** + * Test http access log files. + * + * @param configurationFile + * the configuration file + * @throws IOException + * @throws StatoolInfosException + */ + public static void testHttpAccessLogFiles(final File configurationFile) throws StatoolInfosException, IOException + { + if ((configurationFile == null) || (!configurationFile.exists())) + { + System.out.println("No configuration file found."); + } + else + { + System.out.println("Testing HttpAccesLog files from [" + configurationFile.toString() + "]"); + Configuration configuration = Factory.loadConfiguration(configurationFile); + + logger.info("== Testing HttpAccessLog files."); + String source = configuration.getProbeHttpAccessLogSource(); + logger.info("source=[{}]", source); + + Files files = FilesUtils.searchByWildcard(source); + for (File file : files) + { + System.out.println(file.getAbsolutePath()); + } + } + } + + /** + * Test http access log lines. + * + * @param configurationFile + * the configuration file + * @throws StatoolInfosException + * the statool infos exception + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static void testHttpAccessLogLines(final File configurationFile) throws StatoolInfosException, IOException + { + if ((configurationFile == null) || (!configurationFile.exists())) + { + System.out.println("No configuration file found."); + } + else + { + System.out.println("Testing HttpAccesLog lines from [" + configurationFile.toString() + "]"); + Configuration configuration = Factory.loadConfiguration(configurationFile); + + logger.info("== Testing HttpAccessLog lines."); + String source = configuration.getProbeHttpAccessLogSource(); + String pattern = configuration.getProbeHttpAccessLogPattern(); + String pathFilter = configuration.getProbeHttpAccessLogPathFilter(); + logger.info("source=[{}]", source); + logger.info("pattern=[{}]", pattern); + logger.info("path=[{}]", pathFilter); + + HttpAccessLogs logs = new HttpAccessLogs(FilesUtils.searchByWildcard(source), pattern, pathFilter); + + for (HttpAccessLog log : logs) + { + System.out.println(log.toStringLog()); + } + } + } + /** * Uptime. *