1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008,2009 Mark Atwood
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
20
#include <drizzled/server_includes.h>
21
#include <drizzled/plugin_logging.h>
22
#include <drizzled/gettext.h>
23
#include <drizzled/session.h>
25
#include <libgearman/gearman.h>
28
/* TODO make this dynamic as needed */
29
static const int MAX_MSG_LEN= 32*1024;
31
static bool sysvar_logging_gearman_enable= false;
32
static char* sysvar_logging_gearman_host= NULL;
34
static gearman_client_st gearman_client;
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 */
42
static uint64_t get_microtime()
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) */
58
/* quote a string to be safe to include in a CSV line
59
that means backslash quoting all commas, doublequotes, backslashes,
60
and all the ASCII unprintable characters
61
as long as we pass the high-bit bytes unchanged
62
this is safe to do to a UTF8 string
63
we dont allow overrunning the targetbuffer
64
to avoid having a very long query overwrite memory
66
TODO consider remapping the unprintables instead to "Printable
67
Representation", the Unicode characters from the area U+2400 to
68
U+2421 reserved for representing control characters when it is
69
necessary to print or display them rather than have them perform
70
their intended function.
73
static unsigned char *quotify (const unsigned char *src, size_t srclen,
74
unsigned char *dst, size_t dstlen)
76
static const char hexit[]= { '0', '1', '2', '3', '4', '5', '6', '7',
77
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
78
size_t dst_ndx; /* ndx down the dst */
79
size_t src_ndx; /* ndx down the src */
84
for (dst_ndx= 0,src_ndx= 0; src_ndx < srclen; src_ndx++)
87
/* Worst case, need 5 dst bytes for the next src byte.
88
backslash x hexit hexit null
89
so if not enough room, just terminate the string and return
91
if ((dstlen - dst_ndx) < 5)
93
dst[dst_ndx]= (unsigned char)0x00;
97
if (src[src_ndx] > 0x7f)
99
// pass thru high bit characters, they are non-ASCII UTF8 Unicode
100
dst[dst_ndx++]= src[src_ndx];
102
else if (src[src_ndx] == 0x00) // null
104
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) '0';
106
else if (src[src_ndx] == 0x07) // bell
108
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'a';
110
else if (src[src_ndx] == 0x08) // backspace
112
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'b';
114
else if (src[src_ndx] == 0x09) // horiz tab
116
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 't';
118
else if (src[src_ndx] == 0x0a) // line feed
120
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'n';
122
else if (src[src_ndx] == 0x0b) // vert tab
124
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'v';
126
else if (src[src_ndx] == 0x0c) // formfeed
128
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'f';
130
else if (src[src_ndx] == 0x0d) // carrage return
132
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'r';
134
else if (src[src_ndx] == 0x1b) // escape
136
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'e';
138
else if (src[src_ndx] == 0x22) // quotation mark
140
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x22;
142
else if (src[src_ndx] == 0x2C) // comma
144
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x2C;
146
else if (src[src_ndx] == 0x5C) // backslash
148
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x5C;
150
else if ((src[src_ndx] < 0x20) || (src[src_ndx] == 0x7F)) // other unprintable ASCII
152
dst[dst_ndx++]= 0x5C;
153
dst[dst_ndx++]= (unsigned char) 'x';
154
dst[dst_ndx++]= hexit[(src[src_ndx] >> 4) & 0x0f];
155
dst[dst_ndx++]= hexit[src[src_ndx] & 0x0f];
157
else // everything else
159
dst[dst_ndx++]= src[src_ndx];
166
bool logging_gearman_func_post (Session *session)
168
char msgbuf[MAX_MSG_LEN];
171
assert(session != NULL);
173
if (sysvar_logging_gearman_enable == false)
176
// logging this is far too verbose
177
if (session->command == COM_FIELD_LIST)
180
/* TODO, looks like connect_utime isnt being set in the session
181
object. We could store the time this plugin was loaded, but that
182
would just be a dumb workaround. */
183
/* TODO, the session object should have a "utime command completed"
184
inside itself, so be more accurate, and so this doesnt have to
185
keep calling current_utime, which can be slow */
187
uint64_t t_mark= get_microtime();
189
// buffer to quotify the query
190
unsigned char qs[255];
192
// to avoid trying to printf %s something that is potentially NULL
193
const char *dbs= (session->db) ? session->db : "";
196
dbl= session->db_length;
198
// todo, add hostname, listener port, and server id to this
201
snprintf(msgbuf, MAX_MSG_LEN,
202
"%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
203
"%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"",
207
// dont need to quote the db name, always CSV safe
209
// do need to quote the query
210
quotify((unsigned char *)session->query,
211
session->query_length, qs, sizeof(qs)),
212
// command_name is defined in drizzled/sql_parse.cc
213
// dont need to quote the command name, always CSV safe
214
(int)command_name[session->command].length,
215
command_name[session->command].str,
216
// counters are at end, to make it easier to add more
217
(t_mark - session->connect_utime),
218
(t_mark - session->start_utime),
219
(t_mark - session->utime_after_lock),
220
session->sent_row_count,
221
session->examined_row_count);
223
char job_handle[GEARMAN_JOB_HANDLE_SIZE];
225
(void) gearman_client_do_background(&gearman_client,
235
static int logging_gearman_plugin_init(void *p)
237
logging_t *l= static_cast<logging_t *>(p);
239
gearman_return_t ret;
242
saying "return 0" means "success"
243
right now, if we return an error
244
this causes Drizzle to crash
245
so until that is fixed,
246
just return a success,
247
but leave the function pointers as NULL
250
if (sysvar_logging_gearman_host == NULL)
252
/* no destination gearman server host was specified via system variables
253
return now, dont set the callback pointers
258
if (gearman_client_create(&gearman_client) == NULL)
260
errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
265
/* TODO, be able to override the port */
266
ret= gearman_client_add_server(&gearman_client,
267
sysvar_logging_gearman_host, 0);
268
if (ret != GEARMAN_SUCCESS)
270
errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
271
gearman_client_error(&gearman_client));
275
l->logging_pre= NULL;
276
l->logging_post= logging_gearman_func_post;
281
static int logging_gearman_plugin_deinit(void *p)
283
logging_st *l= (logging_st *) p;
285
gearman_client_free(&gearman_client);
287
l->logging_pre= NULL;
288
l->logging_post= NULL;
293
static DRIZZLE_SYSVAR_BOOL(
295
sysvar_logging_gearman_enable,
297
N_("Enable logging to a gearman server"),
298
NULL, /* check func */
299
NULL, /* update func */
300
false /* default */);
302
static DRIZZLE_SYSVAR_STR(
304
sysvar_logging_gearman_host,
306
N_("Hostname for logging to a Gearman server"),
307
NULL, /* check func */
308
NULL, /* update func*/
311
static struct st_mysql_sys_var* logging_gearman_system_variables[]= {
312
DRIZZLE_SYSVAR(enable),
313
DRIZZLE_SYSVAR(host),
317
drizzle_declare_plugin(logging_gearman)
319
DRIZZLE_LOGGER_PLUGIN,
322
"Mark Atwood <mark@fallenpegasus.com>",
323
N_("Log queries to a Gearman server"),
325
logging_gearman_plugin_init,
326
logging_gearman_plugin_deinit,
327
NULL, /* status variables */
328
logging_gearman_system_variables,
331
drizzle_declare_plugin_end;