功能:实现具体显示mongodb数据库表操作语句的状态和情况,使用awk和shell实现抓取和分析处理。脚本内容如下:
#!/bin/bash
if [ $# == 0 ];then
echo "NO enter any worlds,Please enter three worlds, the first world is db and the second world is collection and last world is host for statics!"
elif [ $# == 1 ];then
echo "Enter a world,Please enter other two worlds, the first world is db and the second world is collection and last world
is host for statics!"
elif [ $# == 2 ];then
echo "Enter two worlds,Please enter another worlds, the first world is db and the second world is collection and last world
is host for statics!"
elif [ $# == 3 ];then
world=$1.$2
date=`date +%Y%m%d%H%M%S`
cd /data/Darren/mongodb2.6/bin/
./mongoexport --host $3 --db $1 --collection system.profile --out /data/Darren/mongo_statics/dump_$3_${date}.json
cd /data/Darren/mongo_statics/
grep op dump_$3_${date}.json | awk -F ":|[,]" '{if($4 ~/"'$world'"/) {print}}' >> /data/Darren/mongo_statics/$3_${world}_${date}.log
cat $3_${world}_${date}.log | awk -F ":|[,]|[{]" '{if($8 ~/ "$query" /){$8=$10}if($3 ~/ "insert" /){$8=$6}}{print $5,$3,$8}' | awk -F '"' 'BEGIN{OFS=" | "}{print $2,$4,$6}'|sort|uniq -c >> /data/Darren/mongo_statics/result_$3_${world}_${date}.txt
cat result_$3_${world}_${date}.txt
else
echo "Please enter the correct worlds number!"
fi