Skip to content

Instantly share code, notes, and snippets.

@rcalsaverini
Created September 26, 2012 13:19
Show Gist options
  • Save rcalsaverini/3787986 to your computer and use it in GitHub Desktop.
Save rcalsaverini/3787986 to your computer and use it in GitHub Desktop.
Script to monitor an indexing operation on mongodb
import pymongo
import re
import time
from datetime import datetime, timedelta
patt = re.compile("\d*/\d*\s\d*%")
class Operations(object):
def __init__(self, db, host='localhost'):
self.conn = pymongo.Connection(host)
self.db = self.conn[db]
def getOperations(self):
currentOperations = self.db.current_op()['inprog']
return iter(currentOperations)
def getSpecificOperation(self, opid):
for op in self.getOperations():
if op['opid'] == opid:
return op
return None
class Operation(object):
def __init__(self, opid, db, host = 'localhost'):
self.dbops = Operations(db, host=host)
self.opid = opid
def getData(self):
self.data = self.dbops.getSpecificOperation(self.opid)
if self.data is not None:
self.active = self.data['active']
self.runtime = self.data['secs_running']
self.msg = self.data['msg']
def progress(self):
progress, percent = patt.search(self.msg).group().split()
return map(int, progress.split('/'))
def percentDone(self):
progress, percent = patt.search(self.msg).group().split()
percent = percent[0:2]
return int(percent)
def message(self):
return self.msg
def ETA(self):
done, todo = self.progress()
eta = self.runtime * float(todo - done) / float(done)
hours = int(eta // 3600)
minutes = int(eta // 60 - hours * 60)
seconds = int(eta - minutes * 60 - hours * 3600)
return timedelta(hours=hours, minutes=minutes, seconds=seconds)
if __name__ == '__main__':
op = Operation(18, 'analytics', host = '192.168.1.91')
while True:
op.getData()
eta = op.ETA()
now = datetime.now()
when = datetime.now() + eta
percent = op.percentDone()
msg = op.message()
print "The current task is {percent}% done.".format(**locals())
print "Message: {msg}".format(**locals())
print "Expected completion time: {eta}".format(**locals())
print "We expect it to end at {when}.".format(**locals())
print "estimative made at {now}.".format(**locals())
print "\n\n\n"
time.sleep(600)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment