nagios-plugin-mongodb icon indicating copy to clipboard operation
nagios-plugin-mongodb copied to clipboard

Connection check always returns OK

Open bbhenry opened this issue 9 years ago • 11 comments

I have been testing the command connect with false information intentionally, but it has always been reporting okay. For example.

[root@ip-10-95-189-114 plugins]# ./check_mongodb.py -H 33.33.33.wer -A connect -P 2736 -W 2 -C 4
OK - Connection took 0 seconds

bbhenry avatar Sep 08 '15 16:09 bbhenry

I can confirm this !

GuillaumeLohez avatar Sep 24 '15 13:09 GuillaumeLohez

I have the same issue.

prankstr avatar Sep 28 '15 16:09 prankstr

I was having the same issue, but can't reproduce it if I use pymongo 2.9 .

It looks like the network_timeout argument was removed, and this has side effects for other things as well.

eg, running --action connections against a server with no running mongod process hangs for 30sec on pymongo3; returns instantly on 2.9.

The Requirements file should probably contain contraints on which version of pymongo to use.

hatchetation avatar Nov 19 '15 22:11 hatchetation

Same issue with pymongo 3.2.2.

barajus avatar Mar 24 '16 10:03 barajus

Unfortunately, I have the same issue with pymongo 3.2.2 also.

ryayon avatar Mar 30 '16 12:03 ryayon

The whole check needs to be re-written with proper testing for pymongo versions.. they just like to change things.. i'll look into fixing it

mzupan avatar Mar 30 '16 17:03 mzupan

Thank you and good job for your work.

ryayon avatar Mar 30 '16 18:03 ryayon

The MongoClient in pymongo 3+ no longer raises a ConnectionFailure when it fails, rather it just retries in the background so the connection is returning straight away. It is explained here:

https://api.mongodb.org/python/current/migrate-to-pymongo3.html#mongoclient-connects-asynchronously

Looking at this, the "check_connect" function could be rewritten and treated as a simple ping for monitoring sake just using the ismaster admin command to determine if the instance is alive.

warrenpnz avatar Mar 30 '16 20:03 warrenpnz

sounds good

ryayon avatar Mar 30 '16 20:03 ryayon

Something like this:

def mongo_connect(host='127.0.0.1', port=27017, useSSL=False, user=None, passwd=None, replica=None, authdb="admin"):
    from pymongo.errors import ConnectionFailure
    from pymongo.errors import PyMongoError
    try:
        # ssl connection for pymongo > 2.3
        if pymongo.version >= "2.3":
            if replica is None:
                con = pymongo.MongoClient(host, port, ssl=useSSL, connectTimeoutMS=5000, connect=False)
            else:
                con = pymongo.MongoClient(host, port, ssl=useSSL, connectTimeoutMS=5000, connect=False, read_preference=pymongo.ReadPreference.SECONDARY, replicaSet=replica )
        else:
            if replica is None:
                con = pymongo.Connection(host, port, slave_okay=True, network_timeout=10)
            else:
                con = pymongo.Connection(host, port, slave_okay=True, network_timeout=10)
                #con = pymongo.Connection(host, port, slave_okay=True, replicaSet=replica, network_timeout=10)

        try:
          result = con.admin.command("ismaster")
        except ConnectionFailure:
          print("Server not available")
          sys.exit(1)

        if user and passwd:
            db = con[authdb]
            try:
              #if not db.authenticate(user, password=passwd):
              db.authenticate(user, password=passwd)
            except PyMongoError:
                sys.exit("Username/Password incorrect")

    except Exception, e:
        if isinstance(e, pymongo.errors.AutoReconnect) and str(e).find(" is an arbiter") != -1:
            # We got a pymongo AutoReconnect exception that tells us we connected to an Arbiter Server
            # This means: Arbiter is reachable and can answer requests/votes - this is all we need to know from an arbiter
            print "OK - State: 7 (Arbiter)"
            sys.exit(0)
        return exit_with_general_critical(e), None
    return 0, con

warrenpnz avatar Mar 30 '16 23:03 warrenpnz

@warrenpnz seems to work but doesn't seem that the timeout is respected.

$ time ./check_mongodb.py -H mongo-host -A connect -p 27018 -D
Server not available

real    0m30.702s
user    0m0.166s
sys     0m0.033s
cpu     0.64%

druchoo avatar Apr 05 '16 14:04 druchoo