0

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 Answers1

0

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
R2D2
  • 9,410
  • 2
  • 12
  • 28
  • When I run this it says Database object not callable – Sanjay M S Nov 22 '22 at 04:59
  • rs.printReplicationInfo() is a mongo shell command which gives oplog size. I need to run this command through python – Sanjay M S Nov 22 '22 at 05:45
  • Please, note all of this commands work from replicaSet members , they cannot be executed from mongoses , also check https://stackoverflow.com/questions/9349937/pymongo-strange-error-typeerror-database-object-is-not-callable – R2D2 Nov 22 '22 at 06:50