statoolinfosweb/src/fr/devinsy/statoolinfos/cli/StatoolInfosCLI.java

493 lines
16 KiB
Java
Raw Normal View History

2020-09-13 01:28:27 +02:00
/*
2021-05-14 17:20:52 +02:00
* Copyright (C) 2020-2021 Christian Pierre MOMON <christian@momon.org>
2020-09-13 01:28:27 +02:00
*
* 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 <http://www.gnu.org/licenses/>.
*/
package fr.devinsy.statoolinfos.cli;
import java.io.File;
import java.time.LocalDateTime;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2021-06-05 17:56:07 +02:00
import fr.devinsy.statoolinfos.core.LogFilter;
2020-09-13 01:28:27 +02:00
import fr.devinsy.statoolinfos.core.StatoolInfos;
2020-09-19 02:37:52 +02:00
import fr.devinsy.statoolinfos.util.BuildInformation;
import fr.devinsy.statoolinfos.util.Chrono;
2020-09-19 02:37:52 +02:00
import fr.devinsy.statoolinfos.util.Files;
2021-05-15 16:59:04 +02:00
import fr.devinsy.statoolinfos.util.FilesUtils;
2020-09-13 01:28:27 +02:00
import fr.devinsy.strings.StringList;
/**
* The Class <code>StatoolInfosCLI</code> manages a Command Line Interface for
* StatoolInfos.
*
*/
public final class StatoolInfosCLI
{
private static Logger logger = LoggerFactory.getLogger(StatoolInfosCLI.class);
/**
* Instantiates a new statool infos CLI.
*/
private StatoolInfosCLI()
{
}
/**
* Convert path.
*
* @param path
* the path
* @return the files
*/
public static Files convertPath(final String path)
{
Files result;
result = new Files();
if (StringUtils.isNotBlank(path))
{
File input = new File(path);
if (input.exists())
{
if (input.isFile())
{
result.add(input);
}
else
{
for (File file : input.listFiles())
{
if ((file.isFile()) && (file.getName().endsWith(".conf")))
2020-09-13 01:28:27 +02:00
{
result.add(file);
}
}
}
}
else
{
result.add(input);
}
}
//
return result;
}
/**
* Display help.
*/
public static void displayHelp()
{
StringList message = new StringList();
message.append("StatoolInfos CLI version ").appendln(BuildInformation.instance().version());
message.appendln("Usage:");
message.appendln(" statoolinfos [ -h | -help | --help ]");
message.appendln(" statoolinfos [ -v | -version | --version ]");
2021-06-05 17:56:07 +02:00
message.appendln(" statoolinfos build <fileordirectory> build property files from conf and input");
message.appendln(" statoolinfos clear <fileordirectory> remove property files from conf");
message.appendln(" statoolinfos crawl <fileordirectory> crawl all file from conf and input");
message.appendln(" statoolinfos format <fileordirectory> format in tiny way");
message.appendln(" statoolinfos htmlize <fileordirectory> generate web pages from conf");
message.appendln(" statoolinfos list ip [-bot|-nobot] <fileordirectory> generate ip list from log file");
message.appendln(" statoolinfos list ua [-bot|-nobot] <fileordirectory> generate user agent list from log file");
message.appendln(" statoolinfos list visitors [-bot|-nobot] <fileordirectory> generate visitors (ip+ua) list from log file");
message.appendln(" statoolinfos probe <fileordirectory> generate metrics files from conf");
message.appendln(" statoolinfos stat ip [-bot|-nobot] <fileordirectory> generate stats about ip from log file");
message.appendln(" statoolinfos stat ua [-bot|-nobot] <fileordirectory> generate stats about user agent from log file");
message.appendln(" statoolinfos stat visitors [-bot|-nobot] <fileordirectory> generate stats about visitors (ip+ua) from log file");
message.appendln(" statoolinfos tagdate <fileordirectory> update the file.datetime file");
2021-06-10 03:54:39 +02:00
message.appendln(" statoolinfos uptime <fileordirectory> update uptime journal");
2020-09-13 01:28:27 +02:00
2021-05-14 17:20:52 +02:00
System.out.println(message.toString());
2020-09-13 01:28:27 +02:00
}
/**
* Display version.
*/
public static void displayVersion()
{
StringList message = new StringList();
message.appendln(BuildInformation.instance().version());
2021-05-14 17:20:52 +02:00
System.out.println(message.toString());
2020-09-13 01:28:27 +02:00
}
/**
* 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;
}
2021-06-05 17:56:07 +02:00
/**
* Parses the log filter option.
*
* @param source
* the source
* @return the log filter
*/
private static LogFilter parseLogFilterOption(final String source)
{
LogFilter result;
if (StringUtils.equals(source, "-all"))
{
result = LogFilter.ALL;
}
else if (StringUtils.equals(source, "-bot"))
{
result = LogFilter.BOT;
}
else if (StringUtils.equals(source, "-nobot"))
{
result = LogFilter.NOBOT;
}
else
{
result = null;
}
//
return result;
}
2020-09-13 01:28:27 +02:00
/**
*
* 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.");
}
});
2021-04-15 03:55:18 +02:00
logger.debug("{} StatoolInfos call: {}", LocalDateTime.now(), new StringList(args).toStringSeparatedBy(" "));
2020-09-13 01:28:27 +02:00
if (isMatching(args))
{
logger.info("No parameter.");
displayHelp();
}
else if (isMatching(args, "(-h|--h|--help)"))
{
displayHelp();
}
else if (isMatching(args, "(-v|-version|--version)"))
{
displayVersion();
}
2021-05-14 17:20:52 +02:00
else if (isMatching(args, "build", "\\s*.+\\s*"))
2020-09-13 01:28:27 +02:00
{
Files inputs = convertPath(StringUtils.trim(args[1]));
for (File input : inputs)
{
try
{
2021-05-14 17:20:52 +02:00
StatoolInfos.build(input);
2020-09-13 01:28:27 +02:00
}
catch (Exception exception)
{
logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage());
}
}
}
2021-05-14 17:20:52 +02:00
else if (isMatching(args, "clear", "\\s*.+\\s*"))
2020-10-15 01:47:48 +02:00
{
Files inputs = convertPath(StringUtils.trim(args[1]));
for (File input : inputs)
{
try
{
2021-05-14 17:20:52 +02:00
StatoolInfos.clear(input);
2020-10-15 01:47:48 +02:00
}
catch (Exception exception)
{
logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage());
}
}
}
2021-05-14 17:20:52 +02:00
else if (isMatching(args, "crawl", "\\s*.+\\s*"))
2020-09-13 01:28:27 +02:00
{
Chrono chrono = new Chrono().start();
2020-09-13 01:28:27 +02:00
Files inputs = convertPath(StringUtils.trim(args[1]));
for (File input : inputs)
{
try
{
2021-05-14 17:20:52 +02:00
StatoolInfos.crawl(input);
2020-09-13 01:28:27 +02:00
}
catch (Exception exception)
{
logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage());
2021-05-14 17:20:52 +02:00
exception.printStackTrace();
2020-09-13 01:28:27 +02:00
}
}
System.out.println(chrono.format());
2020-09-13 01:28:27 +02:00
}
2021-05-15 16:59:04 +02:00
else if (isMatching(args, "format", "\\s*.+\\s*"))
{
Files inputs = FilesUtils.searchEndingWith(new File(args[1]), ".properties");
for (File input : inputs)
{
try
{
StatoolInfos.format(input);
}
catch (Exception exception)
{
logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage());
exception.printStackTrace();
}
}
}
2021-05-14 17:20:52 +02:00
else if (isMatching(args, "htmlize", "\\s*.+\\s*"))
2020-09-13 01:28:27 +02:00
{
Chrono chrono = new Chrono().start();
2020-09-13 01:28:27 +02:00
Files inputs = convertPath(StringUtils.trim(args[1]));
for (File input : inputs)
{
try
{
2021-05-14 17:20:52 +02:00
StatoolInfos.htmlize(input);
2020-09-13 01:28:27 +02:00
}
catch (Exception exception)
{
logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage());
exception.printStackTrace();
}
}
System.out.println(chrono.format());
2020-09-13 01:28:27 +02:00
}
2021-06-05 17:56:07 +02:00
else if (isMatching(args, "list", "ip", "\\s*\\S+\\s*"))
{
File source = new File(args[2]);
StatoolInfos.listIps(source, LogFilter.ALL);
}
else if (isMatching(args, "list", "ip", "(-all|-bot|-nobot)", "\\s*\\S+\\s*"))
{
LogFilter filter = parseLogFilterOption(args[2]);
File source = new File(args[3]);
StatoolInfos.listIps(source, filter);
}
else if (isMatching(args, "list", "(useragent|ua)", "\\s*\\S+\\s*"))
{
File source = new File(args[2]);
StatoolInfos.listUserAgents(source, LogFilter.ALL);
}
else if (isMatching(args, "list", "(useragent|ua)", "(-all|-bot|-nobot)", "\\s*\\S+\\s*"))
{
LogFilter filter = parseLogFilterOption(args[2]);
File source = new File(args[3]);
StatoolInfos.listUserAgents(source, filter);
}
else if (isMatching(args, "list", "visitors", "\\s*\\S+\\s*"))
{
File source = new File(args[2]);
StatoolInfos.listVisitors(source, LogFilter.ALL);
}
else if (isMatching(args, "list", "visitors", "(-all|-bot|-nobot)", "\\s*\\S+\\s*"))
{
LogFilter filter = parseLogFilterOption(args[2]);
File source = new File(args[3]);
StatoolInfos.listVisitors(source, filter);
}
2021-05-14 17:20:52 +02:00
else if (isMatching(args, "probe", "\\s*.+\\s*"))
2020-09-13 01:28:27 +02:00
{
Files inputs = convertPath(StringUtils.trim(args[1]));
for (File input : inputs)
{
try
{
2021-05-14 17:20:52 +02:00
StatoolInfos.probe(input);
2020-09-13 01:28:27 +02:00
}
catch (Exception exception)
{
logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage());
2020-09-15 03:16:26 +02:00
exception.printStackTrace();
2020-09-13 01:28:27 +02:00
}
}
}
2021-06-05 17:56:07 +02:00
else if (isMatching(args, "stat", "ip", "\\s*\\S+\\s*"))
{
File source = new File(args[2]);
StatoolInfos.statIps(source, LogFilter.ALL);
}
else if (isMatching(args, "stat", "ip", "(-all|-bot|-nobot)", "\\s*\\S+\\s*"))
{
LogFilter filter = parseLogFilterOption(args[2]);
File source = new File(args[3]);
StatoolInfos.statIps(source, filter);
}
else if (isMatching(args, "stat", "(useragent|ua)", "\\s*\\S+\\s*"))
{
File source = new File(args[2]);
StatoolInfos.statUserAgents(source, LogFilter.ALL);
}
else if (isMatching(args, "stat", "(useragent|ua)", "(-all|-bot|-nobot)", "\\s*\\S+\\s*"))
{
LogFilter filter = parseLogFilterOption(args[2]);
File source = new File(args[3]);
StatoolInfos.statUserAgents(source, filter);
}
else if (isMatching(args, "stat", "visitors", "\\s*\\S+\\s*"))
{
File source = new File(args[2]);
StatoolInfos.statVisitors(source, LogFilter.ALL);
}
else if (isMatching(args, "stat", "visitors", "(-all|-bot|-nobot)", "\\s*\\S+\\s*"))
{
LogFilter filter = parseLogFilterOption(args[2]);
File source = new File(args[3]);
StatoolInfos.statVisitors(source, filter);
}
2021-05-15 16:59:04 +02:00
else if (isMatching(args, "tagdate", "\\s*.+\\s*"))
{
Files inputs = FilesUtils.searchEndingWith(new File(args[1]), ".properties");
for (File input : inputs)
{
try
{
StatoolInfos.tagDate(input);
}
catch (Exception exception)
{
logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage());
exception.printStackTrace();
}
}
}
2021-06-10 03:54:39 +02:00
else if (isMatching(args, "uptime", "\\s*.+\\s*"))
{
Chrono chrono = new Chrono().start();
Files inputs = convertPath(StringUtils.trim(args[1]));
for (File input : inputs)
{
try
{
StatoolInfos.uptime(input);
}
catch (Exception exception)
{
logger.error("Error with [{}]: {}", input.getAbsoluteFile(), exception.getMessage());
exception.printStackTrace();
}
}
System.out.println(chrono.format());
}
2020-09-13 01:28:27 +02:00
else
{
2021-05-14 17:20:52 +02:00
System.out.println("Bad usage.");
2020-09-13 01:28:27 +02:00
displayHelp();
}
2020-09-19 02:37:52 +02:00
//
logger.info("Done.");
2020-09-13 01:28:27 +02:00
}
}