Search in sources :

Example 1 with WorkerLogs

use of org.apache.storm.daemon.logviewer.utils.WorkerLogs in project storm by apache.

the class LogviewerServer method main.

/**
 * Main method to start the server.
 */
public static void main(String[] args) throws Exception {
    Utils.setupDefaultUncaughtExceptionHandler();
    Map<String, Object> conf = ConfigUtils.readStormConfig();
    StormMetricsRegistry metricsRegistry = new StormMetricsRegistry();
    String logRoot = ConfigUtils.workerArtifactsRoot(conf);
    File logRootDir = new File(logRoot);
    logRootDir.mkdirs();
    WorkerLogs workerLogs = new WorkerLogs(conf, logRootDir.toPath(), metricsRegistry);
    DirectoryCleaner directoryCleaner = new DirectoryCleaner(metricsRegistry);
    try (LogviewerServer server = new LogviewerServer(conf, metricsRegistry);
        LogCleaner logCleaner = new LogCleaner(conf, workerLogs, directoryCleaner, logRootDir.toPath(), metricsRegistry)) {
        metricsRegistry.startMetricsReporters(conf);
        Utils.addShutdownHookWithForceKillIn1Sec(() -> {
            server.meterShutdownCalls.mark();
            metricsRegistry.stopMetricsReporters();
            server.close();
        });
        logCleaner.start();
        server.start();
        server.awaitTermination();
    }
}
Also used : StormMetricsRegistry(org.apache.storm.metric.StormMetricsRegistry) DirectoryCleaner(org.apache.storm.daemon.logviewer.utils.DirectoryCleaner) WorkerLogs(org.apache.storm.daemon.logviewer.utils.WorkerLogs) File(java.io.File) LogCleaner(org.apache.storm.daemon.logviewer.utils.LogCleaner)

Example 2 with WorkerLogs

use of org.apache.storm.daemon.logviewer.utils.WorkerLogs in project storm by apache.

the class LogviewerLogDownloadHandlerTest method createHandlerTraversalTests.

private LogviewerLogDownloadHandler createHandlerTraversalTests(Path rootPath) throws IOException {
    Path daemonLogRoot = rootPath.resolve("logs");
    Path fileOutsideDaemonRoot = rootPath.resolve("evil.sh");
    Path workerLogRoot = daemonLogRoot.resolve("workers-artifacts");
    Path daemonFile = daemonLogRoot.resolve("nimbus.log");
    Path topoA = workerLogRoot.resolve("topoA");
    Path file1 = topoA.resolve("1111").resolve("worker.log");
    Path file2 = topoA.resolve("2222").resolve("worker.log");
    Path file3 = workerLogRoot.resolve("topoB").resolve("1111").resolve("worker.log");
    Files.createDirectories(file1.getParent());
    Files.createDirectories(file2.getParent());
    Files.createDirectories(file3.getParent());
    Files.createFile(file1);
    Files.createFile(file2);
    Files.createFile(file3);
    Files.createFile(fileOutsideDaemonRoot);
    Files.createFile(daemonFile);
    Map<String, Object> stormConf = Utils.readStormConfig();
    StormMetricsRegistry metricsRegistry = new StormMetricsRegistry();
    return new LogviewerLogDownloadHandler(workerLogRoot.toString(), daemonLogRoot.toString(), new WorkerLogs(stormConf, workerLogRoot, metricsRegistry), new ResourceAuthorizer(stormConf), metricsRegistry);
}
Also used : TmpPath(org.apache.storm.testing.TmpPath) Path(java.nio.file.Path) StormMetricsRegistry(org.apache.storm.metric.StormMetricsRegistry) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) WorkerLogs(org.apache.storm.daemon.logviewer.utils.WorkerLogs) ResourceAuthorizer(org.apache.storm.daemon.logviewer.utils.ResourceAuthorizer)

Example 3 with WorkerLogs

use of org.apache.storm.daemon.logviewer.utils.WorkerLogs in project storm by apache.

the class LogviewerLogPageHandlerTest method testListLogFiles.

/**
 * list-log-files filter selects the correct log files to return.
 */
