nagios-plugin-mongodb
nagios-plugin-mongodb copied to clipboard
Connection check always returns OK
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
I can confirm this !
I have the same issue.
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.
Same issue with pymongo 3.2.2.
Unfortunately, I have the same issue with pymongo 3.2.2 also.
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
Thank you and good job for your work.
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.
sounds good
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 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%