From af0ee48e4b913e866e5f0398dead0b1485f181a4 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Mon, 27 Feb 2023 18:19:05 +0100 Subject: [PATCH] Improved CLI commands. --- src/fr/devinsy/statoolinfos/cli/CLIUtils.java | 163 +++++++++++ .../statoolinfos/cli/StatoolInfosCLI.java | 198 +++++-------- .../statoolinfos/core/StatoolInfos.java | 277 ++++++++++-------- .../metrics/http/HttpAccessLogs.java | 46 +++ 4 files changed, 440 insertions(+), 244 deletions(-) create mode 100644 src/fr/devinsy/statoolinfos/cli/CLIUtils.java diff --git a/src/fr/devinsy/statoolinfos/cli/CLIUtils.java b/src/fr/devinsy/statoolinfos/cli/CLIUtils.java new file mode 100644 index 0000000..e56a319 --- /dev/null +++ b/src/fr/devinsy/statoolinfos/cli/CLIUtils.java @@ -0,0 +1,163 @@ +/* + * Copyright (C) 2020-2023 Christian Pierre MOMON + * + * This file is part of StatoolInfos, simple service statistics tool. + * + * StatoolInfos is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * StatoolInfos is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with StatoolInfos. If not, see . + */ +package fr.devinsy.statoolinfos.cli; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Class CLIUtils. + */ +public final class CLIUtils +{ + private static Logger logger = LoggerFactory.getLogger(CLIUtils.class); + + /** + * Instantiates a new statool infos CLI. + */ + private CLIUtils() + { + } + + /** + * Checks if is matching. + * + * @param args + * the args + * @param regexps + * the regexps + * @return true, if is matching + */ + public static boolean isMatching(final String[] args, final String... regexps) + { + boolean result; + + if ((args.length == 0) && (regexps == null)) + { + result = true; + } + else if ((args.length != 0) && (regexps == null)) + { + result = false; + } + else if (args.length != regexps.length) + { + result = false; + } + else + { + boolean ended = false; + int index = 0; + result = false; + while (!ended) + { + if (index < args.length) + { + String arg = args[index]; + String regexp = regexps[index]; + + if (arg.matches(regexp)) + { + index += 1; + } + else + { + ended = true; + result = false; + } + } + else + { + ended = true; + result = true; + } + } + } + + // + return result; + } + + /** + * Checks if is matching ellipsis. + * + * @param args + * the args + * @param regexps + * the regexps + * @return true, if is matching ellipsis + */ + public static boolean isMatchingEllipsis(final String[] args, final String... regexps) + { + boolean result; + + if ((args.length == 0) && (regexps == null)) + { + result = true; + } + else if ((args.length != 0) && (regexps == null)) + { + result = false; + } + else if (args.length < regexps.length) + { + result = false; + } + else + { + boolean ended = false; + int index = 0; + result = false; + while (!ended) + { + if (index < args.length) + { + String arg = args[index]; + String regexp; + if (index < regexps.length) + { + regexp = regexps[index]; + } + else + { + regexp = regexps[regexps.length - 1]; + } + + if (arg.matches(regexp)) + { + index += 1; + } + else + { + ended = true; + result = false; + } + } + else + { + ended = true; + result = true; + } + } + } + + // + return result; + } +} diff --git a/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java b/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java index 3148464..c111438 100644 --- a/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java +++ b/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java @@ -116,15 +116,14 @@ public final class StatoolInfosCLI message.appendln(" statoolinfos tagdate update the file.datetime file"); message.appendln(" statoolinfos uptime update uptime journal"); message.appendln(); - message.appendln(" statoolinfos list logs [-bot|-nobot] display logs from http log file"); + message.appendln(" statoolinfos list files display http access log files"); + message.appendln(" statoolinfos list logs [-bot|-nobot] display http access log lines"); message.appendln(" statoolinfos list ip [-bot|-nobot] generate ip list from http log file"); message.appendln(" statoolinfos list ua [-bot|-nobot] generate user agent list from http log file"); message.appendln(" statoolinfos list visitors [-bot|-nobot] generate visitors (ip+ua) list from http log file"); 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()); } @@ -141,65 +140,6 @@ public final class StatoolInfosCLI System.out.print(message.toString()); } - /** - * Checks if is matching. - * - * @param args - * the args - * @param regexps - * the regexps - * @return true, if is matching - */ - public static boolean isMatching(final String[] args, final String... regexps) - { - boolean result; - - if ((args.length == 0) && (regexps == null)) - { - result = true; - } - else if ((args.length != 0) && (regexps == null)) - { - result = false; - } - else if (args.length != regexps.length) - { - result = false; - } - else - { - boolean ended = false; - int index = 0; - result = false; - while (!ended) - { - if (index < args.length) - { - String arg = args[index]; - String regexp = regexps[index]; - - if (arg.matches(regexp)) - { - index += 1; - } - else - { - ended = true; - result = false; - } - } - else - { - ended = true; - result = true; - } - } - } - - // - return result; - } - /** * Normalize bash parameter. * @@ -289,20 +229,20 @@ public final class StatoolInfosCLI logger.debug("{} StatoolInfos call: {}", LocalDateTime.now(), new StringList(args).toStringSeparatedBy(" ")); - if (isMatching(args)) + if (CLIUtils.isMatching(args)) { logger.info("No parameter."); displayHelp(); } - else if (isMatching(args, "(-h|--h|--help)")) + else if (CLIUtils.isMatching(args, "(-h|--h|--help)")) { displayHelp(); } - else if (isMatching(args, "(-v|-version|--version)")) + else if (CLIUtils.isMatching(args, "(-v|-version|--version)")) { displayVersion(); } - else if (isMatching(args, "build", "\\s*.+\\.conf\\s*")) + else if (CLIUtils.isMatching(args, "build", "\\s*.+\\.conf\\s*")) { File configurationFile = new File(StringUtils.trim(args[1])); try @@ -314,7 +254,7 @@ public final class StatoolInfosCLI logger.error("Error with [{}]: {}", configurationFile.getAbsoluteFile(), exception.getMessage()); } } - else if (isMatching(args, "clear", "\\s*.+\\.conf\\s*")) + else if (CLIUtils.isMatching(args, "clear", "\\s*.+\\.conf\\s*")) { File configurationFile = new File(StringUtils.trim(args[1])); try @@ -326,7 +266,7 @@ public final class StatoolInfosCLI logger.error("Error with [{}]: {}", configurationFile.getAbsoluteFile(), exception.getMessage()); } } - else if (isMatching(args, "crawl", "\\s*.+\\.conf\\s*")) + else if (CLIUtils.isMatching(args, "crawl", "\\s*.+\\.conf\\s*")) { Chrono chrono = new Chrono().start(); @@ -342,7 +282,7 @@ public final class StatoolInfosCLI } System.out.println(chrono.format()); } - else if (isMatching(args, "format", "\\s*.+\\s*")) + else if (CLIUtils.isMatching(args, "format", "\\s*.+\\s*")) { Files inputs = FilesUtils.searchEndingWith(new File(args[1]), ".properties"); for (File input : inputs) @@ -358,7 +298,7 @@ public final class StatoolInfosCLI } } } - else if (isMatching(args, "htmlize", "\\s*.+\\.conf\\s*")) + else if (CLIUtils.isMatching(args, "htmlize", "\\s*.+\\.conf\\s*")) { Chrono chrono = new Chrono().start(); File configurationFile = new File(StringUtils.trim(args[1])); @@ -373,46 +313,90 @@ public final class StatoolInfosCLI } System.out.println(chrono.format()); } - else if (isMatching(args, "list", "ip", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatching(args, "list", "ip", "\\s*\\S+\\s*")) { File source = new File(args[2]); StatoolInfos.listIps(source, BotFilter.ALL); } - else if (isMatching(args, "list", "ip", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatching(args, "list", "ip", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) { BotFilter filter = parseLogFilterOption(args[2]); File source = new File(args[3]); StatoolInfos.listIps(source, filter); } - else if (isMatching(args, "list", "(useragent|ua)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatching(args, "list", "files", "\\s*.+\\.conf\\s*")) + { + File configurationFile = new File(StringUtils.trim(args[2])); + StatoolInfos.listFiles(configurationFile); + } + else if (CLIUtils.isMatchingEllipsis(args, "list", "files", "\\s*\\S+\\s*")) + { + Files files = new Files(); + for (int index = 2; index < args.length; index++) + { + files.add(new File(args[index])); + } + StatoolInfos.listFiles(files); + } + else if (CLIUtils.isMatching(args, "list", "logs", "\\s*.+\\.conf\\s*")) + { + File configurationFile = new File(StringUtils.trim(args[2])); + StatoolInfos.listLogs(configurationFile, BotFilter.ALL); + } + else if (CLIUtils.isMatching(args, "list", "logs", "(-all|-bot|-nobot)", "\\s*.+\\.conf\\s*")) + { + BotFilter filter = parseLogFilterOption(args[2]); + File configurationFile = new File(StringUtils.trim(args[3])); + StatoolInfos.listLogs(configurationFile, filter); + } + else if (CLIUtils.isMatchingEllipsis(args, "list", "logs", "\\s*\\S+\\s*")) + { + Files files = new Files(); + for (int index = 2; index < args.length; index++) + { + files.add(new File(args[index])); + } + StatoolInfos.listLogs(files, BotFilter.ALL); + } + else if (CLIUtils.isMatchingEllipsis(args, "list", "logs", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) + { + BotFilter filter = parseLogFilterOption(args[2]); + Files files = new Files(); + for (int index = 2; index < args.length; index++) + { + files.add(new File(args[index])); + } + StatoolInfos.listLogs(files, filter); + } + else if (CLIUtils.isMatching(args, "list", "(useragent|ua)", "\\s*\\S+\\s*")) { File source = new File(args[2]); StatoolInfos.listUserAgents(source, BotFilter.ALL); } - else if (isMatching(args, "list", "(useragent|ua)", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatching(args, "list", "(useragent|ua)", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) { BotFilter filter = parseLogFilterOption(args[2]); File source = new File(args[3]); StatoolInfos.listUserAgents(source, filter); } - else if (isMatching(args, "list", "visitors", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatching(args, "list", "visitors", "\\s*\\S+\\s*")) { File source = new File(args[2]); StatoolInfos.listVisitors(source, BotFilter.ALL); } - else if (isMatching(args, "list", "visitors", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatching(args, "list", "visitors", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) { BotFilter filter = parseLogFilterOption(args[2]); File source = new File(args[3]); StatoolInfos.listVisitors(source, filter); } - else if (isMatching(args, "probe", "\\S*(-full|-today|-previousday|-\\d+)\\s*", "\\s*.+\\s*")) + else if (CLIUtils.isMatching(args, "probe", "\\S*(-full|-today|-previousday|-\\d+)\\s*", "\\s*.+\\s*")) { File configurationFile = new File(StringUtils.trim(args[2])); @@ -465,45 +449,45 @@ public final class StatoolInfosCLI exception.printStackTrace(); } } - else if (isMatching(args, "stat", "ip", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatching(args, "stat", "ip", "\\s*\\S+\\s*")) { File source = new File(args[2]); StatoolInfos.statIps(source, BotFilter.ALL); } - else if (isMatching(args, "stat", "ip", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatching(args, "stat", "ip", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) { BotFilter filter = parseLogFilterOption(args[2]); File source = new File(args[3]); StatoolInfos.statIps(source, filter); } - else if (isMatching(args, "stat", "(useragent|ua)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatching(args, "stat", "(useragent|ua)", "\\s*\\S+\\s*")) { File source = new File(args[2]); StatoolInfos.statUserAgents(source, BotFilter.ALL); } - else if (isMatching(args, "stat", "(useragent|ua)", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatching(args, "stat", "(useragent|ua)", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) { BotFilter filter = parseLogFilterOption(args[2]); File source = new File(args[3]); StatoolInfos.statUserAgents(source, filter); } - else if (isMatching(args, "stat", "visitors", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatching(args, "stat", "visitors", "\\s*\\S+\\s*")) { File source = new File(args[2]); StatoolInfos.statVisitors(source, BotFilter.ALL); } - else if (isMatching(args, "stat", "visitors", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) + else if (CLIUtils.isMatching(args, "stat", "visitors", "(-all|-bot|-nobot)", "\\s*\\S+\\s*")) { BotFilter filter = parseLogFilterOption(args[2]); File source = new File(args[3]); StatoolInfos.statVisitors(source, filter); } - else if (isMatching(args, "tagdate", "\\s*.+\\s*")) + else if (CLIUtils.isMatching(args, "tagdate", "\\s*.+\\s*")) { Files inputs = FilesUtils.searchEndingWith(new File(args[1]), ".properties"); for (File input : inputs) @@ -519,7 +503,7 @@ public final class StatoolInfosCLI } } } - else if (isMatching(args, "uptime", "\\s*.+\\.conf\\s*")) + else if (CLIUtils.isMatching(args, "uptime", "\\s*.+\\.conf\\s*")) { Chrono chrono = new Chrono().start(); @@ -535,50 +519,6 @@ public final class StatoolInfosCLI } System.out.println(chrono.format()); } - else if (isMatching(args, "testHttpAccessLog", "\\s*\\S+\\s*")) - { - System.out.println("param1=" + args[1]); - String source = normalizeBashParameter(args[1]); - System.out.println("source=" + source); - Files files = FilesUtils.searchByWildcard(source); - System.out.println(files.size()); - try - { - StatoolInfos.testHttpAccessLog(files); - } - catch (Exception exception) - { - logger.error("Error: {}", exception.getMessage()); - 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 34d25ec..625cab2 100644 --- a/src/fr/devinsy/statoolinfos/core/StatoolInfos.java +++ b/src/fr/devinsy/statoolinfos/core/StatoolInfos.java @@ -34,8 +34,6 @@ import fr.devinsy.statoolinfos.crawl.Crawler; import fr.devinsy.statoolinfos.htmlize.Htmlizer; 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; @@ -50,7 +48,6 @@ 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; @@ -166,6 +163,72 @@ public class StatoolInfos Htmlizer.htmlize(configurationFile); } + /** + * Test http access log files. + * + * @param configurationFile + * the configuration file + * @throws IOException + * @throws StatoolInfosException + */ + public static void listFiles(final File configurationFile) + { + try + { + if ((configurationFile == null) || (!configurationFile.exists())) + { + System.out.println("No configuration file found."); + } + else + { + System.out.println("Listing HttpAccesLog files from [" + configurationFile.toString() + "]"); + Configuration configuration = Factory.loadConfiguration(configurationFile); + + logger.info("== List HttpAccessLog files."); + String source = configuration.getProbeHttpAccessLogSource(); + logger.info("source=[{}]", source); + + Files files = FilesUtils.searchByWildcard(source); + for (File file : files) + { + System.out.println(file.getAbsolutePath()); + } + } + } + catch (StatoolInfosException exception) + { + exception.printStackTrace(); + } + catch (IOException exception) + { + exception.printStackTrace(); + } + } + + /** + * List http access log files. + * + * @param files + * the files + * @throws StatoolInfosException + * the statool infos exception + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static void listFiles(final Files files) + { + if (files != null) + { + System.out.println("Listing HttpAccesLog files from [" + files.toString() + "]"); + + logger.info("== List HttpAccessLog files"); + for (File file : files) + { + System.out.println(file.getAbsolutePath()); + } + } + } + /** * List ips. * @@ -180,12 +243,9 @@ public class StatoolInfos IpStator stator = new IpStator(); Chrono chrono = new Chrono().start(); - LineIterator iterator = new LineIterator(source); - while (iterator.hasNext()) + HttpAccessLogs logs = new HttpAccessLogs(source); + for (HttpAccessLog log : logs) { - String line = iterator.next(); - HttpAccessLog log = HttpAccessLogParser.parseLog(line, HttpAccessLogParser.COMBINED_PATTERN); - if (filter.matches(log)) { stator.putLog(log); @@ -217,6 +277,90 @@ public class StatoolInfos } } + /** + * List logs. + * + * @param configurationFile + * the configuration file + * @param filter + * the filter + */ + public static void listLogs(final File configurationFile, final BotFilter filter) + { + try + { + 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) + { + if (filter.matches(log)) + { + System.out.println(log.toStringLog()); + } + } + } + } + catch (Exception exception) + { + logger.error("Error: {}", exception.getMessage()); + exception.printStackTrace(); + } + } + + /** + * List logs. + * + * @param source + * the source + * @param filter + * the filter + */ + public static void listLogs(final Files source, final BotFilter filter) + { + try + { + HttpAccessLogs logs = new HttpAccessLogs(source); + for (HttpAccessLog log : logs) + { + if (filter.matches(log)) + { + System.out.println(log.toStringLog()); + } + } + } + catch (IllegalArgumentException exception) + { + System.out.println("Bad format line detected. Aborting…"); + exception.printStackTrace(); + } + catch (DateTimeParseException exception) + { + System.out.println("Bad datetime format detected. Aborting…"); + } + catch (IOException exception) + { + System.out.println("File error detected. Aborting…"); + exception.printStackTrace(); + } + } + /** * List user agents. * @@ -230,12 +374,9 @@ public class StatoolInfos UserAgentStator stator = new UserAgentStator(); Chrono chrono = new Chrono().start(); - LineIterator iterator = new LineIterator(source); - while (iterator.hasNext()) + HttpAccessLogs logs = new HttpAccessLogs(source); + for (HttpAccessLog log : logs) { - String line = iterator.next(); - HttpAccessLog log = HttpAccessLogParser.parseLog(line, HttpAccessLogParser.COMBINED_PATTERN); - if (filter.matches(log)) { stator.putLog(log); @@ -281,12 +422,9 @@ public class StatoolInfos VisitorStator stator = new VisitorStator(); Chrono chrono = new Chrono().start(); - LineIterator iterator = new LineIterator(source); - while (iterator.hasNext()) + HttpAccessLogs logs = new HttpAccessLogs(source); + for (HttpAccessLog log : logs) { - String line = iterator.next(); - HttpAccessLog log = HttpAccessLogParser.parseLog(line, HttpAccessLogParser.COMBINED_PATTERN); - if (filter.matches(log)) { stator.putLog(log); @@ -347,11 +485,9 @@ public class StatoolInfos IpStator stator = new IpStator(); Chrono chrono = new Chrono().start(); - LineIterator iterator = new LineIterator(source); - while (iterator.hasNext()) + HttpAccessLogs logs = new HttpAccessLogs(source); + for (HttpAccessLog log : logs) { - String line = iterator.next(); - HttpAccessLog log = HttpAccessLogParser.parseLog(line, HttpAccessLogParser.COMBINED_PATTERN); if (filter.matches(log)) { stator.putLog(log); @@ -398,11 +534,9 @@ public class StatoolInfos UserAgentStator stator = new UserAgentStator(); Chrono chrono = new Chrono().start(); - LineIterator iterator = new LineIterator(source); - while (iterator.hasNext()) + HttpAccessLogs logs = new HttpAccessLogs(source); + for (HttpAccessLog log : logs) { - String line = iterator.next(); - HttpAccessLog log = HttpAccessLogParser.parseLog(line, HttpAccessLogParser.COMBINED_PATTERN); if (filter.matches(log)) { stator.putLog(log); @@ -449,11 +583,9 @@ public class StatoolInfos VisitorStator stator = new VisitorStator(); Chrono chrono = new Chrono().start(); - LineIterator iterator = new LineIterator(source); - while (iterator.hasNext()) + HttpAccessLogs logs = new HttpAccessLogs(source); + for (HttpAccessLog log : logs) { - String line = iterator.next(); - HttpAccessLog log = HttpAccessLogParser.parseLog(line, HttpAccessLogParser.COMBINED_PATTERN); if (filter.matches(log)) { stator.putLog(log); @@ -535,91 +667,6 @@ public class StatoolInfos } } - /** - * Test http access log. - * - * @param source - * the source - * @throws IOException - */ - public static void testHttpAccessLog(final Files source) throws IOException - { - HttpAccessLogIterator logs = new HttpAccessLogIterator(source); - while (logs.hasNext()) - { - System.out.println(logs.next().toStringLog()); - } - } - - /** - * 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. * diff --git a/src/fr/devinsy/statoolinfos/metrics/http/HttpAccessLogs.java b/src/fr/devinsy/statoolinfos/metrics/http/HttpAccessLogs.java index 38306e1..7788cec 100644 --- a/src/fr/devinsy/statoolinfos/metrics/http/HttpAccessLogs.java +++ b/src/fr/devinsy/statoolinfos/metrics/http/HttpAccessLogs.java @@ -18,6 +18,7 @@ */ package fr.devinsy.statoolinfos.metrics.http; +import java.io.File; import java.io.IOException; import java.util.Iterator; @@ -37,6 +38,51 @@ public class HttpAccessLogs implements Iterable private String pattern; private String pathFilter; + /** + * Instantiates a new http access logs. + * + * @param source + * the source + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public HttpAccessLogs(final File source) throws IOException + { + this(new Files(source), null, null); + } + + /** + * Instantiates a new http access logs. + * + * @param source + * the source + * @param pattern + * the pattern + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public HttpAccessLogs(final File source, final String pattern) throws IOException + { + this(new Files(source), pattern, null); + } + + /** + * Instantiates a new http access logs. + * + * @param source + * the source + * @param pattern + * the pattern + * @param pathFilter + * the path filter + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public HttpAccessLogs(final File source, final String pattern, final String pathFilter) throws IOException + { + this(new Files(source), pattern, null); + } + /** * Instantiates a new http access logs. *