logar/src/org/april/logar/cli/LogarCLI.java

263 lines
8.1 KiB
Java

/*
* Copyright (C) 2021 Christian Pierre MOMON <christian@momon.org>
*
* This file is part of Logar, simple tool to manage http log files.
*
* Logar 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.
*
* Logar 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 Logar. If not, see <http://www.gnu.org/licenses/>.
*/
package org.april.logar.cli;
import java.io.File;
import java.time.LocalDateTime;
import org.april.logar.util.BuildInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.logar.app.ExtractOptions;
import fr.devinsy.logar.app.Logar;
import fr.devinsy.logar.app.OnOffOption;
import fr.devinsy.strings.StringList;
/**
* The Class <code>Logar</code> manages a Command Line Interface for Logar.
*
*/
public final class LogarCLI
{
private static Logger logger = LoggerFactory.getLogger(LogarCLI.class);
/**
* Instantiates a new log tool CLI.
*/
private LogarCLI()
{
}
/**
* Display help.
*/
public static void displayHelp()
{
StringList message = new StringList();
message.append("Logar CLI version ").appendln(BuildInformation.instance().version());
message.appendln("Usage:");
message.appendln(" logar [ -h | -help | --help ]");
message.appendln(" logar [ -v | -version | --version ]");
message.appendln(" logar anonymize <fileordirectory> [<mapfile>] anonymize ip and user");
message.appendln(" logar archive [-dry] <source> <target> archive previous month from /var/log/nginx/ tree");
message.appendln(" logar check <fileordirectory> check line format in log files");
message.appendln(" logar checksort <fileordirectory> check sort in log files");
message.appendln(" logar extract <fields> <fileordirectory> extract one or more fields (ip,user,datetime,useragent) from log files");
message.appendln(" logar sort <fileordirectory> sort log files by datetime");
message.appendln(" logar testconcate <fileordirectory> test line concate in log files");
System.out.println(message.toString());
}
/**
* Display version.
*/
public static void displayVersion()
{
StringList message = new StringList();
message.appendln(BuildInformation.instance().version());
System.out.println(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;
}
/**
*
* This method launch CLI.
*
* @param args
* necessary arguments
*/
public static void run(final String[] args)
{
// Set default catch.
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException(final Thread thread, final Throwable exception)
{
String message;
if (exception instanceof OutOfMemoryError)
{
message = "Java ran out of memory!\n\n";
}
else
{
message = String.format("An error occured: %1s(%2s)", exception.getClass(), exception.getMessage());
}
logger.error("uncaughtException ", exception);
logger.error(message);
logger.info("Oups, an unexpected error occured. Please try again.");
}
});
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*"))
{
File source = new File(args[1]);
Logar.anonymize(source);
}
else if (isMatching(args, "anonymize", "\\s*\\S+\\s*", "\\s*\\S+\\s*"))
{
File source = new File(args[1]);
File map = new File(args[2]);
Logar.anonymize(source, map);
}
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, OnOffOption.OFF);
}
else if (isMatching(args, "archive", "-dry", "\\s*\\S+\\s*", "\\s*\\S+\\s*"))
{
File source = new File(args[2]);
File target = new File(args[3]);
Logar.archive(source, target, OnOffOption.ON);
}
else if (isMatching(args, "check", "\\s*\\S+\\s*"))
{
File source = new File(args[1]);
Logar.checkLogFiles(source);
}
else if (isMatching(args, "checksort", "\\s*\\S+\\s*"))
{
File source = new File(args[1]);
Logar.checkSort(source);
}
else if (isMatching(args, "extract", "\\s*((ip)?(,)?(remoteuser)?(,)?(datetime)?(,)?(useragent)?)\\s*", "\\s*\\S+\\s*"))
{
ExtractOptions options = ExtractOptions.of(args[1]);
File source = new File(args[2]);
Logar.extract(source, options);
}
else if (isMatching(args, "sort", "\\s*\\S+\\s*"))
{
File source = new File(args[1]);
Logar.sort(source);
}
else if (isMatching(args, "testconcate", "\\s*\\S+\\s*"))
{
File source = new File(args[1]);
Logar.testConcate(source);
}
else
{
logger.info("Bad usage.");
displayHelp();
}
}
catch (Exception exception)
{
logger.error("Error: {}", exception.getMessage());
exception.printStackTrace();
}
//
logger.info("Done.");
}
}