From 920ddf6cb7b6b4f6e10134a66e969cd73148b990 Mon Sep 17 00:00:00 2001 From: kon Date: Thu, 19 Apr 2012 09:35:46 +0200 Subject: [PATCH] Added script and docs --- README.md | 54 +++++++++++++++++++++++++++- mysql-proxy-log-error-queries.lua | 60 +++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 mysql-proxy-log-error-queries.lua diff --git a/README.md b/README.md index 579044a..10accea 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,56 @@ 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 \ No newline at end of file diff --git a/mysql-proxy-log-error-queries.lua b/mysql-proxy-log-error-queries.lua new file mode 100644 index 0000000..a1be463 --- /dev/null +++ b/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