yohoPerformanceTestAuto_Mysql_InfluxDB.sh 5.12 KB
#!/bin/bash
if [ -f ~/.bash_profile ];
then
  . ~/.bash_profile
fi

MYSQL_IP=192.168.103.44
MYSQL_PORT=3306
MYSQL_USERNAME=ypt
MYSQL_PASSWORD=123456
MYSQL_DBNAME=ypt

INFLUXDB_IP=192.168.103.44
INFLUXDB_DB=ypt

YPT_IP=192.168.103.44

YPTLOCKPATH_USER=/Data/logs/jmeter/ypt.user.lock
YPTLOCKPATH_RESULTID=/Data/logs/jmeter/ypt.resultId.lock
RUNTIMELOG_PATH=/Data/logs/jmeter/runtime.log

function exeMysql(){
mysql -h${MYSQL_IP} -P${MYSQL_PORT} -u${MYSQL_USERNAME} -p${MYSQL_PASSWORD} ${MYSQL_DBNAME} --default-character-set=utf8 -e "$1"
}
function exeInfluxdb(){
curl -XPOST "http://${INFLUXDB_IP}:8086/write?db=${INFLUXDB_DB}" --data-binary "$1"
}
function exeYptInterface(){
curl -XPOST "http://${YPT_IP}:8080/ypt/machines/updateMachines" --data-binary "$1"
}

