PageRenderTime 20ms CodeModel.GetById 11ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/mysql_watcher/mysql_logger_yoz

https://bitbucket.org/lindenlab/apiary/
Python | 115 lines | 66 code | 18 blank | 31 comment | 7 complexity | fd92899bba695480e99846134f560375 MD5 | raw file
  1#!/usr/bin/env python
  2#
  3# $LicenseInfo:firstyear=2010&license=mit$
  4# 
  5# Copyright (c) 2010, Linden Research, Inc.
  6# 
  7# Permission is hereby granted, free of charge, to any person obtaining a copy
  8# of this software and associated documentation files (the "Software"), to deal
  9# in the Software without restriction, including without limitation the rights
 10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11# copies of the Software, and to permit persons to whom the Software is
 12# furnished to do so, subject to the following conditions:
 13# 
 14# The above copyright notice and this permission notice shall be included in
 15# all copies or substantial portions of the Software.
 16# 
 17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23# THE SOFTWARE.
 24# $/LicenseInfo$
 25#
 26
 27"""
 28Log all queries hitting a particular mysql database
 29For poppy
 30"""
 31
 32try:
 33    import psyco
 34    psyco.full()
 35except:
 36    pass
 37
 38import curses
 39import curses.wrapper
 40import getopt
 41import os.path
 42import re
 43import socket
 44import sys
 45import time
 46
 47from dblibs.dbutil import LLQueryStream, remote_mysql_stream
 48
 49
 50
 51
 52
 53def watch_host(query_stream, host):
 54
 55    output_path = "./%s" % host
 56    os.system("mkdir -p %s" % output_path)
 57    query_log_file = open("%s/query.log" % output_path, "a")
 58
 59    ip_re = re.compile("\d+\.\d+\.\d+\.\d+")
 60
 61    done = False
 62    count = 0
 63    try:
 64        while not done:
 65            (event_type, query) = query_stream.getNextEvent()
 66
 67            named_host = query.mData['host']
 68            if ip_re.match(query.mData['host']):
 69                # Look up the hostname
 70                try:
 71                    named_host = socket.gethostbyaddr(query.mData['host'])[0]
 72                except:
 73                    pass
 74
 75            if event_type == "QueryStart":
 76                query_log_file.write("%f\t%s:%d\t%s\tQueryStart\n" % (query.mStartTime, query.mData['host'], query.mData['port'], named_host))
 77                query_log_file.write("%s\n" % (query.mData['query_clean']))
 78                query_log_file.write("**************************************\n")
 79                count += 1
 80            elif (event_type == "QueryResponse"):
 81                query_log_file.write("%f\t%s:%d\t%s\tQueryResponse\n" % (query.mResponseTime, query.mData['host'], query.mData['port'], named_host))
 82                query_log_file.write("%s\n" % (query.mData['query']))
 83                query_log_file.write("**************************************\n")
 84            elif event_type == "Quit":
 85                # Quit is an "instantaneous" query, both start and response
 86                continue
 87            if not (count % 1000):
 88                try:
 89                    os.waitpid(-1, os.WNOHANG)
 90                except OSError:
 91                    pass
 92
 93            
 94    except KeyboardInterrupt:
 95        pass
 96    query_log_file.close()
 97
 98
 99if __name__ == "__main__":
100    opts, args = getopt.getopt(sys.argv[1:], "", ["host="])
101
102    host = None
103    for o, a in opts:
104        if o in ("--host"):
105            host = a
106    if not host:
107        print "Specify a host using --host="
108        sys.exit(1)
109
110    # Start up the stream from the target host and create a file
111    # that we can hand to LLQueryStream
112    query_stream_file = remote_mysql_stream(host)
113    query_stream = LLQueryStream(query_stream_file)
114
115    watch_host(query_stream, host)