Skip to content

Commit

Permalink
Added script and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
koopa committed Apr 19, 2012
1 parent cbd61be commit 920ddf6
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
54 changes: 53 additions & 1 deletion README.md
@@ -1,4 +1,56 @@
mysql-proxy-log-error-queries mysql-proxy-log-error-queries
============================= =============================
This is a mysql proxy lua script to log erroneous queries to a predefined table


Requirements:
-------------
mysql-proxy >= 0.8.2


Installation:
-------------
Create the following table:

CREATE TABLE `somedb`.`mysql_error` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL,
`err_num` smallint(6) NOT NULL,
`err_type` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`err_message` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`problem_query` varchar(8000) COLLATE utf8_unicode_ci NOT NULL,
`conn_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

**Change `somedb`.`mysql_error` to match your log destination**

Also adjust this in the lua script.

**Edit the if condition in read_query to match your preferences.**

By default, only queries of the user "someuser" will be logged.


Usage:
------
1. > \>v0.8.2
/path/to/mysql-proxy --proxy-lua-script=/path/to/mysql-proxy-log-error-queries.lua
2. > \>v0.9
/path/to/mysql-proxy --proxy-lua-script=/path/to/mysql-proxy-log-error-queries.lua --plugins=proxy

By default, mysql-proxy listens on :4040.

**NB: The hostname will always be localhost!**

To connection from the shell to test, use:

mysql -u username -p --host=127.0.0.1 --port=4040

When using a remote proxy, simply replace `127.0.0.1` with the correct remote address.





log mysql erroneuos queries into a predefined table
60 changes: 60 additions & 0 deletions mysql-proxy-log-error-queries.lua
@@ -0,0 +1,60 @@
local err_flag = false
function read_query( packet )
if packet:byte() == proxy.COM_QUERY then
local user = proxy.connection.client.username
local host = proxy.connection.client.src.name
-- print(user .. '@' .. host)
if --[[true or]] user:lower() == 'someuser' then
--[[
-- host will always be localhost because of proxy
and not ( host:lower() == 'localhost'
or host:lower() == '127.0.0.1' ) then
]]
proxy.queries:append(1, packet, {resultset_is_needed = true})
proxy.queries:append(2, string.char(proxy.COM_QUERY) .. "SET @last_query = '" .. string.sub(packet, 2) .. "'", {resultset_is_needed = true} )
proxy.queries:append(3, string.char(proxy.COM_QUERY) .. "SHOW WARNINGS", {resultset_is_needed = true} )
end
return proxy.PROXY_SEND_QUERY
end
end


function insert_query(err_t, err_n, err_m)
local query = "INSERT INTO `somedb`.`mysql_error` " ..
"(`date`, `err_num`,`err_type`, `err_message`, `problem_query`, `conn_id`)" ..
"VALUES( NOW(), " ..
err_n .. "," .. "\"" ..
err_t .."\"" .. "," .. "\"" ..
err_m .. "\"" .. "," ..
"@last_query" .. "," ..
proxy.connection.server.thread_id .. ")"
-- print(query)
proxy.queries:append(4, string.char(proxy.COM_QUERY) .. query, {resultset_is_needed = true})
return proxy.PROXY_SEND_QUERY
end


function read_query_result(inj)
local res = assert(inj.resultset)
if inj.id == 1 then
err_flag = false
if res.query_status == proxy.MYSQLD_PACKET_ERR then
err_flag = true
return proxy.PROXY_IGNORE_RESULT
end
elseif inj.id == 2 then
return proxy.PROXY_IGNORE_RESULT
elseif inj.id == 3 then
if err_flag == true then
for row in res.rows do
proxy.response.type = proxy.MYSQLD_PACKET_ERR
proxy.response.errmsg = row[3]
insert_query(row[1], row[2], row[3])
end
return proxy.PROXY_SEND_RESULT
end
return proxy.PROXY_IGNORE_RESULT
elseif inj.id == 4 then
return proxy.PROXY_IGNORE_RESULT
end
end

0 comments on commit 920ddf6

Please sign in to comment.