~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to plugin/logging_gearman/logging_gearman.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

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
 
 
25
#include <libgearman/gearman.h>
 
26
#include <limits.h>
 
27
#include <sys/time.h>
 
28
#include <sys/types.h>
 
29
#include <sys/stat.h>
 
30
#include <fcntl.h>
 
31
 
 
32
 
 
33
using namespace drizzled;
 
34
 
 
35
 
 
36
/* TODO make this dynamic as needed */
 
37
static const int MAX_MSG_LEN= 32*1024;
 
38
 
 
39
static bool sysvar_logging_gearman_enable= false;
 
40
static char* sysvar_logging_gearman_host= NULL;
 
41
static char* sysvar_logging_gearman_function= NULL;
 
42
 
 
43
 
 
44
/* stolen from mysys/my_getsystime
 
45
   until the Session has a good utime "now" we can use
 
46
   will have to use this instead */
 
47
 
 
48
static uint64_t get_microtime()
 
49
{
 
50
#if defined(HAVE_GETHRTIME)
 
51
  return gethrtime()/1000;
 
52
#else
 
53
  uint64_t newtime;
 
54
  struct timeval t;
 
55
  /*
 
56
    The following loop is here because gettimeofday may fail on some systems
 
57
  */
 
58
  while (gettimeofday(&t, NULL) != 0) {}
 
59
  newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
 
60
  return newtime;
 
61
#endif  /* defined(HAVE_GETHRTIME) */
 
62
}
 
63
 
 
64
/* quote a string to be safe to include in a CSV line
 
65
   that means backslash quoting all commas, doublequotes, backslashes,
 
66
   and all the ASCII unprintable characters
 
67
   as long as we pass the high-bit bytes unchanged
 
68
   this is safe to do to a UTF8 string
 
69
   we dont allow overrunning the targetbuffer
 
70
   to avoid having a very long query overwrite memory
 
71
 
 
72
   TODO consider remapping the unprintables instead to "Printable
 
73
   Representation", the Unicode characters from the area U+2400 to
 
74
   U+2421 reserved for representing control characters when it is
 
75
   necessary to print or display them rather than have them perform
 
76
   their intended function.
 
77
 
 
78
*/
 
79
static unsigned char *quotify (const unsigned char *src, size_t srclen,
 
80
                               unsigned char *dst, size_t dstlen)
 
81
{
 
82
  static const char hexit[]= { '0', '1', '2', '3', '4', '5', '6', '7',
 
83
                               '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
 
84
  size_t dst_ndx;  /* ndx down the dst */
 
85
  size_t src_ndx;  /* ndx down the src */
 
86
 
 
87
  assert(dst);
 
88
  assert(dstlen > 0);
 
89
 
 
90
  for (dst_ndx= 0,src_ndx= 0; src_ndx < srclen; src_ndx++)
 
91
    {
 
92
 
 
93
      /* Worst case, need 5 dst bytes for the next src byte.
 
94
         backslash x hexit hexit null
 
95
         so if not enough room, just terminate the string and return
 
96
      */
 
97
      if ((dstlen - dst_ndx) < 5)
 
98
        {
 
99
          dst[dst_ndx]= (unsigned char)0x00;
 
100
          return dst;
 
101
        }
 
102
 
 
103
      if (src[src_ndx] > 0x7f)
 
104
        {
 
105
          // pass thru high bit characters, they are non-ASCII UTF8 Unicode
 
106
          dst[dst_ndx++]= src[src_ndx];
 
107
        }
 
108
      else if (src[src_ndx] == 0x00)  // null
 
109
        {
 
110
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) '0';
 
111
        }
 
112
      else if (src[src_ndx] == 0x07)  // bell
 
113
        {
 
114
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'a';
 
115
        }
 
116
      else if (src[src_ndx] == 0x08)  // backspace
 
117
        {
 
118
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'b';
 
119
        }
 
120
      else if (src[src_ndx] == 0x09)  // horiz tab
 
121
        {
 
122
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 't';
 
123
        }
 
124
      else if (src[src_ndx] == 0x0a)  // line feed
 
125
        {
 
126
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'n';
 
127
        }
 
128
      else if (src[src_ndx] == 0x0b)  // vert tab
 
129
        {
 
130
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'v';
 
131
        }
 
132
      else if (src[src_ndx] == 0x0c)  // formfeed
 
133
        {
 
134
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'f';
 
135
        }
 
136
      else if (src[src_ndx] == 0x0d)  // carrage return
 
137
        {
 
138
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'r';
 
139
        }
 
140
      else if (src[src_ndx] == 0x1b)  // escape
 
141
        {
 
142
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'e';
 
143
        }
 
144
      else if (src[src_ndx] == 0x22)  // quotation mark
 
145
        {
 
146
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x22;
 
147
        }
 
148
      else if (src[src_ndx] == 0x2C)  // comma
 
149
        {
 
150
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x2C;
 
151
        }
 
152
      else if (src[src_ndx] == 0x5C)  // backslash
 
153
        {
 
154
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x5C;
 
155
        }
 
156
      else if ((src[src_ndx] < 0x20) || (src[src_ndx] == 0x7F))  // other unprintable ASCII
 
157
        {
 
158
          dst[dst_ndx++]= 0x5C;
 
159
          dst[dst_ndx++]= (unsigned char) 'x';
 
160
          dst[dst_ndx++]= hexit[(src[src_ndx] >> 4) & 0x0f];
 
161
          dst[dst_ndx++]= hexit[src[src_ndx] & 0x0f];
 
162
        }
 
163
      else  // everything else
 
164
        {
 
165
          dst[dst_ndx++]= src[src_ndx];
 
166
        }
 
167
      dst[dst_ndx]= '\0';
 
168
    }
 
169
  return dst;
 
170
}
 
