~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_gearman/logging_gearman.cc

Reverted 1103

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
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include "config.h"
21
 
 
22
 
#include <boost/scoped_array.hpp>
23
 
 
24
 
#include <drizzled/plugin/logging.h>
 
20
#include <drizzled/server_includes.h>
 
21
#include <drizzled/plugin/logging_handler.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
 
#include <boost/program_options.hpp>
30
 
#include <drizzled/module/option_map.h>
 
24
 
31
25
#include <libgearman/gearman.h>
32
 
#include <limits.h>
33
 
#include <sys/types.h>
34
 
#include <sys/stat.h>
35
 
#include <fcntl.h>
36
 
#include <cstdio>
37
 
#include <cerrno>
38
 
#include <memory>
39
 
 
40
 
 
41
 
namespace drizzle_plugin
42
 
{
43
 
 
44
 
namespace po= boost::program_options;
 
26
 
45
27
 
46
28
/* TODO make this dynamic as needed */
47
29
static const int MAX_MSG_LEN= 32*1024;
48
30
 
 
31
static bool sysvar_logging_gearman_enable= false;
 
32
static char* sysvar_logging_gearman_host= NULL;
 
33
static char* sysvar_logging_gearman_function= NULL;
 
34
 
 
35
 
 
36
/* stolen from mysys/my_getsystime
 
37
   until the Session has a good utime "now" we can use
 
38
   will have to use this instead */
 
39
 
 
40
#include <sys/time.h>
 
41
static uint64_t get_microtime()
 
42
{
 
43
#if defined(HAVE_GETHRTIME)
 
44
  return gethrtime()/1000;
 
45
#else
 
46
  uint64_t newtime;
 
47
  struct timeval t;
 
48
  /*
 
49
    The following loop is here because gettimeofday may fail on some systems
 
50
  */
 
51
  while (gettimeofday(&t, NULL) != 0) {}
 
52
  newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
 
53
  return newtime;
 
54
#endif  /* defined(HAVE_GETHRTIME) */
 
55
}
 
56
 
49
57
/* quote a string to be safe to include in a CSV line
50
58
   that means backslash quoting all commas, doublequotes, backslashes,
51
59
   and all the ASCII unprintable characters
154
162
  return dst;
155
163
}
156
164
 
157
 
class LoggingGearman :
158
 
  public drizzled::plugin::Logging
 
165
class LoggingGearman : public Logging_handler
159
166
{
160
167
 
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&);
 
168
  int gearman_client_ok;
 
169
  gearman_client_st gearman_client;
169
170
 
170
171
public:
171
172
 
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()
 
173
  LoggingGearman() : Logging_handler("LoggingGearman"), gearman_client_ok(0)
179
174
  {
180
175
    gearman_return_t ret;
181
176
 
182
 
 
183
 
    if (gearman_client_create(&_gearman_client) == NULL)
 
177
    if (sysvar_logging_gearman_enable == false)
 
178
      return;
 
179
 
 
180
    if (sysvar_logging_gearman_host == NULL)
 
181
      return;
 
182
 
 
183
 
 
184
    if (gearman_client_create(&gearman_client) == NULL)
184
185
    {
185
 
      drizzled::sql_perror(_("fail gearman_client_create()"));
 
186
      errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
 
187
                    strerror(errno));
186
188
      return;
187
189
    }
188
190
 
189
191
    /* TODO, be able to override the port */
190
192
    /* TODO, be able send to multiple servers */
191
 
    ret= gearman_client_add_server(&_gearman_client,
192
 
                                   host.c_str(), 0);
 
193
    ret= gearman_client_add_server(&gearman_client,
 
194
                                   sysvar_logging_gearman_host, 0);
193
195
    if (ret != GEARMAN_SUCCESS)
194
196
    {
195
 
      drizzled::errmsg_printf(drizzled::error::ERROR, _("fail gearman_client_add_server(): %s"),
196
 
                              gearman_client_error(&_gearman_client));
 
197
      errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
 
198
                    gearman_client_error(&gearman_client));
197
199
      return;
198
200
    }
199
201
 
200
 
    _gearman_client_ok= 1;
 
202
    gearman_client_ok= 1;
201
203
 
202
204
  }
203
205
 
204
206
  ~LoggingGearman()
205
207
  {
206
 
    if (_gearman_client_ok)
 
208
    if (gearman_client_ok)
207
209
    {
208
 
      gearman_client_free(&_gearman_client);
 
210
      gearman_client_free(&gearman_client);
209
211
    }
210
212
  }
211
213
 
212
 
  virtual bool post(drizzled::Session *session)
 
214
  virtual bool post(Session *session)
213
215
  {
214
 
    boost::scoped_array<char> msgbuf(new char[MAX_MSG_LEN]);
 
216
    char msgbuf[MAX_MSG_LEN];
215
217
    int msgbuf_len= 0;
216
218
  
217
219
    assert(session != NULL);
220
222
       but that crashes the server, so for now, we just lie a little bit
221
223
    */
222
224
 
223
 
    if (not _gearman_client_ok)
 
225
    if (!gearman_client_ok)
224
226
        return false;
225
227
  
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);
232
 
  
233
 
 
 
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 */
 
231
  
 
232
    uint64_t t_mark= get_microtime();
 
233
  
234
234
    // buffer to quotify the query
235
235
    unsigned char qs[255];
236
236
  
237
237
    // to avoid trying to printf %s something that is potentially NULL
238
 
    drizzled::util::string::const_shared_ptr dbs(session->schema());
 
238
    const char *dbs= (session->db) ? session->db : "";
 
239
    int dbl= 0;
 
240
    if (dbs != NULL)
 
