Skip to content

Commit

Permalink
check_mongodb.py - fix compatibility with pymongo v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Pulec committed Feb 28, 2018
1 parent 391da25 commit fe94620
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ check_puppetdb-populations 1.0
check_puppetdb-queue 1.0
check_puppetdb-memory 1.0
check_puppetdb-processed 1.0
check_mongodb.py 1.0
check_mongodb.py 1.1
check_printer 1.0
check_jstat 1.3
check_ntpd-health.pl 2.1
Expand Down
28 changes: 16 additions & 12 deletions check_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def mongo_connect(host=None, port=None, ssl=False, user=None, passwd=None, repli
if replica is None:
con = pymongo.MongoClient(host, port)
else:
con = pymongo.Connection(host, port, read_preference=pymongo.ReadPreference.SECONDARY, ssl=ssl, replicaSet=replica, network_timeout=10)
con = pymongo.MongoClient(host, port, readPreference='secondaryPreferred', ssl=ssl, replicaSet=replica)
else:
if replica is None:
con = pymongo.Connection(host, port, slave_okay=True, network_timeout=10)
Expand Down Expand Up @@ -306,10 +306,7 @@ def exit_with_general_critical(e):


def set_read_preference(db):
if pymongo.version >= "2.2":
pymongo.read_preferences.Secondary
else:
db.read_preference = pymongo.ReadPreference.SECONDARY
db.read_preference = pymongo.ReadPreference.SECONDARY

def check_connect(host, port, warning, critical, perf_data, user, passwd, conn_time):
warning = warning or 3
Expand Down Expand Up @@ -358,8 +355,6 @@ def check_rep_lag(con, host, port, warning, critical, percent, perf_data, max_la
rs_status = {}
slaveDelays = {}
try:
set_read_preference(con.admin)

# Get replica set status
try:
rs_status = con.admin.command("replSetGetStatus")
Expand Down Expand Up @@ -1011,7 +1006,7 @@ def check_queries_per_second(con, query_type, warning, critical, perf_data, mong
if mongo_version == "2":
db.nagios_check.update({u'_id': last_count['_id']}, {'$set': {"data.%s" % query_type: {'count': num, 'ts': int(time.time())}}})
else:
db.nagios_check.update_one({u'_id': last_count['_id']}, {'$set': {"data.%s" % query_type: {'count': num, 'ts': int(time.time())}}})
db.nagios_check.updateOne({u'_id': last_count['_id']}, {'$set': {"data.%s" % query_type: {'count': num, 'ts': int(time.time())}}})

message = "Queries / Sec: %f" % query_per_sec
message += performance_data(perf_data, [(query_per_sec, "%s_per_sec" % query_type, warning, critical, message)])
Expand All @@ -1023,7 +1018,7 @@ def check_queries_per_second(con, query_type, warning, critical, perf_data, mong
if mongo_version == "2":
db.nagios_check.update({u'_id': last_count['_id']}, {'$set': {"data.%s" % query_type: {'count': num, 'ts': int(time.time())}}})
else:
db.nagios_check.update_one({u'_id': last_count['_id']}, {'$set': {"data.%s" % query_type: {'count': num, 'ts': int(time.time())}}})
db.nagios_check.updateOne({u'_id': last_count['_id']}, {'$set': {"data.%s" % query_type: {'count': num, 'ts': int(time.time())}}})

except TypeError:
#
Expand Down Expand Up @@ -1258,8 +1253,17 @@ def check_asserts(con, host, warning, critical, perf_data):

def get_stored_primary_server_name(db):
""" get the stored primary server name from db. """

collections = ''
try:
collections = db.command('listCollections').get('cursor').get('firstBatch')[0].get('name')
except:
pass

if "last_primary_server" in db.collection_names():
stored_primary_server = db.last_primary_server.find_one()["server"]
elif "last_primary_server" in collections:
stored_primary_server = db.last_primary_server.find_one()["server"]
else:
stored_primary_server = None

Expand Down Expand Up @@ -1290,9 +1294,9 @@ def check_replica_primary(con, host, warning, critical, perf_data, replicaset, m
if current_primary != saved_primary:
last_primary_server_record = {"server": current_primary}
if mongo_version == "2":
db.last_primary_server.update({"_id": "last_primary"}, {"$set": last_primary_server_record}, upsert=True, safe=True)
db.last_primary_server.update({"_id": "last_primary"}, {"$set": last_primary_server_record}, upsert=True)
else:
db.last_primary_server.update_one({"_id": "last_primary"}, {"$set": last_primary_server_record}, upsert=True, safe=True)
db.last_primary_server.update({"_id": "last_primary"}, {"$set": last_primary_server_record}, upsert=True)
message = "Primary server has changed from %s to %s" % (saved_primary, current_primary)
primary_status = 1
return check_levels(primary_status, warning, critical, message)
Expand Down Expand Up @@ -1516,4 +1520,4 @@ def replication_get_time_diff(con):
# main app
#
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
sys.exit(main(sys.argv[1:]))

0 comments on commit fe94620

Please sign in to comment.