17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
#include <drizzled/plugin/logging.h>
20
#include <drizzled/server_includes.h>
21
#include <drizzled/plugin/logging_handler.h>
22
22
#include <drizzled/gettext.h>
23
23
#include <drizzled/session.h>
25
/* TODO make this dynamic as needed */
26
static const int MAX_MSG_LEN= 32*1024;
28
static bool sysvar_logging_query_enable= false;
29
static char* sysvar_logging_query_filename= NULL;
30
/* TODO fix these to not be unsigned long one we have sensible sys_var system */
31
static unsigned long sysvar_logging_query_threshold_slow= 0;
32
static unsigned long sysvar_logging_query_threshold_big_resultset= 0;
33
static unsigned long sysvar_logging_query_threshold_big_examined= 0;
37
/* stolen from mysys/my_getsystime
38
until the Session has a good utime "now" we can use
39
will have to use this instead */
26
41
#include <sys/time.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
42
static uint64_t get_microtime()
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;
44
#if defined(HAVE_GETHRTIME)
45
return gethrtime()/1000;
50
The following loop is here because gettimeofday may fail on some systems
52
while (gettimeofday(&t, NULL) != 0) {}
53
newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
55
#endif /* defined(HAVE_GETHRTIME) */
53
58
/* quote a string to be safe to include in a CSV line
54
59
that means backslash quoting all commas, doublequotes, backslashes,
69
static void quotify(const string &src, string &dst)
74
static unsigned char *quotify (const unsigned char *src, size_t srclen,
75
unsigned char *dst, size_t dstlen)
71
77
static const char hexit[]= { '0', '1', '2', '3', '4', '5', '6', '7',
72
78
'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)
79
size_t dst_ndx; /* ndx down the dst */
80
size_t src_ndx; /* ndx down the src */
85
for (dst_ndx= 0,src_ndx= 0; src_ndx < srclen; src_ndx++)
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]);
88
/* Worst case, need 5 dst bytes for the next src byte.
89
backslash x hexit hexit null
90
so if not enough room, just terminate the string and return
92
if ((dstlen - dst_ndx) < 5)
94
dst[dst_ndx]= (unsigned char)0x00;
98
if (src[src_ndx] > 0x7f)
100
// pass thru high bit characters, they are non-ASCII UTF8 Unicode
101
dst[dst_ndx++]= src[src_ndx];
103
else if (src[src_ndx] == 0x00) // null
105
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) '0';
107
else if (src[src_ndx] == 0x07) // bell
109
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'a';
111
else if (src[src_ndx] == 0x08) // backspace
113
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'b';
115
else if (src[src_ndx] == 0x09) // horiz tab
117
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 't';
119
else if (src[src_ndx] == 0x0a) // line feed
121
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'n';
123
else if (src[src_ndx] == 0x0b) // vert tab
125
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'v';
127
else if (src[src_ndx] == 0x0c) // formfeed
129
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'f';
131
else if (src[src_ndx] == 0x0d) // carrage return
133
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'r';
135
else if (src[src_ndx] == 0x1b) // escape
137
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'e';
139
else if (src[src_ndx] == 0x22) // quotation mark
141
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x22;
143
else if (src[src_ndx] == 0x2C) // comma
145
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x2C;
147
else if (src[src_ndx] == 0x5C) // backslash
149
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x5C;
151
else if ((src[src_ndx] < 0x20) || (src[src_ndx] == 0x7F)) // other unprintable ASCII
153
dst[dst_ndx++]= 0x5C;
154
dst[dst_ndx++]= (unsigned char) 'x';
155
dst[dst_ndx++]= hexit[(src[src_ndx] >> 4) & 0x0f];
156
dst[dst_ndx++]= hexit[src[src_ndx] & 0x0f];
136
158
else // everything else
138
dst.push_back(*src_iter);
160
dst[dst_ndx++]= src[src_ndx];
144
class Logging_query: public drizzled::plugin::Logging
168
/* we could just not have a pre entrypoint at all,
169
and have logging_pre == NULL
170
but we have this here for the sake of being an example */
171
class Logging_query: public Logging_handler
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,
176
char errmsg[STRERROR_MAX];
177
strerror_r(errno, errmsg, sizeof(errmsg));
178
errmsg_printf(ERRMSG_LVL_ERROR, _("fail open() fn=%s er=%s\n"),
184
if (not _query_pcre.empty())
186
const char *this_pcre_error;
187
int this_pcre_erroffset;
188
re= pcre_compile(_query_pcre.c_str(), 0, &this_pcre_error,
189
&this_pcre_erroffset, NULL);
190
pe= pcre_study(re, 0, &this_pcre_error);
191
/* TODO emit error messages if there is a problem */
173
virtual bool pre (Session *)
213
178
virtual bool post (Session *session)
180
char msgbuf[MAX_MSG_LEN];
217
184
assert(session != NULL);
229
196
// return if not enabled or query was too fast or resultset was too small
230
197
if (sysvar_logging_query_enable == false)
232
if (session->sent_row_count < sysvar_logging_query_threshold_big_resultset.get())
199
if (session->sent_row_count < sysvar_logging_query_threshold_big_resultset)
234
if (session->examined_row_count < sysvar_logging_query_threshold_big_examined.get())
201
if (session->examined_row_count < sysvar_logging_query_threshold_big_examined)
204
/* TODO, looks like connect_utime isnt being set in the session
205
object. We could store the time this plugin was loaded, but that
206
would just be a dumb workaround. */
237
207
/* TODO, the session object should have a "utime command completed"
238
208
inside itself, so be more accurate, and so this doesnt have to
239
209
keep calling current_utime, which can be slow */
241
boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
242
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
243
uint64_t t_mark= (mytime-epoch).total_microseconds();
245
if ((t_mark - session->start_utime) < (sysvar_logging_query_threshold_slow.get()))
211
uint64_t t_mark= get_microtime();
213
if ((t_mark - session->start_utime) < (sysvar_logging_query_threshold_slow))
248
Session::QueryString query_string(session->getQueryString());
252
this_pcre_rc= pcre_exec(re, pe, query_string->c_str(), query_string->length(), 0, 0, NULL, 0);
253
if (this_pcre_rc < 0)
257
216
// buffer to quotify the query
260
// Since quotify() builds the quoted string incrementally, we can
261
// avoid some reallocating if we reserve some space up front.
262
qs.reserve(query_string->length());
264
quotify(*query_string, qs);
217
unsigned char qs[255];
266
219
// to avoid trying to printf %s something that is potentially NULL
267
util::string::const_shared_ptr schema(session->schema());
268
const char *dbs= (schema and not schema->empty()) ? schema->c_str() : "";
272
% session->getQueryId()
275
% command_name[session->command].str
276
% (t_mark - session->getConnectMicroseconds())
277
% (t_mark - session->start_utime)
278
% (t_mark - session->utime_after_lock)
279
% session->sent_row_count
280
% session->examined_row_count
282
% session->total_warn_count
283
% session->getServerId()
286
string msgbuf= formatter.str();
220
const char *dbs= (session->db) ? session->db : "";
223
dbl= session->db_length;
226
snprintf(msgbuf, MAX_MSG_LEN,
227
"%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
228
"%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"\n",
232
// dont need to quote the db name, always CSV safe
234
// do need to quote the query
235
quotify((unsigned char *)session->query,
236
session->query_length, qs, sizeof(qs)),
237
// command_name is defined in drizzled/sql_parse.cc
238
// dont need to quote the command name, always CSV safe
239
(int)command_name[session->command].length,
240
command_name[session->command].str,
241
// counters are at end, to make it easier to add more
242
(t_mark - session->connect_utime),
243
(t_mark - session->start_utime),
244
(t_mark - session->utime_after_lock),
245
session->sent_row_count,
246
session->examined_row_count);
288
249
// a single write has a kernel thread lock, thus no need mutex guard this
289
wrv= write(fd, msgbuf.c_str(), msgbuf.length());
290
assert(wrv == msgbuf.length());
250
wrv= write(fd, msgbuf, msgbuf_len);
251
assert(wrv == msgbuf_len);
296
static int logging_query_plugin_init(drizzled::module::Context &context)
299
const module::option_map &vm= context.getOptions();
301
if (vm.count("filename") > 0)
303
context.add(new Logging_query(vm["filename"].as<string>(),
304
vm["pcre"].as<string>()));
305
context.registerVariable(new sys_var_bool_ptr("enable", &sysvar_logging_query_enable));
306
context.registerVariable(new sys_var_const_string_val("filename", vm["filename"].as<string>()));
307
context.registerVariable(new sys_var_const_string_val("pcre", vm["pcre"].as<string>()));
308
context.registerVariable(new sys_var_constrained_value<uint32_t>("threshold_slow", sysvar_logging_query_threshold_slow));
309
context.registerVariable(new sys_var_constrained_value<uint32_t>("threshold_big_resultset", sysvar_logging_query_threshold_big_resultset));
310
context.registerVariable(new sys_var_constrained_value<uint32_t>("threshold_big_examined", sysvar_logging_query_threshold_big_examined));
316
static void init_options(drizzled::module::option_context &context)
319
po::value<bool>(&sysvar_logging_query_enable)->default_value(false)->zero_tokens(),
320
N_("Enable logging to CSV file"));
323
N_("File to log to"));
325
po::value<string>()->default_value(""),
326
N_("PCRE to match the query against"));
327
context("threshold-slow",
328
po::value<uint32_constraint>(&sysvar_logging_query_threshold_slow)->default_value(0),
329
N_("Threshold for logging slow queries, in microseconds"));
330
context("threshold-big-resultset",
331
po::value<uint32_constraint>(&sysvar_logging_query_threshold_big_resultset)->default_value(0),
332
N_("Threshold for logging big queries, for rows returned"));
333
context("threshold-big-examined",
334
po::value<uint32_constraint>(&sysvar_logging_query_threshold_big_examined)->default_value(0),
335
N_("Threshold for logging big queries, for rows examined"));
338
} /* namespace drizzle_plugin */
340
DRIZZLE_DECLARE_PLUGIN
257
static int logging_query_plugin_init(void *p)
259
Logging_handler **handler= static_cast<Logging_handler **>(p);
262
if (sysvar_logging_query_filename == NULL)
264
/* no destination filename was specified via system variables
265
return now, dont set the callback pointers
270
fd= open(sysvar_logging_query_filename,
271
O_WRONLY | O_APPEND | O_CREAT,
275
errmsg_printf(ERRMSG_LVL_ERROR, _("fail open() fn=%s er=%s\n"),
276
sysvar_logging_query_filename,
280
we should return an error here, so the plugin doesnt load
281
but this causes Drizzle to crash
282
so until that is fixed,
283
just return a success,
284
but leave the function pointers as NULL and the fd as -1
289
*handler= new Logging_query();
294
static int logging_query_plugin_deinit(void *p)
296
Logging_query *handler= static_cast<Logging_query *>(p);
309
static DRIZZLE_SYSVAR_BOOL(
311
sysvar_logging_query_enable,
313
N_("Enable logging to CSV file"),
314
NULL, /* check func */
315
NULL, /* update func */
316
false /* default */);
318
static DRIZZLE_SYSVAR_STR(
320
sysvar_logging_query_filename,
322
N_("File to log to"),
323
NULL, /* check func */
324
NULL, /* update func*/
327
static DRIZZLE_SYSVAR_ULONG(
329
sysvar_logging_query_threshold_slow,
331
N_("Threshold for logging slow queries, in microseconds"),
332
NULL, /* check func */
333
NULL, /* update func */
336
UINT32_MAX, /* max */
339
static DRIZZLE_SYSVAR_ULONG(
340
threshold_big_resultset,
341
sysvar_logging_query_threshold_big_resultset,
343
N_("Threshold for logging big queries, for rows returned"),
344
NULL, /* check func */
345
NULL, /* update func */
348
UINT32_MAX, /* max */
351
static DRIZZLE_SYSVAR_ULONG(
352
threshold_big_examined,
353
sysvar_logging_query_threshold_big_examined,
355
N_("Threshold for logging big queries, for rows examined"),
356
NULL, /* check func */
357
NULL, /* update func */
360
UINT32_MAX, /* max */
363
static struct st_mysql_sys_var* logging_query_system_variables[]= {
364
DRIZZLE_SYSVAR(enable),
365
DRIZZLE_SYSVAR(filename),
366
DRIZZLE_SYSVAR(threshold_slow),
367
DRIZZLE_SYSVAR(threshold_big_resultset),
368
DRIZZLE_SYSVAR(threshold_big_examined),
372
drizzle_declare_plugin(logging_query)
374
DRIZZLE_LOGGER_PLUGIN,
345
377
"Mark Atwood <mark@fallenpegasus.com>",
346
378
N_("Log queries to a CSV file"),
347
379
PLUGIN_LICENSE_GPL,
348
drizzle_plugin::logging_query_plugin_init,
350
drizzle_plugin::init_options
380
logging_query_plugin_init,
381
logging_query_plugin_deinit,
382
NULL, /* status variables */
383
logging_query_system_variables,
352
DRIZZLE_DECLARE_PLUGIN_END;
386
drizzle_declare_plugin_end;