241
      dbl= session->db_length;
 
242
  
 
243
    // todo, add hostname, listener port, and server id to this
239
244
  
240
245
    msgbuf_len=
241
 
      snprintf(msgbuf.get(), MAX_MSG_LEN,
 
246
      snprintf(msgbuf, MAX_MSG_LEN,
242
247
               "%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
243
 
               "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
244
 
               "%"PRIu32",%"PRIu32",%"PRIu32",\"%s\"",
 
248
               "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64
 
249
               "%"PRIu32",%"PRIu32"",
245
250
               t_mark,
246
251
               session->thread_id,
247
 
               session->getQueryId(),
 
252
               session->query_id,
248
253
               // dont need to quote the db name, always CSV safe
249
 
               (int)dbs->size(), dbs->c_str(),
 
254
               dbl, dbs,
250
255
               // 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(),
 
256
               quotify((unsigned char *)session->query,
 
257
                       session->query_length, qs, sizeof(qs)),
 
258
               // command_name is defined in drizzled/sql_parse.cc
 
259
               // dont need to quote the command name, always CSV safe
 
260
               (int)command_name[session->command].length,
 
261
               command_name[session->command].str,
256
262
               // counters are at end, to make it easier to add more
257
263
               (t_mark - session->getConnectMicroseconds()),
258
 
               (session->getElapsedTime()),
 
264
               (t_mark - session->start_utime),
259
265
               (t_mark - session->utime_after_lock),
260
266
               session->sent_row_count,
261
267
               session->examined_row_count,
262
268
               session->tmp_table,
263
 
               session->total_warn_count,
264
 
               session->getServerId(),
265
 
               drizzled::glob_hostname
266
 
               );
 
269
               session->total_warn_count);
267
270
  
268
271
    char job_handle[GEARMAN_JOB_HANDLE_SIZE];
269
272
  
270
 
    (void) gearman_client_do_background(&_gearman_client,
271
 
                                        _function.c_str(),
 
273
    (void) gearman_client_do_background(&gearman_client,
 
274
                                        sysvar_logging_gearman_function,
272
275
                                        NULL,
273
 
                                        (void *) msgbuf.get(),
 
276
                                        (void *) msgbuf,
274
277
                                        (size_t) msgbuf_len,
275
278
                                        job_handle);
276
279
  
278
281
  }
279
282
};
280
283
 
281
 
static LoggingGearman *handler= NULL;
282
 
 
283
 
static int logging_gearman_plugin_init(drizzled::module::Context &context)
284
 
{
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>());
289
 
  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
 
 
293
 
  return 0;
294
 
}
295
 
 
296
 
static void init_options(drizzled::module::option_context &context)
297
 
{
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"));
304
 
}
305
 
 
306
 
} /* namespace drizzle_plugin */
307
 
 
308
 
DRIZZLE_DECLARE_PLUGIN
309
 
{
310
 
  DRIZZLE_VERSION_ID,
311
 
    "logging-gearman",
 
284
static Logging_handler *handler= NULL;
 
285
 
 
286
static int logging_gearman_plugin_init(PluginRegistry &registry)
 
287
{
 
288
  handler= new LoggingGearman();
 
289
  registry.add(handler);
 
290
 
 
291
  return 0;
 
292
}
 
293
 
 
294
static int logging_gearman_plugin_deinit(PluginRegistry &registry)
 
295
{
 
296
  registry.remove(handler);
 
297
  delete(handler);
 
298
 
 
299
  return 0;
 
300
}
 
301
 
 
302
static DRIZZLE_SYSVAR_BOOL(
 
303
                           enable,
 
304
                           sysvar_logging_gearman_enable,
 
305
                           PLUGIN_VAR_NOCMDARG,
 
306
                           N_("Enable logging to a gearman server"),
 
307
                           NULL, /* check func */
 
308
                           NULL, /* update func */
 
309
                           false /* default */);
 
310
 
 
311
static DRIZZLE_SYSVAR_STR(
 
312
                          host,
 
313
                          sysvar_logging_gearman_host,
 
314
                          PLUGIN_VAR_READONLY,
 
315
                          N_("Hostname for logging to a Gearman server"),
 
316
                          NULL, /* check func */
 
317
                          NULL, /* update func*/
 
318
                          "localhost" /* default */);
 
319
 
 
320
static DRIZZLE_SYSVAR_STR(
 
321
                          function,
 
322
                          sysvar_logging_gearman_function,
 
323
                          PLUGIN_VAR_READONLY,
 
324
                          N_("Gearman Function to send logging to"),
 
325
                          NULL, /* check func */
 
326
                          NULL, /* update func*/
 
327
                          "drizzlelog" /* default */);
 
328
 
 
329
static struct st_mysql_sys_var* logging_gearman_system_variables[]= {
 
330
  DRIZZLE_SYSVAR(enable),
 
331
  DRIZZLE_SYSVAR(host),
 
332
  DRIZZLE_SYSVAR(function),
 
333
  NULL
 
334
};
 
335
 
 
336
drizzle_declare_plugin(logging_gearman)
 
337
{
 
338
    "logging_gearman",
312
339
    "0.1",
313
340
    "Mark Atwood <mark@fallenpegasus.com>",
314
341
    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
 
342
    PLUGIN_LICENSE_GPL,
 
343
    logging_gearman_plugin_init,
 
344
    logging_gearman_plugin_deinit,
 
345
    NULL,   /* status variables */
 
346
    logging_gearman_system_variables,
 
347
    NULL
319
348
}
320
 
DRIZZLE_DECLARE_PLUGIN_END;
 
349
drizzle_declare_plugin_end;