~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_gearman/logging_gearman.cc

  • Committer: Lee Bieber
  • Date: 2010-12-03 01:16:19 UTC
  • mfrom: (1819.9.81 update-innobase)
  • Revision ID: kalebral@gmail.com-20101203011619-n6v584rijwdet05b
Merge Stewart - update Innobase plugin based on InnoDB 1.1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008, 2009 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008,2009 Sun Microsystems
5
5
 *
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
24
24
#include <drizzled/plugin/logging.h>
25
25
#include <drizzled/gettext.h>
26
26
#include <drizzled/session.h>
27
 
#include <drizzled/errmsg_print.h>
28
27
#include <boost/date_time.hpp>
29
28
#include <boost/program_options.hpp>
30
29
#include <drizzled/module/option_map.h>
37
36
#include <cerrno>
38
37
#include <memory>
39
38
 
40
 
 
41
 
namespace drizzle_plugin
42
 
{
43
 
 
 
39
using namespace drizzled;
44
40
namespace po= boost::program_options;
45
41
 
46
42
/* TODO make this dynamic as needed */
47
43
static const int MAX_MSG_LEN= 32*1024;
48
44
 
 
45
static bool sysvar_logging_gearman_enable;
 
46
static char* sysvar_logging_gearman_host= NULL;
 
47
static char* sysvar_logging_gearman_function= NULL;
 
48
 
49
49
/* quote a string to be safe to include in a CSV line
50
50
   that means backslash quoting all commas, doublequotes, backslashes,
51
51
   and all the ASCII unprintable characters
154
154
  return dst;
155
155
}
156
156
 
157
 
class LoggingGearman :
158
 
  public drizzled::plugin::Logging
 
157
class LoggingGearman : public plugin::Logging
159
158
{
160
159
 
161
 
  const std::string _host;
162
 
  const std::string _function;
163
 
 
164
 
  int _gearman_client_ok;
165
 
  gearman_client_st _gearman_client;
166
 
 
167
 
  LoggingGearman();
168
 
  LoggingGearman(const LoggingGearman&);
 
160
  int gearman_client_ok;
 
161
  gearman_client_st gearman_client;
169
162
 
170
163
public:
171
164
 
172
 
  LoggingGearman(const std::string &host,
173
 
                 const std::string &function) :
174
 
    drizzled::plugin::Logging("LoggingGearman"),
175
 
    _host(host),
176
 
    _function(function),
177
 
    _gearman_client_ok(0),
178
 
    _gearman_client()
 
165
  LoggingGearman()
 
166
    : plugin::Logging("LoggingGearman"),
 
167
      gearman_client_ok(0)
179
168
  {
180
169
    gearman_return_t ret;
181
170
 
182
 
 
183
 
    if (gearman_client_create(&_gearman_client) == NULL)
 
171
    if (sysvar_logging_gearman_enable == false)
 
172
      return;
 
173
 
 
174
    if (sysvar_logging_gearman_host == NULL)
 
175
      return;
 
176
 
 
177
 
 
178
    if (gearman_client_create(&gearman_client) == NULL)
184
179
    {
185
 
      drizzled::sql_perror(_("fail gearman_client_create()"));
 
180
      char errmsg[STRERROR_MAX];
 
181
      strerror_r(errno, errmsg, sizeof(errmsg));
 
182
      errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
 
183
                    errmsg);
186
184
      return;
187
185
    }
188
186
 
189
187
    /* TODO, be able to override the port */
190
188
    /* TODO, be able send to multiple servers */
191
 
    ret= gearman_client_add_server(&_gearman_client,
192
 
                                   host.c_str(), 0);
 
189
    ret= gearman_client_add_server(&gearman_client,
 
190
                                   sysvar_logging_gearman_host, 0);
193
191
    if (ret != GEARMAN_SUCCESS)
194
192
    {
195
 
      drizzled::errmsg_printf(drizzled::error::ERROR, _("fail gearman_client_add_server(): %s"),
196
 
                              gearman_client_error(&_gearman_client));
 
193
      errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
 
194
                    gearman_client_error(&gearman_client));
197
195
      return;
198
196
    }
199
197
 
200
 
    _gearman_client_ok= 1;
 
198
    gearman_client_ok= 1;
201
199
 
202
200
  }
203
201
 
204
202
  ~LoggingGearman()
205
203
  {
206
 
    if (_gearman_client_ok)
 
204
    if (gearman_client_ok)
207
205
    {
208
 
      gearman_client_free(&_gearman_client);
 
206
      gearman_client_free(&gearman_client);
209
207
    }
210
208
  }
211
209
 
212
 
  virtual bool post(drizzled::Session *session)
 
210
  virtual bool post(Session *session)
