PageRenderTime 31ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/mysql_watcher/log_watcher

https://bitbucket.org/lindenlab/apiary/
Python | 186 lines | 120 code | 23 blank | 43 comment | 23 complexity | f42556873849fab90118eb17bc0e9d77 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. Summarizes data collected by mysql_logger
  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
  38. import re
  39. import socket
  40. import sys
  41. import time
  42. import traceback
  43. from llbase import llsd
  44. from dblibs.dbutil import LLQueryStream, LLQueryStatMap, LLBinnedQueryStats, LLLogQueryStream, LLLogIter
  45. from dblibs.dbbrowser import *
  46. DISPLAY_INTERVAL = 5.0
  47. DUMP_INTERVAL = 360.0
  48. def watch_host(stdscr, host, query_metadata):
  49. "Watches query traffic for a particular host. Returns the overall query counts when exited by breaking"
  50. #
  51. # Options/parameters
  52. #
  53. output_path = "./" + host
  54. # Generate a chronologically sorted list of all log files in the directory.
  55. MAX_LOGS=36
  56. log_files = []
  57. for i in range(0, MAX_LOGS):
  58. # Count down from the max and rename
  59. n = MAX_LOGS - i
  60. log_filename = output_path + "/query.log.%d.gz" % n
  61. if os.path.exists(log_filename):
  62. log_files.append(log_filename)
  63. log_filename = output_path + "/query.log"
  64. if os.path.exists(log_filename):
  65. log_files.append(log_filename)
  66. # Start listening to data stream
  67. query_stream = LLLogQueryStream(LLLogIter(log_files))
  68. # Curses configuration stuff
  69. size = stdscr.getmaxyx()
  70. stdscr.nodelay(True) # Non-blocking keyboard input
  71. sort_by = "total_time"
  72. count = 0
  73. cur_time = 0
  74. last_display_time = time.time()
  75. last_dump_time = time.time()
  76. last_count = 0
  77. total_stats = LLQueryStatMap("Total", time.time())
  78. binned_stats = LLBinnedQueryStats()
  79. display_stats = total_stats
  80. query_browser = LLQueryBrowser(stdscr, total_stats, query_metadata)
  81. done = False
  82. try:
  83. while not done:
  84. (event_type, query) = query_stream.getNextEvent()
  85. if event_type == "QueryStart":
  86. total_stats.queryStart(query)
  87. binned_stats.queryStart(query)
  88. elif (event_type == "QueryResponse"):
  89. total_stats.queryResponse(query)
  90. binned_stats.queryResponse(query)
  91. count += 1
  92. elif event_type == "Quit":
  93. # Quit is an "instantaneous" query, both start and response
  94. total_stats.queryStart(query)
  95. total_stats.queryResponse(query)
  96. binned_stats.queryStart(query)
  97. binned_stats.queryResponse(query)
  98. continue
  99. elif event_type == None:
  100. # no more data
  101. # Open the next file
  102. if len(log_files):
  103. query_stream
  104. time.sleep(0.01)
  105. # Spam
  106. if (0 == (count % 100)) or (None == event_type):
  107. # Check keyboard input
  108. c = 0
  109. done = False
  110. while -1 != c:
  111. c = stdscr.getch()
  112. if -1 == c:
  113. pass
  114. elif query_browser.handleKey(c):
  115. query_browser.redraw()
  116. pass
  117. elif c == ord('q'):
  118. # Quit
  119. done = True
  120. break
  121. elif c == ord(' '):
  122. # Switch which bins we're displaying
  123. query_browser.setQueryMap(display_stats)
  124. query_browser.redraw()
  125. elif c == ord('d'):
  126. # Dump output
  127. total_stats.dumpTiming("%s/query_timing.txt" % output_path)
  128. total_stats.dumpLLSD("%s/query_dump.llsd" % output_path)
  129. binned_stats.dumpTiming(output_path)
  130. binned_stats.dumpLLSD(output_path)
  131. else:
  132. print "Pressed key %s" % c
  133. cur_time = time.time()
  134. if (cur_time - last_display_time > DISPLAY_INTERVAL):
  135. last_display_time = cur_time
  136. last_count = count
  137. query_browser.setQueryMap(display_stats)
  138. query_browser.redraw()
  139. if (cur_time - last_dump_time > DUMP_INTERVAL):
  140. last_dump_time = cur_time
  141. total_stats.dumpTiming("%s/query_timing.txt" % output_path)
  142. total_stats.dumpLLSD("%s/query_dump.llsd" % output_path)
  143. binned_stats.dumpTiming(output_path)
  144. binned_stats.dumpLLSD(output_path)
  145. except:
  146. traceback.print_exc()
  147. total_stats.dumpTiming("%s/query_timing.txt" % output_path)
  148. total_stats.dumpLLSD("%s/query_dump.llsd" % output_path)
  149. if __name__ == "__main__":
  150. opts, args = getopt.getopt(sys.argv[1:], "", ["host="])
  151. host = None
  152. for o, a in opts:
  153. if o in ("--host"):
  154. host = a
  155. if not host:
  156. raise "No host specified"
  157. query_metadata = LLQueryMetadata("./query_info.llsd")
  158. #watch_host(None, host, query_metadata)
  159. curses.wrapper(watch_host, host, query_metadata)