PageRenderTime 109ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mysql_watcher/mysql_logger_yoz

https://bitbucket.org/lindenlab/apiary/
Python | 115 lines | 66 code | 18 blank | 31 comment | 13 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. Log all queries hitting a particular mysql database
  28. For poppy
  29. """
  30. try:
  31. import psyco
  32. psyco.full()
  33. except:
  34. pass
  35. import curses
  36. import curses.wrapper
  37. import getopt
  38. import os.path
  39. import re
  40. import socket
  41. import sys
  42. import time
  43. from dblibs.dbutil import LLQueryStream, remote_mysql_stream
  44. def watch_host(query_stream, host):
  45. output_path = "./%s" % host
  46. os.system("mkdir -p %s" % output_path)
  47. query_log_file = open("%s/query.log" % output_path, "a")
  48. ip_re = re.compile("\d+\.\d+\.\d+\.\d+")
  49. done = False
  50. count = 0
  51. try:
  52. while not done:
  53. (event_type, query) = query_stream.getNextEvent()
  54. named_host = query.mData['host']
  55. if ip_re.match(query.mData['host']):
  56. # Look up the hostname
  57. try:
  58. named_host = socket.gethostbyaddr(query.mData['host'])[0]
  59. except:
  60. pass
  61. if event_type == "QueryStart":
  62. query_log_file.write("%f\t%s:%d\t%s\tQueryStart\n" % (query.mStartTime, query.mData['host'], query.mData['port'], named_host))
  63. query_log_file.write("%s\n" % (query.mData['query_clean']))
  64. query_log_file.write("**************************************\n")
  65. count += 1
  66. elif (event_type == "QueryResponse"):
  67. query_log_file.write("%f\t%s:%d\t%s\tQueryResponse\n" % (query.mResponseTime, query.mData['host'], query.mData['port'], named_host))
  68. query_log_file.write("%s\n" % (query.mData['query']))
  69. query_log_file.write("**************************************\n")
  70. elif event_type == "Quit":
  71. # Quit is an "instantaneous" query, both start and response
  72. continue
  73. if not (count % 1000):
  74. try:
  75. os.waitpid(-1, os.WNOHANG)
  76. except OSError:
  77. pass
  78. except KeyboardInterrupt:
  79. pass
  80. query_log_file.close()
  81. if __name__ == "__main__":
  82. opts, args = getopt.getopt(sys.argv[1:], "", ["host="])
  83. host = None
  84. for o, a in opts:
  85. if o in ("--host"):
  86. host = a
  87. if not host:
  88. print "Specify a host using --host="
  89. sys.exit(1)
  90. # Start up the stream from the target host and create a file
  91. # that we can hand to LLQueryStream
  92. query_stream_file = remote_mysql_stream(host)
  93. query_stream = LLQueryStream(query_stream_file)
  94. watch_host(query_stream, host)