Skip to content

Commit

Permalink
Merge pull request mzupan#28 from cangove/master
Browse files Browse the repository at this point in the history
Support for pymongo >= 1.9
  • Loading branch information
mzupan committed Apr 9, 2012
2 parents 01a3aa9 + 647aaea commit c6899d7
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions check_mongodb.py
Expand Up @@ -29,11 +29,17 @@


try: try:
import pymongo import pymongo
import pymongo.son
except ImportError, e: except ImportError, e:
print e print e
sys.exit(2) sys.exit(2)


# As of pymongo v 1.9 the SON API is part of the BSON package, therefore attempt
# to import from there and fall back to pymongo in cases of older pymongo
if pymongo.version >= "1.9":
import bson.son as son
else:
import pymongo.son as son

# #
# thanks to http://stackoverflow.com/a/1229667/72987 # thanks to http://stackoverflow.com/a/1229667/72987
# #
Expand Down Expand Up @@ -163,7 +169,7 @@ def check_connections(con, warning, critical, perf_data):
set_read_preference(con.admin) set_read_preference(con.admin)
data = con.admin.command(pymongo.son_manipulator.SON([('serverStatus', 1), ('repl', 1)])) data = con.admin.command(pymongo.son_manipulator.SON([('serverStatus', 1), ('repl', 1)]))
except: except:
data = con.admin.command(pymongo.son.SON([('serverStatus', 1), ('repl', 1)])) data = con.admin.command(son.SON([('serverStatus', 1), ('repl', 1)]))


current = float(data['connections']['current']) current = float(data['connections']['current'])
available = float(data['connections']['available']) available = float(data['connections']['available'])
Expand Down Expand Up @@ -249,7 +255,7 @@ def check_memory(con, warning, critical, perf_data):
set_read_preference(con.admin) set_read_preference(con.admin)
data = con.admin.command(pymongo.son_manipulator.SON([('serverStatus', 1)])) data = con.admin.command(pymongo.son_manipulator.SON([('serverStatus', 1)]))
except: except:
data = con.admin.command(pymongo.son.SON([('serverStatus', 1)])) data = con.admin.command(son.SON([('serverStatus', 1)]))




if not data['mem']['supported']: if not data['mem']['supported']:
Expand Down Expand Up @@ -288,7 +294,7 @@ def check_lock(con, warning, critical, perf_data):
set_read_preference(con.admin) set_read_preference(con.admin)
data = con.admin.command(pymongo.son_manipulator.SON([('serverStatus', 1)])) data = con.admin.command(pymongo.son_manipulator.SON([('serverStatus', 1)]))
except: except:
data = con.admin.command(pymongo.son.SON([('serverStatus', 1)])) data = con.admin.command(son.SON([('serverStatus', 1)]))


# #
# calculate percentage # calculate percentage
Expand Down Expand Up @@ -326,7 +332,7 @@ def check_flushing(con, warning, critical, avg, perf_data):
set_read_preference(con.admin) set_read_preference(con.admin)
data = con.admin.command(pymongo.son_manipulator.SON([('serverStatus', 1)])) data = con.admin.command(pymongo.son_manipulator.SON([('serverStatus', 1)]))
except: except:
data = con.admin.command(pymongo.son.SON([('serverStatus', 1)])) data = con.admin.command(son.SON([('serverStatus', 1)]))


if avg: if avg:
flush_time = float(data['backgroundFlushing']['average_ms']) flush_time = float(data['backgroundFlushing']['average_ms'])
Expand Down Expand Up @@ -361,7 +367,7 @@ def index_miss_ratio(con, warning, critical, perf_data):
set_read_preference(con.admin) set_read_preference(con.admin)
data = con.admin.command(pymongo.son_manipulator.SON([('serverStatus', 1)])) data = con.admin.command(pymongo.son_manipulator.SON([('serverStatus', 1)]))
except: except:
data = con.admin.command(pymongo.son.SON([('serverStatus', 1)])) data = con.admin.command(son.SON([('serverStatus', 1)]))


try: try:
miss_ratio = float(data['indexCounters']['btree']['missRatio']) miss_ratio = float(data['indexCounters']['btree']['missRatio'])
Expand Down Expand Up @@ -398,7 +404,7 @@ def check_replset_state(con):
set_read_preference(con.admin) set_read_preference(con.admin)
data = con.admin.command(pymongo.son_manipulator.SON([('replSetGetStatus', 1)])) data = con.admin.command(pymongo.son_manipulator.SON([('replSetGetStatus', 1)]))
except: except:
data = con.admin.command(pymongo.son.SON([('replSetGetStatus', 1)])) data = con.admin.command(son.SON([('replSetGetStatus', 1)]))


state = int(data['myState']) state = int(data['myState'])


Expand Down Expand Up @@ -439,7 +445,7 @@ def check_databases(con, warning, critical):
set_read_preference(con.admin) set_read_preference(con.admin)
data = con.admin.command(pymongo.son_manipulator.SON([('listDatabases', 1)])) data = con.admin.command(pymongo.son_manipulator.SON([('listDatabases', 1)]))
except: except:
data = con.admin.command(pymongo.son.SON([('listDatabases', 1)])) data = con.admin.command(son.SON([('listDatabases', 1)]))


count = len(data['databases']) count = len(data['databases'])


Expand All @@ -462,7 +468,7 @@ def check_collections(con, warning, critical):
set_read_preference(con.admin) set_read_preference(con.admin)
data = con.admin.command(pymongo.son_manipulator.SON([('listDatabases', 1)])) data = con.admin.command(pymongo.son_manipulator.SON([('listDatabases', 1)]))
except: except:
data = con.admin.command(pymongo.son.SON([('listDatabases', 1)])) data = con.admin.command(son.SON([('listDatabases', 1)]))


count = 0 count = 0
for db in data['databases']: for db in data['databases']:
Expand Down

0 comments on commit c6899d7

Please sign in to comment.