~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_gearman/logging_gearman.cc

  • Committer: Monty Taylor
  • Date: 2008-08-02 00:06:32 UTC
  • mto: (236.1.42 codestyle)
  • mto: This revision was merged to the branch mainline in revision 261.
  • Revision ID: monty@inaugust.com-20080802000632-jsse0zdd9r6ic5ku
Actually turn gettext on...

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008,2009 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
#include "config.h"
21
 
#include <drizzled/plugin/logging.h>
22
 
#include <drizzled/gettext.h>
23
 
#include <drizzled/session.h>
24
 
#include <boost/program_options.hpp>
25
 
#include <drizzled/module/option_map.h>
26
 
#include <libgearman/gearman.h>
27
 
#include <limits.h>
28
 
#include <sys/time.h>
29
 
#include <sys/types.h>
30
 
#include <sys/stat.h>
31
 
#include <fcntl.h>
32
 
#include <cstdio>
33
 
#include <cerrno>
34
 
 
35
 
using namespace drizzled;
36
 
namespace po= boost::program_options;
37
 
 
38
 
/* TODO make this dynamic as needed */
39
 
static const int MAX_MSG_LEN= 32*1024;
40
 
 
41
 
static bool sysvar_logging_gearman_enable;
42
 
static char* sysvar_logging_gearman_host= NULL;
43
 
static char* sysvar_logging_gearman_function= NULL;
44
 
 
45
 
 
46
 
/* stolen from mysys/my_getsystime
47
 
   until the Session has a good utime "now" we can use
48
 
   will have to use this instead */
49
 
 
50
 
static uint64_t get_microtime()
51
 
{
52
 
#if defined(HAVE_GETHRTIME)
53
 
  return gethrtime()/1000;
54
 
#else
55
 
  uint64_t newtime;
56
 
  struct timeval t;
57
 
  /*
58
 
    The following loop is here because gettimeofday may fail on some systems
59
 
  */
60
 
  while (gettimeofday(&t, NULL) != 0) {}
61
 
  newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
62
 
  return newtime;
63
 
#endif  /* defined(HAVE_GETHRTIME) */
64
 
}
65
 
 
66
 
/* quote a string to be safe to include in a CSV line
67
 
   that means backslash quoting all commas, doublequotes, backslashes,
68
 
   and all the ASCII unprintable characters
69
 
   as long as we pass the high-bit bytes unchanged
70
 
   this is safe to do to a UTF8 string
71
 
   we dont allow overrunning the targetbuffer
72
 
   to avoid having a very long query overwrite memory
73
 
 
74
 
   TODO consider remapping the unprintables instead to "Printable
75
 
   Representation", the Unicode characters from the area U+2400 to
76
 
   U+2421 reserved for representing control characters when it is
77
 
   necessary to print or display them rather than have them perform
78
 
   their intended function.
79
 
 
80
 
*/
81
 
static unsigned char *quotify (const unsigned char *src, size_t srclen,
82
 
                               unsigned char *dst, size_t dstlen)
83
 
