Hadoop记录-queue mysql

#!/bin/sh
ip=xxx
port=8088
export HADOOP_HOME=xxx
rmstate1=$($HADOOP_HOME/yarn rmadmin -getServiceState rm1)
rmstate2=$($HADOOP_HOME/yarn rmadmin -getServiceState rm2)
if [[ $rmstate1 == *active* ]]
then
   ip=xxx
fi
cd /home/yarn/queuecheck
rm -f maxmemory.txt  
rm -f usedmemory.txt  
rm -f rate.txt
rm -f qrate.txt
curl http://$ip:$port/ws/v1/cluster/scheduler | jq . | sed 's/\"//g' > scheduler.json
sed -i '1,5d' scheduler.json 
sed -n -e :a -e '1,33!{P;N;D;};N;ba' scheduler.json > nscheduler.json
cat nscheduler.json | grep -i "queueName" | awk -F ": " '{print $2}' | awk -F "," '{print $1}' > qname.txt
cat nscheduler.json | grep -i "memory" | awk -F ": " '{print $2}' | awk -F "," '{print $1}' > memory.txt
line=$(cat memory.txt | wc -l)
for((k=2;k<$line;k=k+6))
do
  sed -n "$k p" memory.txt | awk '{print $1}' >> maxmemory.txt  
done
for((j=3;j<$line;j=j+6))
do
  sed -n "$j p" memory.txt | awk '{print $1}' >> usedmemory.txt  
done
uarr=($(cat usedmemory.txt | awk '{print $1}'))
marr=($(cat maxmemory.txt | awk '{print $1}'))
for((m=0;m<${#uarr[@]};m++))
do 
   for((n=0;n<${#marr[@]};n++))
   do
     if [ $m -eq $n ]
     then 
        if [[ ${uarr[$m]} == *e* ]]
        then
           u1=$(echo ${uarr[$m]} | grep -i "e+0" | awk -F "e" '{print $1}')
           u2=$(echo ${uarr[$m]} | grep -i "e+0" | awk -F "e" '{print $2}' | awk -F "+0" '{print $2}')
           if [ $u2 -eq 6 ]
           then
              u3=`echo "$u1*1000000/1024" | bc`
           elif [ $u2 -eq 7 ]
           then
              u3=`echo "$u1*10000000/1024" | bc` 
           elif [ $u2 -eq 8 ]
           then
              u3=`echo "$u1*100000000/1024" | bc` 
           fi
        else
           u3=`echo "sclae=2; ${uarr[$m]}/1024" | bc`           
        fi        
        if [[ ${marr[$n]} == *e* ]]
        then
           m1=$(echo ${marr[$n]} | grep -i "e+0" | awk -F "e" '{print $1}')
           m2=$(echo ${marr[$n]} | grep -i "e+0" | awk -F "e" '{print $2}' | awk -F "+0" '{print $2}')
           if [ $m2 -eq 6 ]
           then
              m3=`echo "$m1*1000000/1024" | bc`
           elif [ $m2 -eq 7 ]
           then
              m3=`echo "$m1*10000000/1024" | bc` 
           elif [ $m2 -eq 8 ]
           then
              m3=`echo "$m1*100000000/1024" | bc` 
           fi
        else
           m3=`echo "sclae=2; ${marr[$n]}/1024" | bc`           
        fi
        rate=$(printf "%.2f" `echo "scale=2;$u3/$m3"|bc`)
        nrate=`echo "$rate*100" | bc`
        echo $nrate >> rate.txt
     fi
   done
done
qarr=($(cat qname.txt | awk '{print $1}'))
rarr=($(cat rate.txt | awk '{print $1}'))
for((a=0;a<${#qarr[@]};a++))
do 
   for((b=0;b<${#rarr[@]};b++))
   do
     if [ $a -eq $b ]
     then 
        echo ${qarr[$a]}":"${rarr[$b]} >> qrate.txt
     fi
   done
done
linenum=$(cat qrate.txt | wc -l)
let linenum+=1
echo $linenum
create_date=`date +"%Y-%m-%d %H:%M"`
#上报数据
function postdata(){
for (( k = 1; k< $linenum ; k++ ))
  do
  qname=$(echo $(sed -n "$k p" qrate.txt | awk -F ":" '{print $1}') | awk '{gsub(/[[:blank:]]*/,"",$0);print $0;}' )
  qrate=$(sed -n "$k p" qrate.txt | awk -F ":" '{print $2}') 
  qmax=$(sed -n "$k p" maxmemory.txt | awk -F ":" '{print $1}')
  qused=$(sed -n "$k p" usedmemory.txt | awk -F ":" '{print $1}')
  /usr/bin/mysql -uroot -p0 -e "insert into xxx.queue_resource(queue_name,queue_used,queue_max,queue_used_rate,create_date) values ('$qname','$qused','$qmax','$qrate','$create_date')" 
  done
}
postdata
#创建
create table xxx.queue_resource
(
queue_id INT(20) not null AUTO_INCREMENT,
queue_name char(20) not null,
queue_used char(32) not null,
queue_max char(32) not null,
queue_used_rate char(32) not null,
create_date char(32) not null,
primary key (queue_id )
)
posted @ 2019-04-22 11:13  信方  阅读(307)  评论(0编辑  收藏  举报