Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close streams properly #487

Merged
merged 3 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,55 @@ public class RetrieveSystemDigest implements Command {
private final Logger logger = LogManager.getLogger(RetrieveSystemDigest.class);

public void execute(DiagnosticContext context) {

try {
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
OperatingSystem os = si.getOperatingSystem();
File sysFileJson = new File(context.tempDir + SystemProperties.fileSeparator + "system-digest.json");
OutputStream outputStreamJson = new FileOutputStream(sysFileJson);
BufferedWriter jsonWriter = new BufferedWriter(new OutputStreamWriter(outputStreamJson));
String jsonInfo = si.toPrettyJSON();
jsonWriter.write(jsonInfo);
jsonWriter.close();

try (
OutputStream outputStreamJson = new FileOutputStream(sysFileJson);
BufferedWriter jsonWriter = new BufferedWriter(new OutputStreamWriter(outputStreamJson));
) {
String jsonInfo = si.toPrettyJSON();
jsonWriter.write(jsonInfo);
}

File sysFile = new File(context.tempDir + SystemProperties.fileSeparator + "system-digest.txt");
OutputStream outputStream = new FileOutputStream(sysFile);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));

printComputerSystem(writer, hal.getComputerSystem());
writer.newLine();
try (
OutputStream outputStream = new FileOutputStream(sysFile);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
) {
printComputerSystem(writer, hal.getComputerSystem());
writer.newLine();

printProcessor(writer, hal.getProcessor());
writer.newLine();
printProcessor(writer, hal.getProcessor());
writer.newLine();

printMemory(writer, hal.getMemory());
writer.newLine();
printMemory(writer, hal.getMemory());
writer.newLine();

printCpu(writer, hal.getProcessor());
writer.newLine();
printCpu(writer, hal.getProcessor());
writer.newLine();

printProcesses(writer, os, hal.getMemory());
writer.newLine();
printProcesses(writer, os, hal.getMemory());
writer.newLine();

printDisks(writer, hal.getDiskStores());
writer.newLine();
printDisks(writer, hal.getDiskStores());
writer.newLine();

printFileSystem(writer, os.getFileSystem());
writer.newLine();
printFileSystem(writer, os.getFileSystem());
writer.newLine();

printNetworkInterfaces(writer, hal.getNetworkIFs());
writer.newLine();
printNetworkInterfaces(writer, hal.getNetworkIFs());
writer.newLine();

printNetworkParameters(writer, os.getNetworkParams());
writer.newLine();
printNetworkParameters(writer, os.getNetworkParams());
writer.newLine();
}

writer.close();
logger.info("Finished querying SysInfo.");

} catch (final Exception e) {
logger.info("Failed saving system-digest.txt file.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ public class MonitoringImportService extends ElasticRestClientService {
private Logger logger = LogManager.getLogger(MonitoringImportService.class);
private static final String SCROLL_ID = "{ \"scroll_id\" : \"{{scrollId}}\" }";

void execImport(MonitoringImportInputs inputs){

Map configMap = JsonYamlUtils.readYamlFromClasspath(Constants.DIAG_CONFIG, true);
void execImport(MonitoringImportInputs inputs) throws DiagnosticException {
Map<String, Object> configMap = JsonYamlUtils.readYamlFromClasspath(Constants.DIAG_CONFIG, true);
MonitoringImportConfig config = new MonitoringImportConfig(configMap);

try (RestClient client = getClient(inputs, config)){
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/elastic/support/scrub/ScrubProcessor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.elastic.support.scrub;

import com.elastic.support.Constants;
import com.elastic.support.diagnostics.DiagnosticException;
import com.elastic.support.util.JsonYamlUtils;
import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.lang3.ObjectUtils;
Expand Down Expand Up @@ -33,18 +34,17 @@ public class ScrubProcessor {



public ScrubProcessor(String nodes) {

public ScrubProcessor(String nodes) throws DiagnosticException {
this();

if (StringUtils.isNotEmpty(nodes)) {
initAutoScrub(nodes);
}
}

public ScrubProcessor() {
scrubConfig =
JsonYamlUtils.readYamlFromClasspath("scrub.yml", false);
public ScrubProcessor() throws DiagnosticException {
scrubConfig = JsonYamlUtils.readYamlFromClasspath("scrub.yml", false);

Collection auto = (Collection) scrubConfig.get("auto-scrub");
if (auto != null) {
autoScrub.addAll(auto);
Expand Down
66 changes: 35 additions & 31 deletions src/main/java/com/elastic/support/util/ArchiveUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,39 +39,42 @@ public static File createArchive(String dir, String archiveFileName) throws Diag
}
}

public static File createZipArchive(String dir, String archiveFileName) throws IOException {
private static File createZipArchive(String dir, String archiveFileName) throws IOException {
File srcDir = new File(dir);
String filename = dir + "-" + archiveFileName + ".zip";
File file = new File(filename);
FileOutputStream fout = new FileOutputStream(filename);
ZipArchiveOutputStream taos = new ZipArchiveOutputStream(fout);
archiveResultsZip(archiveFileName, taos, srcDir, "", true);
taos.close();

logger.info(Constants.CONSOLE, "Archive: " + filename + " was created");

return file;
try (
FileOutputStream fout = new FileOutputStream(filename);
ZipArchiveOutputStream taos = new ZipArchiveOutputStream(fout)
) {
archiveResultsZip(archiveFileName, taos, srcDir, "", true);
logger.info(Constants.CONSOLE, "Archive: " + filename + " was created");
return file;
}
}

public static File createTarArchive(String dir, String archiveFileName) throws IOException {
private static File createTarArchive(String dir, String archiveFileName) throws IOException {
File srcDir = new File(dir);
String filename = dir + "-" + archiveFileName + ".tar.gz";
File file = new File(filename);
FileOutputStream fout = new FileOutputStream(filename);
CompressorOutputStream cout = new GzipCompressorOutputStream(fout);
TarArchiveOutputStream taos = new TarArchiveOutputStream(cout);

taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
archiveResultsTar(archiveFileName, taos, srcDir, "", true);
taos.close();
try (
FileOutputStream fout = new FileOutputStream(filename);
CompressorOutputStream cout = new GzipCompressorOutputStream(fout);
TarArchiveOutputStream taos = new TarArchiveOutputStream(cout)
) {
taos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR);
taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
archiveResultsTar(archiveFileName, taos, srcDir, "", true);

logger.info(Constants.CONSOLE, "Archive: " + filename + " was created");
logger.info(Constants.CONSOLE, "Archive: " + filename + " was created");

return file;
return file;
}
}

public static void archiveResultsZip(String archiveFilename, ZipArchiveOutputStream taos, File file, String path, boolean append) {
private static void archiveResultsZip(String archiveFilename, ZipArchiveOutputStream taos, File file, String path, boolean append) {
String relPath = "";

try {
Expand All @@ -84,11 +87,10 @@ public static void archiveResultsZip(String archiveFilename, ZipArchiveOutputStr
taos.putArchiveEntry(tae);

if (file.isFile()) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
IOUtils.copy(bis, taos);
taos.closeArchiveEntry();
bis.close();

try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
IOUtils.copy(bis, taos);
taos.closeArchiveEntry();
}
} else if (file.isDirectory()) {
taos.closeArchiveEntry();
for (File childFile : file.listFiles()) {
Expand All @@ -100,7 +102,7 @@ public static void archiveResultsZip(String archiveFilename, ZipArchiveOutputStr
}
}

public static void archiveResultsTar(String archiveFilename, TarArchiveOutputStream taos, File file, String path, boolean append) {
private static void archiveResultsTar(String archiveFilename, TarArchiveOutputStream taos, File file, String path, boolean append) {
String relPath = "";

try {
Expand All @@ -113,13 +115,13 @@ public static void archiveResultsTar(String archiveFilename, TarArchiveOutputStr
taos.putArchiveEntry(tae);

if (file.isFile()) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
IOUtils.copy(bis, taos);
taos.closeArchiveEntry();
bis.close();

try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
IOUtils.copy(bis, taos);
taos.closeArchiveEntry();
}
} else if (file.isDirectory()) {
taos.closeArchiveEntry();

for (File childFile : file.listFiles()) {
archiveResultsTar(archiveFilename, taos, childFile, relPath, false);
}
Expand Down Expand Up @@ -183,7 +185,9 @@ else if (entry.isDirectory()) {
logger.error(e);
}
finally {
ais.close();
if (ais != null) {
ais.close();
}
}
}

Expand Down
Loading