{
84
 
  static const char hexit[]= { '0', '1', '2', '3', '4', '5', '6', '7',
85
 
                               '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
86
 
  size_t dst_ndx;  /* ndx down the dst */
87
 
  size_t src_ndx;  /* ndx down the src */
88
 
 
89
 
  assert(dst);
90
 
  assert(dstlen > 0);
91
 
 
92
 
  for (dst_ndx= 0,src_ndx= 0; src_ndx < srclen; src_ndx++)
93
 
    {
94
 
 
95
 
      /* Worst case, need 5 dst bytes for the next src byte.
96
 
         backslash x hexit hexit null
97
 
         so if not enough room, just terminate the string and return
98
 
      */
99
 
      if ((dstlen - dst_ndx) < 5)
100
 
        {
101
 
          dst[dst_ndx]= (unsigned char)0x00;
102
 
          return dst;
103
 
        }
104
 
 
105
 
      if (src[src_ndx] > 0x7f)
106
 
        {
107
 
          // pass thru high bit characters, they are non-ASCII UTF8 Unicode
108
 
          dst[dst_ndx++]= src[src_ndx];
109
 
        }
110
 
      else if (src[src_ndx] == 0x00)  // null
111
 
        {
112
 
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) '0';
113
 
        }
114
 
      else if (src[src_ndx] == 0x07)  // bell
115
 
        {
116
 
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'a';
117
 
        }
118
 
      else if (src[src_ndx] == 0x08)  // backspace
119
 
        {
120
 
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'b';
121
 
        }
122
 
      else if (src[src_ndx] == 0x09)  // horiz tab
123
 
        {
124
 
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 't';
125
 
        }
126
 
      else if (src[src_ndx] == 0x0a)  // line feed
127
 
        {
128
 
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'n';
129
 
        }
130
 
      else if (src[src_ndx] == 0x0b)  // vert tab
131
 
        {
132
 
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'v';
133
 
        }
134
 
      else if (src[src_ndx] == 0x0c)  // formfeed
135
 
        {
136
 
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'f';
137
 
        }
138
 
      else if (src[src_ndx] == 0x0d)  // carrage return
139
 
        {
140
 
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'r';
141
 
        }
142
 
      else if (src[src_ndx] == 0x1b)  // escape
143
 
        {
144
 
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'e';
145
 
        }
146
 
      else if (src[src_ndx] == 0x22)  // quotation mark
147
 
        {
148
 
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x22;
149
 
        }
150
 
      else if (src[src_ndx] == 0x2C)  // comma
151
 
        {
152
 
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x2C;
153
 
        }
154
 
      else if (src[src_ndx] == 0x5C)  // backslash
155
 
        {
156
 
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x5C;
157
 
        }
158
 
      else if ((src[src_ndx] < 0x20) || (src[src_ndx] == 0x7F))  // other unprintable ASCII
159
 
        {
160
 
          dst[dst_ndx++]= 0x5C;
161
 
          dst[dst_ndx++]= (unsigned char) 'x';
162
 
          dst[dst_ndx++]= hexit[(src[src_ndx] >> 4) & 0x0f];
163
 
          dst[dst_ndx++]= hexit[src[src_ndx] & 0x0f];
164
 
        }
165
 
      else  // everything else
166
 
        {
167
 
          dst[dst_ndx++]= src[src_ndx];
168
 
        }
169
 
      dst[dst_ndx]= '\0';
170
 
    }
171
 
  return dst;
172
 
}
173
 
 
174
 
class LoggingGearman : public plugin::Logging
175
 
{
176
 
 
177
 
  int gearman_client_ok;
178
 
  gearman_client_st gearman_client;
179
 
 
180
 
public:
181
 
 
182
 
  LoggingGearman()
183
 
    : plugin::Logging("LoggingGearman"),
184
 
      gearman_client_ok(0)
185
 
  {
186
 
    gearman_return_t ret;
187
 
 
188
 
    if (sysvar_logging_gearman_enable == false)
189
 
      return;
190
 
 
191
 
    if (sysvar_logging_gearman_host == NULL)
192
 
      return;
193
 
 
194
 
 
195
 
    if (gearman_client_create(&gearman_client) == NULL)
196
 
    {
197
 
      char errmsg[STRERROR_MAX];
198
 
      strerror_r(errno, errmsg, sizeof(errmsg));
199
 
      errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
200
 
                    errmsg);
201
 
      return;
202
 
    }
203
 
 
204
 
    /* TODO, be able to override the port */
205
 
    /* TODO, be able send to multiple servers */
206
 
    ret= gearman_client_add_server(&gearman_client,
207
 
                                   sysvar_logging_gearman_host, 0);
208
 
    if (ret != GEARMAN_SUCCESS)
209
 
    {
210
 
      errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
211
 
                    gearman_client_error(&gearman_client));
212
 
      return;
213
 
    }
214
 
 
215
 
    gearman_client_ok= 1;
216
 
 
217
 
  }
218
 
 
219
 
  ~LoggingGearman()
220
 
  {
221
 
    if (gearman_client_ok)
222
 
    {
223
 
      gearman_client_free(&gearman_client);
224
 
    }
225
 
  }
226
 
 
227
 
  virtual bool post(Session *session)
