Search in sources :

Example 1 with ExportLogsFunction

use of org.apache.geode.management.internal.cli.functions.ExportLogsFunction in project geode by apache.

the class ExportLogsCommand method exportLogs.

@CliCommand(value = CliStrings.EXPORT_LOGS, help = CliStrings.EXPORT_LOGS__HELP)
@CliMetaData(shellOnly = false, isFileDownloadOverHttp = true, interceptor = "org.apache.geode.management.internal.cli.commands.ExportLogsInterceptor", relatedTopic = { CliStrings.TOPIC_GEODE_SERVER, CliStrings.TOPIC_GEODE_DEBUG_UTIL })
@ResourceOperation(resource = ResourcePermission.Resource.CLUSTER, operation = ResourcePermission.Operation.READ)
public Result exportLogs(@CliOption(key = CliStrings.EXPORT_LOGS__DIR, help = CliStrings.EXPORT_LOGS__DIR__HELP, mandatory = false) String dirName, @CliOption(key = CliStrings.EXPORT_LOGS__GROUP, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, optionContext = ConverterHint.MEMBERGROUP, help = CliStrings.EXPORT_LOGS__GROUP__HELP) String[] groups, @CliOption(key = CliStrings.EXPORT_LOGS__MEMBER, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, optionContext = ConverterHint.ALL_MEMBER_IDNAME, help = CliStrings.EXPORT_LOGS__MEMBER__HELP) String[] memberIds, @CliOption(key = CliStrings.EXPORT_LOGS__LOGLEVEL, unspecifiedDefaultValue = DEFAULT_EXPORT_LOG_LEVEL, optionContext = ConverterHint.LOG_LEVEL, help = CliStrings.EXPORT_LOGS__LOGLEVEL__HELP) String logLevel, @CliOption(key = CliStrings.EXPORT_LOGS__UPTO_LOGLEVEL, unspecifiedDefaultValue = "false", help = CliStrings.EXPORT_LOGS__UPTO_LOGLEVEL__HELP) boolean onlyLogLevel, @CliOption(key = CliStrings.EXPORT_LOGS__MERGELOG, unspecifiedDefaultValue = "false", help = CliStrings.EXPORT_LOGS__MERGELOG__HELP) boolean mergeLog, @CliOption(key = CliStrings.EXPORT_LOGS__STARTTIME, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.EXPORT_LOGS__STARTTIME__HELP) String start, @CliOption(key = CliStrings.EXPORT_LOGS__ENDTIME, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.EXPORT_LOGS__ENDTIME__HELP) String end, @CliOption(key = CliStrings.EXPORT_LOGS__LOGSONLY, unspecifiedDefaultValue = "false", specifiedDefaultValue = "true", help = CliStrings.EXPORT_LOGS__LOGSONLY__HELP) boolean logsOnly, @CliOption(key = CliStrings.EXPORT_LOGS__STATSONLY, unspecifiedDefaultValue = "false", specifiedDefaultValue = "true", help = CliStrings.EXPORT_LOGS__STATSONLY__HELP) boolean statsOnly, @CliOption(key = CliStrings.EXPORT_LOGS__FILESIZELIMIT, unspecifiedDefaultValue = CliStrings.EXPORT_LOGS__FILESIZELIMIT__UNSPECIFIED_DEFAULT, specifiedDefaultValue = CliStrings.EXPORT_LOGS__FILESIZELIMIT__SPECIFIED_DEFAULT, help = CliStrings.EXPORT_LOGS__FILESIZELIMIT__HELP) String fileSizeLimit) {
    long totalEstimatedExportSize = 0;
    Result result;
    InternalCache cache = getCache();
    try {
        Set<DistributedMember> targetMembers = getMembers(groups, memberIds);
        if (targetMembers.isEmpty()) {
            return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
        }
        if (parseFileSizeLimit(fileSizeLimit) > 0) {
            // Get estimated size of exported logs from all servers before exporting anything
            for (DistributedMember server : targetMembers) {
                SizeExportLogsFunction.Args args = new SizeExportLogsFunction.Args(start, end, logLevel, onlyLogLevel, logsOnly, statsOnly);
                List<Object> results = (List<Object>) estimateLogSize(args, server).getResult();
                long estimatedSize = 0;
                if (!results.isEmpty()) {
                    List<?> res = (List<?>) results.get(0);
                    if (res.get(0) instanceof Long) {
                        estimatedSize = (Long) res.get(0);
                    }
                }
                logger.info("Received estimated export size from member {}: {}", server.getId(), estimatedSize);
                totalEstimatedExportSize += estimatedSize;
            }
            // disk available on the locator
            try {
                checkIfExportLogsOverflowsDisk("locator", parseFileSizeLimit(fileSizeLimit), totalEstimatedExportSize, getLocalDiskAvailable());
            } catch (ManagementException e) {
                return ResultBuilder.createUserErrorResult(e.getMessage());
            }
        }
        // get zipped files from all servers next
        Map<String, Path> zipFilesFromMembers = new HashMap<>();
        for (DistributedMember server : targetMembers) {
            Region region = ExportLogsFunction.createOrGetExistingExportLogsRegion(true, cache);
            ExportLogsCacheWriter cacheWriter = (ExportLogsCacheWriter) region.getAttributes().getCacheWriter();
            cacheWriter.startFile(server.getName());
            CliUtil.executeFunction(new ExportLogsFunction(), new ExportLogsFunction.Args(start, end, logLevel, onlyLogLevel, logsOnly, statsOnly), server).getResult();
            Path zipFile = cacheWriter.endFile();
            ExportLogsFunction.destroyExportLogsRegion(cache);
            // only put the zipfile in the map if it is not null
            if (zipFile != null) {
                logger.info("Received zip file from member {}: {}", server.getId(), zipFile);
                zipFilesFromMembers.put(server.getId(), zipFile);
            }
        }
        if (zipFilesFromMembers.isEmpty()) {
            return ResultBuilder.createUserErrorResult("No files to be exported.");
        }
        Path tempDir = Files.createTempDirectory("exportedLogs");
        // make sure the directory is created, so that even if there is no files unzipped to this
        // dir, we can still zip it and send an empty zip file back to the client
        Path exportedLogsDir = tempDir.resolve("exportedLogs");
        FileUtils.forceMkdir(exportedLogsDir.toFile());
        for (Path zipFile : zipFilesFromMembers.values()) {
            Path unzippedMemberDir = exportedLogsDir.resolve(zipFile.getFileName().toString().replace(".zip", ""));
            ZipUtils.unzip(zipFile.toAbsolutePath().toString(), unzippedMemberDir.toString());
            FileUtils.deleteQuietly(zipFile.toFile());
        }
        Path dirPath;
        if (StringUtils.isBlank(dirName)) {
            dirPath = Paths.get(System.getProperty("user.dir"));
        } else {
            dirPath = Paths.get(dirName);
        }
        Path exportedLogsZipFile = dirPath.resolve("exportedLogs_" + System.currentTimeMillis() + ".zip").toAbsolutePath();
        logger.info("Zipping into: " + exportedLogsZipFile.toString());
        ZipUtils.zipDirectory(exportedLogsDir, exportedLogsZipFile);
        try {
            checkFileSizeWithinLimit(parseFileSizeLimit(fileSizeLimit), exportedLogsZipFile.toFile());
        } catch (ManagementException e) {
            FileUtils.deleteQuietly(exportedLogsZipFile.toFile());
            return ResultBuilder.createUserErrorResult(e.getMessage());
        } finally {
            FileUtils.deleteDirectory(tempDir.toFile());
        }
        result = ResultBuilder.createInfoResult(exportedLogsZipFile.toString());
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
        result = ResultBuilder.createGemFireErrorResult(ex.getMessage());
    } finally {
        ExportLogsFunction.destroyExportLogsRegion(cache);
    }
    logger.debug("Exporting logs returning = {}", result);
    return result;
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) ExportLogsCacheWriter(org.apache.geode.management.internal.cli.util.ExportLogsCacheWriter) SizeExportLogsFunction(org.apache.geode.management.internal.cli.functions.SizeExportLogsFunction) InternalCache(org.apache.geode.internal.cache.InternalCache) BytesToString(org.apache.geode.management.internal.cli.util.BytesToString) ManagementException(org.apache.geode.management.ManagementException) Result(org.apache.geode.management.cli.Result) ManagementException(org.apache.geode.management.ManagementException) DistributedMember(org.apache.geode.distributed.DistributedMember) SizeExportLogsFunction(org.apache.geode.management.internal.cli.functions.SizeExportLogsFunction) ExportLogsFunction(org.apache.geode.management.internal.cli.functions.ExportLogsFunction) Region(org.apache.geode.cache.Region) List(java.util.List) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Aggregations

Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Region (org.apache.geode.cache.Region)1 DistributedMember (org.apache.geode.distributed.DistributedMember)1 InternalCache (org.apache.geode.internal.cache.InternalCache)1 ManagementException (org.apache.geode.management.ManagementException)1 CliMetaData (org.apache.geode.management.cli.CliMetaData)1 Result (org.apache.geode.management.cli.Result)1 ExportLogsFunction (org.apache.geode.management.internal.cli.functions.ExportLogsFunction)1 SizeExportLogsFunction (org.apache.geode.management.internal.cli.functions.SizeExportLogsFunction)1 BytesToString (org.apache.geode.management.internal.cli.util.BytesToString)1 ExportLogsCacheWriter (org.apache.geode.management.internal.cli.util.ExportLogsCacheWriter)1 ResourceOperation (org.apache.geode.management.internal.security.ResourceOperation)1 CliCommand (org.springframework.shell.core.annotation.CliCommand)1