Skip to content

Fix check_oplog check #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions check_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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']
Expand Down