228
 
  {
229
 
    char msgbuf[MAX_MSG_LEN];
230
 
    int msgbuf_len= 0;
231
 
  
232
 
    assert(session != NULL);
233
 
 
234
 
    /* in theory, we should return "true", meaning that the plugin isn't happy,
235
 
       but that crashes the server, so for now, we just lie a little bit
236
 
    */
237
 
 
238
 
    if (!gearman_client_ok)
239
 
        return false;
240
 
  
241
 
    /* TODO, the session object should have a "utime command completed"
242
 
       inside itself, so be more accurate, and so this doesnt have to
243
 
       keep calling current_utime, which can be slow */
244
 
  
245
 
    uint64_t t_mark= get_microtime();
246
 
  
247
 
    // buffer to quotify the query
248
 
    unsigned char qs[255];
249
 
  
250
 
    // to avoid trying to printf %s something that is potentially NULL
251
 
    const char *dbs= session->db.empty() ? "" : session->db.c_str();
252
 
  
253
 
    msgbuf_len=
254
 
      snprintf(msgbuf, MAX_MSG_LEN,
255
 
               "%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
256
 
               "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
257
 
               "%"PRIu32",%"PRIu32",%"PRIu32",\"%s\"",
258
 
               t_mark,
259
 
               session->thread_id,
260
 
               session->getQueryId(),
261
 
               // dont need to quote the db name, always CSV safe
262
 
               (int)session->db.length(), dbs,
263
 
               // do need to quote the query
264
 
               quotify((const unsigned char *)session->getQueryString().c_str(),
265
 
                       session->getQueryLength(), qs, sizeof(qs)),
266
 
               // command_name is defined in drizzled/sql_parse.cc
267
 
               // dont need to quote the command name, always CSV safe
268
 
               (int)command_name[session->command].length,
269
 
               command_name[session->command].str,
270
 
               // counters are at end, to make it easier to add more
271
 
               (t_mark - session->getConnectMicroseconds()),
272
 
               (t_mark - session->start_utime),
273
 
               (t_mark - session->utime_after_lock),
274
 
               session->sent_row_count,
275
 
               session->examined_row_count,
276
 
               session->tmp_table,
277
 
               session->total_warn_count,
278
 
               session->getServerId(),
279
 
               glob_hostname
280
 
               );
281
 
  
282
 
    char job_handle[GEARMAN_JOB_HANDLE_SIZE];
283
 
  
284
 
    (void) gearman_client_do_background(&gearman_client,
285
 
                                        sysvar_logging_gearman_function,
286
 
                                        NULL,
287
 
                                        (void *) msgbuf,
288
 
                                        (size_t) msgbuf_len,
289
 
                                        job_handle);
290
 
  
291
 
    return false;
292
 
  }
293
 
};
294
 
 
295
 
static LoggingGearman *handler= NULL;
296
 
 
297
 
static int logging_gearman_plugin_init(module::Context &context)
298
 
{
299
 
  handler= new LoggingGearman();
300
 
  context.add(handler);
301
 
 
302
 
  return 0;
303
 
}
304
 
 
305
 
static void init_options(drizzled::module::option_context &context)
306
 
{
307
 
  context("enable",
308
 
          po::value<bool>(&sysvar_logging_gearman_enable)->default_value(false)->zero_tokens(),
309
 
          N_("Enable logging to a gearman server"));
310
 
}
311
 
 
312
 
static DRIZZLE_SYSVAR_BOOL(
313
 
                           enable,
314
 
                           sysvar_logging_gearman_enable,
315
 
                           PLUGIN_VAR_NOCMDARG,
316
 
                           N_("Enable logging to a gearman server"),
317
 
                           NULL, /* check func */
318
 
                           NULL, /* update func */
319
 
                           false /* default */);
320
 
 
321
 
static DRIZZLE_SYSVAR_STR(
322
 
                          host,
323
 
                          sysvar_logging_gearman_host,
324
 
                          PLUGIN_VAR_READONLY,
325
 
                          N_("Hostname for logging to a Gearman server"),
326
 
                          NULL, /* check func */
327
 
                          NULL, /* update func*/
328
 
                          "localhost" /* default */);
329
 
 
330
 
static DRIZZLE_SYSVAR_STR(
331
 
                          function,
332
 
                          sysvar_logging_gearman_function,
333
 
                          PLUGIN_VAR_READONLY,
334
 
                          N_("Gearman Function to send logging to"),
335
 
                          NULL, /* check func */
336
 
                          NULL, /* update func*/
337
 
                          "drizzlelog" /* default */);
338
 
 
339
 
static drizzle_sys_var* logging_gearman_system_variables[]= {
340
 
  DRIZZLE_SYSVAR(enable),
341
 
  DRIZZLE_SYSVAR(host),
342
 
  DRIZZLE_SYSVAR(function),
343
 
  NULL
344
 
};
345
 
 
346
 
DRIZZLE_DECLARE_PLUGIN
347
 
{
348
 
  DRIZZLE_VERSION_ID,
349
 
    "logging-gearman",
350
 
    "0.1",
351
 
    "Mark Atwood <mark@fallenpegasus.com>",
352
 
    N_("Log queries to a Gearman server"),
353
 
    PLUGIN_LICENSE_GPL,
354
 
    logging_gearman_plugin_init,
355
 
    logging_gearman_system_variables,
356
 
    init_options
357
 
}
358
 
DRIZZLE_DECLARE_PLUGIN_END;