mongoprof/mongoprof.py

83 lines
2.7 KiB
Python
Raw Normal View History

2012-06-25 19:34:19 +00:00
#!/usr/bin/env python
2016-11-30 06:26:32 +00:00
__version__ = '0.3.0'
2012-06-25 19:34:19 +00:00
import time
import datetime
2012-06-26 05:19:58 +00:00
import signal
2012-06-25 19:34:19 +00:00
import argparse
from pymongo import Connection
from termcolor import colored
2012-06-26 05:21:02 +00:00
# global quit monitor
2012-06-26 05:19:58 +00:00
quit = False
2012-06-25 19:34:19 +00:00
2012-06-26 05:21:02 +00:00
2014-02-07 22:26:15 +00:00
def watch(host, dbname, refresh, slowms=0):
2012-06-26 05:19:58 +00:00
global quit
2014-02-07 22:26:15 +00:00
db = getattr(Connection(host), dbname)
2014-02-14 15:21:27 +00:00
# 1 - slow operations
# 2 - all operations
2016-11-30 06:26:32 +00:00
db.set_profiling_level(1 if slowms else 2, slowms)
2012-06-25 19:34:19 +00:00
last_ts = datetime.datetime.utcnow()
exclude_name = '{0}.system.profile'.format(dbname)
2012-06-26 05:19:58 +00:00
def ctrl_c(signal, frame):
global quit
print('returning profiling level to 0...')
db.set_profiling_level(0)
db.system.profile.drop()
quit = True
signal.signal(signal.SIGINT, ctrl_c)
while not quit:
2014-02-14 15:21:27 +00:00
for e in db.system.profile.find({'ns': {'$ne': exclude_name}, 'ts': {'$gt': last_ts}}):
2012-06-25 19:34:19 +00:00
output = []
output.append(colored('{ts:%H:%M:%S}'.format(**e), 'white'))
2012-06-25 22:16:21 +00:00
if 'ns' in e:
output.append(colored('{ns}'.format(**e), 'blue'))
# operation
op = e.get('op')
if op == 'query':
2012-06-25 22:16:21 +00:00
output.append(colored('query {query}'.format(**e), 'cyan'))
elif op == 'update':
2012-06-25 22:16:21 +00:00
output.append(colored('update {query}'.format(**e), 'green'))
elif op == 'getmore':
2014-02-14 15:21:27 +00:00
output.append(colored('getmore {0}'.format(e.get('query', '')), 'grey'))
elif op == 'command':
2012-06-25 19:34:19 +00:00
output.append(colored('{command}'.format(**e), 'cyan'))
else:
2014-02-14 15:21:27 +00:00
output.append(colored('unknown operation: {0}'.format(op), 'red'))
2012-06-26 05:19:58 +00:00
print(e)
2012-06-25 22:16:21 +00:00
2012-06-25 19:34:19 +00:00
if 'nscanned' in e:
2014-02-14 15:21:27 +00:00
output.append(colored('scanned {nscanned}'.format(**e), 'yellow'))
2012-06-25 19:34:19 +00:00
if 'ntoskip' in e:
output.append(colored('skip {ntoskip}'.format(**e), 'blue'))
if 'nreturned' in e:
2014-02-14 15:21:27 +00:00
output.append(colored('returned {nreturned}'.format(**e), 'green'))
2012-06-25 19:34:19 +00:00
if e.get('scanAndOrder'):
output.append(colored('scanAndOrder', 'red'))
if 'millis' in e:
output.append(colored('{millis}ms'.format(**e), 'green'))
2012-06-26 05:19:58 +00:00
print(' '.join(output))
2012-06-25 19:34:19 +00:00
last_ts = e['ts']
time.sleep(refresh)
2012-06-26 05:21:02 +00:00
2012-06-25 19:34:19 +00:00
def main():
parser = argparse.ArgumentParser(description='watch mongo queries')
parser.add_argument('dbname', help='name of database to watch')
2014-02-07 22:26:15 +00:00
parser.add_argument('--host', help='hostname', default='localhost')
2013-07-30 20:32:12 +00:00
parser.add_argument('--slowms', type=int, default=0,
help='only show transactions slower than ms')
2012-06-25 19:34:19 +00:00
args = parser.parse_args()
2014-02-07 22:26:15 +00:00
watch(args.host, args.dbname, 0.1, args.slowms)
2012-06-25 19:34:19 +00:00
if __name__ == '__main__':
main()