@Test
public void testListLogFiles() throws IOException {
    String rootPath = Files.createTempDirectory("workers-artifacts").toFile().getCanonicalPath();
    File file1 = new File(String.join(File.separator, rootPath, "topoA", "1111"), "worker.log");
    File file2 = new File(String.join(File.separator, rootPath, "topoA", "2222"), "worker.log");
    File file3 = new File(String.join(File.separator, rootPath, "topoB", "1111"), "worker.log");
    file1.getParentFile().mkdirs();
    file2.getParentFile().mkdirs();
    file3.getParentFile().mkdirs();
    file1.createNewFile();
    file2.createNewFile();
    file3.createNewFile();
    String origin = "www.origin.server.net";
    Map<String, Object> stormConf = Utils.readStormConfig();
    StormMetricsRegistry metricsRegistry = new StormMetricsRegistry();
    LogviewerLogPageHandler handler = new LogviewerLogPageHandler(rootPath, rootPath, new WorkerLogs(stormConf, Paths.get(rootPath), metricsRegistry), new ResourceAuthorizer(stormConf), metricsRegistry);
    final Response expectedAll = LogviewerResponseBuilder.buildSuccessJsonResponse(Lists.newArrayList("topoA/port1/worker.log", "topoA/port2/worker.log", "topoB/port1/worker.log"), null, origin);
    final Response expectedFilterPort = LogviewerResponseBuilder.buildSuccessJsonResponse(Lists.newArrayList("topoA/port1/worker.log", "topoB/port1/worker.log"), null, origin);
    final Response expectedFilterTopoId = LogviewerResponseBuilder.buildSuccessJsonResponse(Lists.newArrayList("topoB/port1/worker.log"), null, origin);
    final Response returnedAll = handler.listLogFiles("user", null, null, null, origin);
    final Response returnedFilterPort = handler.listLogFiles("user", 1111, null, null, origin);
    final Response returnedFilterTopoId = handler.listLogFiles("user", null, "topoB", null, origin);
    Utils.forceDelete(rootPath);
    assertEqualsJsonResponse(expectedAll, returnedAll, List.class);
    assertEqualsJsonResponse(expectedFilterPort, returnedFilterPort, List.class);
    assertEqualsJsonResponse(expectedFilterTopoId, returnedFilterTopoId, List.class);
}
Also used : Response(javax.ws.rs.core.Response) StormMetricsRegistry(org.apache.storm.metric.StormMetricsRegistry) WorkerLogs(org.apache.storm.daemon.logviewer.utils.WorkerLogs) ResourceAuthorizer(org.apache.storm.daemon.logviewer.utils.ResourceAuthorizer) File(java.io.File) Test(org.junit.Test)

Example 4 with WorkerLogs

use of org.apache.storm.daemon.logviewer.utils.WorkerLogs in project storm by apache.

the class LogviewerLogPageHandler method logPage.

/**
 * Provides a worker log file to view, starting from the specified position
 * or default starting position of the most recent page.
 *
 * @param fileName file to view
 * @param start start offset, or null if the most recent page is desired
 * @param length length to read in this page, or null if default page length is desired
 * @param grep search string if request is a result of the search, can be null
 * @param user username
 * @return HTML view page of worker log
 */
