1. 程式人生 > >linux-監控查詢mongo索引片鍵指令碼

linux-監控查詢mongo索引片鍵指令碼

#!/bin/bash
#file name      :watch_dog.sh
#function       :1、檢視mongodb資料庫集合;2、檢視各個集合索引;3、檢視各個集合片鍵
#version        :V 1.0

#配置引數
#base_data_path='../data/'
#mongodb_url='172.20.72.42:20000'
#mongodb_config_url='172.20.72.42:20000'
#mysql_host_ip="10.40.66.184"
#mysql_host_port="3306"
#mysql_user="root"
#mysql_pwd="Ab123456"
#mysql_database_name="XXXX"
################################

PATH="$PATH:/data/mongodb/bin/"

#配置指令碼需要的引數
if [ $# -ne 1  ] ;then
  echo "Warn:請輸入使用的配置引數名稱,如local,pro等,在路徑../conf中配置的引數檔名"
  echo
  exit 1
fi


source_url="/data/mongodb/watch_mongoDb/conf/$1.conf"
if [ ! -e  "$source_url" ];then
  echo "Warn:配置檔案不存在"
  echo 
  exit 1
fi

source $source_url
###############################



#提醒資訊
warn_info(){
  echo "$(date +"%Y-%m-%d %T"):$1"
}

#獲取路徑
data_path(){
   if [ $# -eq 1  ] ;then
      echo "${base_data_path}/data/"$1.$$
   else
      warn_info "Warn:路徑有問題"
      warn_info
      exit 1
   fi
}

#執行mysql儲存過程
handler_sql(){
  warn_info 'B:handler shard sql,begin'
  mysql -u${mysql_user} -p${mysql_pwd} -h${mysql_host_ip} -P${mysql_host_port}  ${mysql_database_name} < $(data_path sql)
  warn_info 'F:handler shards sql success,finish'
}

#打包語句成sql語句
package_sql(){
  warn_info "B:package sql execute,begin,M:${1},DB:${2},DATA:${3}"
  database=${2%.*}
  collection=${2#*.}
  #賦值
  warn_info "===>database:${database}==>collection:${collection}==>value:${3}"
  echo "call mongo_collection_handler(${1},${3},\"${collection}\",\"${database}\");" >> $(data_path "sql")
  warn_info "F:package sql execute,finish,M:${1},DB:${2},DATA:${3}"
}

#處理片鍵檔案資訊
handler_shards(){
  warn_info "B:handler shards execute,begin,DB:$1"

  while read line
  do
    my_id=$(echo "$line" | jq --compact-output  ._id |sed 's/"//g')
    mykey=$(echo "$line" | jq --compact-output  .key |sed -e 's/"/\\\"/g'  -e 's/{/"{/g' -e 's/}/}\"/g')
    #database=${my_id%.*}
    #collection=${my_id#*.}
    package_sql 2 "$my_id" "$mykey"
  
  done < $(data_path "shard.$1")
  warn_info "F:handler shards execute,finish,DB:$1"
}

#查詢片鍵資訊
search_shard(){
  warn_info "B:search shard execute,begin,DB:$1,URL:$2"
  collection_count=$(jq '.|length' $(data_path "col.$1"))
  warn_info "=>${1} collection count :$collection_count"  

  for (( i=0; i< ${collection_count}; i=i+1 )); 
  do
     collection_name=`jq .[$i] $(data_path "col.$1") | sed 's/\"//g'`;
     result=`mongo --quiet $2/config --eval "printjson(db.collections.findOne({'_id':'$1.$collection_name'},{'key':1}))"`
     #列印原始資訊
     #echo $result >> $(data_path "raw.shard.$1") 
     if [[ $result != "null" || $(echo $result | jq .key) != "null"  ]] ;then
	#echo "{ \"_id\" : \"AAAA_XXXX.${collection_name}\", \"key\" :null}" >> $(data_path "shard.$1")
        #else
	echo $result >> $(data_path "shard.$1")
     fi
  done
  #刪除null的一行資料
  sed -i '/null/d' $(data_path "shard.$1") 
  warn_info "F:search shard execute,finsh,DB:$1,URL:$2"
}

#處理集合索引資訊
handler_indexes(){
  warn_info "B:handler indexes,begin,DB:$1"
   
  while read line
   do
    #獲取索引資訊
    indexes="\"$(echo $line | jq --compact-output .[].key | sed 's/"/\\"/g')\""
    #獲取db-集合資訊
    db_collection_name=$(echo $line | jq --compact-output .[0].ns|sed 's/"//g')
    #打包成sql語句
    #echo "<<<<<<========="
    #echo "---->$(echo $indexes)---->"
    #echo "++++> ${indexes}"
    #echo "=====>>>>>>>>>"
    package_sql 1 "$db_collection_name" "$(echo $indexes)"
  
  done < $(data_path "index.$1")

  warn_info "F:handler indexes,finish,DB:$1"
}

#查詢集合索引資訊
search_index(){
  warn_info "search collection index,begin,DB:$1,URL:$2"
  collection_count=$(jq '.|length' $(data_path "col.$1"))
  warn_info "collection_count:${collection_count}"
  for (( i=0; i< ${collection_count}; i=i+1 ));
  do
     collection=$(jq --compact-output .[$i] $(data_path "col.$1") | sed 's/\"//g');
     temp_indexes=$(mongo --quiet "$2/$1" --eval "printjson(db.getCollection(\"${collection}\").getIndexes())")
     echo $temp_indexes | jq --compact-output . >> $(data_path "index.$1")
  done

  warn_info "search collection index,finish,DB:$1,URL:$2"
}

#查詢所有表集合
search_collection(){
 warn_info "B:searh collection executed,begin,DB:$1,URL:$2"
 mongo --quiet "$2/$1"  --eval "printjson(db.getCollectionNames())" > $(data_path "col.${1}")
 warn_info "F:search collection execute,finish,DB:S1,URL:$2"
}

#集合從mysql資料庫中查,原因是隻顯示mysql中的列出的集合資訊
#search_collection2(){
#  warn_info "B:search collection2 executed,begin,DB:$1"
#  collection_sql="SELECT collection_name from mongo_collection_info where enabled_flag=1 and database_name=\"${1}\";"
#  warn_info "===execute sql=>${collection_sql}=>"
#  mysql  -u${mysql_user} -p${mysql_pwd} -h${mysql_host_ip} -P${mysql_host_port}  ${mysql_database_name} -e "${collection_sql}" > $(data_path "col.${1}")
#  #資料封裝成陣列,相容search_collection的結果
#  sed -i  -e 's/^/"&/g' -e 's/$/&",/g' -e "1i [" -e '$s/.$//' $(data_path "col.${1}")
#  echo "]" >> $(data_path "col.${1}")
#  warn_info "F:search collection2 executed,fi:nish,DB:$1"
#}


#程式執行
watch_dog2_work(){
 warn_info "B:watch dog starts woring data"

 search_collection "AAAA_XXXX" "$mongodb_url"
 search_collection "BBBB_XXXX" "$mongodb_url"

 search_index "AAAA_XXXX" "$mongodb_url"
 search_index "BBBB_XXXX" "$mongodb_url"

 search_shard "AAAA_XXXX" "$mongodb_config_url"
 search_shard "BBBB_XXXX" "$mongodb_config_url"

 handler_indexes "mapper_XXXX"
 handler_indexes "BBBB_XXXX"

 handler_shards "AAAA_XXXX"
 handler_shards "BBBB_XXXX"

 #執行sql語句
 handler_sql
 warn_info "F:watch dog finishes work"
}

echo 
warn_info "==============================="
#開始工作
watch_dog2_work