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

if [[ ! $1 ]];then
  echo "not input tc_name"
  exit 1
fi

INFLUXDB_IP=192.168.104.43
INFLUXDB_DB=jmeter
INFLUXDB_DB2=ypt

TC_NAME=$1
GOR_LOG_PATH=/Data/logs/jmeter/gor.log

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

basepath=$(cd `dirname $0`; pwd)

function exeInfluxdb(){
  curl -XPOST "http://${INFLUXDB_IP}:8086/write?db=${INFLUXDB_DB}" --data-binary "$1"
}
function exeInfluxdb2(){
  curl -XPOST "http://${INFLUXDB_IP}:8086/write?db=${INFLUXDB_DB2}" --data-binary "$1"
}

function uploadLoadInfo(){
  #
  tmp_file_path=/tmp/yohoPerformanceTestAuto_Mysql_Monitor.nmon
  #生成1次nmon结果文件
  ${basepath}/nmon_linux_x86_64 -F ${tmp_file_path} -N -s 1 -c 1
  sleep 2

  #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})
  memory_ratio=$(echo $memory_ratio | awk -v n=$memory_ratio '{printf("%.2f", n)}')
  #网络收发吞吐量  Mbps
  net=$(awk -F ',' '$1=="NET"&&$2=="T0001"{max=$4;if($6>$4) max=$6;print max*8/1024}' <${tmp_file_path})
  net=$(echo $net | awk -v n=$net '{printf("%.2f", n)}')
  #磁盘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}')

  exeInfluxdbStr2="machine_info,ip=${IP}  cpu_load=${cpu_load},cpu_ratio=${cpu_ratio},memory_ratio=${memory_ratio},net=${net},disk_busy=${disk_busy}"
  exeInfluxdb2 "$exeInfluxdbStr2"
  #gor
  v1=$(cat ${GOR_LOG_PATH} | grep output_http | tail -n 1 | awk -F ',' '{print $2}')
  v2=$(cat ${GOR_LOG_PATH} | grep output_http | tail -n 1 | awk -F ',' '{print $5}')
  exeInfluxdbStr="${IP}.${TC_NAME}.ok.count value=${v2}
${IP}.${TC_NAME}.ok.pct90 value=${v1}"
  exeInfluxdb "$exeInfluxdbStr"
}

uploadLoadInfo