Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2809377
  • 博文数量: 587
  • 博客积分: 6356
  • 博客等级: 准将
  • 技术积分: 6410
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-23 10:54
个人简介

器量大者,福泽必厚

文章分类

全部博文(587)

文章存档

2019年(3)

2018年(1)

2017年(29)

2016年(39)

2015年(66)

2014年(117)

2013年(136)

2012年(58)

2011年(34)

2010年(50)

2009年(38)

2008年(16)

分类: LINUX

2013-05-24 15:44:02

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条记录!
下面修改如下:
点击(此处)折叠或打开
  1. [root@s4 scripts]# cat print.js
  2. var cursor = db.document.find({status:"0"},{ip:1,port:1,"_id":0});  ##status 为0 表示端口有问题!
  3. while (cursor.hasNext()) {
  4. printjson(cursor.next());
  5.  }

点击(此处)折叠或打开

  1. [root@s4 scripts]# cat serverList.txt
  2. 1
  3. 2
  4. 3
  5. 6

点击(此处)折叠或打开

  1. [root@s4 scripts]# cat getallrecord.sh
  2. #!/bin/sh
  3. for var in `cat /home/scripts/serverList.txt`
  4. do
  5. sl=serverList${var}
  6. sed -i "1s/document/${sl}/g" /home/scripts/print.js
  7. mongo l*****p /home/scripts/print.js | grep -v connecting| grep -v MongoDB >> /home/scripts/ip.txt
  8. sed -i "1s/${sl}/document/g" /home/scripts/print.js
  9. done
发邮件的脚本调用这个脚本,发现有端口宕掉就发邮件:

点击(此处)折叠或打开

  1. [root@s4 scripts]# cat mongo.sh
  2. #!/bin/sh
  3. while :
  4. do
  5. sh /home/scripts/getallrecord.sh
  6. if [ -s /home/scripts/ip.txt ] ; then
  7. echo "It has errors" >/home/scripts/mongodbstatus.txt
  8. mail -s "mongodb error" user1@***.com user2@***.com  < /home/scripts/ip.txt
  9. fi
阅读(2686) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~