diff --git a/check_mongodb.py b/check_mongodb.py index 9f96116..3484fd0 100755 --- a/check_mongodb.py +++ b/check_mongodb.py @@ -211,9 +211,9 @@ def main(argv): if action == "connections": return check_connections(con, warning, critical, perf_data) elif action == "replication_lag": - return check_rep_lag(con, host_to_check, port_to_check, warning, critical, False, perf_data, max_lag, user, passwd) + return check_rep_lag(con, host, port, warning, critical, False, perf_data, max_lag, user, passwd, authdb) elif action == "replication_lag_percent": - return check_rep_lag(con, host_to_check, port_to_check, warning, critical, True, perf_data, max_lag, user, passwd, ssl, insecure, ssl_ca_cert_file, cert_file) + return check_rep_lag(con, host, port, warning, critical, True, perf_data, max_lag, user, passwd, authdb, ssl, insecure, cert_file) elif action == "replset_state": return check_replset_state(con, perf_data, warning, critical) elif action == "memory": @@ -402,7 +402,7 @@ def check_connections(con, warning, critical, perf_data): return exit_with_general_critical(e) -def check_rep_lag(con, host, port, warning, critical, percent, perf_data, max_lag, user, passwd, ssl=None, insecure=None, ssl_ca_cert_file=None, cert_file=None): +def check_rep_lag(con, host, port, warning, critical, percent, perf_data, max_lag, user, passwd, authdb="admin", ssl=None, insecure=None, cert_file=None): # Get mongo to tell us replica set member name when connecting locally if "127.0.0.1" == host: if not "me" in con.admin.command("ismaster","1").keys(): @@ -511,7 +511,7 @@ def check_rep_lag(con, host, port, warning, critical, percent, perf_data, max_la lag = float(optime_lag.seconds + optime_lag.days * 24 * 3600) if percent: - err, con = mongo_connect(primary_node['name'].split(':')[0], int(primary_node['name'].split(':')[1]), ssl, user, passwd, None, None, insecure, ssl_ca_cert_file, cert_file) + err, con = mongo_connect(primary_node['name'].split(':')[0], int(primary_node['name'].split(':')[1]), ssl, user, passwd, None, authdb, insecure, cert_file) if err != 0: return err primary_timediff = replication_get_time_diff(con) @@ -1169,15 +1169,18 @@ def check_oplog(con, warning, critical, perf_data): critical = critical or 4 try: db = con.local - ol = db.system.namespaces.find_one({"name": "local.oplog.rs"}) - if (db.system.namespaces.find_one({"name": "local.oplog.rs"}) != None): + + # check if replicaset member + ol = db.oplog.rs.find_one() + if ( ol != None): oplog = "oplog.rs" else: - ol = db.system.namespaces.find_one({"name": "local.oplog.$main"}) - if (db.system.namespaces.find_one({"name": "local.oplog.$main"}) != None): - oplog = "oplog.$main" + # check if master/slave setup + ol = "db.oplog.$main.find_one()" + if ( ol != None): + oplog = 'oplog.$main' else: - message = "neither master/slave nor replica set replication detected" + message = "neither master/slave nor replica set replication detected. Could not determine oplog location" return check_levels(None, warning, critical, message) try: @@ -1187,8 +1190,8 @@ def check_oplog(con, warning, critical, perf_data): data = con.admin.command(son.SON([('collstats', oplog)])) ol_size = data['size'] - ol_storage_size = data['storageSize'] - ol_used_storage = int(float(ol_size) / ol_storage_size * 100 + 1) + ol_storage_size = data['maxSize'] + ol_used_storage = int(float(ol_size) / float(ol_storage_size) * 100 + 1) ol = con.local[oplog] firstc = ol.find().sort("$natural", pymongo.ASCENDING).limit(1)[0]['ts'] lastc = ol.find().sort("$natural", pymongo.DESCENDING).limit(1)[0]['ts']