欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mongodb显示shard状态 博客分类: mongodb

程序员文章站 2024-03-17 21:04:04
...
将如下代码保存到文件mongodbstats.js
function checkRS(members){
	var valid=true;
	members.forEach(function(member){
		print(member.name+" is "+member.stateStr)
		if (member.stateStr != "SECONDARY" && member.stateStr != "PRIMARY" && member.stateStr != "ARBITER"){
			valid=false;
		}
	});
	return valid;
}
db = db.getSiblingDB('config');
shards=db.shards.find();
print("\ncurrent shards:\n")
while ( shards.hasNext() ) {
	printjson( shards.next() );
}

print("\ncheck rs status:\n")
shards=db.shards.find();
while ( shards.hasNext() ) {
	shard=shards.next();
	print("checking "+shard._id)
	connStr=shard.host;
	mongo=new Mongo(connStr);
	rsStats=mongo.getDB("admin")._adminCommand("replSetGetStatus");
	var valid=checkRS(rsStats.members);
	if(!valid){
		print("please check this rs!!")
		printjson(rsStats)
	}
	print("")
}


在mongos上运行mongo mongodbstats.js --quiet | grep -v "I NETWORK"

效果如下
引用

current shards:

{ "_id" : "s1", "host" : "s1/dev185.smarket:31001,dev186.smarket:31001" }
{ "_id" : "s2", "host" : "s2/dev250.smarket:31002" }

check rs status:

checking s1
dev186.smarket:31001 is PRIMARY
dev185.smarket:31001 is SECONDARY
dev250.smarket:31001 is ARBITER

checking s2
dev250.smarket:31002 is PRIMARY