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
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_handler.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;
33
static char* sysvar_logging_gearman_function= NULL;
35
static gearman_client_st gearman_client;
38
/* stolen from mysys/my_getsystime
39
until the Session has a good utime "now" we can use
40
will have to use this instead */
43
static uint64_t get_microtime()
45
#if defined(HAVE_GETHRTIME)
46
return gethrtime()/1000;
51
The following loop is here because gettimeofday may fail on some systems
53
while (gettimeofday(&t, NULL) != 0) {}
54
newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
56
#endif /* defined(HAVE_GETHRTIME) */
59
/* quote a string to be safe to include in a CSV line
60
that means backslash quoting all commas, doublequotes, backslashes,
61
and all the ASCII unprintable characters
62
as long as we pass the high-bit bytes unchanged
63
this is safe to do to a UTF8 string
64
we dont allow overrunning the targetbuffer
65
to avoid having a very long query overwrite memory
67
TODO consider remapping the unprintables instead to "Printable
68
Representation", the Unicode characters from the area U+2400 to
69
U+2421 reserved for representing control characters when it is
70
necessary to print or display them rather than have them perform
71
their intended function.
74
static unsigned char *quotify (const unsigned char *src, size_t srclen,
75
unsigned char *dst, size_t dstlen)
77
static const char hexit[]= { '0', '1', '2', '3', '4', '5', '6', '7',
78
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
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++)
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];
158
else // everything else
160
dst[dst_ndx++]= src[src_ndx];
167
class LoggingGearman : public Logging_handler
170
LoggingGearman() : Logging_handler("LoggingGearman") {}
172
virtual bool post(Session *session)
174
char msgbuf[MAX_MSG_LEN];
177
assert(session != NULL);
179
if (sysvar_logging_gearman_enable == false)
182
/* TODO, looks like connect_utime isnt being set in the session
183
object. We could store the time this plugin was loaded, but that
184
would just be a dumb workaround. */
185
/* TODO, the session object should have a "utime command completed"
186
inside itself, so be more accurate, and so this doesnt have to
187
keep calling current_utime, which can be slow */
189
uint64_t t_mark= get_microtime();
191
// buffer to quotify the query
192
unsigned char qs[255];
194
// to avoid trying to printf %s something that is potentially NULL
195
const char *dbs= (session->db) ? session->db : "";
198
dbl= session->db_length;
200
// todo, add hostname, listener port, and server id to this
203
snprintf(msgbuf, MAX_MSG_LEN,
204
"%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
205
"%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"",
209
// dont need to quote the db name, always CSV safe
211
// do need to quote the query
212
quotify((unsigned char *)session->query,
213
session->query_length, qs, sizeof(qs)),
214
// command_name is defined in drizzled/sql_parse.cc
215
// dont need to quote the command name, always CSV safe
216
(int)command_name[session->command].length,
217
command_name[session->command].str,
218
// counters are at end, to make it easier to add more
219
(t_mark - session->connect_utime),
220
(t_mark - session->start_utime),
221
(t_mark - session->utime_after_lock),
222
session->sent_row_count,
223
session->examined_row_count);
225
char job_handle[GEARMAN_JOB_HANDLE_SIZE];
227
(void) gearman_client_do_background(&gearman_client,
228
sysvar_logging_gearman_function,
238
static Logging_handler *handler= NULL;
240
static int logging_gearman_plugin_init(PluginRegistry ®istry)
242
gearman_return_t ret;
245
saying "return 0" means "success"
246
right now, if we return an error
247
this causes Drizzle to crash
248
so until that is fixed,
249
just return a success,
250
but leave the function pointers as NULL
253
if (sysvar_logging_gearman_host == NULL)
255
/* no destination gearman server host was specified via system variables
256
return now, dont set the callback pointers
261
if (gearman_client_create(&gearman_client) == NULL)
263
errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
268
/* TODO, be able to override the port */
269
ret= gearman_client_add_server(&gearman_client,
270
sysvar_logging_gearman_host, 0);
271
if (ret != GEARMAN_SUCCESS)
273
errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
274
gearman_client_error(&gearman_client));
278
handler= new LoggingGearman();
279
registry.add(handler);
284
static int logging_gearman_plugin_deinit(PluginRegistry ®istry)
287
gearman_client_free(&gearman_client);
289
registry.remove(handler);
295
static DRIZZLE_SYSVAR_BOOL(
297
sysvar_logging_gearman_enable,
299
N_("Enable logging to a gearman server"),
300
NULL, /* check func */
301
NULL, /* update func */
302
false /* default */);
304
static DRIZZLE_SYSVAR_STR(
306
sysvar_logging_gearman_host,
308
N_("Hostname for logging to a Gearman server"),
309
NULL, /* check func */
310
NULL, /* update func*/
311
"localhost" /* default */);
313
static DRIZZLE_SYSVAR_STR(
315
sysvar_logging_gearman_function,
317
N_("Gearman Function to send logging to"),
318
NULL, /* check func */
319
NULL, /* update func*/
320
"drizzlelog" /* default */);
322
static struct st_mysql_sys_var* logging_gearman_system_variables[]= {
323
DRIZZLE_SYSVAR(enable),
324
DRIZZLE_SYSVAR(host),
325
DRIZZLE_SYSVAR(function),
329
drizzle_declare_plugin(logging_gearman)
333
"Mark Atwood <mark@fallenpegasus.com>",
334
N_("Log queries to a Gearman server"),
336
logging_gearman_plugin_init,
337
logging_gearman_plugin_deinit,
338
NULL, /* status variables */
339
logging_gearman_system_variables,
342
drizzle_declare_plugin_end;