does anybody know an option to identify from mongos if all shard replicaSet members in sharded cluster are alive ?
I can check for example what are the configured non-hidden members with:
sh.status()
But , I dont know how to check if all shard members are alive and electable and I dont want to access to all replicaSet's individually (imagine if I have 100 shards , I dont want to execute 100 times rs.status() , is there any such option and what is the best practice for this?
Thanks
CodePudding user response:
Use a function like this:
function printClusterStatus(MONGO_PASSWROD) {
if (db.hello().msg != "isdbgrid") return; // not a sharded cluster
const user = db.runCommand({ connectionStatus: 1 }).authInfo.authenticatedUsers.shift().user;
const map = db.adminCommand("getShardMap").map;
try {
for (let rs of Object.keys(map)) {
let uri = map[rs].split("/");
let connectionString = "mongodb://" `${user}:${MONGO_PASSWROD}@${uri[1]}/admin?replicaSet=${uri[0]}&authSource=admin`;
let replicaSet = Mongo(connectionString).getDB("admin");
for (let member of replicaSet.adminCommand({ replSetGetStatus: 1 }).members) {
// replicaSet.hello().isWritablePrimary could be also useful
if (!replicaSet.hello().hosts.includes(member.name)) continue; // skip Arbiter, hidden members, etc.
if (member.health != 1 || !Array("PRIMARY", "SECONDARY").includes(member.stateStr)) {
print(`ERROR in shard ${rs}: Member state of ${member.name} is '${member.stateStr}'`);
}
}
}
} catch (err) {
print(tojsononeline(err));
}
}
printClusterStatus("secret")