public Response logPage(String fileName, Integer start, Integer length, String grep, String user) throws IOException, InvalidRequestException {
    Path rawFile = logRoot.resolve(fileName);
    Path absFile = rawFile.toAbsolutePath().normalize();
    if (!absFile.startsWith(logRoot) || !rawFile.normalize().toString().equals(rawFile.toString())) {
        // Ensure filename doesn't contain ../ parts
        return LogviewerResponseBuilder.buildResponsePageNotFound();
    }
    if (resourceAuthorizer.isUserAllowedToAccessFile(user, fileName)) {
        workerLogs.setLogFilePermission(fileName);
        Path topoDir = absFile.getParent().getParent();
        if (absFile.toFile().exists()) {
            SortedSet<Path> logFiles;
            try {
                logFiles = Arrays.stream(topoDir.toFile().listFiles()).flatMap(Unchecked.function(portDir -> directoryCleaner.getFilesForDir(portDir.toPath()).stream())).filter(Files::isRegularFile).collect(toCollection(TreeSet::new));
            } catch (UncheckedIOException e) {
                throw e.getCause();
            }
            List<String> reorderedFilesStr = logFiles.stream().map(WorkerLogs::getTopologyPortWorkerLog).filter(fileStr -> !StringUtils.equals(fileName, fileStr)).collect(toList());
            reorderedFilesStr.add(fileName);
            length = length != null ? Math.min(10485760, length) : LogviewerConstant.DEFAULT_BYTES_PER_PAGE;
            final boolean isZipFile = absFile.getFileName().toString().endsWith(".gz");
            long fileLength = getFileLength(absFile.toFile(), isZipFile);
            if (start == null) {
                start = Long.valueOf(fileLength - length).intValue();
            }
            String logString = isTxtFile(fileName) ? escapeHtml(pageFile(absFile.toString(), isZipFile, fileLength, start, length)) : escapeHtml("This is a binary file and cannot display! You may download the full file.");
            List<DomContent> bodyContents = new ArrayList<>();
            if (StringUtils.isNotEmpty(grep)) {
                String matchedString = String.join("\n", Arrays.stream(logString.split("\n")).filter(str -> str.contains(grep)).collect(toList()));
                bodyContents.add(pre(matchedString).withId("logContent"));
            } else {
                DomContent pagerData = null;
                if (isTxtFile(fileName)) {
                    pagerData = pagerLinks(fileName, start, length, Long.valueOf(fileLength).intValue(), "log");
                }
                bodyContents.add(searchFileForm(fileName, "no"));
                // list all files for this topology
                bodyContents.add(logFileSelectionForm(reorderedFilesStr, fileName, "log"));
                if (pagerData != null) {
                    bodyContents.add(pagerData);
                }
                bodyContents.add(downloadLink(fileName));
                bodyContents.add(pre(logString).withClass("logContent"));
                if (pagerData != null) {
                    bodyContents.add(pagerData);
                }
            }
            String content = logTemplate(bodyContents, fileName, user).render();
            return LogviewerResponseBuilder.buildSuccessHtmlResponse(content);
        } else {
            return LogviewerResponseBuilder.buildResponsePageNotFound();
        }
    } else {
        if (resourceAuthorizer.getLogUserGroupWhitelist(fileName) == null) {
            return LogviewerResponseBuilder.buildResponsePageNotFound();
        } else {
            return LogviewerResponseBuilder.buildResponseUnauthorizedUser(user);
        }
    }
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) StringUtils(org.apache.commons.lang.StringUtils) SortedSet(java.util.SortedSet) LogviewerConstant(org.apache.storm.daemon.logviewer.LogviewerConstant) TagCreator.h3(j2html.TagCreator.h3) TagCreator.head(j2html.TagCreator.head) Matcher(java.util.regex.Matcher) TagCreator.link(j2html.TagCreator.link) Map(java.util.Map) ExceptionMeterNames(org.apache.storm.daemon.logviewer.utils.ExceptionMeterNames) UIHelpers(org.apache.storm.daemon.ui.UIHelpers) TagCreator.input(j2html.TagCreator.input) Path(java.nio.file.Path) UrlBuilder(org.apache.storm.daemon.utils.UrlBuilder) Unchecked(org.jooq.lambda.Unchecked) LogviewerResponseBuilder(org.apache.storm.daemon.logviewer.utils.LogviewerResponseBuilder) FileNotFoundException(java.io.FileNotFoundException) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) ConfigUtils(org.apache.storm.utils.ConfigUtils) Response(javax.ws.rs.core.Response) TagCreator.title(j2html.TagCreator.title) Pattern(java.util.regex.Pattern) InvalidRequestException(org.apache.storm.daemon.ui.InvalidRequestException) TagCreator.text(j2html.TagCreator.text) TagCreator.body(j2html.TagCreator.body) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TagCreator.html(j2html.TagCreator.html) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) TagCreator.pre(j2html.TagCreator.pre) ArrayList(java.util.ArrayList) TagCreator.option(j2html.TagCreator.option) Collectors.toCollection(java.util.stream.Collectors.toCollection) Meter(com.codahale.metrics.Meter) DirectoryCleaner(org.apache.storm.daemon.logviewer.utils.DirectoryCleaner) ServerUtils(org.apache.storm.utils.ServerUtils) ResourceAuthorizer(org.apache.storm.daemon.logviewer.utils.ResourceAuthorizer) StormMetricsRegistry(org.apache.storm.metric.StormMetricsRegistry) TagCreator.p(j2html.TagCreator.p) Files(java.nio.file.Files) TagCreator.select(j2html.TagCreator.select) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TagCreator.form(j2html.TagCreator.form) TagCreator.a(j2html.TagCreator.a) WorkerLogs(org.apache.storm.daemon.logviewer.utils.WorkerLogs) File(java.io.File) StreamUtil(org.apache.storm.daemon.utils.StreamUtil) DomContent(j2html.tags.DomContent) Collectors.toList(java.util.stream.Collectors.toList) StringEscapeUtils.escapeHtml(org.apache.commons.lang.StringEscapeUtils.escapeHtml) Paths(java.nio.file.Paths) TagCreator.div(j2html.TagCreator.div) Collections(java.util.Collections) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) DomContent(j2html.tags.DomContent) UncheckedIOException(java.io.UncheckedIOException) WorkerLogs(org.apache.storm.daemon.logviewer.utils.WorkerLogs) TreeSet(java.util.TreeSet) Files(java.nio.file.Files)

