最近一个监控业务的mongdb,数据量猛增,磁盘吃紧,本来应该都是capped表,不应该空间增长这么快,进去看了一下,是很多新建的表不是capped,但是又由于表很多,在mongdb命令行实在看的眼花,写个脚本查看是否capped。
举个例子:
先取出表检查语句:
$mongo 127.0.0.1:8888/skymobi
MongoDB shell version: 3.2.1
connecting to: 127.0.0.1:8888/skymobi
> var c = db.getCollectionNames();
> for (x in c) { SQL = `db.${c[x]}.isCapped();`; print(SQL)}
db.cc.isCapped();
db.sky.isCapped();
db.sky1.isCapped();
db.test.isCapped();
shell实现:
要执行的检查语句写入一个文件
$cat test.txt
db.cc.isCapped();
db.sky.isCapped();
db.sky1.isCapped();
db.test.isCapped();
检查是否capped collection的shell
$ cat capped_check.sh
#!/bin/bash
for i in $(cat /home/mongo/test.txt); do
rs=`/opt/mongodb3.2.1/bin/mongo 127.0.0.1:8888/skymobi --eval "$i" | sed -n 3p`
if [ "$rs" == "false" ]; then
echo $i | awk -F"." '{print $2}'
fi
done
$sh capped_check.sh
cc
sky
sky1
test
以下新脚本,直接执行即可
#cat check_iscapped.sh
#! /bin/bash
portlist=`netstat -anltp|grep mongod|grep 'LISTEN'|awk -F':' '{print $2}'|awk '{print $1}'`
for port in $portlist;do
dbname=`echo "show dbs"|mongo --quiet 127.0.0.1:${port} | awk -F ':' '{print $$2}' |awk '{print $1}'|grep -vw 'local'|grep -vw 'admin'`
for db in $dbname;do
tablist=`echo "show tables"|mongo --quiet 127.0.0.1:${port}/${db} | awk -F ':' '{print $$2}' |awk '{print $1}'`
for tab in ${tablist};do
capped=`echo "db.${tab}.isCapped()"|mongo --quiet 127.0.0.1:${port}/${db} | awk -F ':' '{print $$2}' |awk '{print $1}'`
if [[ ${capped} = false ]];then
echo ${port}','${db}':'${tab}
fi
done
done
done