public void execute()

in src/main/java/co/elastic/support/diagnostics/commands/CollectLogs.java [34:93]


    public void execute(DiagnosticContext context) {
        // If we hit a snafu earlier in determining the details on where and how to run, then just get out.
        if(!context.runSystemCalls){
            logger.info(Constants.CONSOLE, "There was an issue in setting up system logs collection - bypassing. {}", Constants.CHECK_LOG);
            return;
        }

        // Should be cached from the PlatformDetails check.
        SystemCommand sysCmd = context.resourceCache.getSystemCommand(Constants.systemCommands);
        String targetDir = context.tempDir + SystemProperties.fileSeparator + "logs";
        ProcessProfile targetNode = context.targetNode;
        JavaPlatform javaPlatform = targetNode.javaPlatform;
        String logDir = targetNode.logDir;
        String clusterName = context.clusterName;
        Map<String, Map<String, String>> osCmds = context.diagsConfig.getSysCalls(javaPlatform.platform);
        Map<String, String> logCalls = osCmds.get("logs");

        // Get the remote systemobject
        try{

            // Create the log directory - should never exist but always pays to be cautious.
            File tempLogDir = new File(targetDir);
            if(!tempLogDir.exists()){
                tempLogDir.mkdir();
            }

            // Build up a list of files to copy
            List<String> fileList = new ArrayList<>();

            // Check for the base elasticsearch log - will always be <clustername>.log
            // If it's not there, they probably have insufficient permissions so log it
            // to the console and exit gracefully.
            String logStatement = logCalls.get("elastic");
            logStatement = logStatement.replace("{{CLUSTERNAME}}", clusterName);
            logStatement = logStatement.replace("{{LOGPATH}}", logDir);
            String logListing = sysCmd.runCommand(logStatement).trim();
            if (StringUtils.isEmpty(logListing)) {
                logger.info(Constants.CONSOLE, "No Elasticsearch log could be located at the path configured for this node. The cause of this is usually insufficient read authority. Please be sure the account you are using to access the logs has the necessary permissions or use sudo. See the diagnostic README for more information. ");
                return;
            }

            fileList.addAll(Arrays.asList(logListing.split("\n")));
            logStatement = logCalls.get("elastic-arc");
            logStatement = logStatement.replace("{{CLUSTERNAME}}", clusterName);
            logStatement = logStatement.replace("{{LOGPATH}}", logDir);
            logListing = sysCmd.runCommand(logStatement).trim();
            fileList = extractFilesFromList(logListing, fileList, 2);

            logStatement = logCalls.get("gc");
            logStatement = logStatement.replace("{{LOGPATH}}", logDir);
            logListing = sysCmd.runCommand(logStatement).trim();
            fileList = extractFilesFromList(logListing, fileList, 3);

            sysCmd.copyLogs(fileList, logDir, targetDir );

        } catch (Exception e) {
            logger.info(Constants.CONSOLE, "An error occurred while copying the logs. It may not have completed normally {}.", Constants.CHECK_LOG);
            logger.error( e.getMessage(), e);
        }
    }