remove default value in as.sh/arthas-boot. #1561

This commit is contained in:
hengyunabc
2020-11-03 00:50:38 +08:00
parent e13ced6fdf
commit cdec5061b4
8 changed files with 176 additions and 86 deletions
+76 -31
View File
@@ -88,17 +88,20 @@ ARTHAS_LIB_DIR=${HOME}/.arthas/lib
# target process id to attach
TARGET_PID=
# target process id to attach
TARGET_IP="127.0.0.1"
# target process id to attach, default 127.0.0.1
TARGET_IP=
DEFAULT_TARGET_IP="127.0.0.1"
# telnet port
TELNET_PORT="3658"
# telnet port, default 3658
TELNET_PORT=
DEFAULT_TELNET_PORT="3658"
# http port
HTTP_PORT="8563"
# http port, default 8563
HTTP_PORT=
DEFAULT_HTTP_PORT="8563"
# telnet session timeout seconds, default 1800
SESSION_TIMEOUT=1800
SESSION_TIMEOUT=
# use specify version
USE_VERSION=
@@ -466,6 +469,33 @@ find_listen_port_process()
fi
}
getTargetIPOrDefault()
{
local targetIP=${DEFAULT_TARGET_IP}
if [ "${TARGET_IP}" ]; then
targetIP=${TARGET_IP}
fi
echo $targetIP
}
getTelnetPortOrDefault()
{
local telnetPort=${DEFAULT_TELNET_PORT}
if [ "${TELNET_PORT}" ]; then
telnetPort=${TELNET_PORT}
fi
echo $telnetPort
}
getHttpPortOrDefault()
{
local httpPort=${DEFAULT_HTTP_PORT}
if [ "${HTTP_PORT}" ]; then
httpPort=${HTTP_PORT}
fi
echo $httpPort
}
# Status from com.taobao.arthas.client.TelnetConsole
# Execute commands timeout
STATUS_EXEC_TIMEOUT=100
@@ -484,8 +514,8 @@ find_listen_port_process_by_client()
"${JAVA_HOME}/bin/java" ${ARTHAS_OPTS} ${JVM_OPTS} \
-jar "${arthas_lib_dir}/arthas-client.jar" \
${TARGET_IP} \
${TELNET_PORT} \
$(getTargetIPOrDefault) \
$(getTelnetPortOrDefault) \
-c "session" \
--execution-timeout 2000 \
2>&1
@@ -647,16 +677,18 @@ parse_arguments()
# check telnet port/http port
local telnetPortPid
local httpPortPid
if [[ $TELNET_PORT > 0 ]]; then
telnetPortPid=$(find_listen_port_process $TELNET_PORT)
local telnetPortOrDefault=$(getTelnetPortOrDefault)
local httpPortOrDefault=$(getHttpPortOrDefault)
if [[ $telnetPortOrDefault > 0 ]]; then
telnetPortPid=$(find_listen_port_process $telnetPortOrDefault)
if [ $telnetPortPid ]; then
echo "[INFO] Process $telnetPortPid already using port $TELNET_PORT"
echo "[INFO] Process $telnetPortPid already using port $telnetPortOrDefault"
fi
fi
if [[ $HTTP_PORT > 0 ]]; then
httpPortPid=$(find_listen_port_process $HTTP_PORT)
if [[ $httpPortOrDefault > 0 ]]; then
httpPortPid=$(find_listen_port_process $httpPortOrDefault)
if [ $telnetPortPid ]; then
echo "[INFO] Process $httpPortPid already using port $HTTP_PORT"
echo "[INFO] Process $httpPortPid already using port $httpPortOrDefault"
fi
fi
@@ -727,7 +759,7 @@ parse_arguments()
exit 1
fi
if [[ ($httpPortPid) && ($TARGET_PID != $httpPortPid) ]]; then
echo "Target process $TARGET_PID is not the process using port $HTTP_PORT, you will connect to an unexpected process."
echo "Target process $TARGET_PID is not the process using port $(getHttpPortOrDefault), you will connect to an unexpected process."
echo "1. Try to restart as.sh, select process $httpPortPid, shutdown it first with running the 'stop' command."
echo "2. Try to use different http port, for example: as.sh --telnet-port 9998 --http-port 9999"
exit 1
@@ -774,14 +806,27 @@ attach_jvm()
tempArgs+=("${APP_NAME}")
fi
if [ "${TARGET_IP}" ]; then
tempArgs+=("-target-ip")
tempArgs+=("${TARGET_IP}")
fi
if [ "${TELNET_PORT}" ]; then
tempArgs+=("-telnet-port")
tempArgs+=("${TELNET_PORT}")
fi
if [ "${HTTP_PORT}" ]; then
tempArgs+=("-http-port")
tempArgs+=("${HTTP_PORT}")
fi
if [ "${SESSION_TIMEOUT}" ]; then
tempArgs+=("-session-timeout")
tempArgs+=("${SESSION_TIMEOUT}")
fi
"${java_command[@]}" \
${ARTHAS_OPTS} ${JVM_OPTS} \
-jar "${arthas_lib_dir}/arthas-core.jar" \
-pid ${TARGET_PID} \
-target-ip ${TARGET_IP} \
-telnet-port ${TELNET_PORT} \
-http-port ${HTTP_PORT} \
-session-timeout ${SESSION_TIMEOUT} \
"${tempArgs[@]}" \
-core "${arthas_lib_dir}/arthas-core.jar" \
-agent "${arthas_lib_dir}/arthas-agent.jar"
@@ -826,7 +871,7 @@ sanity_check() {
}
port_pid_check() {
if [[ $TELNET_PORT > 0 ]]; then
if [[ $(getTelnetPortOrDefault) > 0 ]]; then
local telnet_output
local find_process_status
# declare local var before var=$()
@@ -859,15 +904,15 @@ port_pid_check() {
}
print_telnet_port_pid_error() {
echo "[ERROR] The telnet port $TELNET_PORT is used by process $telnetPortPid instead of target process $TARGET_PID, you will connect to an unexpected process."
echo "[ERROR] The telnet port $(getTelnetPortOrDefault) is used by process $telnetPortPid instead of target process $TARGET_PID, you will connect to an unexpected process."
echo "[ERROR] 1. Try to restart as.sh, select process $telnetPortPid, shutdown it first with running the 'stop' command."
echo "[ERROR] 2. Try to stop the existing arthas instance: java -jar arthas-client.jar 127.0.0.1 $TELNET_PORT -c \"stop\""
echo "[ERROR] 2. Try to stop the existing arthas instance: java -jar arthas-client.jar 127.0.0.1 $(getTelnetPortOrDefault) -c \"stop\""
echo "[ERROR] 3. Try to use different telnet port, for example: as.sh --telnet-port 9998 --http-port -1"
}
print_telnet_port_used_error() {
local error_msg=$1
echo "[ERROR] The telnet port $TELNET_PORT is used, but process $error_msg, you will connect to an unexpected process."
echo "[ERROR] The telnet port $(getTelnetPortOrDefault) is used, but process $error_msg, you will connect to an unexpected process."
echo "[ERROR] Try to use different telnet port, for example: as.sh --telnet-port 9998 --http-port -1"
}
@@ -896,16 +941,16 @@ active_console()
if [ "${COMMAND}" ] ; then
"${JAVA_HOME}/bin/java" ${ARTHAS_OPTS} ${JVM_OPTS} \
-jar "${arthas_lib_dir}/arthas-client.jar" \
${TARGET_IP} \
${TELNET_PORT} \
$(getTargetIPOrDefault) \
$(getTelnetPortOrDefault) \
"${tempArgs[@]}" \
-c "${COMMAND}"
fi
if [ "${BATCH_FILE}" ] ; then
"${JAVA_HOME}/bin/java" ${ARTHAS_OPTS} ${JVM_OPTS} \
-jar "${arthas_lib_dir}/arthas-client.jar" \
${TARGET_IP} \
${TELNET_PORT} \
$(getTargetIPOrDefault) \
$(getTelnetPortOrDefault) \
"${tempArgs[@]}" \
-f ${BATCH_FILE}
fi
@@ -914,12 +959,12 @@ active_console()
if [[ $(command -v telnet) == *"system32"* ]] ; then
# Windows/system32/telnet.exe can not run in Cygwin/MinGw
echo "It seems that current bash is under Windows. $(command -v telnet) can not run under bash."
echo "Please start cmd.exe from Windows start menu, and then run telnet ${TARGET_IP} ${TELNET_PORT} to connect to target process."
echo "Or visit http://127.0.0.1:${HTTP_PORT} to connect to target process."
echo "Please start cmd.exe from Windows start menu, and then run telnet $(getTargetIPOrDefault) $(getTelnetPortOrDefault) to connect to target process."
echo "Or visit http://127.0.0.1:$(getHttpPortOrDefault) to connect to target process."
return 1
fi
echo "telnet connecting to arthas server... current timestamp is `date +%s`"
telnet ${TARGET_IP} ${TELNET_PORT}
telnet $(getTargetIPOrDefault) $(getTelnetPortOrDefault)
else
echo "'telnet' is required." 1>&2
return 1
@@ -67,9 +67,9 @@ public class Bootstrap {
private boolean help = false;
private long pid = -1;
private String targetIp = DEFAULT_TARGET_IP;
private int telnetPort = DEFAULT_TELNET_PORT;
private int httpPort = DEFAULT_HTTP_PORT;
private String targetIp;
private Integer telnetPort;
private Integer httpPort;
/**
* @see com.taobao.arthas.core.config.Configure#DEFAULT_SESSION_TIMEOUT_SECONDS
*/
@@ -333,16 +333,16 @@ public class Bootstrap {
// check telnet/http port
long telnetPortPid = -1;
long httpPortPid = -1;
if (bootstrap.getTelnetPort() > 0) {
telnetPortPid = SocketUtils.findTcpListenProcess(bootstrap.getTelnetPort());
if (bootstrap.getTelnetPortOrDefault() > 0) {
telnetPortPid = SocketUtils.findTcpListenProcess(bootstrap.getTelnetPortOrDefault());
if (telnetPortPid > 0) {
AnsiLog.info("Process {} already using port {}", telnetPortPid, bootstrap.getTelnetPort());
AnsiLog.info("Process {} already using port {}", telnetPortPid, bootstrap.getTelnetPortOrDefault());
}
}
if (bootstrap.getHttpPort() > 0) {
httpPortPid = SocketUtils.findTcpListenProcess(bootstrap.getHttpPort());
if (bootstrap.getHttpPortOrDefault() > 0) {
httpPortPid = SocketUtils.findTcpListenProcess(bootstrap.getHttpPortOrDefault());
if (httpPortPid > 0) {
AnsiLog.info("Process {} already using port {}", httpPortPid, bootstrap.getHttpPort());
AnsiLog.info("Process {} already using port {}", httpPortPid, bootstrap.getHttpPortOrDefault());
}
}
@@ -365,7 +365,7 @@ public class Bootstrap {
if (httpPortPid > 0 && pid != httpPortPid) {
AnsiLog.error("Target process {} is not the process using port {}, you will connect to an unexpected process.",
pid, bootstrap.getHttpPort());
pid, bootstrap.getHttpPortOrDefault());
AnsiLog.error("1. Try to restart arthas-boot, select process {}, shutdown it first with running the 'stop' command.",
httpPortPid);
AnsiLog.error("2. Or try to use different http port, for example: java -jar arthas-boot.jar --telnet-port 9998 --http-port 9999", httpPortPid);
@@ -469,10 +469,10 @@ public class Bootstrap {
AnsiLog.info("arthas home: " + arthasHomeDir);
if (telnetPortPid > 0 && pid == telnetPortPid) {
AnsiLog.info("The target process already listen port {}, skip attach.", bootstrap.getTelnetPort());
AnsiLog.info("The target process already listen port {}, skip attach.", bootstrap.getTelnetPortOrDefault());
} else {
//double check telnet port and pid before attach
telnetPortPid = findProcessByTelnetClient(arthasHomeDir.getAbsolutePath(), bootstrap.getTelnetPort());
telnetPortPid = findProcessByTelnetClient(arthasHomeDir.getAbsolutePath(), bootstrap.getTelnetPortOrDefault());
checkTelnetPortPid(bootstrap, telnetPortPid, pid);
// start arthas-core.jar
@@ -481,12 +481,21 @@ public class Bootstrap {
attachArgs.add(new File(arthasHomeDir, "arthas-core.jar").getAbsolutePath());
attachArgs.add("-pid");
attachArgs.add("" + pid);
attachArgs.add("-target-ip");
attachArgs.add(bootstrap.getTargetIp());
attachArgs.add("-telnet-port");
attachArgs.add("" + bootstrap.getTelnetPort());
attachArgs.add("-http-port");
attachArgs.add("" + bootstrap.getHttpPort());
if (bootstrap.getTargetIp() != null) {
attachArgs.add("-target-ip");
attachArgs.add(bootstrap.getTargetIp());
}
if (bootstrap.getTelnetPort() != null) {
attachArgs.add("-telnet-port");
attachArgs.add("" + bootstrap.getTelnetPort());
}
if (bootstrap.getHttpPort() != null) {
attachArgs.add("-http-port");
attachArgs.add("" + bootstrap.getHttpPort());
}
attachArgs.add("-core");
attachArgs.add(new File(arthasHomeDir, "arthas-core.jar").getAbsolutePath());
attachArgs.add("-agent");
@@ -546,10 +555,10 @@ public class Bootstrap {
}
// telnet port ,ip
telnetArgs.add(bootstrap.getTargetIp());
telnetArgs.add("" + bootstrap.getTelnetPort());
telnetArgs.add(bootstrap.getTargetIpOrDefault());
telnetArgs.add("" + bootstrap.getTelnetPortOrDefault());
AnsiLog.info("arthas-client connect {} {}", bootstrap.getTargetIp(), bootstrap.getTelnetPort());
AnsiLog.info("arthas-client connect {} {}", bootstrap.getTargetIpOrDefault(), bootstrap.getTelnetPortOrDefault());
AnsiLog.debug("Start arthas-client.jar args: " + telnetArgs);
// fix https://github.com/alibaba/arthas/issues/833
@@ -560,10 +569,10 @@ public class Bootstrap {
private static void checkTelnetPortPid(Bootstrap bootstrap, long telnetPortPid, long targetPid) {
if (telnetPortPid > 0 && targetPid != telnetPortPid) {
AnsiLog.error("The telnet port {} is used by process {} instead of target process {}, you will connect to an unexpected process.",
bootstrap.getTelnetPort(), telnetPortPid, targetPid);
bootstrap.getTelnetPortOrDefault(), telnetPortPid, targetPid);
AnsiLog.error("1. Try to restart arthas-boot, select process {}, shutdown it first with running the 'stop' command.",
telnetPortPid);
AnsiLog.error("2. Or try to stop the existing arthas instance: java -jar arthas-client.jar 127.0.0.1 {} -c \"stop\"", bootstrap.getTelnetPort());
AnsiLog.error("2. Or try to stop the existing arthas instance: java -jar arthas-client.jar 127.0.0.1 {} -c \"stop\"", bootstrap.getTelnetPortOrDefault());
AnsiLog.error("3. Or try to use different telnet port, for example: java -jar arthas-boot.jar --telnet-port 9998 --http-port -1");
System.exit(1);
}
@@ -712,14 +721,38 @@ public class Bootstrap {
return targetIp;
}
public int getTelnetPort() {
return telnetPort;
public String getTargetIpOrDefault() {
if (this.targetIp == null) {
return DEFAULT_TARGET_IP;
} else {
return this.targetIp;
}
}
public int getHttpPort() {
public Integer getTelnetPort() {
return telnetPort;
}
public int getTelnetPortOrDefault() {
if (this.telnetPort == null) {
return DEFAULT_TELNET_PORT;
} else {
return this.telnetPort;
}
}
public Integer getHttpPort() {
return httpPort;
}
public int getHttpPortOrDefault() {
if (this.httpPort == null) {
return DEFAULT_HTTP_PORT;
} else {
return this.httpPort;
}
}
public String getCommand() {
return command;
}
@@ -14,7 +14,7 @@ public class ArthasConstants {
*/
public static final String NETTY_LOCAL_ADDRESS = "arthas-netty-LocalAddress";
public static int MAX_HTTP_CONTENT_LENGTH = 1024 * 1024 * 8;
public static final int MAX_HTTP_CONTENT_LENGTH = 1024 * 1024 * 8;
public static final String ARTHAS_OUTPUT = "arthas-output";
@@ -22,4 +22,6 @@ public class ArthasConstants {
public static final String PROJECT_NAME = "project.name";
public static final String SPRING_APPLICATION_NAME = "spring.application.name";
public static final int TELNET_PORT = 3658;
}
+4 -1
View File
@@ -1,7 +1,10 @@
#arthas.config.overrideAll=true
arthas.telnetPort=3658
arthas.httpPort=8563
arthas.ip=localhost
arthas.ip=127.0.0.1
# seconds
arthas.sessionTimeout=1800
#arthas.tunnelServer=ws://127.0.0.1:7777/ws
#arthas.agentId=mmmmmmyiddddd
@@ -22,9 +22,6 @@ import java.util.Properties;
*/
public class Arthas {
private static final String DEFAULT_TELNET_PORT = "3658";
private static final String DEFAULT_HTTP_PORT = "8563";
private Arthas(String[] args) throws Exception {
attachAgent(parse(args));
}
@@ -35,11 +32,11 @@ public class Arthas {
Option agent = new TypedOption<String>().setType(String.class).setShortName("agent").setRequired(true);
Option target = new TypedOption<String>().setType(String.class).setShortName("target-ip");
Option telnetPort = new TypedOption<Integer>().setType(Integer.class)
.setShortName("telnet-port").setDefaultValue(DEFAULT_TELNET_PORT);
.setShortName("telnet-port");
Option httpPort = new TypedOption<Integer>().setType(Integer.class)
.setShortName("http-port").setDefaultValue(DEFAULT_HTTP_PORT);
.setShortName("http-port");
Option sessionTimeout = new TypedOption<Integer>().setType(Integer.class)
.setShortName("session-timeout").setDefaultValue("" + Configure.DEFAULT_SESSION_TIMEOUT_SECONDS);
.setShortName("session-timeout");
Option tunnelServer = new TypedOption<String>().setType(String.class).setShortName("tunnel-server");
Option agentId = new TypedOption<String>().setType(String.class).setShortName("agent-id");
@@ -55,15 +52,20 @@ public class Arthas {
configure.setJavaPid((Long) commandLine.getOptionValue("pid"));
configure.setArthasAgent((String) commandLine.getOptionValue("agent"));
configure.setArthasCore((String) commandLine.getOptionValue("core"));
configure.setSessionTimeout((Integer)commandLine.getOptionValue("session-timeout"));
if (commandLine.getOptionValue("target-ip") == null) {
throw new IllegalStateException("as.sh is too old to support web console, " +
"please run the following command to upgrade to latest version:" +
"\ncurl -sLk https://arthas.aliyun.com/install.sh | sh");
if (commandLine.getOptionValue("session-timeout") != null) {
configure.setSessionTimeout((Integer) commandLine.getOptionValue("session-timeout"));
}
if (commandLine.getOptionValue("target-ip") != null) {
configure.setIp((String) commandLine.getOptionValue("target-ip"));
}
if (commandLine.getOptionValue("telnet-port") != null) {
configure.setTelnetPort((Integer) commandLine.getOptionValue("telnet-port"));
}
if (commandLine.getOptionValue("http-port") != null) {
configure.setHttpPort((Integer) commandLine.getOptionValue("http-port"));
}
configure.setIp((String) commandLine.getOptionValue("target-ip"));
configure.setTelnetPort((Integer) commandLine.getOptionValue("telnet-port"));
configure.setHttpPort((Integer) commandLine.getOptionValue("http-port"));
configure.setTunnelServer((String) commandLine.getOptionValue("tunnel-server"));
configure.setAgentId((String) commandLine.getOptionValue("agent-id"));
@@ -17,11 +17,11 @@ import static java.lang.reflect.Modifier.isStatic;
*/
@Config(prefix = "arthas")
public class Configure {
public static final long DEFAULT_SESSION_TIMEOUT_SECONDS = ShellServerOptions.DEFAULT_SESSION_TIMEOUT/1000;
private String ip;
private int telnetPort;
private int httpPort;
private long javaPid;
private Integer telnetPort;
private Integer httpPort;
private Long javaPid;
private String arthasCore;
private String arthasAgent;
@@ -43,8 +43,9 @@ public class Configure {
/**
* session timeout seconds
* @see ShellServerOptions#DEFAULT_SESSION_TIMEOUT
*/
private long sessionTimeout = DEFAULT_SESSION_TIMEOUT_SECONDS;
private Long sessionTimeout;
public String getIp() {
return ip;
@@ -54,7 +55,7 @@ public class Configure {
this.ip = ip;
}
public int getTelnetPort() {
public Integer getTelnetPort() {
return telnetPort;
}
@@ -66,7 +67,7 @@ public class Configure {
this.httpPort = httpPort;
}
public int getHttpPort() {
public Integer getHttpPort() {
return httpPort;
}
@@ -94,7 +95,7 @@ public class Configure {
this.arthasCore = arthasCore;
}
public long getSessionTimeout() {
public Long getSessionTimeout() {
return sessionTimeout;
}
@@ -285,12 +285,12 @@ public class ArthasBootstrap {
}
// init random port
if (configure.getTelnetPort() == 0) {
if (configure.getTelnetPort() != null && configure.getTelnetPort() == 0) {
int newTelnetPort = SocketUtils.findAvailableTcpPort();
configure.setTelnetPort(newTelnetPort);
logger().info("generate random telnet port: " + newTelnetPort);
}
if (configure.getHttpPort() == 0) {
if (configure.getHttpPort() != null && configure.getHttpPort() == 0) {
int newHttpPort = SocketUtils.findAvailableTcpPort();
configure.setHttpPort(newHttpPort);
logger().info("generate random http port: " + newHttpPort);
@@ -322,8 +322,10 @@ public class ArthasBootstrap {
try {
ShellServerOptions options = new ShellServerOptions()
.setInstrumentation(instrumentation)
.setPid(PidUtils.currentLongPid())
.setSessionTimeout(configure.getSessionTimeout() * 1000);
.setPid(PidUtils.currentLongPid());
if (configure.getSessionTimeout() != null) {
options.setSessionTimeout(configure.getSessionTimeout() * 1000);
}
if (agentId != null) {
Map<String, String> welcomeInfos = new HashMap<String, String>();
@@ -1,5 +1,6 @@
package com.taobao.arthas.core.shell.term;
import com.taobao.arthas.common.ArthasConstants;
import com.taobao.arthas.core.config.Configure;
import com.taobao.arthas.core.shell.ShellServerOptions;
import com.taobao.arthas.core.shell.future.Future;
@@ -20,7 +21,8 @@ public abstract class TermServer {
* @return the term server
*/
public static TermServer createTelnetTermServer(Configure configure, ShellServerOptions options) {
return new TelnetTermServer(configure.getIp(), configure.getTelnetPort(), options.getConnectionTimeout());
int port = configure.getTelnetPort() != null ? configure.getTelnetPort() : ArthasConstants.TELNET_PORT;
return new TelnetTermServer(configure.getIp(), port, options.getConnectionTimeout());
}
/**