1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008, 2009 Sun Microsystems, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
#include <drizzled/plugin/logging.h>
22
#include <drizzled/gettext.h>
23
#include <drizzled/session.h>
27
#include <sys/types.h>
31
#include <boost/format.hpp>
32
#include <boost/program_options.hpp>
33
#include <drizzled/module/option_map.h>
37
namespace po= boost::program_options;
38
using namespace drizzled;
41
#define ESCAPE_CHAR '\\'
42
#define SEPARATOR_CHAR ','
44
namespace drizzle_plugin
47
static bool sysvar_logging_query_enable= false;
48
/* TODO fix these to not be unsigned long once we have sensible sys_var system */
49
static uint32_constraint sysvar_logging_query_threshold_slow;
50
static uint32_constraint sysvar_logging_query_threshold_big_resultset;
51
static uint32_constraint sysvar_logging_query_threshold_big_examined;
53
/* quote a string to be safe to include in a CSV line
54
that means backslash quoting all commas, doublequotes, backslashes,
55
and all the ASCII unprintable characters
56
as long as we pass the high-bit bytes unchanged
57
this is safe to do to a UTF8 string
58
we dont allow overrunning the targetbuffer
59
to avoid having a very long query overwrite memory
61
TODO consider remapping the unprintables instead to "Printable
62
Representation", the Unicode characters from the area U+2400 to
63
U+2421 reserved for representing control characters when it is
64
necessary to print or display them rather than have them perform
65
their intended function.
69
static void quotify(const string &src, string &dst)
71
static const char hexit[]= { '0', '1', '2', '3', '4', '5', '6', '7',
72
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
73
string::const_iterator src_iter;
75
for (src_iter= src.begin(); src_iter < src.end(); ++src_iter)
77
if (static_cast<unsigned char>(*src_iter) > 0x7f)
79
dst.push_back(*src_iter);
81
else if (*src_iter == 0x00) // null
83
dst.push_back(ESCAPE_CHAR); dst.push_back('0');
85
else if (*src_iter == 0x07) // bell
87
dst.push_back(ESCAPE_CHAR); dst.push_back('a');
89
else if (*src_iter == 0x08) // backspace
91
dst.push_back(ESCAPE_CHAR); dst.push_back('b');
93
else if (*src_iter == 0x09) // horiz tab
95
dst.push_back(ESCAPE_CHAR); dst.push_back('t');
97
else if (*src_iter == 0x0a) // line feed
99
dst.push_back(ESCAPE_CHAR); dst.push_back('n');
101
else if (*src_iter == 0x0b) // vert tab
103
dst.push_back(ESCAPE_CHAR); dst.push_back('v');
105
else if (*src_iter == 0x0c) // formfeed
107
dst.push_back(ESCAPE_CHAR); dst.push_back('f');
109
else if (*src_iter == 0x0d) // carrage return
111
dst.push_back(ESCAPE_CHAR); dst.push_back('r');
113
else if (*src_iter == 0x1b) // escape
115
dst.push_back(ESCAPE_CHAR); dst.push_back('e');
117
else if (*src_iter == 0x22) // quotation mark
119
dst.push_back(ESCAPE_CHAR); dst.push_back(0x22);
121
else if (*src_iter == SEPARATOR_CHAR)
123
dst.push_back(ESCAPE_CHAR); dst.push_back(SEPARATOR_CHAR);
125
else if (*src_iter == ESCAPE_CHAR)
127
dst.push_back(ESCAPE_CHAR); dst.push_back(ESCAPE_CHAR);
129
else if ((*src_iter < 0x20) || (*src_iter == 0x7F)) // other unprintable ASCII
131
dst.push_back(ESCAPE_CHAR);
133
dst.push_back(hexit[(*src_iter >> 4) & 0x0f]);
134
dst.push_back(hexit[*src_iter & 0x0f]);
136
else // everything else
138
dst.push_back(*src_iter);
144
class Logging_query: public drizzled::plugin::Logging
146
const std::string _filename;
147
const std::string _query_pcre;
152
/** Format of the output string */
153
boost::format formatter;
157
Logging_query(const std::string &filename,
158
const std::string &query_pcre) :
159
drizzled::plugin::Logging("Logging_query"),
161
_query_pcre(query_pcre),
162
fd(-1), re(NULL), pe(NULL),
163
formatter("%1%,%2%,%3%,\"%4%\",\"%5%\",\"%6%\",%7%,%8%,"
164
"%9%,%10%,%11%,%12%,%13%,%14%,\"%15%\"\n")
167
/* if there is no destination filename, dont bother doing anything */
168
if (_filename.empty())
171
fd= open(_filename.c_str(),
172
O_WRONLY | O_APPEND | O_CREAT,
177
sql_perror( _("fail open()"), _filename);
181
if (not _query_pcre.empty())
183
const char *this_pcre_error;
184
int this_pcre_erroffset;
185
re= pcre_compile(_query_pcre.c_str(), 0, &this_pcre_error,
186
&this_pcre_erroffset, NULL);
187
pe= pcre_study(re, 0, &this_pcre_error);
188
/* TODO emit error messages if there is a problem */
210
virtual bool post (Session *session)
214
assert(session != NULL);
219
/* Yes, we know that checking sysvar_logging_query_enable,
220
sysvar_logging_query_threshold_big_resultset, and
221
sysvar_logging_query_threshold_big_examined is not threadsafe,
222
because some other thread might change these sysvars. But we
223
don't care. We might start logging a little late as it spreads
224
to other threads. Big deal. */
226
// return if not enabled or query was too fast or resultset was too small
227
if (sysvar_logging_query_enable == false)
229
if (session->sent_row_count < sysvar_logging_query_threshold_big_resultset.get())
231
if (session->examined_row_count < sysvar_logging_query_threshold_big_examined.get())
235
TODO, the session object should have a "utime command completed"
236
inside itself, so be more accurate, and so this doesnt have to
237
keep calling current_utime, which can be slow.
239
uint64_t t_mark= session->getCurrentTimestamp(false);
241
if (session->getElapsedTime() < (sysvar_logging_query_threshold_slow.get()))
244
Session::QueryString query_string(session->getQueryString());
248
this_pcre_rc= pcre_exec(re, pe, query_string->c_str(), query_string->length(), 0, 0, NULL, 0);
249
if (this_pcre_rc < 0)
253
// buffer to quotify the query
256
// Since quotify() builds the quoted string incrementally, we can
257
// avoid some reallocating if we reserve some space up front.
258
qs.reserve(query_string->length());
260
quotify(*query_string, qs);
262
// to avoid trying to printf %s something that is potentially NULL
263
util::string::const_shared_ptr schema(session->schema());
264
const char *dbs= (schema and not schema->empty()) ? schema->c_str() : "";
268
% session->getQueryId()
271
% getCommandName(session->command)
272
% (t_mark - session->getConnectMicroseconds())
273
% session->getElapsedTime()
274
% (t_mark - session->utime_after_lock)
275
% session->sent_row_count
276
% session->examined_row_count
278
% session->total_warn_count
279
% session->getServerId()
282
string msgbuf= formatter.str();
284
// a single write has a kernel thread lock, thus no need mutex guard this
285
wrv= write(fd, msgbuf.c_str(), msgbuf.length());
286
assert(wrv == msgbuf.length());
292
static int logging_query_plugin_init(drizzled::module::Context &context)
295
const module::option_map &vm= context.getOptions();
297
if (vm.count("filename") > 0)
299
context.add(new Logging_query(vm["filename"].as<string>(),
300
vm["pcre"].as<string>()));
301
context.registerVariable(new sys_var_bool_ptr("enable", &sysvar_logging_query_enable));
302
context.registerVariable(new sys_var_const_string_val("filename", vm["filename"].as<string>()));
303
context.registerVariable(new sys_var_const_string_val("pcre", vm["pcre"].as<string>()));
304
context.registerVariable(new sys_var_constrained_value<uint32_t>("threshold_slow", sysvar_logging_query_threshold_slow));
305
context.registerVariable(new sys_var_constrained_value<uint32_t>("threshold_big_resultset", sysvar_logging_query_threshold_big_resultset));
306
context.registerVariable(new sys_var_constrained_value<uint32_t>("threshold_big_examined", sysvar_logging_query_threshold_big_examined));
312
static void init_options(drizzled::module::option_context &context)
315
po::value<bool>(&sysvar_logging_query_enable)->default_value(false)->zero_tokens(),
316
_("Enable logging to CSV file"));
319
_("File to log to"));
321
po::value<string>()->default_value(""),
322
_("PCRE to match the query against"));
323
context("threshold-slow",
324
po::value<uint32_constraint>(&sysvar_logging_query_threshold_slow)->default_value(0),
325
_("Threshold for logging slow queries, in microseconds"));
326
context("threshold-big-resultset",
327
po::value<uint32_constraint>(&sysvar_logging_query_threshold_big_resultset)->default_value(0),
328
_("Threshold for logging big queries, for rows returned"));
329
context("threshold-big-examined",
330
po::value<uint32_constraint>(&sysvar_logging_query_threshold_big_examined)->default_value(0),
331
_("Threshold for logging big queries, for rows examined"));
334
} /* namespace drizzle_plugin */
336
DRIZZLE_DECLARE_PLUGIN
341
"Mark Atwood <mark@fallenpegasus.com>",
342
N_("Log queries to a CSV file"),
344
drizzle_plugin::logging_query_plugin_init,
346
drizzle_plugin::init_options
348
DRIZZLE_DECLARE_PLUGIN_END;