Example 5 with WorkerLogs

use of org.apache.storm.daemon.logviewer.utils.WorkerLogs in project storm by apache.

the class LogviewerLogPageHandlerTest method createHandlerForTraversalTests.

private LogviewerLogPageHandler createHandlerForTraversalTests(Path rootPath) throws IOException {
    Path daemonLogRoot = rootPath.resolve("logs");
    Path fileOutsideDaemonRoot = rootPath.resolve("evil.sh");
    Path daemonFile = daemonLogRoot.resolve("nimbus.log");
    Path workerLogRoot = daemonLogRoot.resolve("workers-artifacts");
    Path topoA = workerLogRoot.resolve("topoA");
    Path file1 = topoA.resolve("1111").resolve("worker.log");
    Path file2 = topoA.resolve("2222").resolve("worker.log");
    Path file3 = workerLogRoot.resolve("topoB").resolve("1111").resolve("worker.log");
    Files.createDirectories(file1.getParent());
    Files.createDirectories(file2.getParent());
    Files.createDirectories(file3.getParent());
    Files.createFile(file1);
    Files.createFile(file2);
    Files.createFile(file3);
    Files.createFile(fileOutsideDaemonRoot);
    Files.createFile(daemonFile);
    Map<String, Object> stormConf = Utils.readStormConfig();
    StormMetricsRegistry metricsRegistry = new StormMetricsRegistry();
    return new LogviewerLogPageHandler(workerLogRoot.toString(), daemonLogRoot.toString(), new WorkerLogs(stormConf, workerLogRoot, metricsRegistry), new ResourceAuthorizer(stormConf), metricsRegistry);
}
Also used : TmpPath(org.apache.storm.testing.TmpPath) Path(java.nio.file.Path) StormMetricsRegistry(org.apache.storm.metric.StormMetricsRegistry) WorkerLogs(org.apache.storm.daemon.logviewer.utils.WorkerLogs) ResourceAuthorizer(org.apache.storm.daemon.logviewer.utils.ResourceAuthorizer)

Aggregations

WorkerLogs (org.apache.storm.daemon.logviewer.utils.WorkerLogs)5 StormMetricsRegistry (org.apache.storm.metric.StormMetricsRegistry)5 ResourceAuthorizer (org.apache.storm.daemon.logviewer.utils.ResourceAuthorizer)4 File (java.io.File)3 Path (java.nio.file.Path)3 Meter (com.codahale.metrics.Meter)1 TagCreator.a (j2html.TagCreator.a)1 TagCreator.body (j2html.TagCreator.body)1 TagCreator.div (j2html.TagCreator.div)1 TagCreator.form (j2html.TagCreator.form)1 TagCreator.h3 (j2html.TagCreator.h3)1 TagCreator.head (j2html.TagCreator.head)1 TagCreator.html (j2html.TagCreator.html)1 TagCreator.input (j2html.TagCreator.input)1 TagCreator.link (j2html.TagCreator.link)1 TagCreator.option (j2html.TagCreator.option)1 TagCreator.p (j2html.TagCreator.p)1 TagCreator.pre (j2html.TagCreator.pre)1 TagCreator.select (j2html.TagCreator.select)1 TagCreator.text (j2html.TagCreator.text)1