0

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

1 Answer 1

2

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")
1
  • If there is no built in command this looks very promising ... thanks alot! Marking as accepted !
    – R2D2
    Commented Apr 6, 2022 at 12:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.