Skip to content

Commit

Permalink
remove default value in as.sh/arthas-boot. alibaba#1561
Browse files Browse the repository at this point in the history
  • Loading branch information
hengyunabc committed Nov 2, 2020
1 parent e13ced6 commit cdec506
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 85 deletions.
107 changes: 76 additions & 31 deletions bin/as.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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=$()
Expand Down Expand Up @@ -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"
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
83 changes: 58 additions & 25 deletions boot/src/main/java/com/taobao/arthas/boot/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -333,16 +333,16 @@ public static void main(String[] args) throws ParserConfigurationException, SAXE
// 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());
}
}

Expand All @@ -365,7 +365,7 @@ public static void main(String[] args) throws ParserConfigurationException, SAXE

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);
Expand Down Expand Up @@ -469,10 +469,10 @@ public static void main(String[] args) throws ParserConfigurationException, SAXE
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
Expand All @@ -481,12 +481,21 @@ public static void main(String[] args) throws ParserConfigurationException, SAXE
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");
Expand Down Expand Up @@ -546,10 +555,10 @@ public static void main(String[] args) throws ParserConfigurationException, SAXE
}

// 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
Expand All @@ -560,10 +569,10 @@ public static void main(String[] args) throws ParserConfigurationException, SAXE
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);
}
Expand Down Expand Up @@ -712,14 +721,38 @@ public String getTargetIp() {
return targetIp;
}

public int getTelnetPort() {
public String getTargetIpOrDefault() {
if (this.targetIp == null) {
return DEFAULT_TARGET_IP;
} else {
return this.targetIp;
}
}

public Integer getTelnetPort() {
return telnetPort;
}

public int getTelnetPortOrDefault() {
if (this.telnetPort == null) {
return DEFAULT_TELNET_PORT;
} else {
return this.telnetPort;
}
}

public int getHttpPort() {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ 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";

public static final String APP_NAME = "app-name";

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;
}
5 changes: 4 additions & 1 deletion core/src/main/java/arthas.properties
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit cdec506

Please sign in to comment.