简体   繁体   中英

What's the best way to check if a mongo db in Atlas is reachable?

I would like to monitor in Azure the external services i depend on to check if they are up and running, how can i check if my mongo instance on Atlas is working?

One way i found is to ping the database shards, they seem to reply 200 if they are up, is this the best way? I also noticed that i can't ping shards for shared instances, is it true? (even though i don't need to check the shared instances, i just wanted to know)

You can use a script like this:

const map = db.adminCommand("getShardMap").map;
for (let aShard in map) {
   const uri = map[aShard].split('/');
   try {
      replicaSet = Mongo(`mongodb://username:password@${uri[1]}/admin?authSource=admin&replicaSet=${uri[0]}`).getDB('admin');
      let hello = replicaSet.hello();
      if (hello.isWritablePrimary) {
         print(`${aShard} is operational`)
      } else {
         print(`${aShard} has problems`)
      }
   } catch (err) {
      print(`${aShard} has problems: ${err.message}`)
   }
}

Depending on your requirements, you also use replicaSet.adminCommand({ replSetGetStatus: 1 }) instead of replicaSet.hello() or evaluate other attributes of db.hello()

If you like to check the hosts one by one, you can also use db.adminCommand("getShardMap").hosts

If you like to check only the entire database, have a look at Simple HTTP/TCP health check for MongoDB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM