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
22
#include <boost/scoped_array.hpp>
24
#include <drizzled/plugin/logging.h>
25
#include <drizzled/gettext.h>
26
#include <drizzled/session.h>
27
#include <boost/date_time.hpp>
28
#include <boost/program_options.hpp>
29
#include <drizzled/module/option_map.h>
30
#include <libgearman/gearman.h>
32
#include <sys/types.h>
40
namespace drizzle_plugin
43
namespace po= boost::program_options;
45
/* TODO make this dynamic as needed */
46
static const int MAX_MSG_LEN= 32*1024;
48
/* quote a string to be safe to include in a CSV line
49
that means backslash quoting all commas, doublequotes, backslashes,
50
and all the ASCII unprintable characters
51
as long as we pass the high-bit bytes unchanged
52
this is safe to do to a UTF8 string
53
we dont allow overrunning the targetbuffer
54
to avoid having a very long query overwrite memory
56
TODO consider remapping the unprintables instead to "Printable
57
Representation", the Unicode characters from the area U+2400 to
58
U+2421 reserved for representing control characters when it is
59
necessary to print or display them rather than have them perform
60
their intended function.
63
static unsigned char *quotify (const unsigned char *src, size_t srclen,
64
unsigned char *dst, size_t dstlen)
66
static const char hexit[]= { '0', '1', '2', '3', '4', '5', '6', '7',
67
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
68
size_t dst_ndx; /* ndx down the dst */
69
size_t src_ndx; /* ndx down the src */
74
for (dst_ndx= 0,src_ndx= 0; src_ndx < srclen; src_ndx++)
77
/* Worst case, need 5 dst bytes for the next src byte.
78
backslash x hexit hexit null
79
so if not enough room, just terminate the string and return
81
if ((dstlen - dst_ndx) < 5)
83
dst[dst_ndx]= (unsigned char)0x00;
87
if (src[src_ndx] > 0x7f)
89
// pass thru high bit characters, they are non-ASCII UTF8 Unicode
90
dst[dst_ndx++]= src[src_ndx];
92
else if (src[src_ndx] == 0x00) // null
94
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) '0';
96
else if (src[src_ndx] == 0x07) // bell
98
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'a';
100
else if (src[src_ndx] == 0x08) // backspace
102
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'b';
104
else if (src[src_ndx] == 0x09) // horiz tab
106
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 't';
108
else if (src[src_ndx] == 0x0a) // line feed
110
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'n';
112
else if (src[src_ndx] == 0x0b) // vert tab
114
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'v';
116
else if (src[src_ndx] == 0x0c) // formfeed
118
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'f';
120
else if (src[src_ndx] == 0x0d) // carrage return
122
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'r';
124
else if (src[src_ndx] == 0x1b) // escape
126
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'e';
128
else if (src[src_ndx] == 0x22) // quotation mark
130
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x22;
132
else if (src[src_ndx] == 0x2C) // comma
134
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x2C;
136
else if (src[src_ndx] == 0x5C) // backslash
138
dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x5C;
140
else if ((src[src_ndx] < 0x20) || (src[src_ndx] == 0x7F)) // other unprintable ASCII
142
dst[dst_ndx++]= 0x5C;
143
dst[dst_ndx++]= (unsigned char) 'x';
144
dst[dst_ndx++]= hexit[(src[src_ndx] >> 4) & 0x0f];
145
dst[dst_ndx++]= hexit[src[src_ndx] & 0x0f];
147
else // everything else
149
dst[dst_ndx++]= src[src_ndx];
156
class LoggingGearman :
157
public drizzled::plugin::Logging
160
const std::string _host;
161
const std::string _function;
163
int _gearman_client_ok;
164
gearman_client_st _gearman_client;
167
LoggingGearman(const LoggingGearman&);
171
LoggingGearman(const std::string &host,
172
const std::string &function) :
173
drizzled::plugin::Logging("LoggingGearman"),
176
_gearman_client_ok(0),
179
gearman_return_t ret;
182
if (gearman_client_create(&_gearman_client) == NULL)
184
char errmsg[STRERROR_MAX];
185
strerror_r(errno, errmsg, sizeof(errmsg));
186
drizzled::errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
191
/* TODO, be able to override the port */
192
/* TODO, be able send to multiple servers */
193
ret= gearman_client_add_server(&_gearman_client,
195
if (ret != GEARMAN_SUCCESS)
197
drizzled::errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
198
gearman_client_error(&_gearman_client));
202
_gearman_client_ok= 1;
208
if (_gearman_client_ok)
210
gearman_client_free(&_gearman_client);
214
virtual bool post(drizzled::Session *session)
216
boost::scoped_array<char> msgbuf(new char[MAX_MSG_LEN]);
219
assert(session != NULL);
221
/* in theory, we should return "true", meaning that the plugin isn't happy,
222
but that crashes the server, so for now, we just lie a little bit
225
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
boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
233
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
234
uint64_t t_mark= (mytime-epoch).total_microseconds();
237
// buffer to quotify the query
238
unsigned char qs[255];
240
// to avoid trying to printf %s something that is potentially NULL
241
drizzled::util::string::const_shared_ptr dbs(session->schema());
244
snprintf(msgbuf.get(), MAX_MSG_LEN,
245
"%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
246
"%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
247
"%"PRIu32",%"PRIu32",%"PRIu32",\"%s\"",
250
session->getQueryId(),
251
// dont need to quote the db name, always CSV safe
252
(int)dbs->size(), dbs->c_str(),
253
// do need to quote the query
254
quotify((const unsigned char *)session->getQueryString()->c_str(), session->getQueryString()->length(), qs, sizeof(qs)),
255
// command_name is defined in drizzled/sql_parse.cc
256
// dont need to quote the command name, always CSV safe
257
(int)drizzled::command_name[session->command].length,
258
drizzled::command_name[session->command].str,
259
// counters are at end, to make it easier to add more
260
(t_mark - session->getConnectMicroseconds()),
261
(t_mark - session->start_utime),
262
(t_mark - session->utime_after_lock),
263
session->sent_row_count,
264
session->examined_row_count,
266
session->total_warn_count,
267
session->getServerId(),
268
drizzled::glob_hostname
271
char job_handle[GEARMAN_JOB_HANDLE_SIZE];
273
(void) gearman_client_do_background(&_gearman_client,
276
(void *) msgbuf.get(),
284
static LoggingGearman *handler= NULL;
286
static int logging_gearman_plugin_init(drizzled::module::Context &context)
288
const drizzled::module::option_map &vm= context.getOptions();
290
handler= new LoggingGearman(vm["host"].as<std::string>(),
291
vm["function"].as<std::string>());
292
context.add(handler);
293
context.registerVariable(new drizzled::sys_var_const_string_val("host", vm["host"].as<std::string>()));
294
context.registerVariable(new drizzled::sys_var_const_string_val("function", vm["function"].as<std::string>()));
299
static void init_options(drizzled::module::option_context &context)
302
po::value<std::string>()->default_value("localhost"),
303
N_("Hostname for logging to a Gearman server"));
305
po::value<std::string>()->default_value("drizzlelog"),
306
N_("Gearman Function to send logging to"));
309
} /* namespace drizzle_plugin */
311
DRIZZLE_DECLARE_PLUGIN
316
"Mark Atwood <mark@fallenpegasus.com>",
317
N_("Log queries to a Gearman server"),
318
drizzled::PLUGIN_LICENSE_GPL,
319
drizzle_plugin::logging_gearman_plugin_init,
321
drizzle_plugin::init_options
323
DRIZZLE_DECLARE_PLUGIN_END;