To find Oplog size using python

280 views Asked by At

How to find oplog size in mongodb using python?

For example :

replGetSetStatus is equivalent to rs.status()

Is there any similar command to find rs.printReplicationInfo()

uri = "mongodb://usernamen:password@host:port/admin"
conn = pymongo.MongoClient(uri)
db = conn['admin']
db_stats = db.command({'replSetGetStatus'  :1})


primary_optime = 0
secondary_optime = 0

for key in db_stats['members'] : 
    if key['stateStr'] == 'SECONDARY' :
        secondary_optime = key['optimeDate']
    if key['stateStr'] == 'PRIMARY' : 
        primary_optime =key['optimeDate']

print 'primary_optime : ' + str(primary_optime)
print 'secondary_optime : ' + str(secondary_optime)

seconds_lag = (primary_optime - secondary_optime ).total_seconds()
#total_seconds() userd to get the lag in seconds rather than datetime object
print 'secondary_lag : ' + str(seconds_lag)

This is my code. The db.command({'replSetGetStatus' :1}) is working. Similarly I need for the oplog size.

1

There are 1 answers

3
R2D2 On

The following commands executed from any replicaSet member will give you the size of oplog:

Uncompressed size in MB:

 db.getReplicationInfo().logSizeMB

Uncompressed current size in Bytes:

 db.getSiblingDB('local').oplog.rs.stats().size

Compressed current size in Bytes:

  db.getSiblingDB('local').oplog.rs.stats().storageSize
 

Max configured size:

 db.getSiblingDB('local').oplog.rs.stats().maxSize