mongodb记录如下:
[root@varnish1 ~]# mongo lo****p
MongoDB shell version: 2.4.3
connecting to: lo****p
Server has startup warnings:
Fri May 24 10:27:29.880 [initandlisten]
Fri May 24 10:27:29.880 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Fri May 24 10:27:29.880 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Fri May 24 10:27:29.880 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
Fri May 24 10:27:29.880 [initandlisten] ** See
Fri May 24 10:27:29.881 [initandlisten]
> db.serverList.find();
{ "_id" : ObjectId("519ed7e10ca3766ebe3411b0"), "ip" : "192.168.1.2", "port" : "3346", "status" : "1" }
{ "_id" : ObjectId("519ed7e50ca3766ebe3411b1"), "ip" : "192.168.1.3", "port" : "3346", "status" : "1" }
{ "_id" : ObjectId("519ed7e90ca3766ebe3411b2"), "ip" : "192.168.1.4", "port" : "3346", "status" : "1" }
{ "_id" : ObjectId("519ed7ed0ca3766ebe3411b3"), "ip" : "192.168.1.5", "port" : "3346", "status" : "1" }
{ "_id" : ObjectId("519ed7f10ca3766ebe3411b4"), "ip" : "192.168.1.6", "port" : "3346", "status" : "1" }
{ "_id" : ObjectId("519ed7f80ca3766ebe3411b5"), "ip" : "192.168.1.7", "port" : "3346", "status" : "1" }
{ "_id" : ObjectId("519ed8030ca3766ebe3411b6"), "ip" : "192.168.1.8", "port" : "3346", "status" : "1" }
{ "_id" : ObjectId("519ed8060ca3766ebe3411b7"), "ip" : "192.168.1.9", "port" : "3346", "status" : "1" }
[root@varnish1 ~]# cat mongo.sh.bak
#!/bin/sh
echo "db.serverList.find({status:\"0\"},{ip:1,\"_id\":0})" | mongo lo***p | grep ip >/root/ip.txt
if [ -s /root/ip.txt ] ; then
echo "It has errors" >/root/mongodbstatus.txt
/bin/awk -F\" '{ print $4}' /root/ip.txt>/root/problem.txt
mail -s "mongodb error" mashouliu@k****.com < /root/problem.txt
for var in `cat /root/problem.txt`
do
echo "db.serverList.update({ip:\"${var}\"},{\$set:{status:\"1\"}})"| mongo l****p
done
else
echo "It works"
fi
或者使用while循环的方式,每间隔10秒测试一次:
[root@varnish1 ~]# cat mongo.sh
#!/bin/sh
port=3346
while :
do
echo "db.serverList.find({status:\"0\"},{ip:1,\"_id\":0})" | mongo lo****p | grep ip >/root/ip.txt
if [ -s /root/ip.txt ] ; then
echo "It has errors" >/root/mongodbstatus.txt
/bin/awk -F\" '{ print $4}' /root/ip.txt>/root/problem.txt
mail -s "mongodb error" mashouliu@****.com < /root/problem.txt
for var in `cat /root/problem.txt`
do
echo "db.serverList.update({ip:\"${var}\"},{\$set:{status:\"1\"}})"| mongo l****p
done
else
echo "It works"
fi
sleep 10
done
shell中直接处理
mongodb:
##我找了好久了,都准备放弃这种方式了,最后还是找到了
[root@varnish1 ~]# echo "show collections" |mongo lo****p
MongoDB shell version: 2.4.3
connecting to: lo
****p
serverList
system.indexes
bye
[root@varnish1 ~]#
领导新要求:10个端口出现了问题才发邮件!稍微修改了下代码:
[root@localhost scripts]# cat mongo.sh
#!/bin/sh
while :
do
echo "db.serverList1.find({status:\"0\"},{ip:1,port:1,\"_id\":0})" | mongo l****p | grep ip >/home/scripts/ip.txt
#echo "db.serverList6.find({status:\"0\"},{ip:1,port:1,\"_id\":0})" | mongo l****p | grep ip >>/home/scripts/ip.txt
ipcount=`cat /home/scripts/ip.txt|wc -l`
if [ -s /home/scripts/ip.txt -a "$ipcount" -gt "10" ] ; then
echo "It has errors" >/home/scripts/mongodbstatus.txt
mail -s "mongodb error" mashouliu@****.com meadhu@****.com < /home/scripts/ip.txt
fi
sleep 180
done
###上面的脚本有个bug,如果记录超过20个,只能显示20条记录,因为mongodb中默认一屏幕显示20条记录!
下面修改如下:
点击(此处)折叠或打开
-
[root@s4 scripts]# cat print.js
-
var cursor = db.document.find({status:"0"},{ip:1,port:1,"_id":0}); ##status 为0 表示端口有问题!
-
while (cursor.hasNext()) {
-
printjson(cursor.next());
-
}
-
[root@s4 scripts]# cat serverList.txt
-
1
-
2
-
3
-
6
-
[root@s4 scripts]# cat getallrecord.sh
-
#!/bin/sh
-
for var in `cat /home/scripts/serverList.txt`
-
do
-
sl=serverList${var}
-
sed -i "1s/document/${sl}/g" /home/scripts/print.js
-
mongo l*****p /home/scripts/print.js | grep -v connecting| grep -v MongoDB >> /home/scripts/ip.txt
-
sed -i "1s/${sl}/document/g" /home/scripts/print.js
-
done
发邮件的脚本调用这个脚本,发现有端口宕掉就发邮件:
-
[root@s4 scripts]# cat mongo.sh
-
#!/bin/sh
-
while :
-
do
-
sh /home/scripts/getallrecord.sh
-
if [ -s /home/scripts/ip.txt ] ; then
-
echo "It has errors" >/home/scripts/mongodbstatus.txt
-
mail -s "mongodb error" user1@***.com user2@***.com < /home/scripts/ip.txt
-
fi
阅读(2686) | 评论(0) | 转发(0) |