1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008, 2009 Sun Microsystems, Inc.
4
* Copyright (C) 2008,2009 Sun Microsystems
6
6
* This program is free software; you can redistribute it and/or modify
7
7
* it under the terms of the GNU General Public License as published by
20
20
#include "config.h"
22
#include <boost/scoped_array.hpp>
24
21
#include <drizzled/plugin/logging.h>
25
22
#include <drizzled/gettext.h>
26
23
#include <drizzled/session.h>
27
#include <drizzled/errmsg_print.h>
28
#include <boost/date_time.hpp>
29
24
#include <boost/program_options.hpp>
30
25
#include <drizzled/module/option_map.h>
31
26
#include <libgearman/gearman.h>
32
27
#include <limits.h>
33
29
#include <sys/types.h>
34
30
#include <sys/stat.h>
41
namespace drizzle_plugin
35
using namespace drizzled;
44
36
namespace po= boost::program_options;
46
38
/* TODO make this dynamic as needed */
47
39
static const int MAX_MSG_LEN= 32*1024;
41
static bool sysvar_logging_gearman_enable;
42
static char* sysvar_logging_gearman_host= NULL;
43
static char* sysvar_logging_gearman_function= NULL;
46
/* stolen from mysys/my_getsystime
47
until the Session has a good utime "now" we can use
48
will have to use this instead */
50
static uint64_t get_microtime()
52
#if defined(HAVE_GETHRTIME)
53
return gethrtime()/1000;
58
The following loop is here because gettimeofday may fail on some systems
60
while (gettimeofday(&t, NULL) != 0) {}
61
newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
63
#endif /* defined(HAVE_GETHRTIME) */
49
66
/* quote a string to be safe to include in a CSV line
50
67
that means backslash quoting all commas, doublequotes, backslashes,
51
68
and all the ASCII unprintable characters
157
class LoggingGearman :
158
public drizzled::plugin::Logging
174
class LoggingGearman : public plugin::Logging
161
const std::string _host;
162
const std::string _function;
164
int _gearman_client_ok;
165
gearman_client_st _gearman_client;
168
LoggingGearman(const LoggingGearman&);
177
int gearman_client_ok;
178
gearman_client_st gearman_client;
172
LoggingGearman(const std::string &host,
173
const std::string &function) :
174
drizzled::plugin::Logging("LoggingGearman"),
177
_gearman_client_ok(0),
183
: plugin::Logging("LoggingGearman"),
180
186
gearman_return_t ret;
183
if (gearman_client_create(&_gearman_client) == NULL)
188
if (sysvar_logging_gearman_enable == false)
191
if (sysvar_logging_gearman_host == NULL)
195
if (gearman_client_create(&gearman_client) == NULL)
185
drizzled::sql_perror(_("fail gearman_client_create()"));
197
char errmsg[STRERROR_MAX];
198
strerror_r(errno, errmsg, sizeof(errmsg));
199
errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
189
204
/* TODO, be able to override the port */
190
205
/* TODO, be able send to multiple servers */
191
ret= gearman_client_add_server(&_gearman_client,
206
ret= gearman_client_add_server(&gearman_client,
207
sysvar_logging_gearman_host, 0);
193
208
if (ret != GEARMAN_SUCCESS)
195
drizzled::errmsg_printf(drizzled::error::ERROR, _("fail gearman_client_add_server(): %s"),
196
gearman_client_error(&_gearman_client));
210
errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
211
gearman_client_error(&gearman_client));
200
_gearman_client_ok= 1;
215
gearman_client_ok= 1;
204
219
~LoggingGearman()
206
if (_gearman_client_ok)
221
if (gearman_client_ok)
208
gearman_client_free(&_gearman_client);
223
gearman_client_free(&gearman_client);
212
virtual bool post(drizzled::Session *session)
227
virtual bool post(Session *session)
214
boost::scoped_array<char> msgbuf(new char[MAX_MSG_LEN]);
229
char msgbuf[MAX_MSG_LEN];
215
230
int msgbuf_len= 0;
217
232
assert(session != NULL);
220
235
but that crashes the server, so for now, we just lie a little bit
223
if (not _gearman_client_ok)
238
if (!gearman_client_ok)
227
TODO, the session object should have a "utime command completed"
228
inside itself, so be more accurate, and so this doesnt have to
229
keep calling current_utime, which can be slow.
231
uint64_t t_mark= session->getCurrentTimestamp(false);
241
/* TODO, the session object should have a "utime command completed"
242
inside itself, so be more accurate, and so this doesnt have to
243
keep calling current_utime, which can be slow */
245
uint64_t t_mark= get_microtime();
234
247
// buffer to quotify the query
235
248
unsigned char qs[255];
237
250
// to avoid trying to printf %s something that is potentially NULL
238
drizzled::util::string::const_shared_ptr dbs(session->schema());
251
const char *dbs= session->db.empty() ? "" : session->db.c_str();
241
snprintf(msgbuf.get(), MAX_MSG_LEN,
254
snprintf(msgbuf, MAX_MSG_LEN,
242
255
"%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
243
256
"%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
244
257
"%"PRIu32",%"PRIu32",%"PRIu32",\"%s\"",
246
259
session->thread_id,
247
260
session->getQueryId(),
248
261
// dont need to quote the db name, always CSV safe
249
(int)dbs->size(), dbs->c_str(),
262
(int)session->db.length(), dbs,
250
263
// do need to quote the query
251
quotify((const unsigned char *)session->getQueryString()->c_str(), session->getQueryString()->length(), qs, sizeof(qs)),
252
// getCommandName is defined in drizzled/sql_parse.h dont
253
// need to quote the command name, always CSV safe
254
(int)drizzled::getCommandName(session->command).size(),
255
drizzled::getCommandName(session->command).c_str(),
264
quotify((const unsigned char *)session->getQueryString().c_str(),
265
session->getQueryLength(), qs, sizeof(qs)),
266
// command_name is defined in drizzled/sql_parse.cc
267
// dont need to quote the command name, always CSV safe
268
(int)command_name[session->command].length,
269
command_name[session->command].str,
256
270
// counters are at end, to make it easier to add more
257
271
(t_mark - session->getConnectMicroseconds()),
258
(session->getElapsedTime()),
272
(t_mark - session->start_utime),
259
273
(t_mark - session->utime_after_lock),
260
274
session->sent_row_count,
261
275
session->examined_row_count,
262
276
session->tmp_table,
263
277
session->total_warn_count,
264
278
session->getServerId(),
265
drizzled::glob_hostname
268
282
char job_handle[GEARMAN_JOB_HANDLE_SIZE];
270
(void) gearman_client_do_background(&_gearman_client,
284
(void) gearman_client_do_background(&gearman_client,
285
sysvar_logging_gearman_function,
273
(void *) msgbuf.get(),
274
288
(size_t) msgbuf_len,
281
295
static LoggingGearman *handler= NULL;
283
static int logging_gearman_plugin_init(drizzled::module::Context &context)
297
static int logging_gearman_plugin_init(module::Context &context)
285
const drizzled::module::option_map &vm= context.getOptions();
287
handler= new LoggingGearman(vm["host"].as<std::string>(),
288
vm["function"].as<std::string>());
299
handler= new LoggingGearman();
289
300
context.add(handler);
290
context.registerVariable(new drizzled::sys_var_const_string_val("host", vm["host"].as<std::string>()));
291
context.registerVariable(new drizzled::sys_var_const_string_val("function", vm["function"].as<std::string>()));
296
305
static void init_options(drizzled::module::option_context &context)
299
po::value<std::string>()->default_value("localhost"),
300
_("Hostname for logging to a Gearman server"));
302
po::value<std::string>()->default_value("drizzlelog"),
303
_("Gearman Function to send logging to"));
308
po::value<bool>(&sysvar_logging_gearman_enable)->default_value(false)->zero_tokens(),
309
N_("Enable logging to a gearman server"));
306
} /* namespace drizzle_plugin */
312
static DRIZZLE_SYSVAR_BOOL(
314
sysvar_logging_gearman_enable,
316
N_("Enable logging to a gearman server"),
317
NULL, /* check func */
318
NULL, /* update func */
319
false /* default */);
321
static DRIZZLE_SYSVAR_STR(
323
sysvar_logging_gearman_host,
325
N_("Hostname for logging to a Gearman server"),
326
NULL, /* check func */
327
NULL, /* update func*/
328
"localhost" /* default */);
330
static DRIZZLE_SYSVAR_STR(
332
sysvar_logging_gearman_function,
334
N_("Gearman Function to send logging to"),
335
NULL, /* check func */
336
NULL, /* update func*/
337
"drizzlelog" /* default */);
339
static drizzle_sys_var* logging_gearman_system_variables[]= {
340
DRIZZLE_SYSVAR(enable),
341
DRIZZLE_SYSVAR(host),
342
DRIZZLE_SYSVAR(function),
308
346
DRIZZLE_DECLARE_PLUGIN
313
351
"Mark Atwood <mark@fallenpegasus.com>",
314
352
N_("Log queries to a Gearman server"),
315
drizzled::PLUGIN_LICENSE_GPL,
316
drizzle_plugin::logging_gearman_plugin_init,
318
drizzle_plugin::init_options
354
logging_gearman_plugin_init,
355
logging_gearman_system_variables,
320
358
DRIZZLE_DECLARE_PLUGIN_END;