-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSlowQueryLogParser.rb
executable file
·335 lines (284 loc) · 8.54 KB
/
SlowQueryLogParser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/ruby -w
#
# --------------------------------------------------------------------------------
# MYSQL SLOW QUERY LOG PARSER
# --------------------------------------------------------------------------------
#
# http://code.google.com/p/mysql-slow-query-log-parser
#
# Inspired by on the perl MySQL slow query log parser written by
# Nathanial Hendler (http://retards.org/)
#
# Any suggestions or fixes are more then welcome.
# lee (at) kumkee (dot) com
#
# --------------------------------------------------------------------------------
# USAGE
# --------------------------------------------------------------------------------
#
# ruby SlowQueryLogParser [Path to log] [Order By]
#
# eg.
# ruby SlowQueryLogParser query.log lock
#
# Order By Options:
# - lock
# - time
# - number
#
# --------------------------------------------------------------------------------
# TODO
# --------------------------------------------------------------------------------
#
# - Parse server info at the top of the log
# - Add date / time selection
# - XML Output
# - Totals information / stats
#
# --------------------------------------------------------------------------------
# UPADATE LOG
# --------------------------------------------------------------------------------
#
# 2007-06-06 - Version 0.1 Alpha
# First version with basic parsing of log file & basic sorting
# 2009-02-20
# Support for multiline queries by Jacob Kjeldahl
# 2011-06-27
# Fix for bug where minimum time and lock always compute to 0 by Benoit Soenen
#
# --------------------------------------------------------------------------------
#
# MySQL Slow Query Log Parser.
#
# Copyright 2007-2011 Lee Kemp
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'date'
# Vars
logPath = ARGV[0]
orderBy = ARGV[1]
version = "0.1 Alpha"
spacer = "#" * 80
# Print page header
puts spacer
puts
puts "MySQL Slow Query Log Parser v #{version}"
puts
puts Time::now()
puts "Output for #{logPath} ordered by #{orderBy}"
puts
puts spacer
puts
# This array holds all the query objects after they have been read from the text file
queries = Array.new
# This hash holds the QueryTotals using the normalized SQL query as the key
queryTotals = Hash.new
class Query
def initialize(sql, date, time, lock, rows, sent)
@sql = sql
@date = date
@time = time.to_i
@lock = lock.to_i
@rows = rows.to_i
@sent = sent.to_i
# Normalize sql query using RegExp from perl parser
@normalized_query = @sql.gsub(/\d+/, "XXX") # Replace numbers
@normalized_query = @normalized_query.gsub(/([\'\"]).+?([\'\"])/, "XXX") # Replace strings
#@normalized_query = @normalized_query.gsub(/\/\*[A-Za-z0-9\W\S]*/, "") # Remove comments '/* blah */
end
def getNormalizedQuery()
@normalized_query
end
def getTime()
@time
end
def getLock()
@lock
end
def to_s
"Date: #{@date}, Time #{@time}, Lock #{@lock}, Sent #{@sent}, Rows #{@rows} \n #{@sql}"
end
end
class QueryTotal
def initialize(sql)
@sql = sql
@queries = Array.new
@max_time = 0
@max_lock = 0
@min_time = -1
@min_lock = -1
end
def addQuery(query)
@queries.push(query)
if @max_time < query.getTime then
@max_time = query.getTime
end
if @max_lock < query.getLock then
@max_lock = query.getLock
end
if @min_time > query.getTime or @min_time == -1 then
@min_time = query.getTime
end
if @min_lock > query.getLock or @min_lock == -1 then
@min_lock = query.getLock
end
end
def getMax_time
@max_time
end
def getMax_lock
@max_lock
end
def getMin_time
@min_time
end
def getMin_lock
@min_lock
end
def getNumberQueries
@queries.length
end
def getMedianTime
@queries.sort{ |a,b| a.getTime <=> b.getTime }[@queries.length / 2].getTime
end
def getMedianLock
@queries.sort{ |a,b| a.getLock <=> b.getLock }[@queries.length / 2].getLock
end
def getAverageTime
total = 0
for query in @queries
total = total + query.getTime
end
total / @queries.length
end
def getAverageLock
total = 0
for query in @queries
total = total + query.getLock
end
total / @queries.length
end
def to_s
"Max time: #{@max_time}, Max lock #{@max_lock}, Number of queries #{@queries.length} \n #{@sql}"
end
def display
puts "#{@queries.length} Queries"
if @queries.length < 10 then
@queries.sort!{ |a,b| a.getTime <=> b.getTime }
print "Taking "
@queries.each do |q|
print "#{q.getTime} "
end
puts "seconds to complete"
@queries.sort{ |a,b| a.getLock <=> b.getLock }
print "Locking for "
@queries.each do |q|
print "#{q.getLock} "
end
puts "seconds"
else
puts "Taking #{@min_time} to #{@max_time} seconds to complete"
puts "Locking for #{@min_lock} to #{@max_lock} seconds"
end
puts "Average time: #{getAverageTime}, Median time #{getMedianTime}"
puts "Average lock: #{getAverageLock}, Median lock #{getMedianLock}"
puts
puts "#{@sql}"
end
end
#
# Starts Here
#
begin
file = File.new(logPath, "r")
while (line = file.gets)
# First line in the query header is the time in which the query happened
if line[0,1] == '#'
if line[0,7] == "# Time:" then
date = "#{line}".delete("#Time:").lstrip.chop
# Ignore next line in the log (server info)
file.gets
else
# puts "Found line missing Date info. Date set to 0"
date = 0
end
# This line (3rd) has all the important info. Time, Lock etc.
line = file.gets
sl = line.split(" ")
time = sl[2]
lock = sl[4]
sent = sl[6]
rows = sl[8]
# The next line is the sql query
sql = file.gets
if sql[0,3] == 'use' then
# When a use statement has been passed as a part of the query the next line is the actual query
sql = file.gets
end
# Some queries span multiple lines
position = file.pos # Store the position
while ((next_line = file.gets) && !(next_line =~ /^#/))
position = file.pos
sql += next_line
end
file.pos = position
# Create and store query object
# If it is more than one week ago or if it doesn't have a valid timestamp we ignore it
if date != 0 and not (Date.strptime(date, '%y%m%d') < Date.jd((DateTime.now.jd)) - 8)
query = Query.new(sql, date, time, lock, rows, sent)
queries.push(query)
end
else
# puts "Ignoring line (This normally means the querys header is messed up)"
# puts line
end
end
file.close
#
# Go over all the query objects and group them in the appropriate QueryTotals object based on the SQL
#
for query in queries
if queryTotals.has_key?(query.getNormalizedQuery) then
qt = queryTotals.fetch(query.getNormalizedQuery)
qt.addQuery(query)
else
qt = QueryTotal.new(query.getNormalizedQuery)
qt.addQuery(query)
queryTotals.store(query.getNormalizedQuery, qt)
end
end
#
# Sort the query totals by lock time and display the output
#
queryTotalsArray = queryTotals.values
case orderBy
when "lock"
queryTotalsArray.sort! { |a,b| a.getMax_lock <=> b.getMax_lock }
#queryTotalsArray.reverse!
when "time"
queryTotalsArray.sort! { |a,b| a.getMax_time <=> b.getMax_time }
#queryTotalsArray.reverse!
else
queryTotalsArray.sort! { |a,b| a.getNumberQueries <=> b.getNumberQueries }
#queryTotalsArray.reverse!
end
puts
for queryTotal in queryTotalsArray
puts spacer
queryTotal.display
end
rescue => err
puts "Exception: #{err}"
err
end