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
22
#include <boost/scoped_array.hpp>
24
#include <drizzled/plugin/logging.h>
25
#include <drizzled/gettext.h>
26
#include <drizzled/session.h>
27
#include <drizzled/sql_parse.h>
28
#include <drizzled/errmsg_print.h>
29
#include <boost/date_time.hpp>
30
#include <boost/program_options.hpp>
31
#include <drizzled/module/option_map.h>
32
#include <libgearman/gearman.h>
34
#include <sys/types.h>
42
namespace drizzle_plugin
45
namespace po= boost::program_options;
47
/* TODO make this dynamic as needed */
48
static const int MAX_MSG_LEN= 32*1024;
50
/* quote a string to be safe to include in a CSV line
51
that means backslash quoting all commas, doublequotes, backslashes,
52
and all the ASCII unprintable characters
53
as long as we pass the high-bit bytes unchanged
54
this is safe to do to a UTF8 string
55
we dont allow overrunning the targetbuffer
56
to avoid having a very long query overwrite memory
58
TODO consider remapping the unprintables instead to "Printable
59
Representation", the Unicode characters from the area U+2400 to
60
U+2421 reserved for representing control characters when it is
61
necessary to print or display them rather than have them perform
62
their intended function.
65
static unsigned char *quotify (const unsigned char *src, size_t srclen,
66
unsigned char *dst, size_t dstlen)
68
static const char hexit[]= { '0', '1', '2', '3', '4', '5', '6', '7',
69
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
70
size_t dst_ndx; /* ndx down the dst */
71
size_t src_ndx; /* ndx down the src */
76
for (dst_ndx= 0,src_ndx= 0; src_ndx < srclen; src_ndx++)
79
/* Worst case, need 5 dst bytes for the next src byte.
80
backslash x hexit hexit null
81
so if not enough room, just terminate the string and return
83
if ((dstlen - dst_ndx) < 5)
85
dst[dst_ndx]= (unsigned char)0x00;
89
if (src[src_ndx] > 0x7f)
91
// pass thru high bit characters, they are non-ASCII UTF8 Unicode
92
dst[dst_ndx++]= src[src_ndx];
94
else if (src[src_ndx] == 0x00) // null
96
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) '0';
98
else if (src[src_ndx] == 0x07) // bell
100
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'a';
102
else if (src[src_ndx] == 0x08) // backspace
104
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'b';
106
else if (src[src_ndx] == 0x09) // horiz tab
108
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 't';
110
else if (src[src_ndx] == 0x0a) // line feed
112
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'n';
114
else if (src[src_ndx] == 0x0b) // vert tab
116
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'v';
118
else if (src[src_ndx] == 0x0c) // formfeed
120
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'f';
122
else if (src[src_ndx] == 0x0d) // carrage return
124
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'r';
126
else if (src[src_ndx] == 0x1b) // escape
128
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'e';
130
else if (src[src_ndx] == 0x22) // quotation mark
132
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x22;
134
else if (src[src_ndx] == 0x2C) // comma
136
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x2C;
138
else if (src[src_ndx] == 0x5C) // backslash
140
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x5C;
142
else if ((src[src_ndx] < 0x20) || (src[src_ndx] == 0x7F)) // other unprintable ASCII
144
dst[dst_ndx++]= 0x5C;
145
dst[dst_ndx++]= (unsigned char) 'x';
146
dst[dst_ndx++]= hexit[(src[src_ndx] >> 4) & 0x0f];
147
dst[dst_ndx++]= hexit[src[src_ndx] & 0x0f];
149
else // everything else
151
dst[dst_ndx++]= src[src_ndx];
158
class LoggingGearman :
159
public drizzled::plugin::Logging
162
const std::string _host;
163
const std::string _function;
165
int _gearman_client_ok;
166
gearman_client_st _gearman_client;
169
LoggingGearman(const LoggingGearman&);
173
LoggingGearman(const std::string &host,
174
const std::string &function) :
175
drizzled::plugin::Logging("LoggingGearman"),
178
_gearman_client_ok(0),
181
gearman_return_t ret;
184
if (gearman_client_create(&_gearman_client) == NULL)
186
drizzled::sql_perror(_("fail gearman_client_create()"));
190
/* TODO, be able to override the port */
191
/* TODO, be able send to multiple servers */
192
ret= gearman_client_add_server(&_gearman_client,
194
if (ret != GEARMAN_SUCCESS)
196
drizzled::errmsg_printf(drizzled::error::ERROR, _("fail gearman_client_add_server(): %s"),
197
gearman_client_error(&_gearman_client));
201
_gearman_client_ok= 1;
207
if (_gearman_client_ok)
209
gearman_client_free(&_gearman_client);
213
virtual bool post(drizzled::Session *session)
215
boost::scoped_array<char> msgbuf(new char[MAX_MSG_LEN]);
218
assert(session != NULL);
220
/* in theory, we should return "true", meaning that the plugin isn't happy,
221
but that crashes the server, so for now, we just lie a little bit
224
if (not _gearman_client_ok)
228
TODO, the session object should have a "utime command completed"
229
inside itself, so be more accurate, and so this doesnt have to
230
keep calling current_utime, which can be slow.
232
uint64_t t_mark= session->getCurrentTimestamp(false);
235
// buffer to quotify the query
236
unsigned char qs[255];
238
// to avoid trying to printf %s something that is potentially NULL
239
drizzled::util::string::const_shared_ptr dbs(session->schema());
242
snprintf(msgbuf.get(), MAX_MSG_LEN,
243
"%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
244
"%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
245
"%"PRIu32",%"PRIu32",%"PRIu32",\"%s\"",
248
session->getQueryId(),
249
// dont need to quote the db name, always CSV safe
250
(int)dbs->size(), dbs->c_str(),
251
// do need to quote the query
252
quotify((const unsigned char *)session->getQueryString()->c_str(), session->getQueryString()->length(), qs, sizeof(qs)),
253
// getCommandName is defined in drizzled/sql_parse.h dont
254
// need to quote the command name, always CSV safe
255
(int)drizzled::getCommandName(session->command).size(),
256
drizzled::getCommandName(session->command).c_str(),
257
// counters are at end, to make it easier to add more
258
(t_mark - session->getConnectMicroseconds()),
259
(session->getElapsedTime()),
260
(t_mark - session->utime_after_lock),
261
session->sent_row_count,
262
session->examined_row_count,
264
session->total_warn_count,
265
session->getServerId(),
266
drizzled::getServerHostname().c_str()
269
char job_handle[GEARMAN_JOB_HANDLE_SIZE];
271
(void) gearman_client_do_background(&_gearman_client,
274
(void *) msgbuf.get(),
282
static LoggingGearman *handler= NULL;
284
static int logging_gearman_plugin_init(drizzled::module::Context &context)
286
const drizzled::module::option_map &vm= context.getOptions();
288
handler= new LoggingGearman(vm["host"].as<std::string>(),
289
vm["function"].as<std::string>());
290
context.add(handler);
291
context.registerVariable(new drizzled::sys_var_const_string_val("host", vm["host"].as<std::string>()));
292
context.registerVariable(new drizzled::sys_var_const_string_val("function", vm["function"].as<std::string>()));
297
static void init_options(drizzled::module::option_context &context)
300
po::value<std::string>()->default_value("localhost"),
301
_("Hostname for logging to a Gearman server"));
303
po::value<std::string>()->default_value("drizzlelog"),
304
_("Gearman Function to send logging to"));
307
} /* namespace drizzle_plugin */
309
DRIZZLE_DECLARE_PLUGIN
314
"Mark Atwood <mark@fallenpegasus.com>",
315
N_("Log queries to a Gearman server"),
316
drizzled::PLUGIN_LICENSE_GPL,
317
drizzle_plugin::logging_gearman_plugin_init,
319
drizzle_plugin::init_options
321
DRIZZLE_DECLARE_PLUGIN_END;