function uploadLoadInfo(){
tmp_file_path=/tmp/yohoPerformanceTestAuto_Mysql_Monitor.nmon
#生成1次nmon结果文件
/opt/jmeter/shell/auto/nmon_linux_x86_64 -F ${tmp_file_path} -N -s 1 -c 1
sleep 2
#ip=$(/sbin/ifconfig | grep "inet "|grep -v "127.0.0.1" | awk '{print $2}' | awk '{print substr($0 , index($0 , ":")+1)}')
ip=$(/sbin/ip addr show | grep eth | grep inet | awk '{print $2}' | awk -F '/' '{print $1}')

#cpu负载-1分钟
cpu_load=$(cat /proc/loadavg  | awk '{print $1}')
#cpu使用率 %
cpu_ratio=$(awk -F ',' '$1=="CPU_ALL"&&$2=="T0001"{print 100-$6}' <${tmp_file_path})
#内存使用率 %
memory_ratio=$(awk -F ',' '$1=="MEM"&&$2=="T0001"{print ($3-$7)*100/$3}' <${tmp_file_path})
#网络收发吞吐量  Mbps
net=$(awk -F ',' '$1=="NET"&&$2=="T0001"{max=$4;if($6>$4) max=$6;print max*8/1024}' <${tmp_file_path})
#磁盘IO 使用率  %
disk_busy=$(cat ${tmp_file_path} | grep DISKBUSY,T0001 | awk -F ',' '{max=0.0;for(i=3;i<=NF;i++) if($i>max) max=$i ;print max}')
#使用的端口号数量
#tcp_num=$(netstat -a | grep tcp | wc -l)



#获取服务器配置信息
hostname=$(hostname)
cpu_num=$(cat /proc/cpuinfo  | grep processor | wc -l)
memory_total=$(cat /proc/meminfo  | grep MemTotal  | awk '{print int($2/1024/1024)==$2/1024/1024?$2/1024/1024:int(int($2/1024/1024*10/10+1))}')


#检查是否已经装好:java、svn、jmeter、gor、log_directory、


#last:检查jmeter是否已经启动,如未占用,清空占用者
#exeSqlStr=""
#username=""
#if [ -f ${YPTLOCKPATH_USER} ];then
#username=$(cat ${YPTLOCKPATH_USER})
#fi
#if [ "$ip" == "$MYSQL_IP" ];then
#username="CONSOLE"
#fi
#is_jmeter_running=0
#jmeterPsCount=$(ps -ef| grep -e "/opt/jmeter/bin/jmeter.*-n.*-t.*-X" -e "java -server -XX:+HeapDumpOnOutOfMemoryError.*-XX:+CMSClassUnloadingEnabled -jar /opt/jmeter/bin/ApacheJMeter.jar -n -t"  -e "/opt/jmeter/bin/jmeter-server" -e "/opt/jmeter/bin/jmeter.*-Djava.rmi.server.hostname=" -e "java -server -XX:+HeapDumpOnOutOfMemoryError.*-jar /opt/jmeter/bin/ApacheJMeter.jar -Dserver_port=" | grep -v grep | wc -l)
#if [ $jmeterPsCount -gt 0 ];then
#is_jmeter_running=1
#exeSqlStr="UPDATE machines SET hostname=\"${hostname}\" , cpu=${cpu_num} , memory=${memory_total} , monitor_cpu_load=${cpu_load} , monitor_cpu_ratio=${cpu_ratio} , monitor_memory_ratio=${memory_ratio} , monitor_net=${net} , monitor_disk_busy=${disk_busy} ,is_jmeter_running=${is_jmeter_running} WHERE ip=\"${ip}\""
#else
#is_jmeter_running=0
#exeSqlStr="UPDATE machines SET hostname=\"${hostname}\" , cpu=${cpu_num} , memory=${memory_total},username=\"${username}\",monitor_cpu_load=${cpu_load} , monitor_cpu_ratio=${cpu_ratio} , monitor_memory_ratio=${memory_ratio} , monitor_net=${net} , monitor_disk_busy=${disk_busy} ,is_jmeter_running=${is_jmeter_running} WHERE ip=\"${ip}\""
#fi
#exeSqlStr="UPDATE machines SET hostname=\"${hostname}\" , cpu=${cpu_num} , memory=${memory_total} , monitor_cpu_load=${cpu_load} , monitor_cpu_ratio=${cpu_ratio} , monitor_memory_ratio=${memory_ratio} , monitor_net=${net} , monitor_disk_busy=${disk_busy} WHERE ip=\"${ip}\""
exeYptInterfaceStr="hostname=${hostname}&cpu=${cpu_num}&memory=${memory_total}&monitor_cpu_load=${cpu_load}&monitor_cpu_ratio=${cpu_ratio}&monitor_memory_ratio=${memory_ratio}&monitor_net=${net}&monitor_disk_busy=${disk_busy}&ip=${ip}"
#
#exeSqlStr2=""
#resultId=0
#if [ -f ${YPTLOCKPATH_RESULTID} ];then
#resultId=$(cat ${YPTLOCKPATH_RESULTID})
#fi
#if [ -f ${RUNTIMELOG_PATH} -a $resultId -gt 0 ];then
#startTime=$(cat ${RUNTIMELOG_PATH} | grep "Starting the test" | awk -F '(' '{print $2}' | awk -F ')' '{print $1}')
#endTime=$(cat ${RUNTIMELOG_PATH} | grep "Tidying up remote" | awk -F '(' '{print $2}' | awk -F ')' '{print $1}')
#if [ "$startTime" != "" ];then
#startTime=$(echo ${startTime} | cut -c 1-10 | xargs -t -i date -d @{} "+%F %T")
#if [ "$endTime" = "" ];then
#exeSqlStr2="UPDATE test_result SET start_time=\"${startTime}\" WHERE id=${resultId}"
#else
#endTime=$(echo ${endTime} | cut -c 1-10 | xargs -t -i date -d @{} "+%F %T")
#exeSqlStr2="UPDATE test_result SET start_time=\"${startTime}\" , end_time=\"${endTime}\" WHERE id=${resultId}"
#fi
#exeMysql "$exeSqlStr2"
#fi
#fi
exeInfluxdbStr="machine_info,ip=${ip}  cpu_load=${cpu_load},cpu_ratio=${cpu_ratio},memory_ratio=${memory_ratio},net=${net},disk_busy=${disk_busy}"
#上传到mysql
#exeMysql "$exeSqlStr"
exeYptInterface "$exeYptInterfaceStr"
#上传到influxdb
exeInfluxdb "$exeInfluxdbStr"
}

#while true
#do
uploadLoadInfo

#sleep 7
#done