commit 81e1a92c14747a9debfb582b09fdb0c2c6337cfd Author: James Turk Date: Mon Jun 25 15:34:19 2012 -0400 initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f7cc1ce --- /dev/null +++ b/LICENSE @@ -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. diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..c84b531 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,2 @@ +include README.rst +LICENSE diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..7e36d25 --- /dev/null +++ b/README.rst @@ -0,0 +1,8 @@ +mongowatch +========== + +Log watcher for `MongoDB `_. + +BSD-licensed. (see LICENSE) + +Copyright 2012 Sunlight Foundation, James Turk diff --git a/mongoprof.py b/mongoprof.py new file mode 100644 index 0000000..23511ed --- /dev/null +++ b/mongoprof.py @@ -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() diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..62dcf3b --- /dev/null +++ b/setup.py @@ -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'], +)