Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple HTTP/TCP health check for MongoDB

Tags:

http

mongodb

tcp

I need to create a Health Check for a MongoDB instance inside a Docker container.

Although I can make a workaround and use the Mongo Ping using the CLI, the best option is to create a simple HTTP or TCP testing. There is no response in the default 27017 port in standard ping testings.

Is there any trustworthy way to do it?

like image 813
Luís Brito Avatar asked Jun 15 '16 15:06

Luís Brito


3 Answers

I've created a simple health check for mongodb, it uses the mongo client to send a simple query request (eg. db.stats()) to the server.

$ mongo 192.168.5.51:30000/test

MongoDB shell version: 3.2.3
connecting to: 192.168.5.51:30000/test

mongos> db.stats()
{
    "raw" : {
        "set1/192.168.5.52:27000,192.168.5.53:27000" : {
            "db" : "test",
            "collections" : 8,
            "objects" : 50,
            "avgObjSize" : 73.12,
            "dataSize" : 3656,
            "storageSize" : 53248,
            "numExtents" : 8,
            "indexes" : 8,
            "indexSize" : 65408,
            "fileSize" : 469762048,
            "nsSizeMB" : 16,
            "dataFileVersion" : {
                "major" : 4,
                "minor" : 6
            },
            "extentFreeList" : {
                "num" : 28,
                "totalSize" : 184807424
            },
            "ok" : 1
        }
    },
    "objects" : 50,
    "avgObjSize" : 73,
    "dataSize" : 3656,
    "storageSize" : 53248,
    "numExtents" : 8,
    "indexes" : 8,
    "indexSize" : 65408,
    "fileSize" : 469762048,
    "extentFreeList" : {
        "num" : 28,
        "totalSize" : 184807424
    },
    "ok" : 1
}

You can also do this in one line:

$ echo 'db.stats().ok' | mongo 192.168.5.51:30000/test --quiet
1

Hope it's help.

UPDATE:

As @luckydonald said, the ping command is better, so you can do it like:

$ mongo --eval 'db.runCommand("ping").ok' localhost:27017/test --quiet
1

Thanks for @luckydonald.

like image 149
Shawyeok Avatar answered Nov 18 '22 04:11

Shawyeok


If you need only a simple "ping" then you can also use curl:

curl --connect-timeout 10 --silent --show-error hostname:27017

If you get the "error" It looks like you are trying to access MongoDB over HTTP on the native driver port. then your MongoDB is running and reply an answer.

Or use one of these

mongo --norc --quiet --host=hostname:27017 <<< "db.getMongo()"
mongo --norc --quiet --host=hostname:27017 --eval "db.getMongo()" 
echo "db.getMongo()" | mongo --norc --quiet --host=hostname:27017

One benefit against db.stats(): Command db.getMongo() works without authentication, i.e. you don't need any credentials.

like image 41
Wernfried Domscheit Avatar answered Nov 18 '22 04:11

Wernfried Domscheit


One solution is to use a minimal MongoDB client written in a script language for which there is an interpreter in your container.

For example, here is a zero-dependency one in Python: mongo_ping_client.py

like image 38
Lucas Cimon Avatar answered Nov 18 '22 05:11

Lucas Cimon