haveno/apitest/src/main/java/bisq/apitest/linux/BashCommand.java
woodser 88578bed10 general rebase in order to update payment methods and desktop app
Co-authored-by: Alva Swanson <alvasw@protonmail.com>
Co-authored-by: andyheko <haoen.ko@gmail.com>
Co-authored-by: Bisq GitHub Admin <51445974+bisq-github-admin-3@users.noreply.github.com>
Co-authored-by: BtcContributor <79100296+BtcContributor@users.noreply.github.com>
Co-authored-by: cd2357 <cd2357@users.noreply.github.com>
Co-authored-by: chimp1984 <chimp1984@gmx.com>
Co-authored-by: Chris Beams <chris@beams.io>
Co-authored-by: Christoph Atteneder <christoph.atteneder@gmail.com>
Co-authored-by: Devin Bileck <603793+devinbileck@users.noreply.github.com>
Co-authored-by: ghubstan <36207203+ghubstan@users.noreply.github.com>
Co-authored-by: Huey <hueydane@gmail.com>
Co-authored-by: Jakub Loucký <jakub.loucky@outlook.cz>
Co-authored-by: jmacxx <47253594+jmacxx@users.noreply.github.com>
Co-authored-by: KanoczTomas <tomas.kanocz@cnl.sk>
Co-authored-by: m52go <735155+m52go@users.noreply.github.com>
Co-authored-by: Marcus0x <marcus0x@xrhodium.org>
Co-authored-by: MarnixCroes <93143998+MarnixCroes@users.noreply.github.com>
Co-authored-by: Martin Harrigan <martinharrigan@gmail.com>
Co-authored-by: MwithM <50149324+MwithM@users.noreply.github.com>
Co-authored-by: sqrrm <sqrrm@users.noreply.github.com>
Co-authored-by: Stan <36207203+ghubstan@users.noreply.github.com>
Co-authored-by: Stephan Oeste <emzy@emzy.de>
Co-authored-by: Steven Barclay <stejbac@gmail.com>
Co-authored-by: WAT <shiido.it@gmail.com>
Co-authored-by: wiz <j@wiz.biz>
Co-authored-by: xyzmaker123 <84982606+xyzmaker123@users.noreply.github.com>
2022-05-28 13:56:29 -04:00

158 lines
5.6 KiB
Java

/*
* This file is part of Haveno.
*
* Haveno is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Haveno is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Haveno. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.apitest.linux;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Nullable;
import static bisq.apitest.config.ApiTestConfig.BASH_PATH_VALUE;
import static java.lang.management.ManagementFactory.getRuntimeMXBean;
@Slf4j
public class BashCommand {
private int exitStatus = -1;
@Nullable
private String output;
@Nullable
private String error;
private final String command;
private final int numResponseLines;
public BashCommand(String command) {
this(command, 0);
}
public BashCommand(String command, int numResponseLines) {
this.command = command;
this.numResponseLines = numResponseLines; // only want the top N lines of output
}
public BashCommand run() throws IOException, InterruptedException {
SystemCommandExecutor commandExecutor = new SystemCommandExecutor(tokenizeSystemCommand());
exitStatus = commandExecutor.exec();
processOutput(commandExecutor);
return this;
}
public BashCommand runInBackground() throws IOException, InterruptedException {
SystemCommandExecutor commandExecutor = new SystemCommandExecutor(tokenizeSystemCommand());
exitStatus = commandExecutor.exec(false);
processOutput(commandExecutor);
return this;
}
private void processOutput(SystemCommandExecutor commandExecutor) {
// Get the error status and stderr from system command.
StringBuilder stderr = commandExecutor.getStandardErrorFromCommand();
if (stderr.length() > 0)
error = stderr.toString();
if (exitStatus != 0)
return;
// Format and cache the stdout from system command.
StringBuilder stdout = commandExecutor.getStandardOutputFromCommand();
String[] rawLines = stdout.toString().split("\n");
StringBuilder truncatedLines = new StringBuilder();
int limit = numResponseLines > 0 ? Math.min(numResponseLines, rawLines.length) : rawLines.length;
for (int i = 0; i < limit; i++) {
String line = rawLines[i].length() >= 220 ? rawLines[i].substring(0, 220) + " ..." : rawLines[i];
truncatedLines.append(line).append((i < limit - 1) ? "\n" : "");
}
output = truncatedLines.toString();
}
public String getCommand() {
return this.command;
}
public int getExitStatus() {
return this.exitStatus;
}
// TODO return Optional<String>
@Nullable
public String getOutput() {
return this.output;
}
// TODO return Optional<String>
public String getError() {
return this.error;
}
private List<String> tokenizeSystemCommand() {
return new ArrayList<>() {{
add(BASH_PATH_VALUE);
add("-c");
add(command);
}};
}
@SuppressWarnings("unused")
// Convenience method for getting system load info.
public static String printSystemLoadString(Exception tracingException) throws IOException, InterruptedException {
StackTraceElement[] stackTraceElement = tracingException.getStackTrace();
StringBuilder stackTraceBuilder = new StringBuilder(tracingException.getMessage()).append("\n");
int traceLimit = Math.min(stackTraceElement.length, 4);
for (int i = 0; i < traceLimit; i++) {
stackTraceBuilder.append(stackTraceElement[i]).append("\n");
}
stackTraceBuilder.append("...");
log.info(stackTraceBuilder.toString());
BashCommand cmd = new BashCommand("ps -aux --sort -rss --headers", 2).run();
return cmd.getOutput() + "\n"
+ "System load: Memory (MB): " + getUsedMemoryInMB() + " / No. of threads: " + Thread.activeCount()
+ " JVM uptime (ms): " + getRuntimeMXBean().getUptime();
}
public static long getUsedMemoryInMB() {
Runtime runtime = Runtime.getRuntime();
long free = runtime.freeMemory() / 1024 / 1024;
long total = runtime.totalMemory() / 1024 / 1024;
return total - free;
}
public static long getPid(String processName) throws IOException, InterruptedException {
String psCmd = "ps aux | pgrep " + processName + " | grep -v grep";
String psCmdOutput = new BashCommand(psCmd).run().getOutput();
if (psCmdOutput == null || psCmdOutput.isEmpty())
return -1;
return Long.parseLong(psCmdOutput);
}
@SuppressWarnings("unused")
public static BashCommand grep(String processName) throws IOException, InterruptedException {
String c = "ps -aux | grep " + processName + " | grep -v grep";
return new BashCommand(c).run();
}
public static boolean isAlive(long pid) throws IOException, InterruptedException {
String isAliveScript = "if ps -p " + pid + " > /dev/null; then echo true; else echo false; fi";
return new BashCommand(isAliveScript).run().getOutput().equals("true");
}
}