39
using namespace drizzled;
40
namespace drizzle_plugin
40
43
namespace po= boost::program_options;
42
45
/* TODO make this dynamic as needed */
43
46
static const int MAX_MSG_LEN= 32*1024;
45
static bool sysvar_logging_gearman_enable;
46
static char* sysvar_logging_gearman_host= NULL;
47
static char* sysvar_logging_gearman_function= NULL;
49
48
/* quote a string to be safe to include in a CSV line
50
49
that means backslash quoting all commas, doublequotes, backslashes,
51
50
and all the ASCII unprintable characters
157
class LoggingGearman : public plugin::Logging
156
class LoggingGearman :
157
public drizzled::plugin::Logging
160
int gearman_client_ok;
161
gearman_client_st gearman_client;
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&);
166
: plugin::Logging("LoggingGearman"),
171
LoggingGearman(const std::string &host,
172
const std::string &function) :
173
drizzled::plugin::Logging("LoggingGearman"),
176
_gearman_client_ok(0),
169
179
gearman_return_t ret;
171
if (sysvar_logging_gearman_enable == false)
174
if (sysvar_logging_gearman_host == NULL)
178
if (gearman_client_create(&gearman_client) == NULL)
182
if (gearman_client_create(&_gearman_client) == NULL)
180
184
char errmsg[STRERROR_MAX];
181
185
strerror_r(errno, errmsg, sizeof(errmsg));
182
errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
186
drizzled::errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
187
191
/* TODO, be able to override the port */
188
192
/* TODO, be able send to multiple servers */
189
ret= gearman_client_add_server(&gearman_client,
190
sysvar_logging_gearman_host, 0);
193
ret= gearman_client_add_server(&_gearman_client,
191
195
if (ret != GEARMAN_SUCCESS)
193
errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
194
gearman_client_error(&gearman_client));
197
drizzled::errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
198
gearman_client_error(&_gearman_client));
198
gearman_client_ok= 1;
202
_gearman_client_ok= 1;
202
206
~LoggingGearman()
204
if (gearman_client_ok)
208
if (_gearman_client_ok)
206
gearman_client_free(&gearman_client);
210
gearman_client_free(&_gearman_client);
210
virtual bool post(Session *session)
214
virtual bool post(drizzled::Session *session)
212
216
boost::scoped_array<char> msgbuf(new char[MAX_MSG_LEN]);
213
217
int msgbuf_len= 0;
250
254
quotify((const unsigned char *)session->getQueryString()->c_str(), session->getQueryString()->length(), qs, sizeof(qs)),
251
255
// command_name is defined in drizzled/sql_parse.cc
252
256
// dont need to quote the command name, always CSV safe
253
(int)command_name[session->command].length,
254
command_name[session->command].str,
257
(int)drizzled::command_name[session->command].length,
258
drizzled::command_name[session->command].str,
255
259
// counters are at end, to make it easier to add more
256
260
(t_mark - session->getConnectMicroseconds()),
257
261
(t_mark - session->start_utime),
261
265
session->tmp_table,
262
266
session->total_warn_count,
263
267
session->getServerId(),
268
drizzled::glob_hostname
267
271
char job_handle[GEARMAN_JOB_HANDLE_SIZE];
269
(void) gearman_client_do_background(&gearman_client,
270
sysvar_logging_gearman_function,
273
(void) gearman_client_do_background(&_gearman_client,
272
276
(void *) msgbuf.get(),
273
277
(size_t) msgbuf_len,
280
284
static LoggingGearman *handler= NULL;
282
static int logging_gearman_plugin_init(module::Context &context)
286
static int logging_gearman_plugin_init(drizzled::module::Context &context)
284
handler= new LoggingGearman();
288
const drizzled::module::option_map &vm= context.getOptions();
290
handler= new LoggingGearman(vm["host"].as<std::string>(),
291
vm["function"].as<std::string>());
285
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>()));
290
299
static void init_options(drizzled::module::option_context &context)
293
po::value<bool>(&sysvar_logging_gearman_enable)->default_value(false)->zero_tokens(),
294
N_("Enable logging to a gearman server"));
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"));
297
static DRIZZLE_SYSVAR_BOOL(
299
sysvar_logging_gearman_enable,
301
N_("Enable logging to a gearman server"),
302
NULL, /* check func */
303
NULL, /* update func */
304
false /* default */);
306
static DRIZZLE_SYSVAR_STR(
308
sysvar_logging_gearman_host,
310
N_("Hostname for logging to a Gearman server"),
311
NULL, /* check func */
312
NULL, /* update func*/
313
"localhost" /* default */);
315
static DRIZZLE_SYSVAR_STR(
317
sysvar_logging_gearman_function,
319
N_("Gearman Function to send logging to"),
320
NULL, /* check func */
321
NULL, /* update func*/
322
"drizzlelog" /* default */);
324
static drizzle_sys_var* logging_gearman_system_variables[]= {
325
DRIZZLE_SYSVAR(enable),
326
DRIZZLE_SYSVAR(host),
327
DRIZZLE_SYSVAR(function),
309
} /* namespace drizzle_plugin */
331
311
DRIZZLE_DECLARE_PLUGIN
336
316
"Mark Atwood <mark@fallenpegasus.com>",
337
317
N_("Log queries to a Gearman server"),
339
logging_gearman_plugin_init,
340
logging_gearman_system_variables,
318
drizzled::PLUGIN_LICENSE_GPL,
319
drizzle_plugin::logging_gearman_plugin_init,
321
drizzle_plugin::init_options
343
323
DRIZZLE_DECLARE_PLUGIN_END;