~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_gearman/logging_gearman.cc

  • Committer: Brian Aker
  • Date: 2010-08-18 19:37:19 UTC
  • mto: This revision was merged to the branch mainline in revision 1720.
  • Revision ID: brian@tangent.org-20100818193719-bxxzn1pi22styowd
created function that can be used to simply crash the server.

Show diffs side-by-side

added added

removed removed

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