0

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)

Mazza
  • 37
  • 6

1 Answers1

0

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

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
  • What if i don't want to write code? Is there something i can ping or configure in azure app insight? – Mazza Jan 11 '23 at 10:56
  • What do you mean by "don't want to write code"? Calling a `ping` is also code (despite it is a bit shorter than my one). If you prefer to ping them one by one, it's up to you. – Wernfried Domscheit Jan 11 '23 at 11:56