PageRenderTime 32ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/mysql_watcher/mysql_logger

https://bitbucket.org/lindenlab/apiary/
Python | 156 lines | 97 code | 23 blank | 36 comment | 19 complexity | 35aaac83b4c8e955480ea378126e3aa2 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. """
  29. try:
  30. import psyco
  31. psyco.full()
  32. except:
  33. pass
  34. import curses
  35. import curses.wrapper
  36. import getopt
  37. import os.path
  38. import re
  39. import socket
  40. import sys
  41. import time
  42. from dblibs.dbutil import LLQueryStream, remote_mysql_stream
  43. LOG_ROTATION_INTERVAL=3600
  44. MAX_LOGS = 36
  45. QUERY_LOG_RE = re.compile("query.log.(.+).gz")
  46. def rotate_logs(log_path, query_log_file):
  47. # Fork to do the actual rotation/compression
  48. print "Rotating query logs"
  49. if query_log_file:
  50. query_log_file.close()
  51. need_gzip = False
  52. if os.path.exists(log_path+"/query.log"):
  53. os.rename(log_path+"/query.log", log_path+"/query.log.tmp")
  54. need_gzip = True
  55. query_log_file = open("%s/query.log" % log_path, "w")
  56. pid = os.fork()
  57. if pid:
  58. return query_log_file
  59. # Child process actually does the log rotation
  60. # Delete the oldest
  61. log_filename = log_path+"/query.log.%d.gz" % (MAX_LOGS)
  62. if os.path.exists(log_filename):
  63. os.remove(log_filename)
  64. for i in range(0, MAX_LOGS):
  65. # Count down from the max and rename
  66. n = MAX_LOGS - i
  67. log_filename = log_path+"/query.log.%d.gz" % n
  68. if os.path.exists(log_filename):
  69. os.rename(log_path + ("/query.log.%d.gz" % n), log_path + ("/query.log.%d.gz" % (n+1)))
  70. if need_gzip:
  71. # Compress the "first" log (query.log.tmp)
  72. os.rename(log_path + "/query.log.tmp", log_path + "/query.log.1")
  73. os.system('gzip -f %s' % (log_path + "/query.log.1"))
  74. print "Done rotating logs!"
  75. sys.exit(0)
  76. def watch_host(query_stream, host):
  77. "Watches query traffic for a particular host. Returns the overall query counts when exited by breaking"
  78. # Make output path
  79. output_path = "./%s" % host
  80. os.system("mkdir -p %s" % output_path)
  81. query_log_file = rotate_logs(output_path, None)
  82. last_log_time = time.time()
  83. done = False
  84. count = 0
  85. try:
  86. while not done:
  87. (event_type, query) = query_stream.getNextEvent()
  88. # Use the start time to determine which hour bin to put the query into
  89. start_time = query.mStartTime
  90. start_hour = time.localtime(start_time)[3]
  91. if event_type == "QueryStart":
  92. query_log_file.write("%f\t%s:%d\t%s\tQueryStart\n" % (query.mStartTime, query.mData['host'], query.mData['port'], query.mData['host_clean']))
  93. query_log_file.write("%s\n" % (query.mData['query']))
  94. query_log_file.write("**************************************\n")
  95. count += 1
  96. elif (event_type == "QueryResponse"):
  97. query_log_file.write("%f\t%s:%d\t%s\tQueryResponse\n" % (query.mResponseTime, query.mData['host'], query.mData['port'], query.mData['host_clean']))
  98. query_log_file.write("%s\n" % (query.mData['query']))
  99. query_log_file.write("**************************************\n")
  100. elif event_type == "Quit":
  101. # Quit is an "instantaneous" query, both start and response
  102. query_log_file.write("%f\t%s:%d\t%s\tQuit\n" % (query.mStartTime, query.mData['host'], query.mData['port'], query.mData['host_clean']))
  103. query_log_file.write("%s\n" % (query.mData['query']))
  104. query_log_file.write("**************************************\n")
  105. continue
  106. if not (count % 1000):
  107. try:
  108. os.waitpid(-1, os.WNOHANG)
  109. except OSError:
  110. pass
  111. if (time.time() - last_log_time) > LOG_ROTATION_INTERVAL:
  112. last_log_time = time.time()
  113. query_log_file = rotate_logs(output_path, query_log_file)
  114. except KeyboardInterrupt:
  115. pass
  116. query_log_file.close()
  117. if __name__ == "__main__":
  118. opts, args = getopt.getopt(sys.argv[1:], "", ["host="])
  119. host = None
  120. for o, a in opts:
  121. if o in ("--host"):
  122. host = a
  123. if not host:
  124. print "Specify a host using --host="
  125. sys.exit(1)
  126. # Start up the stream from the target host and create a file
  127. # that we can hand to LLQueryStream
  128. query_stream_file = remote_mysql_stream(host)
  129. query_stream = LLQueryStream(query_stream_file)
  130. watch_host(query_stream, host)