~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_gearman/logging_gearman.cc

  • Committer: Monty Taylor
  • Date: 2009-04-25 19:24:49 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 1003.
  • Revision ID: mordred@inaugust.com-20090425192449-0htujbt2r9jzupn5
Moved heap.

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