213
211
  {
214
212
    boost::scoped_array<char> msgbuf(new char[MAX_MSG_LEN]);
215
213
    int msgbuf_len= 0;
220
218
       but that crashes the server, so for now, we just lie a little bit
221
219
    */
222
220
 
223
 
    if (not _gearman_client_ok)
 
221
    if (!gearman_client_ok)
224
222
        return false;
225
223
  
226
 
    /* 
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.
230
 
    */
231
 
    uint64_t t_mark= session->getCurrentTimestamp(false);
 
224
    /* TODO, the session object should have a "utime command completed"
 
225
       inside itself, so be more accurate, and so this doesnt have to
 
226
       keep calling current_utime, which can be slow */
 
227
  
 
228
    boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
 
229
    boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
 
230
    uint64_t t_mark= (mytime-epoch).total_microseconds();
232
231
  
233
232
 
234
233
    // buffer to quotify the query
235
234
    unsigned char qs[255];
236
235
  
237
236
    // to avoid trying to printf %s something that is potentially NULL
238
 
    drizzled::util::string::const_shared_ptr dbs(session->schema());
 
237
    const char *dbs= session->db.empty() ? "" : session->db.c_str();
239
238
  
240
239
    msgbuf_len=
241
240
      snprintf(msgbuf.get(), MAX_MSG_LEN,
246
245
               session->thread_id,
247
246
               session->getQueryId(),
248
247
               // dont need to quote the db name, always CSV safe
249
 
               (int)dbs->size(), dbs->c_str(),
 
248
               (int)session->getSchema().length(), dbs,
250
249
               // do need to quote the query
251
250
               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(),
 
251
               // command_name is defined in drizzled/sql_parse.cc
 
252
               // dont need to quote the command name, always CSV safe
 
253
               (int)command_name[session->command].length,
 
254
               command_name[session->command].str,
256
255
               // counters are at end, to make it easier to add more
257
256
               (t_mark - session->getConnectMicroseconds()),
258
 
               (session->getElapsedTime()),
 
257
               (t_mark - session->start_utime),
259
258
               (t_mark - session->utime_after_lock),
260
259
               session->sent_row_count,
261
260
               session->examined_row_count,
262
261
               session->tmp_table,
263
262
               session->total_warn_count,
264
263
               session->getServerId(),
265
 
               drizzled::glob_hostname
 
264
               glob_hostname
266
265
               );
267
266
  
268
267
    char job_handle[GEARMAN_JOB_HANDLE_SIZE];
269
268
  
270
 
    (void) gearman_client_do_background(&_gearman_client,
271
 
                                        _function.c_str(),
 
269
    (void) gearman_client_do_background(&gearman_client,
 
270
                                        sysvar_logging_gearman_function,
272
271
                                        NULL,
273
272
                                        (void *) msgbuf.get(),
274
273
                                        (size_t) msgbuf_len,
280
279
 
281
280
static LoggingGearman *handler= NULL;
282
281
 
283
 
static int logging_gearman_plugin_init(drizzled::module::Context &context)
 
282
static int logging_gearman_plugin_init(module::Context &context)
284
283
{
285
 
  const drizzled::module::option_map &vm= context.getOptions();
286
 
 
287
 
  handler= new LoggingGearman(vm["host"].as<std::string>(),
288
 
                              vm["function"].as<std::string>());
 
284
  handler= new LoggingGearman();
289
285
  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>()));
292
286
 
293
287
  return 0;
294
288
}
295
289
 
296
290
static void init_options(drizzled::module::option_context &context)
297
291
{
298
 
  context("host",
299
 
          po::value<std::string>()->default_value("localhost"),
300
 
          _("Hostname for logging to a Gearman server"));
301
 
  context("function",
302
 
          po::value<std::string>()->default_value("drizzlelog"),
303
 
          _("Gearman Function to send logging to"));
 
292
  context("enable",
 
293
          po::value<bool>(&sysvar_logging_gearman_enable)->default_value(false)->zero_tokens(),
 
294
          N_("Enable logging to a gearman server"));
304
295
}
305
296
 
306
 
} /* namespace drizzle_plugin */
 
297
static DRIZZLE_SYSVAR_BOOL(
 
298
                           enable,
 
299
                           sysvar_logging_gearman_enable,
 
300
                           PLUGIN_VAR_NOCMDARG,
 
301
                           N_("Enable logging to a gearman server"),
 
302
                           NULL, /* check func */
 
303
                           NULL, /* update func */
 
304
                           false /* default */);
 
305
 
 
306
static DRIZZLE_SYSVAR_STR(
 
307
                          host,
 
308
                          sysvar_logging_gearman_host,
 
309
                          PLUGIN_VAR_READONLY,
 
310
                          N_("Hostname for logging to a Gearman server"),
 
311
                          NULL, /* check func */
 
312
                          NULL, /* update func*/
 
313
                          "localhost" /* default */);
 
314
 
 
315
static DRIZZLE_SYSVAR_STR(
 
316
                          function,
 
317
                          sysvar_logging_gearman_function,
 
318
                          PLUGIN_VAR_READONLY,
 
319
                          N_("Gearman Function to send logging to"),
 
320
                          NULL, /* check func */
 
321
                          NULL, /* update func*/
 
322
                          "drizzlelog" /* default */);
 
323
 
 
324
static drizzle_sys_var* logging_gearman_system_variables[]= {
 
325
  DRIZZLE_SYSVAR(enable),
 
326
  DRIZZLE_SYSVAR(host),
 
327
  DRIZZLE_SYSVAR(function),
 
328
  NULL
 
329
};
307
330
 
308
331
DRIZZLE_DECLARE_PLUGIN
309
332
{
312
335
    "0.1",
313
336
    "Mark Atwood <mark@fallenpegasus.com>",
314
337
    N_("Log queries to a Gearman server"),
315
 
    drizzled::PLUGIN_LICENSE_GPL,
316
 
    drizzle_plugin::logging_gearman_plugin_init,
317
 
    NULL,
318
 
    drizzle_plugin::init_options
 
338
    PLUGIN_LICENSE_GPL,
 
339
    logging_gearman_plugin_init,
 
340
    logging_gearman_system_variables,
 
341
    init_options
319
342
}
320
343
DRIZZLE_DECLARE_PLUGIN_END;