#!/usr/bin/env bash
# WIKI: https://alibaba.github.io/arthas
# This script only supports bash, do not support posix sh.
# If you have the problem like Syntax error: "(" unexpected (expecting "fi"),
# Try to run "bash -version" to check the version.
# Try to visit WIKI to find a solution.
# program : Arthas
# author : Core Engine @ Taobao.com
# date : 2019-02-14
# current arthas script version
ARTHAS_SCRIPT_VERSION=3.1.0
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
############ Command Arguments ############
# define arthas's home
ARTHAS_HOME=
# define arthas's lib
ARTHAS_LIB_DIR=${HOME}/.arthas/lib
# target process id to attach
TARGET_PID=
# target process id to attach
TARGET_IP="127.0.0.1"
# telnet port
TELNET_PORT="3658"
# http port
HTTP_PORT="8563"
# telnet session timeout seconds, default 1800
SESSION_TIMEOUT=1800
# use specify version
USE_VERSION=
# maven repo to download arthas
REPO_MIRROR=
# use http to download arthas
USE_HTTP=false
# attach only, do not telnet connect
ATTACH_ONLY=false
# pass debug arguments to the attach java process
DEBUG_ATTACH=false
# arthas-client terminal height
HEIGHT=
# arthas-client terminal width
WIDTH=
# Verbose, print debug info.
VERBOSE=false
# command to execute
COMMAND=
# batch file to execute
BATCH_FILE=
############ Command Arguments ############
# if arguments contains -c/--command or -f/--batch-file, BATCH_MODE will be true
BATCH_MODE=false
# define arthas's temp dir
TMP_DIR=/tmp
# last update arthas version
ARTHAS_VERSION=
# maven-metadata.xml url
# https://repo1.maven.org/maven2/com/taobao/arthas/arthas-packaging/maven-metadata.xml
MAVEN_METADATA_URL="PLACEHOLDER_REPO/com/taobao/arthas/arthas-packaging/maven-metadata.xml"
# arthas remote url
# https://repo1.maven.org/maven2/com/taobao/arthas/arthas-packaging/3.x.x/arthas-packaging-3.x.x-bin.zip
REMOTE_DOWNLOAD_URL="PLACEHOLDER_REPO/com/taobao/arthas/arthas-packaging/PLACEHOLDER_VERSION/arthas-packaging-PLACEHOLDER_VERSION-bin.zip"
# update timeout(sec)
SO_TIMEOUT=5
# define JVM's OPS
JVM_OPTS=""
ARTHAS_OPTS="-Djava.awt.headless=true"
OS_TYPE=
case "$(uname -s)" in
Linux*) OS_TYPE=Linux;;
Darwin*) OS_TYPE=Mac;;
CYGWIN*) OS_TYPE=Cygwin;;
MINGW*) OS_TYPE=MinGw;;
*) OS_TYPE="UNKNOWN"
esac
# check curl/grep/awk/telent/unzip command
if ! [ -x "$(command -v curl)" ]; then
echo 'Error: curl is not installed. Try to use java -jar arthas-boot.jar' >&2
exit 1
fi
if ! [ -x "$(command -v grep)" ]; then
echo 'Error: grep is not installed. Try to use java -jar arthas-boot.jar' >&2
exit 1
fi
if ! [ -x "$(command -v awk)" ]; then
echo 'Error: awk is not installed. Try to use java -jar arthas-boot.jar' >&2
exit 1
fi
if ! [ -x "$(command -v telnet)" ]; then
echo 'Error: telnet is not installed. Try to use java -jar arthas-boot.jar' >&2
exit 1
fi
if ! [ -x "$(command -v unzip)" ]; then
echo 'Error: unzip is not installed. Try to use java -jar arthas-boot.jar' >&2
exit 1
fi
# exit shell with err_code
# $1 : err_code
# $2 : err_msg
exit_on_err()
{
[[ ! -z "${2}" ]] && echo "${2}" 1>&2
exit ${1}
}
# get with default value
# $1 : target value
# $2 : default value
default()
{
[[ ! -z "${1}" ]] && echo "${1}" || echo "${2}"
}
# check arthas permission
check_permission()
{
[ ! -w "${HOME}" ] \
&& exit_on_err 1 "permission denied, ${HOME} is not writable."
}
# SYNOPSIS
# rreadlink <fileOrDirPath>
# DESCRIPTION
# Resolves <fileOrDirPath> to its ultimate target, if it is a symlink, and
# prints its canonical path. If it is not a symlink, its own canonical path
# is printed.
# A broken symlink causes an error that reports the non-existent target.
# LIMITATIONS
# - Won't work with filenames with embedded newlines or filenames containing
# the string ' -> '.
# COMPATIBILITY
# This is a fully POSIX-compliant implementation of what GNU readlink's
# -e option does.
# EXAMPLE
# In a shell script, use the following to get that script's true directory of origin:
# trueScriptDir=$(dirname -- "$(rreadlink "$0")")
rreadlink() ( # Execute the function in a *subshell* to localize variables and the effect of `cd`.
target=$1 fname= targetDir= CDPATH=
# Try to make the execution environment as predictable as possible:
# All commands below are invoked via `command`, so we must make sure that
# `command` itself is not redefined as an alias or shell function.
# (Note that command is too inconsistent across shells, so we don't use it.)
# `command` is a *builtin* in bash, dash, ksh, zsh, and some platforms do not
# even have an external utility version of it (e.g, Ubuntu).
# `command` bypasses aliases and shell functions and also finds builtins
# in bash, dash, and ksh. In zsh, option POSIX_BUILTINS must be turned on for
# that to happen.
{ \unalias command; \unset -f command; } >/dev/null 2>&1
[ -n "$ZSH_VERSION" ] && options[POSIX_BUILTINS]=on # make zsh find *builtins* with `command` too.
while :; do # Resolve potential symlinks until the ultimate target is found.
[ -L "$target" ] || [ -e "$target" ] || { command printf '%s\n' "ERROR: '$target' does not exist." >&2; return 1; }
command cd "$(command dirname -- "$target")" # Change to target dir; necessary for correct resolution of target path.
fname=$(command basename -- "$target") # Extract filename.
[ "$fname" = '/' ] && fname='' # !! curiously, `basename /` returns '/'
if [ -L "$fname" ]; then
# Extract [next] target path, which may be defined
# *relative* to the symlink's own directory.
# Note: We parse `ls -l` output to find the symlink target
# which is the only POSIX-compliant, albeit somewhat fragile, way.
target=$(command ls -l "$fname")
target=${target#* -> }
continue # Resolve [next] symlink target.
fi
break # Ultimate target reached.
done
targetDir=$(command pwd -P) # Get canonical dir. path
# Output the ultimate target's canonical path.
# Note that we manually resolve paths ending in /. and /.. to make sure we have a normalized path.
if [ "$fname" = '.' ]; then
command printf '%s\n' "${targetDir%/}"
elif [ "$fname" = '..' ]; then
# Caveat: something like /var/.. will resolve to /private (assuming /var@ -> /private/var), i.e. the '..' is applied
# AFTER canonicalization.
command printf '%s\n' "$(command dirname -- "${targetDir}")"
else
command printf '%s\n' "${targetDir%/}/$fname"
fi
)
# reset arthas work environment
# reset some options for env
reset_for_env()
{
# init ARTHAS' lib
mkdir -p "${ARTHAS_LIB_DIR}" \
|| exit_on_err 1 "create ${ARTHAS_LIB_DIR} fail."
# if env define the JAVA_HOME, use it first
# if is alibaba opts, use alibaba ops's default JAVA_HOME
[ -z "${JAVA_HOME}" ] && [ -d /opt/taobao/java ] && JAVA_HOME=/opt/taobao/java
if [[ (-z "${JAVA_HOME}") && ( -e "/usr/libexec/java_home") ]]; then
# for mac
JAVA_HOME=`/usr/libexec/java_home`
fi
if [ -z "${JAVA_HOME}" ]; then
# try to find JAVA_HOME from java command
local JAVA_COMMAND_PATH=$( rreadlink $(type -p java) )
JAVA_HOME=$(echo "$JAVA_COMMAND_PATH" | sed -n 's/\/bin\/java$//p')
fi
# iterater throught candidates to find a proper JAVA_HOME at least contains tools.jar which is required by arthas.
if [ ! -d "${JAVA_HOME}" ]; then
JAVA_HOME_CANDIDATES=($(ps aux | grep java | grep -v 'grep java' | awk '{print $11}' | sed -n 's/\/bin\/java$//p'))
for JAVA_HOME_TEMP in ${JAVA_HOME_CANDIDATES[@]}; do
if [ -f "${JAVA_HOME_TEMP}/lib/tools.jar" ]; then
JAVA_HOME=`rreadlink "${JAVA_HOME_TEMP}"`
break
fi
done
fi
if [ -z "${JAVA_HOME}" ]; then
exit_on_err 1 "Can not find JAVA_HOME, please set \$JAVA_HOME bash env first."
fi
# maybe 1.8.0_162 , 11-ea

mzh_cn
- 粉丝: 22
最新资源
- 第一册microsoft-word-文档.doc
- 网络游戏行业分析研究方案促销分析研究.doc
- 隧道养护信息化管理技术.docx
- 工程项目管理实训作业.doc
- 心肌缺血预处理.ppt
- 英语教案-food.doc
- 建筑弱电安装工程量计算详解(天线电视系统+室内电话线路+火灾自动报警系统).ppt
- 音乐欣赏:美丽的蝴蝶.doc
- 状态监测分析案例.pptx
- 防止电气误操作事故应急预案.docx
- 某公司招聘录用管理办法.doc
- 大数据催生个人征信市场百花齐放.docx
- 北京xx房地产发文流程1116黄.doc
- 地面硬化工程施工组织设计方案技术标.doc
- 智慧小区云平台解决方案.doc
- 中建三局-项目结算策划范本精讲.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


