#!/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