initial commit
This commit is contained in:
commit
81e1a92c14
27
LICENSE
Normal file
27
LICENSE
Normal file
@ -0,0 +1,27 @@
|
||||
Copyright (c) 2012, Sunlight Labs
|
||||
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
* Neither the name of Sunlight Labs nor the names of its contributors may be
|
||||
used to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2
MANIFEST.in
Normal file
2
MANIFEST.in
Normal file
@ -0,0 +1,2 @@
|
||||
include README.rst
|
||||
LICENSE
|
8
README.rst
Normal file
8
README.rst
Normal file
@ -0,0 +1,8 @@
|
||||
mongowatch
|
||||
==========
|
||||
|
||||
Log watcher for `MongoDB <http://mongodb.org>`_.
|
||||
|
||||
BSD-licensed. (see LICENSE)
|
||||
|
||||
Copyright 2012 Sunlight Foundation, James Turk
|
57
mongoprof.py
Normal file
57
mongoprof.py
Normal file
@ -0,0 +1,57 @@
|
||||
#!/usr/bin/env python
|
||||
__version__ = '0.1.0'
|
||||
|
||||
import time
|
||||
import datetime
|
||||
import argparse
|
||||
|
||||
from pymongo import Connection
|
||||
from termcolor import colored
|
||||
|
||||
|
||||
def watch(dbname, refresh):
|
||||
db = getattr(Connection('localhost'), dbname)
|
||||
db.set_profiling_level(2)
|
||||
last_ts = datetime.datetime.utcnow()
|
||||
exclude_name = '{0}.system.profile'.format(dbname)
|
||||
while True:
|
||||
for e in db.system.profile.find({'ns': {'$ne': exclude_name},
|
||||
'ts': {'$gt': last_ts}}):
|
||||
output = []
|
||||
output.append(colored('{ts:%H:%M:%S}'.format(**e), 'white'))
|
||||
output.append(colored('{ns}'.format(**e), 'blue'))
|
||||
if e['op'] == 'query':
|
||||
output.append(colored('{query}'.format(**e), 'cyan'))
|
||||
elif e['op'] == 'getmore':
|
||||
output.append(colored('getmore {query}'.format(**e), 'grey'))
|
||||
elif e['op'] == 'command':
|
||||
output.append(colored('{command}'.format(**e), 'cyan'))
|
||||
else:
|
||||
output.append('unknown operation: {op}'.format(**e), 'red')
|
||||
if 'nscanned' in e:
|
||||
output.append(colored('scanned {nscanned}'.format(**e),
|
||||
'yellow'))
|
||||
if 'ntoskip' in e:
|
||||
output.append(colored('skip {ntoskip}'.format(**e), 'blue'))
|
||||
if 'nreturned' in e:
|
||||
output.append(colored('returned {nreturned}'.format(**e),
|
||||
'green'))
|
||||
if e.get('scanAndOrder'):
|
||||
output.append(colored('scanAndOrder', 'red'))
|
||||
output.append(colored('{millis}ms'.format(**e), 'green'))
|
||||
print ' '.join(output)
|
||||
last_ts = e['ts']
|
||||
time.sleep(refresh)
|
||||
db.set_profiling_level(0)
|
||||
db.system.profile.drop()
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='watch mongo queries')
|
||||
parser.add_argument('dbname', help='name of database to watch')
|
||||
args = parser.parse_args()
|
||||
watch(args.dbname, 0.1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
14
setup.py
Normal file
14
setup.py
Normal file
@ -0,0 +1,14 @@
|
||||
from distutils.core import setup
|
||||
|
||||
setup(
|
||||
name='mongoprof',
|
||||
version='0.1.0',
|
||||
author='James Turk',
|
||||
author_email='jturk@sunlightfoundation.com',
|
||||
url='http://github.com/sunlightlabs/mongoprof',
|
||||
scripts=['mongoprof.py'],
|
||||
license='BSD',
|
||||
description='command line tool for watching mongodb queries',
|
||||
long_description=open('README.rst').read(),
|
||||
install_requires=['pymongo', 'termcolor'],
|
||||
)
|
Loading…
Reference in New Issue
Block a user