-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlog_all_queries.lua
100 lines (89 loc) · 2.82 KB
/
log_all_queries.lua
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
-- MySQL Proxy script for development/debugging purposes
--
-- Written by Patrick Allaert <[email protected]>
-- Copyright © 2009-2011 Libereco Technologies
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
logfile="/var/log/mysql-proxy/querydebug.log"
transaction_counter = 0
output_header=0
function read_query( packet )
if packet:byte() ~= proxy.COM_QUERY then
return
end
proxy.queries:append(1, string.char(proxy.COM_QUERY) .. packet:sub(2), {resultset_is_needed = true})
return proxy.PROXY_SEND_QUERY
end
function read_query_result (inj)
local res = assert(inj.resultset)
local error_status = ""
local row_count = 0
if res.affected_rows then
row_count = res.affected_rows
else
local num_cols = string.byte(res.raw, 1)
if num_cols > 0 and num_cols < 255 then
for row in inj.resultset.rows do
row_count = row_count + 1
end
end
end
if res.query_status == proxy.MYSQLD_PACKET_ERR then
error_status = string.format("%q", res.raw:sub(10))
end
local query = string.gsub(string.sub(inj.query, 2), "%s+", " ")
local word = string.upper(string.sub(query,1,6))
if word == "COMMIT" then
transaction_counter = transaction_counter - 1
end
--
-- local i = 0
-- while i < transaction_counter do
-- io.write(" ")
-- i = i +1
-- end
if string.upper(string.sub(query,1,5)) == "BEGIN" then
transaction_counter = transaction_counter + 1
end
logfd:write(
string.format(
"%d\t%s\t%s\t%x\t%s\t%d\t%s:%s:%s\t%d\t%fms\t%s\n",
os.time(),
proxy.connection.client.username,
proxy.connection.client.default_db,
res.query_status,
error_status,
res.warning_count,
tostring(res.flags.in_trans),
tostring(res.flags.no_index_used),
tostring(res.flags.no_good_index_used),
row_count,
inj.response_time / 1e3,
query
)
)
end
function print_header(fd)
fd:write("# time\tusername\tdbname\tquery-status(hex)\terror-status(str)\twarning-count\tin_trans(b):no-index-used(b):no-good-index-used(b)\trow-count\tresponse-time(ms)\tquery\n")
end
--
-- Init code
--
if not io.open(logfile,"r") then
output_header=1
end
logfd=assert(io.open(logfile,"a"))
if output_header then
print_header(logfd)
end