mongodb监控脚本

2021-05-13 15:31:56 浏览数 (1)

1.创建用于监控的用户角色:mongo用户的权限按用户角色进行划分

> use admin

> db.getRoles()

> db.createRole(

{ role:"mongostatRole",

privileges:[{resource:{cluster:true},actions:["serverStatus"]}],

roles:[]}

) //建立一个使用mongostat命令权限的用户角色

2.创建系统监控角色

> db.getUsers()

> db.createUser(

{ user:"dbmon",

pwd:"password",

roles:[{role:"mongostatRole",db:"admin"}]}

)

3.编写脚本,mongodb_sts.sh

#!/bin/bash

host="127.0.0.1"

passwd="password"

rst=''

case "$1" in

conn)

rst=`mongostat -n 1 -u dbbak -p $passwd --authenticationDatabase=admin|awk 'NR==2 {print $(NF-1)}'`

;;

dirty)

rst=`mongostat -n 1 -u dbbak -p $passwd --authenticationDatabase=admin|awk -F[ ] 'NR==2 {print $8}'`

;;

used)

rst=`mongostat -n 1 -u dbbak -p $passwd --authenticationDatabase=admin|awk -F[ ] 'NR==2 {print $9}'`

;;

qr)

rst=`mongostat -n 1 -u dbbak -p $passwd --authenticationDatabase=admin|awk -F[ |] 'NR==2 {print $(NF-7)}'`

;;

qw)

rst=`mongostat -n 1 -u dbbak -p $passwd --authenticationDatabase=admin|awk -F[ |] 'NR==2 {print $(NF-6)}'`

;;

res)

res=$(echo "db.serverStatus().mem" | mongo -u dbbak -p $passwd admin|awk -F[, ] '/"resident"/{print $3}')

rst=$[res*1024*1024]

;;

vsize)

resize=$(echo "db.serverStatus().mem" | mongo -u dbbak -p $passwd admin|awk -F[, ] '/"virtual"/{print $3}')

rst=$[resize*1024*1024]

;;

*)

echo "aaaaaaaaaaaa"

# exit 5

esac

if [ -z $rst ];then

echo 0

else

echo $rst

fi

mongodb重启脚本

[root@localhost ~]# cat rest_mongo.sh

#!/bin/sh

#

#chkconfig: 2345 80 90

#description: mongodb

start() {

/usr/local/mongodb/bin/mongod --dbpath=/data02/mongo_db/db --logpath=/var/mongodb/logs/mongo.log --master --oplogSize 4096 --logappend --port=27017 --fork

}

stop() {

/usr/local/mongodb/bin/mongod --dbpath=/data02/mongo_db/db --logpath=/var/mongodb/logs/mongo.log --master --oplogSize 4096 --logappend --port=27017 --fork --shutdown

}

case "$1" in

start)

start

;;

stop)

stop

;;

restart)

stop

start

;;

*)

echo $"Usage: $0 {start|stop|restart}"

exit 1

esac

根据连接数来重启mongodb

[root@localhost ~]# cat moni_mongo.sh

#!/bin/sh

set -x

max_num=`/usr/local/mongodb/bin/mongostat -n 1 |awk 'NR==2 {print $(NF-1)}'`

if [ "$max_num" -gt 2000 ]

then

/root/rest_mongo.sh restart

echo "[ `date '%h %d %T'` ] mongodb max number is toooooo many $max_num , all processes restarted" >> /tmp/res_mongo.txt

else

echo "max number is $max_num" >> /tmp/res_mongo.txt

echo "[ `date '%h %d %T'` ] mongodb is oked " >> /tmp/res_mongo.txt

fi

db.currentOp(

{ "active" : true,

"secs_running" : { "$gt" : 1000 },

"ns" : "moblie_cloud.newsdatasource"

}

);

过滤的是运行时间大于1000秒的

根据监控链接返回内容判断是否重启

[root@iZ2zei81ixfndvemhjevnwZ ~]# cat monitor_mongodb.sh

#!/bin/bash

curl -s http://ydy.zhongsou.com/Listener/listen_mongo > /tmp/mongostatus.txt

if [ `grep -c SUCCESSFULLY /tmp/mongostatus.txt` -eq 0 ]; then

/root/rest_mongo.sh restart

echo "[ `date '%h %d %T'` ] mongodb all processes restarted" >> /tmp/res_mongo.txt

fi

[root@iZ2zei81ixfndvemhjevnwZ ~]# cat rest_mongo.sh

#!/bin/sh

#

#chkconfig: 2345 80 90

#description: mongodb

start() {

/data01/mongodb-linux-x86_64-3.0.3/bin/mongod --dbpath=/data02/data89 --logpath=/var/log/mongo.log --logappend --master --port=27017 --fork

}

stop() {

/data01/mongodb-linux-x86_64-3.0.3/bin/mongod --dbpath=/data02/data89 --logpath=/var/log/mongo.log --logappend --master --port=27017 --fork --shutdown

}

case "$1" in

start)

start

;;

stop)

stop

;;

restart)

stop

start

;;

*)

echo $"Usage: $0 {start|stop|restart}"

exit 1

esac

0 人点赞