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
21
#include <drizzled/plugin/logging.h>
22
#include <drizzled/gettext.h>
23
#include <drizzled/session.h>
25
#include <libgearman/gearman.h>
28
#include <sys/types.h>
33
using namespace drizzled;
36
/* TODO make this dynamic as needed */
37
static const int MAX_MSG_LEN= 32*1024;
39
static bool sysvar_logging_gearman_enable= false;
40
static char* sysvar_logging_gearman_host= NULL;
41
static char* sysvar_logging_gearman_function= NULL;
44
/* stolen from mysys/my_getsystime
45
until the Session has a good utime "now" we can use
46
will have to use this instead */
48
static uint64_t get_microtime()
50
#if defined(HAVE_GETHRTIME)
51
return gethrtime()/1000;
56
The following loop is here because gettimeofday may fail on some systems
58
while (gettimeofday(&t, NULL) != 0) {}
59
newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
61
#endif /* defined(HAVE_GETHRTIME) */
64
/* quote a string to be safe to include in a CSV line
65
that means backslash quoting all commas, doublequotes, backslashes,
66
and all the ASCII unprintable characters
67
as long as we pass the high-bit bytes unchanged
68
this is safe to do to a UTF8 string
69
we dont allow overrunning the targetbuffer
70
to avoid having a very long query overwrite memory
72
TODO consider remapping the unprintables instead to "Printable
73
Representation", the Unicode characters from the area U+2400 to
74
U+2421 reserved for representing control characters when it is
75
necessary to print or display them rather than have them perform
76
their intended function.
79
static unsigned char *quotify (const unsigned char *src, size_t srclen,
80
unsigned char *dst, size_t dstlen)
82
static const char hexit[]= { '0', '1', '2', '3', '4', '5', '6', '7',
83
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
84
size_t dst_ndx; /* ndx down the dst */
85
size_t src_ndx; /* ndx down the src */
90
for (dst_ndx= 0,src_ndx= 0; src_ndx < srclen; src_ndx++)
93
/* Worst case, need 5 dst bytes for the next src byte.
94
backslash x hexit hexit null
95
so if not enough room, just terminate the string and return
97
if ((dstlen - dst_ndx) < 5)
99
dst[dst_ndx]= (unsigned char)0x00;
103
if (src[src_ndx] > 0x7f)
105
// pass thru high bit characters, they are non-ASCII UTF8 Unicode
106
dst[dst_ndx++]= src[src_ndx];
108
else if (src[src_ndx] == 0x00) // null
110
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) '0';
112
else if (src[src_ndx] == 0x07) // bell
114
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'a';
116
else if (src[src_ndx] == 0x08) // backspace
118
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'b';
120
else if (src[src_ndx] == 0x09) // horiz tab
122
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 't';
124
else if (src[src_ndx] == 0x0a) // line feed
126
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'n';
128
else if (src[src_ndx] == 0x0b) // vert tab
130
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'v';
132
else if (src[src_ndx] == 0x0c) // formfeed
134
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'f';
136
else if (src[src_ndx] == 0x0d) // carrage return
138
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'r';
140
else if (src[src_ndx] == 0x1b) // escape
142
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'e';
144
else if (src[src_ndx] == 0x22) // quotation mark
146
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x22;
148
else if (src[src_ndx] == 0x2C) // comma
150
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x2C;
152
else if (src[src_ndx] == 0x5C) // backslash
154
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x5C;
156
else if ((src[src_ndx] < 0x20) || (src[src_ndx] == 0x7F)) // other unprintable ASCII
158
dst[dst_ndx++]= 0x5C;
159
dst[dst_ndx++]= (unsigned char) 'x';
160
dst[dst_ndx++]= hexit[(src[src_ndx] >> 4) & 0x0f];
161
dst[dst_ndx++]= hexit[src[src_ndx] & 0x0f];
163
else // everything else
165
dst[dst_ndx++]= src[src_ndx];
172
class LoggingGearman : public plugin::Logging
175
int gearman_client_ok;
176
gearman_client_st gearman_client;
181
: plugin::Logging("LoggingGearman"),
184
gearman_return_t ret;
186
if (sysvar_logging_gearman_enable == false)
189
if (sysvar_logging_gearman_host == NULL)
193
if (gearman_client_create(&gearman_client) == NULL)
195
errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
200
/* TODO, be able to override the port */
201
/* TODO, be able send to multiple servers */
202
ret= gearman_client_add_server(&gearman_client,
203
sysvar_logging_gearman_host, 0);
204
if (ret != GEARMAN_SUCCESS)
206
errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
207
gearman_client_error(&gearman_client));
211
gearman_client_ok= 1;
217
if (gearman_client_ok)
219
gearman_client_free(&gearman_client);
223
virtual bool post(Session *session)
225
char msgbuf[MAX_MSG_LEN];
228
assert(session != NULL);
230
/* in theory, we should return "true", meaning that the plugin isn't happy,
231
but that crashes the server, so for now, we just lie a little bit
234
if (!gearman_client_ok)
237
/* TODO, the session object should have a "utime command completed"
238
inside itself, so be more accurate, and so this doesnt have to
239
keep calling current_utime, which can be slow */
241
uint64_t t_mark= get_microtime();
243
// buffer to quotify the query
244
unsigned char qs[255];
246
// to avoid trying to printf %s something that is potentially NULL
247
const char *dbs= session->db.empty() ? "" : session->db.c_str();
250
snprintf(msgbuf, MAX_MSG_LEN,
251
"%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
252
"%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
253
"%"PRIu32",%"PRIu32",%"PRIu32",\"%s\"",
256
session->getQueryId(),
257
// dont need to quote the db name, always CSV safe
258
(int)session->db.length(), dbs,
259
// do need to quote the query
260
quotify((const unsigned char *)session->getQueryString().c_str(),
261
session->getQueryLength(), qs, sizeof(qs)),
262
// command_name is defined in drizzled/sql_parse.cc
263
// dont need to quote the command name, always CSV safe
264
(int)command_name[session->command].length,
265
command_name[session->command].str,
266
// counters are at end, to make it easier to add more
267
(t_mark - session->getConnectMicroseconds()),
268
(t_mark - session->start_utime),
269
(t_mark - session->utime_after_lock),
270
session->sent_row_count,
271
session->examined_row_count,
273
session->total_warn_count,
274
session->getServerId(),
278
char job_handle[GEARMAN_JOB_HANDLE_SIZE];
280
(void) gearman_client_do_background(&gearman_client,
281
sysvar_logging_gearman_function,
291
static LoggingGearman *handler= NULL;
293
static int logging_gearman_plugin_init(plugin::Context &context)
295
handler= new LoggingGearman();
296
context.add(handler);
301
static DRIZZLE_SYSVAR_BOOL(
303
sysvar_logging_gearman_enable,
305
N_("Enable logging to a gearman server"),
306
NULL, /* check func */
307
NULL, /* update func */
308
false /* default */);
310
static DRIZZLE_SYSVAR_STR(
312
sysvar_logging_gearman_host,
314
N_("Hostname for logging to a Gearman server"),
315
NULL, /* check func */
316
NULL, /* update func*/
317
"localhost" /* default */);
319
static DRIZZLE_SYSVAR_STR(
321
sysvar_logging_gearman_function,
323
N_("Gearman Function to send logging to"),
324
NULL, /* check func */
325
NULL, /* update func*/
326
"drizzlelog" /* default */);
328
static drizzle_sys_var* logging_gearman_system_variables[]= {
329
DRIZZLE_SYSVAR(enable),
330
DRIZZLE_SYSVAR(host),
331
DRIZZLE_SYSVAR(function),
335
DRIZZLE_DECLARE_PLUGIN
340
"Mark Atwood <mark@fallenpegasus.com>",
341
N_("Log queries to a Gearman server"),
343
logging_gearman_plugin_init,
344
logging_gearman_system_variables,
347
DRIZZLE_DECLARE_PLUGIN_END;