171
 
 
172
class LoggingGearman : public plugin::Logging
 
173
{
 
174
 
 
175
  int gearman_client_ok;
 
176
  gearman_client_st gearman_client;
 
177
 
 
178
public:
 
179
 
 
180
  LoggingGearman()
 
181
    : plugin::Logging("LoggingGearman"),
 
182
      gearman_client_ok(0)
 
183
  {
 
184
    gearman_return_t ret;
 
185
 
 
186
    if (sysvar_logging_gearman_enable == false)
 
187
      return;
 
188
 
 
189
    if (sysvar_logging_gearman_host == NULL)
 
190
      return;
 
191
 
 
192
 
 
193
    if (gearman_client_create(&gearman_client) == NULL)
 
194
    {
 
195
      errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
 
196
                    strerror(errno));
 
197
      return;
 
198
    }
 
199
 
 
200
    /* TODO, be able to override the port */
 
201
    /* TODO, be able send to multiple servers */
 
202
    ret= gearman_client_add_server(&gearman_client,
 
203
                                   sysvar_logging_gearman_host, 0);
 
204
    if (ret != GEARMAN_SUCCESS)
 
205
    {
 
206
      errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
 
207
                    gearman_client_error(&gearman_client));
 
208
      return;
 
209
    }
 
210
 
 
211
    gearman_client_ok= 1;
 
212
 
 
213
  }
 
214
 
 
215
  ~LoggingGearman()
 
216
  {
 
217
    if (gearman_client_ok)
 
218
    {
 
219
      gearman_client_free(&gearman_client);
 
220
    }
 
221
  }
 
222
 
 
223
  virtual bool post(Session *session)
 
224
  {
 
225
    char msgbuf[MAX_MSG_LEN];
 
226
    int msgbuf_len= 0;
 
227
  
 
228
    assert(session != NULL);
 
229
 
 
230
    /* in theory, we should return "true", meaning that the plugin isn't happy,
 
231
       but that crashes the server, so for now, we just lie a little bit
 
232
    */
 
233
 
 
234
    if (!gearman_client_ok)
 
235
        return false;
 
236
  
 
237
    /* TODO, the session object should have a "utime command completed"
 
238
       inside itself, so be more accurate, and so this doesnt have to
 
239
       keep calling current_utime, which can be slow */
 
240
  
 
241
    uint64_t t_mark= get_microtime();
 
242
  
 
243
    // buffer to quotify the query
 
244
    unsigned char qs[255];
 
245
  
 
246
    // to avoid trying to printf %s something that is potentially NULL
 
247
    const char *dbs= session->db.empty() ? "" : session->db.c_str();
 
248
  
 
249
    msgbuf_len=
 
250
      snprintf(msgbuf, MAX_MSG_LEN,
 
251
               "%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
 
252
               "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
 
253
               "%"PRIu32",%"PRIu32",%"PRIu32",\"%s\"",
 
254
               t_mark,
 
255
               session->thread_id,
 
256
               session->getQueryId(),
 
257
               // dont need to quote the db name, always CSV safe
 
258
               (int)session->db.length(), dbs,
 
259
               // do need to quote the query
 
260
               quotify((const unsigned char *)session->getQueryString().c_str(),
 
261
                       session->getQueryLength(), qs, sizeof(qs)),
 
262
               // command_name is defined in drizzled/sql_parse.cc
 
263
               // dont need to quote the command name, always CSV safe
 
264
               (int)command_name[session->command].length,
 
265
               command_name[session->command].str,
 
266
               // counters are at end, to make it easier to add more
 
267
               (t_mark - session->getConnectMicroseconds()),
 
268
               (t_mark - session->start_utime),
 
269
               (t_mark - session->utime_after_lock),
 
270
               session->sent_row_count,
 
271
               session->examined_row_count,
 
272
               session->tmp_table,
 
273
               session->total_warn_count,
 
274
               session->getServerId(),
 
275
               glob_hostname
 
276
               );
 
277
  
 
278
    char job_handle[GEARMAN_JOB_HANDLE_SIZE];
 
279
  
 
280
    (void) gearman_client_do_background(&gearman_client,
 
281
                                        sysvar_logging_gearman_function,
 
282
                                        NULL,
 
283
                                        (void *) msgbuf,
 
284
                                        (size_t) msgbuf_len,
 
285
                                        job_handle);
 
286
  
 
287
    return false;
 
288
  }
 
289
};
 
290
 
 
291
static LoggingGearman *handler= NULL;
 
292
 
 
293
static int logging_gearman_plugin_init(plugin::Registry &registry)
 
294
{
 
295
  handler= new LoggingGearman();
 
296
  registry.add(handler);
 
297
 
 
298
  return 0;
 
299
}
 
300
 
 
301
static int logging_gearman_plugin_deinit(plugin::Registry &registry)
 
302
{
 
303
  registry.remove(handler);
 
304
  delete handler;
 
305
 
 
306
  return 0;
 
307
}
 
308
 
 
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
};
 
342
 
 
343
DRIZZLE_DECLARE_PLUGIN
 
344
{
 
345
  DRIZZLE_VERSION_ID,
 
346
    "logging_gearman",
 
347
    "0.1",
 
348
    "Mark Atwood <mark@fallenpegasus.com>",
 
349
    N_("Log queries to a Gearman server"),
 
350
    PLUGIN_LICENSE_GPL,
 
351
    logging_gearman_plugin_init,
 
352
    logging_gearman_plugin_deinit,
 
353
    logging_gearman_system_variables,
 
354
    NULL
 
355
}
 
356
DRIZZLE_DECLARE_PLUGIN_END;