startJmeter.sh 3.16 KB
#!/bin/bash
function usage(){
echo "----------------------------------"
echo "参数说明:"
echo "-s : 脚本相对路径"
echo "-e : 要修改的hosts"
echo "-c 不带参数,表示关闭jmeter"
echo ""
echo "举例:sh startJmeter.sh -s \"/script/test.jmx\" -e \"10.10.11.1 test.test.com\" -e \"192.168.1.1 test.yoho.cn\""
echo "----------------------------------"
}

if [[ $# -lt 1 ]];then
usage
exit 1  
fi

while getopts "cs:e:h" arg
do
case $arg in
c)
c="close"
;;
s)
s=$OPTARG
;;
e)
if [ ! $e ];then
e=$OPTARG
else
e="$e\n$OPTARG"
fi
;;
h)
usage
exit 1
;;
?)
echo "未知的参数!"
exit 1
;;
esac
done

PATH_JMETER_HOME="/opt/jmeter"
PATH_JMETER_LOG="/Data/logs/jmeter"
#CMD_PS_JMETER="ps -ef| grep -e \"${PATH_JMETER_HOME}/bin/jmeter.*-n.*-t.*-X\" -e \"java -server -XX:+HeapDumpOnOutOfMemoryError.*-XX:+CMSClassUnloadingEnabled -jar ${PATH_JMETER_HOME}/bin/ApacheJMeter.jar -n -t\" -e \"${PATH_JMETER_HOME}/bin/jmeter-server\" -e \"${PATH_JMETER_HOME}/bin/jmeter.*-Djava.rmi.server.hostname=\" -e \"java -server -XX:+HeapDumpOnOutOfMemoryError.*-jar ${PATH_JMETER_HOME}/bin/ApacheJMeter.jar -Dserver_port=\" | grep -v grep";
CMD_PS_JMETER="ps -ef | grep \"jar /opt/jmeter/bin/.*-n -t ${PATH_JMETER_HOME}${s}\" | grep -v grep"
CMD_KILL_JMETER="${CMD_PS_JMETER} | cut -c 9-15 | xargs kill -9"
CMD_CLEAR_JMETERLOG="echo > ${PATH_JMETER_LOG}/jmeter.jtl;echo > ${PATH_JMETER_LOG}/jmeterRun.log;echo > ${PATH_JMETER_LOG}/runtime.log;"
CMD_RESTART_MONITOR="${PATH_JMETER_HOME}/shell/restart_YPT_AUTO_MONITOR.sh"
CMD_STOP_MONITOR="${CMD_RESTART_MONITOR} stop"
#CMD_UPDATE_SVN="svn up ${PATH_JMETER_HOME}"
CMD_SYNC_SYSTEMTIME="ntpdate asia.pool.ntp.org"

HOST_NAME=$(hostname)
IP=$(/sbin/ip addr show | grep eth | grep inet | awk '{print $2}' | awk -F '/' '{print $1}')

function startJmeter(){
echo "[startJmeter]脚本路径:${PATH_JMETER_HOME}${s}"
#${PATH_JMETER_HOME}/bin/jmeter -n -t ${PATH_JMETER_HOME}${s}  -l ${PATH_JMETER_LOG}/jmeter.jtl  -j ${PATH_JMETER_LOG}/jmeterRun.log  -X > ${PATH_JMETER_LOG}/runtime.log &
${PATH_JMETER_HOME}/bin/jmeter -n -t ${PATH_JMETER_HOME}${s}  -l /dev/null  -j ${PATH_JMETER_LOG}/jmeterRun.log  -X > ${PATH_JMETER_LOG}/runtime.log &
}
function stopJmeter(){
eval $CMD_KILL_JMETER
}
function clearJmeterLogs(){
eval $CMD_CLEAR_JMETERLOG
}
function restartMonitor(){
eval $CMD_RESTART_MONITOR
}
function stopMonitor(){
eval $CMD_STOP_MONITOR
}
#function updateSvn(){
#eval $CMD_UPDATE_SVN
#}
function gitUpdate(){
cd $PATH_JMETER_HOME
git reset --hard origin/master
git pull
}
function modifyHosts(){
echo "[modifyHosts]HOSTS:127.0.0.1      localhost\n::1  localhost\n$IP  $HOST_NAME\n$e"
echo -e "127.0.0.1	localhost\n::1	localhost\n$IP	$HOST_NAME\n$e" > /etc/hosts
}
function syncSystemTime(){
eval $CMD_SYNC_SYSTEMTIME
}
function runJmeter(){
START_TIME=$(date)
echo "[START][$START_TIME]-----------------------------"
stopJmeter
clearJmeterLogs
modifyHosts
#updateSvn
gitUpdate
restartMonitor
syncSystemTime

startJmeter
echo "[Now Start!]"
}
if [ ! $c ];then
 if [ ! $s ];then
  echo "脚本相对路径不能为空,使用 -s 配置脚本路径!"
 else
  runJmeter
 fi
else
stopJmeter
stopMonitor
END_TIME=$(date)
echo "[STOPPED][$END_TIME]-----------------------------"
fi