/** * 최근 x분동안의 ERROR log 카운트 * * @return */ public PipeShellCommand getTomcatLogPipeShellCommand() { return PipeShellCommand.getBuilder() .addShellCommand(Tomcat.sed(daemonMemorizer.restore(DaemonProperties.TOMCAT_LOG_PATH))) .addShellCommand(Tomcat.grep()) .addShellCommand(Tomcat.wc()) .build(); }
/** * 최근 x분동안의 4xx, 5xx 에러 건수 카운트 * * @return */ public PipeShellCommand getAccessSSLLogPipeShellCommand() { return PipeShellCommand.getBuilder() .addShellCommand(Apache.sed(daemonMemorizer.restore(DaemonProperties.APACHE_SSL_LOG_PATH))) .addShellCommand(Apache.awk()) .addShellCommand(Apache.grep()) .addShellCommand(Apache.wc()) .build(); }
/** @author baejunbeom */ public class CommandFactory { private static final CommandFactory INSTANCE = new CommandFactory(); private DaemonMemorizer daemonMemorizer = DaemonMemorizer.getInstance(); private CommandFactory() {} public static CommandFactory getInstance() { return INSTANCE; } /** * 최근 x분동안의 4xx, 5xx 에러 건수 카운트 * * @return */ public PipeShellCommand getAccessLogPipeShellCommand() { return PipeShellCommand.getBuilder() .addShellCommand(Apache.sed(daemonMemorizer.restore(DaemonProperties.APACHE_LOG_PATH))) .addShellCommand(Apache.awk()) .addShellCommand(Apache.grep()) .addShellCommand(Apache.wc()) .build(); } /** * 최근 x분동안의 4xx, 5xx 에러 건수 카운트 * * @return */ public PipeShellCommand getAccessSSLLogPipeShellCommand() { return PipeShellCommand.getBuilder() .addShellCommand(Apache.sed(daemonMemorizer.restore(DaemonProperties.APACHE_SSL_LOG_PATH))) .addShellCommand(Apache.awk()) .addShellCommand(Apache.grep()) .addShellCommand(Apache.wc()) .build(); } /** * 최근 x분동안의 ERROR log 카운트 * * @return */ public PipeShellCommand getTomcatLogPipeShellCommand() { return PipeShellCommand.getBuilder() .addShellCommand(Tomcat.sed(daemonMemorizer.restore(DaemonProperties.TOMCAT_LOG_PATH))) .addShellCommand(Tomcat.grep()) .addShellCommand(Tomcat.wc()) .build(); } /** @author baejunbeom */ static class Apache { private static ShellCommand wc() { ShellCommand shellCommand = new ShellCommand(); shellCommand.setCommand("wc"); shellCommand.setOption("-l"); return shellCommand; } private static ShellCommand grep() { ShellCommand shellCommand = new ShellCommand(); shellCommand.setCommand("grep"); shellCommand.setOption("-E '^(4|5)'"); return shellCommand; } private static ShellCommand awk() { ShellCommand shellCommand = new ShellCommand(); shellCommand.setCommand("awk"); shellCommand.setOption("'{print $9}'"); return shellCommand; } private static ShellCommand sed(String filePath) { ShellCommand shellCommand = new ShellCommand(); shellCommand.setCommand("sed"); shellCommand.setOption(String.format("-n '%s' %s", getSedRegx(), getFile(filePath))); return shellCommand; } static String getSedRegx() { Date computedDate = addDate(new Date(), Calendar.MINUTE, -1); String years = StringUtils.split(computedDate.toString(), " ")[5]; String month = StringUtils.split(computedDate.toString(), " ")[1]; String day = StringUtils.split(computedDate.toString(), " ")[2]; String time = StringUtils.split(computedDate.toString(), " ")[3]; StringBuilder builder = new StringBuilder(); builder.append("/").append(day).append("\\"); builder.append("/").append(month).append("\\"); builder .append("/") .append(years) .append(":") .append(time.substring(0, time.length() - 3)) .append("/"); builder.append(",$ p"); return builder.toString(); } } /** @author baejunbeom */ private static class Tomcat { private static ShellCommand wc() { ShellCommand shellCommand = new ShellCommand(); shellCommand.setCommand("wc"); shellCommand.setOption("-l"); return shellCommand; } private static ShellCommand grep() { ShellCommand shellCommand = new ShellCommand(); shellCommand.setCommand("grep"); shellCommand.setOption("INFO"); return shellCommand; } private static ShellCommand sed(String filePath) { ShellCommand shellCommand = new ShellCommand(); shellCommand.setCommand("sed"); shellCommand.setOption(String.format("-n '%s' %s", getTomcatSedRegx(), filePath)); return shellCommand; } static String getTomcatSedRegx() { Date computedDate = addDate(new Date(), Calendar.MINUTE, -5); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.KOREA); String formattedDate = simpleDateFormat.format(computedDate); StringBuilder builder = new StringBuilder(); builder.append("/").append(formattedDate).append("/"); builder.append(",$ p"); return builder.toString(); } } static Date addDate(Date date, int computeUnit, int computeTime) { if (date == null) { throw new IllegalArgumentException("date is null"); } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(computeUnit, computeTime); return calendar.getTime(); } static String getFile(String filePath) { Date computedDate = addDate(new Date(), Calendar.MINUTE, -5); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd", Locale.KOREA); // home1/irteam/logs/apache/fileName.date.log return filePath + "." + simpleDateFormat.format(computedDate) + ".log"; } }