#!/bin/bash

#########################################################################
# File Name: getparams.sh
# Author: M
# Created Time: 2022/07/18 Monday 21:50:00

# Readme
# 1.The Target TiDB Version Must >= V4
# 2.demo
#   sh getparams.sh 172.16.4.182 4000 root tidb

#########################################################################



# Env_Var_Setting
TARGET_TiDB_IP=$1 
TARGET_TiDB_PORT=$2
TiDB_DB_USER=$3
TiDB_DB_PWD=$4 


# cluster_name=`/home/mfh/.tiup/bin/tiup cluster display tidb-v4 |grep "Cluster name"|awk '{print $3}'`
DB_VERSION=`/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD -N -e "select @@version"|awk -F'-' '{print $NF}'`
VERSION_NUM=`echo $DB_VERSION|awk -F'-' '{print $NF}'|awk -F'.' '{print $1}' |awk -F'v' '{print $2}'`
/bin/chmod +x ./jq



# Base Information Display
# Get The Total Parameter CNT
cat > ./tmp/tidb_tmp_total_cnt.sql << EOF
SELECT count(*) as var_total_cnt FROM mysql.global_variables;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_total_cnt.sql > ./tmp/tmp_total_cnt.txt
var_total_cnt=`tail -1 ./tmp/tmp_total_cnt.txt`

