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;
}
Aggregations