From e324aef88dabf8f194384aaf35f6214a733e5719 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Mon, 15 Mar 2021 22:03:41 +0100 Subject: [PATCH] Added checksort command. --- src/fr/devinsy/logar/app/Logar.java | 161 +++++++++++++++++++++++++- src/org/april/logar/cli/LogarCLI.java | 77 ++++++------ src/org/april/logar/util/Files.java | 26 +++++ 3 files changed, 220 insertions(+), 44 deletions(-) diff --git a/src/fr/devinsy/logar/app/Logar.java b/src/fr/devinsy/logar/app/Logar.java index 1d40663..b18cfc8 100644 --- a/src/fr/devinsy/logar/app/Logar.java +++ b/src/fr/devinsy/logar/app/Logar.java @@ -127,7 +127,7 @@ public final class Logar { if ((!file.isDirectory()) && (file.getName().contains("access"))) { - // logger.info(file.getName()); + logger.info(file.getName()); try { LineIterator iterator = new LineIterator(file); @@ -294,6 +294,165 @@ public final class Logar } } + /** + * Check sort. + * + * @param source + * the source + * @throws IOException + */ + public static void checkSort(final File source) throws IOException + { + if (source != null) + { + if (source.isFile()) + { + checkSortForAccessFiles(source); + } + else + { + for (File file : Files.of(source).removeHidden().sortByName()) + { + if (file.isDirectory()) + { + checkSort(file); + } + else if (file.getName().contains("access")) + { + checkSortForAccessFiles(file); + } + else if (file.getName().contains("error")) + { + checkSortForErrorFiles(file); + } + } + } + } + } + + /** + * Check sort for access files. + * + * @param source + * the source + * @throws IOException + */ + public static void checkSortForAccessFiles(final File file) throws IOException + { + if (file == null) + { + throw new IllegalArgumentException("Null parameter [source]"); + } + else if (!file.isFile()) + { + throw new IllegalArgumentException("Source parameter is not a file."); + } + else + { + System.out.println("== Check sort for [" + file.getName() + "]"); + LocalDateTime currentDate = null; + int count = 0; + int badLineCount = 0; + int badGroupCount = 0; + LineIterator iterator = new LineIterator(file); + while (iterator.hasNext()) + { + String line = iterator.next(); + LocalDateTime date = getLogDate(line); + if (currentDate == null) + { + currentDate = date; + } + else if (date.isBefore(currentDate)) + { + count += 1; + } + else + { + currentDate = date; + + if (count != 0) + { + System.out.println(String.format("detected lines: %d %s", count, currentDate.toString())); + + badLineCount += count; + badGroupCount += 1; + count = 0; + } + } + } + if (count != 0) + { + System.out.println(String.format("detected lines: %d %s", count, currentDate.toString())); + + badLineCount += count; + badGroupCount += 1; + count = 0; + } + + if (badLineCount > 0) + { + System.out.println("Bad group count: " + badGroupCount); + System.out.println("Bad line count: " + badLineCount); + } + } + } + + /** + * Check sort for error files. + * + * @param source + * the source + */ + public static void checkSortForErrorFiles(final File file) + { + if (file == null) + { + throw new IllegalArgumentException("Null parameter [source]"); + } + else if (!file.isFile()) + { + throw new IllegalArgumentException("Source parameter is not a file."); + } + else + { + System.out.println("== Check sort for [" + file.getName() + "]"); + } + } + + /** + * Gets the log date. + * + * @param line + * the line + * @return the log date + */ + private static LocalDateTime getLogDate(final String line) + { + LocalDateTime result; + + Matcher matcher = nginxAccessLogLinePattern.matcher(line); + if (matcher.matches()) + { + String value = matcher.group("time"); + try + { + result = LocalDateTime.parse(value, DateTimeFormatter.ofPattern("dd/MMM/yyyy:HH:mm:ss Z").withLocale(Locale.ENGLISH)); + } + catch (DateTimeParseException exception) + { + throw new IllegalArgumentException("Bad format time [" + value + "]"); + } + } + else + { + throw new IllegalArgumentException("Bad format line [" + line + "]"); + } + + // + return result; + } + /** * Test parsing. * diff --git a/src/org/april/logar/cli/LogarCLI.java b/src/org/april/logar/cli/LogarCLI.java index 57e304b..5cf17bc 100644 --- a/src/org/april/logar/cli/LogarCLI.java +++ b/src/org/april/logar/cli/LogarCLI.java @@ -53,10 +53,11 @@ public final class LogarCLI message.append("Logar CLI version ").appendln(BuildInformation.instance().version()); message.appendln("Usage:"); message.appendln(" logar [ -h | -help | --help ]"); - message.appendln(" logar -version"); - message.appendln(" logar archive source target"); - message.appendln(" logar test source"); - message.appendln(" logar anonymize source target"); + message.appendln(" logar [ -v | -version | --version ]"); + message.appendln(" logar anonymize source target anonymize ip and login"); + message.appendln(" logar archive source target archive previous month"); + message.appendln(" logar checksort file check sort of an access log file"); + message.appendln(" logar test source census bad format line in log files"); logger.info(message.toString()); } @@ -163,69 +164,59 @@ public final class LogarCLI } }); - logger.info("{} Logar call: {}", LocalDateTime.now(), new StringList(args).toStringSeparatedBy(" ")); + try + { + logger.info("{} Logar call: {}", LocalDateTime.now(), new StringList(args).toStringSeparatedBy(" ")); - if (isMatching(args)) - { - logger.info("No parameter."); - displayHelp(); - } - else if (isMatching(args, "(-h|--h|--help)")) - { - displayHelp(); - } - else if (isMatching(args, "(-v|-version|--version)")) - { - displayVersion(); - } - else if (isMatching(args, "anonymize", "\\s*\\S+\\s*", "\\s*\\S+\\s*")) - { - try + if (isMatching(args)) + { + logger.info("No parameter."); + displayHelp(); + } + else if (isMatching(args, "(-h|--h|--help)")) + { + displayHelp(); + } + else if (isMatching(args, "(-v|-version|--version)")) + { + displayVersion(); + } + else if (isMatching(args, "anonymize", "\\s*\\S+\\s*", "\\s*\\S+\\s*")) { File source = new File(args[1]); File target = new File(args[2]); Logar.anonymize(source, target); } - catch (Exception exception) - { - logger.error("Error: {}", exception.getMessage()); - exception.printStackTrace(); - } - } - else if (isMatching(args, "archive", "\\s*\\S+\\s*", "\\s*\\S+\\s*")) - { - try + else if (isMatching(args, "archive", "\\s*\\S+\\s*", "\\s*\\S+\\s*")) { File source = new File(args[1]); File target = new File(args[2]); Logar.archive(source, target); } - catch (Exception exception) + else if (isMatching(args, "checksort", "\\s*\\S+\\s*")) { - logger.error("Error: {}", exception.getMessage()); - exception.printStackTrace(); + File source = new File(args[1]); + + Logar.checkSort(source); } - } - else if (isMatching(args, "test", "\\s*\\S+\\s*")) - { - try + else if (isMatching(args, "test", "\\s*\\S+\\s*")) { File source = new File(args[1]); Logar.testParsing(source); } - catch (Exception exception) + else { - logger.error("Error: {}", exception.getMessage()); - exception.printStackTrace(); + logger.info("Bad usage."); + displayHelp(); } } - else + catch (Exception exception) { - logger.info("Bad usage."); - displayHelp(); + logger.error("Error: {}", exception.getMessage()); + exception.printStackTrace(); } // diff --git a/src/org/april/logar/util/Files.java b/src/org/april/logar/util/Files.java index b9bec9d..e909753 100644 --- a/src/org/april/logar/util/Files.java +++ b/src/org/april/logar/util/Files.java @@ -20,6 +20,7 @@ package org.april.logar.util; import java.io.File; import java.util.ArrayList; +import java.util.Iterator; /** * The Class Files. @@ -47,6 +48,31 @@ public class Files extends ArrayList super(initialCapacity); } + /** + * Removes the hidden. + * + * @return the files + */ + public Files removeHidden() + { + Files result; + + Iterator iterator = iterator(); + while (iterator.hasNext()) + { + File file = iterator.next(); + if (file.getName().startsWith(".")) + { + iterator.remove(); + } + } + + result = this; + + // + return result; + } + /** * Sort. *