cat > ./tmp/tidb_tmp_total_cnt.sql << EOF
SELECT count(*) as pd_tidb_pd_total_cnt from (select type,\`key\`,GROUP_CONCAT(\`value\`) from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config) mx1 group by type,\`key\`) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_total_cnt.sql > ./tmp/tmp_total_cnt.txt
pd_tidb_tikv_total_cnt=`tail -1 ./tmp/tmp_total_cnt.txt`


# Get The Var Parameter CNT
cat > ./tmp/tidb_tmp_total_cnt.sql << EOF
SELECT count(*) as var_cnt1 FROM mysql.global_variables where \`VARIABLE_VALUE\` not like '%"%';
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_total_cnt.sql > ./tmp/tmp_total_cnt.txt
var_cnt1=`tail -1 ./tmp/tmp_total_cnt.txt`
cat > ./tmp/tidb_tmp_total_cnt.sql << EOF
SELECT count(*) as var_cnt2 FROM mysql.global_variables where \`VARIABLE_VALUE\` like '%"%';
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_total_cnt.sql > ./tmp/tmp_total_cnt.txt
var_cnt2=`tail -1 ./tmp/tmp_total_cnt.txt`


# Get The PD Parameter CNT
cat > ./tmp/tidb_tmp_total_cnt.sql << EOF
SELECT count(*) as pd_cnt1 from (select type,\`key\`,GROUP_CONCAT(\`value\`) from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config) mx1 where type='pd' and \`value\` not like '%[%' group by type,\`key\`) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_total_cnt.sql > ./tmp/tmp_total_cnt.txt
pd_cnt1=`tail -1 ./tmp/tmp_total_cnt.txt`
cat > ./tmp/tidb_tmp_total_cnt.sql << EOF
SELECT count(*) as pd_cnt1 from (select type,\`key\`,GROUP_CONCAT(\`value\`) from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config) mx1 where type='pd' and \`value\` like '%[%' group by type,\`key\`) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_total_cnt.sql > ./tmp/tmp_total_cnt.txt
pd_cnt2=`tail -1 ./tmp/tmp_total_cnt.txt`


# Get The TIDB Parameter CNT
cat > ./tmp/tidb_tmp_total_cnt.sql << EOF
SELECT count(*) as tidb_cnt1 from (select type,\`key\`,GROUP_CONCAT(\`value\`) from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config) mx1 where type='tidb' and \`value\` not like '%[%' group by type,\`key\`) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_total_cnt.sql > ./tmp/tmp_total_cnt.txt
tidb_cnt1=`tail -1 ./tmp/tmp_total_cnt.txt`
cat > ./tmp/tidb_tmp_total_cnt.sql << EOF
SELECT count(*) as tidb_cnt1 from (select type,\`key\`,GROUP_CONCAT(\`value\`) from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config) mx1 where type='tidb' and \`value\` like '%[%' group by type,\`key\`) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_total_cnt.sql > ./tmp/tmp_total_cnt.txt
tidb_cnt2=`tail -1 ./tmp/tmp_total_cnt.txt`



# Get The TIKV Parameter CNT
cat > ./tmp/tidb_tmp_total_cnt.sql << EOF
SELECT count(*) as tikv_cnt1 from (select type,\`key\`,GROUP_CONCAT(\`value\`) from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config) mx1 where type='tikv' and \`value\` not like '%[%' group by type,\`key\`) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_total_cnt.sql > ./tmp/tmp_total_cnt.txt
tikv_cnt1=`tail -1 ./tmp/tmp_total_cnt.txt`
cat > ./tmp/tidb_tmp_total_cnt.sql << EOF
SELECT count(*) as tikv_cnt1 from (select type,\`key\`,GROUP_CONCAT(\`value\`) from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config) mx1 where type='tikv' and \`value\` like '%[%' group by type,\`key\`) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_total_cnt.sql > ./tmp/tmp_total_cnt.txt
tikv_cnt2=`tail -1 ./tmp/tmp_total_cnt.txt`



# Get The TIFLASH Parameter CNT
cat > ./tmp/tidb_tmp_total_cnt.sql << EOF
SELECT count(*) as tikv_cnt1 from (select type,\`key\`,GROUP_CONCAT(\`value\`) from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config) mx1 where type='tiflash' and \`value\` not like '%[%' group by type,\`key\`) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_total_cnt.sql > ./tmp/tmp_total_cnt.txt
tiflash_cnt1=`tail -1 ./tmp/tmp_total_cnt.txt`
cat > ./tmp/tidb_tmp_total_cnt.sql << EOF
SELECT count(*) as tikv_cnt1 from (select type,\`key\`,GROUP_CONCAT(\`value\`) from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config) mx1 where type='tiflash' and \`value\` like '%[%' group by type,\`key\`) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_total_cnt.sql > ./tmp/tmp_total_cnt.txt
tiflash_cnt2=`tail -1 ./tmp/tmp_total_cnt.txt`



# Get The PUMP Parameter CNT
cat > ./tmp/tidb_tmp_total_cnt.sql << EOF
SELECT count(*) as tikv_cnt1 from (select type,\`key\`,GROUP_CONCAT(\`value\`) from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config) mx1 where type='pump' and \`value\` not like '%[%' group by type,\`key\`) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_total_cnt.sql > ./tmp/tmp_total_cnt.txt
pump_cnt1=`tail -1 ./tmp/tmp_total_cnt.txt`
cat > ./tmp/tidb_tmp_total_cnt.sql << EOF
SELECT count(*) as tikv_cnt1 from (select type,\`key\`,GROUP_CONCAT(\`value\`) from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config) mx1 where type='pump' and \`value\` like '%[%' group by type,\`key\`) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_total_cnt.sql > ./tmp/tmp_total_cnt.txt
pump_cnt2=`tail -1 ./tmp/tmp_total_cnt.txt`



# Get The Drainer Parameter CNT
cat > ./tmp/tidb_tmp_total_cnt.sql << EOF
SELECT count(*) as tikv_cnt1 from (select type,\`key\`,GROUP_CONCAT(\`value\`) from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config) mx1 where type='drainer' and \`value\` not like '%[%' group by type,\`key\`) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_total_cnt.sql > ./tmp/tmp_total_cnt.txt
drainer_cnt1=`tail -1 ./tmp/tmp_total_cnt.txt`
cat > ./tmp/tidb_tmp_total_cnt.sql << EOF
SELECT count(*) as tikv_cnt1 from (select type,\`key\`,GROUP_CONCAT(\`value\`) from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config) mx1 where type='drainer' and \`value\` like '%[%' group by type,\`key\`) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_total_cnt.sql > ./tmp/tmp_total_cnt.txt
drainer_cnt2=`tail -1 ./tmp/tmp_total_cnt.txt`



#echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
#echo 'The Var Total CNT :'$var_total_cnt
#echo ''
#echo 'The PD TIDB TIKV Total CNT :'$pd_tidb_tikv_total_cnt
#echo ''
#echo ''
#echo 'The Actual Var Total CNT :'$((var_cnt1+var_cnt2))
#echo ''
#echo ''
#echo 'The Actual PD Total CNT :'$((pd_cnt1+pd_cnt2))
#echo ''
#echo ''
#echo 'The Actual TIDB Total CNT :'$((tidb_cnt1+tidb_cnt2))
#echo ''
#echo ''
#echo 'The Actual TIKV Total CNT :'$((tikv_cnt1+tikv_cnt2))
#echo ''
#echo ''




#echo 'The Actual PD TIDB TIKV Total CNT :'$((pd_cnt1+pd_cnt2+tidb_cnt1+tidb_cnt2+tikv_cnt1+tikv_cnt2))
#echo ''
#echo ''
echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
echo ''
echo 'The Total is :'$((var_total_cnt + pd_tidb_tikv_total_cnt))
echo 'The Actual Total is :'$((var_cnt1+var_cnt2+pd_cnt1+pd_cnt2+tidb_cnt1+tidb_cnt2+tikv_cnt1+tikv_cnt2+tiflash_cnt1+tiflash_cnt2+pump_cnt1+pump_cnt2+drainer_cnt1+drainer_cnt2))
echo ''
echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'




echo '' >  ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
echo '' > ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters_`date +%Y%m%d`.json

if [ "$VERSION_NUM" -gt "4" ];then
# Get Var Configuration
# Process the Standard Var Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT JSON_ARRAYAGG(JSON_OBJECT(\`VARIABLE_NAME\`,\`VARIABLE_VALUE\`)) as Var FROM (select \`VARIABLE_NAME\`,\`VARIABLE_VALUE\` from mysql.global_variables where \`VARIABLE_VALUE\` not like '%"%' order by 1) mx1;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
echo -n "{\"Var\":" >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
sed -i 's/}, {/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{/{/g' ./tmp/tmp_json_output.txt
sed -i 's/}\]//g' ./tmp/tmp_json_output.txt
echo -n `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp

# Process the Not Standard Var Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT JSON_ARRAYAGG(JSON_OBJECT(\`VARIABLE_NAME\`,\`VARIABLE_VALUE\`)) as Var FROM (select \`VARIABLE_NAME\`,\`VARIABLE_VALUE\` from mysql.global_variables where \`VARIABLE_VALUE\` like '%"%' order by 1) mx1;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
sed -i 's/\[{/,/g' ./tmp/tmp_json_output.txt
sed -i 's/}\]/},/g' ./tmp/tmp_json_output.txt
echo `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp


# Process The PD Standard PD Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT JSON_ARRAYAGG(JSON_OBJECT(\`key\`,\`value\`)) as PD_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='pd' and \`value\` not like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
echo -n "\"PD\":" >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
sed -i 's/}, {/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{/{/g' ./tmp/tmp_json_output.txt
sed -i 's/}\]/,/g' ./tmp/tmp_json_output.txt
echo -n `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp



# Process The PD Not Standard PD Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT JSON_ARRAYAGG(JSON_OBJECT(\`key\`,\`value\`)) as PD_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='pd' and \`value\` like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
sed -i 's/}, {/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{"/"/' ./tmp/tmp_json_output.txt
sed -i '$s/.$/,/' ./tmp/tmp_json_output.txt
echo `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp



# Process The TiDB Standard Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT JSON_ARRAYAGG(JSON_OBJECT(\`key\`,\`value\`)) as TiDB_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='tidb' and \`value\` not like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
echo -n "\"TiDB\":" >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
sed -i 's/}, {/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{/{/g' ./tmp/tmp_json_output.txt
sed -i 's/}\]/,/g' ./tmp/tmp_json_output.txt
echo -n `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp


# Process The TiDB Not Standard Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT JSON_ARRAYAGG(JSON_OBJECT(\`key\`,\`value\`)) as TiDB_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='tidb' and \`value\` like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
sed -i 's/}, {/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{"/"/' ./tmp/tmp_json_output.txt
sed -i '$s/.$/,/' ./tmp/tmp_json_output.txt
echo `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp





# Process The TiFLASH Standard Parameter
if [ "$tiflash_cnt1" -gt "0" ];then
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT JSON_ARRAYAGG(JSON_OBJECT(\`key\`,\`value\`)) as TiDB_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='tiflash' and \`value\` not like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
echo -n "\"TiFLASH\":" >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
sed -i 's/}, {/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{/{/g' ./tmp/tmp_json_output.txt
sed -i 's/}\]/,/g' ./tmp/tmp_json_output.txt
echo -n `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp


# Process The TiFLASH Not Standard Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT JSON_ARRAYAGG(JSON_OBJECT(\`key\`,\`value\`)) as TiDB_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='tiflash' and \`value\` like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
sed -i 's/}, {/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{"/"/' ./tmp/tmp_json_output.txt
sed -i '$s/.$/,/' ./tmp/tmp_json_output.txt
echo `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp

else
   echo ''
fi





# Process The Pump Standard Parameter
if [ "$pump_cnt1" -gt "0" ];then
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT JSON_ARRAYAGG(JSON_OBJECT(\`key\`,\`value\`)) as TiDB_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='pump' and \`value\` not like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
echo -n "\"Pump\":" >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
sed -i 's/}, {/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{/{/g' ./tmp/tmp_json_output.txt
sed -i 's/}\]/,/g' ./tmp/tmp_json_output.txt
echo -n `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp


# Process The Pump Not Standard Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT JSON_ARRAYAGG(JSON_OBJECT(\`key\`,\`value\`)) as TiDB_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='pump' and \`value\` like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
sed -i 's/}, {/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{"/"/' ./tmp/tmp_json_output.txt
sed -i '$s/.$/,/' ./tmp/tmp_json_output.txt
echo `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp

else
   echo ''
fi



# Process The Drainer Standard Parameter
if [ "$drainer_cnt1" -gt "0" ];then
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT JSON_ARRAYAGG(JSON_OBJECT(\`key\`,\`value\`)) as TiDB_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='drainer' and \`value\` not like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
echo -n "\"Drainer\":" >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
sed -i 's/}, {/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{/{/g' ./tmp/tmp_json_output.txt
sed -i 's/}\]/,/g' ./tmp/tmp_json_output.txt
echo -n `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp


# Process The Drainer Not Standard Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT JSON_ARRAYAGG(JSON_OBJECT(\`key\`,\`value\`)) as TiDB_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='drainer' and \`value\` like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
sed -i 's/}, {/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{"/"/' ./tmp/tmp_json_output.txt
sed -i '$s/.$/,/' ./tmp/tmp_json_output.txt
echo `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp

else
   echo ''
fi




# Process The TiKV Standard Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT JSON_ARRAYAGG(JSON_OBJECT(\`key\`,\`value\`)) as TiKV_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='tikv' and \`value\` not like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
echo -n "\"TiKV\":" >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
sed -i 's/}, {/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{/{/g' ./tmp/tmp_json_output.txt
sed -i 's/}\]/,/g' ./tmp/tmp_json_output.txt
echo -n `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp


# Process The TiKV Not Standard Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT JSON_ARRAYAGG(JSON_OBJECT(\`key\`,\`value\`)) as TiKV_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='tikv' and \`value\` like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
sed -i 's/}, {/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{"/"/' ./tmp/tmp_json_output.txt
sed -i '$s/.$//' ./tmp/tmp_json_output.txt
echo `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
echo '}' >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
sed -i 's/\\\\/\\/g' ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
./jq '.' ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp > ./output/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters_`date +%Y%m%d`.json

else
# Get Var Configuration
# Process the Standard Var Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
use mysql;
SELECT CONCAT('[',GROUP_CONCAT(JSON_OBJECT(\`VARIABLE_NAME\`,\`VARIABLE_VALUE\`)),']') as Var FROM (select \`VARIABLE_NAME\`,\`VARIABLE_VALUE\` from mysql.global_variables where \`VARIABLE_VALUE\` not like '%"%' order by 1) mx1;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
echo -n "{\"Var\":" >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
sed -i 's/},{/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{/{/g' ./tmp/tmp_json_output.txt
sed -i 's/}\]//g' ./tmp/tmp_json_output.txt
echo -n `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp

# Process the Not Standard Var Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT CONCAT('[',GROUP_CONCAT(JSON_OBJECT(\`VARIABLE_NAME\`,\`VARIABLE_VALUE\`)),']') as Var FROM (select \`VARIABLE_NAME\`,\`VARIABLE_VALUE\` from mysql.global_variables where \`VARIABLE_VALUE\` like '%"%' order by 1) mx1;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
sed -i 's/\[{/,/g' ./tmp/tmp_json_output.txt
sed -i 's/}\]/},/g' ./tmp/tmp_json_output.txt
echo `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp


# Process The PD Standard PD Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT CONCAT('[',GROUP_CONCAT(JSON_OBJECT(\`key\`,\`value\`)),']') as PD_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='pd' and \`value\` not like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
echo -n "\"PD\":" >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
sed -i 's/},{/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{/{/g' ./tmp/tmp_json_output.txt
sed -i 's/}\]/,/g' ./tmp/tmp_json_output.txt
echo -n `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp



# Process The PD Not Standard PD Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT CONCAT('[',GROUP_CONCAT(JSON_OBJECT(\`key\`,\`value\`)),']') as PD_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='pd' and \`value\` like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
sed -i 's/},{/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{"/"/' ./tmp/tmp_json_output.txt
sed -i '$s/.$/,/' ./tmp/tmp_json_output.txt
echo `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp



# Process The TiDB Standard Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT CONCAT('[',GROUP_CONCAT(JSON_OBJECT(\`key\`,\`value\`)),']') as TiDB_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='tidb' and \`value\` not like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
echo -n "\"TiDB\":" >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
sed -i 's/},{/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{/{/g' ./tmp/tmp_json_output.txt
sed -i 's/}\]/,/g' ./tmp/tmp_json_output.txt
echo -n `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp


# Process The TiDB Not Standard Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT CONCAT('[',GROUP_CONCAT(JSON_OBJECT(\`key\`,\`value\`)),']') as TiDB_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='tidb' and \`value\` like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
sed -i 's/},{/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{"/"/' ./tmp/tmp_json_output.txt
sed -i '$s/.$/,/' ./tmp/tmp_json_output.txt
echo `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp




# Process The TiKV Standard Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT CONCAT('[',GROUP_CONCAT(JSON_OBJECT(\`key\`,\`value\`)),']') as TiKV_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='tikv' and \`value\` not like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
echo -n "\"TiKV\":" >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
sed -i 's/},{/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{/{/g' ./tmp/tmp_json_output.txt
sed -i 's/}\]/,/g' ./tmp/tmp_json_output.txt
echo -n `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp


# Process The TiKV Not Standard Parameter
cat > ./tmp/tidb_tmp_run_sql.sql << EOF
SET SESSION group_concat_max_len=10240000;
SELECT CONCAT('[',GROUP_CONCAT(JSON_OBJECT(\`key\`,\`value\`)),']') as TiKV_Var from (select type,\`key\`,GROUP_CONCAT(\`value\`) as value from (select distinct type,\`key\`,\`value\` from information_schema.cluster_config order by 1,2,3) mx1 where type='tikv' and \`value\` like '%[%' group by type,\`key\` order by 2) mx2;
EOF
/bin/mysql -h $TARGET_TiDB_IP -P $TARGET_TiDB_PORT -u$TiDB_DB_USER -p$TiDB_DB_PWD < ./tmp/tidb_tmp_run_sql.sql > ./tmp/tmp_json_output.txt
sed -i '1d' ./tmp/tmp_json_output.txt
sed -i 's/},{/,/g' ./tmp/tmp_json_output.txt
sed -i 's/\[{"/"/' ./tmp/tmp_json_output.txt
sed -i '$s/.$//' ./tmp/tmp_json_output.txt
echo `cat ./tmp/tmp_json_output.txt` >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
echo '}' >> ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
sed -i 's/\\\\/\\/g' ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp
./jq '.' ./tmp/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters.tmp > ./output/${TARGET_TiDB_IP}_${DB_VERSION}_Parameters_`date +%Y%m%d`.json
fi


# Clean The Temporary Directory Files
rm -rf ./tmp/*


