~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_gearman/logging_gearman.cc

  • Committer: Brian Aker
  • Date: 2009-05-11 17:50:22 UTC
  • Revision ID: brian@gaz-20090511175022-y35q9ky6uh9ldcjt
Replacing Sun employee copyright headers (aka... anything done by a Sun
employee is copyright by Sun).

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 <drizzled/server_includes.h>
 
21
#include <drizzled/plugin/logging_handler.h>
 
22
#include <drizzled/gettext.h>
 
23
#include <drizzled/session.h>
 
24
 
 
25
#include <libgearman/gearman.h>
 
26
 
 
27
 
 
28
/* TODO make this dynamic as needed */
 
29
static const int MAX_MSG_LEN= 32*1024;
 
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
static gearman_client_st gearman_client;
 
36
 
 
37
 
 
38
/* stolen from mysys/my_getsystime
 
39
   until the Session has a good utime "now" we can use
 
40
   will have to use this instead */
 
41
 
 
42
#include <sys/time.h>
 
43
static uint64_t get_microtime()
 
44
{
 
45
#if defined(HAVE_GETHRTIME)
 
46
  return gethrtime()/1000;
 
47
#else
 
48
  uint64_t newtime;
 
49
  struct timeval t;
 
50
  /*
 
51
    The following loop is here because gettimeofday may fail on some systems
 
52
  */
 
53
  while (gettimeofday(&t, NULL) != 0) {}
 
54
  newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
 
55
  return newtime;
 
56
#endif  /* defined(HAVE_GETHRTIME) */
 
57
}
 
58
 
 
59
/* quote a string to be safe to include in a CSV line
 
60
   that means backslash quoting all commas, doublequotes, backslashes,
 
61
   and all the ASCII unprintable characters
 
62
   as long as we pass the high-bit bytes unchanged
 
63
   this is safe to do to a UTF8 string
 
64
   we dont allow overrunning the targetbuffer
 
65
   to avoid having a very long query overwrite memory
 
66
 
 
67
   TODO consider remapping the unprintables instead to "Printable
 
68
   Representation", the Unicode characters from the area U+2400 to
 
69
   U+2421 reserved for representing control characters when it is
 
70
   necessary to print or display them rather than have them perform
 
71
   their intended function.
 
72
 
 
73
*/
 
74
static unsigned char *quotify (const unsigned char *src, size_t srclen,
 
75
                               unsigned char *dst, size_t dstlen)
 
76
{
 
77
  static const char hexit[]= { '0', '1', '2', '3', '4', '5', '6', '7',
 
78
                               '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
 
79
  size_t dst_ndx;  /* ndx down the dst */
 
80
  size_t src_ndx;  /* ndx down the src */
 
81
 
 
82
  assert(dst);
 
83
  assert(dstlen > 0);
 
84
 
 
85
  for (dst_ndx= 0,src_ndx= 0; src_ndx < srclen; src_ndx++)
 
86
    {
 
87
 
 
88
      /* Worst case, need 5 dst bytes for the next src byte.
 
89
         backslash x hexit hexit null
 
90
         so if not enough room, just terminate the string and return
 
91
      */
 
92
      if ((dstlen - dst_ndx) < 5)
 
93
        {
 
94
          dst[dst_ndx]= (unsigned char)0x00;
 
95
          return dst;
 
96
        }
 
97
 
 
98
      if (src[src_ndx] > 0x7f)
 
99
        {
 
100
          // pass thru high bit characters, they are non-ASCII UTF8 Unicode
 
101
          dst[dst_ndx++]= src[src_ndx];
 
102
        }
 
103
      else if (src[src_ndx] == 0x00)  // null
 
104
        {
 
105
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) '0';
 
106
        }
 
107
      else if (src[src_ndx] == 0x07)  // bell
 
108
        {
 
109
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'a';
 
110
        }
 
111
      else if (src[src_ndx] == 0x08)  // backspace
 
112
        {
 
113
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'b';
 
114
        }
 
115
      else if (src[src_ndx] == 0x09)  // horiz tab
 
116
        {
 
117
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 't';
 
118
        }
 
119
      else if (src[src_ndx] == 0x0a)  // line feed
 
120
        {
 
121
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'n';
 
122
        }
 
123
      else if (src[src_ndx] == 0x0b)  // vert tab
 
124
        {
 
125
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'v';
 
126
        }
 
127
      else if (src[src_ndx] == 0x0c)  // formfeed
 
128
        {
 
129
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'f';
 
130
        }
 
131
      else if (src[src_ndx] == 0x0d)  // carrage return
 
132
        {
 
133
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'r';
 
134
        }
 
135
      else if (src[src_ndx] == 0x1b)  // escape
 
136
        {
 
137
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'e';
 
138
        }
 
139
      else if (src[src_ndx] == 0x22)  // quotation mark
 
140
        {
 
141
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x22;
 
142
        }
 
143
      else if (src[src_ndx] == 0x2C)  // comma
 
144
        {
 
145
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x2C;
 
146
        }
 
147
      else if (src[src_ndx] == 0x5C)  // backslash
 
148
        {
 
149
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x5C;
 
150
        }
 
151
      else if ((src[src_ndx] < 0x20) || (src[src_ndx] == 0x7F))  // other unprintable ASCII
 
152
        {
 
153
          dst[dst_ndx++]= 0x5C;
 
154
          dst[dst_ndx++]= (unsigned char) 'x';
 
155
          dst[dst_ndx++]= hexit[(src[src_ndx] >> 4) & 0x0f];
 
156
          dst[dst_ndx++]= hexit[src[src_ndx] & 0x0f];
 
157
        }
 
158
      else  // everything else
 
159
        {
 
160
          dst[dst_ndx++]= src[src_ndx];
 
161
        }
 
162
      dst[dst_ndx]= '\0';
 
163
    }
 
164
  return dst;
 
165
}
 
166
 
 
167
class LoggingGearman : public Logging_handler
 
168
{
 
169
public:
 
170
  LoggingGearman() : Logging_handler("LoggingGearman") {}
 
171
 
 
172
  virtual bool post(Session *session)
 
173
  {
 
174
    char msgbuf[MAX_MSG_LEN];
 
175
    int msgbuf_len= 0;
 
176
  
 
177
    assert(session != NULL);
 
178
  
 
179
    if (sysvar_logging_gearman_enable == false)
 
180
      return false;
 
181
  
 
182
    /* TODO, looks like connect_utime isnt being set in the session
 
183
       object.  We could store the time this plugin was loaded, but that
 
184
       would just be a dumb workaround. */
 
185
    /* TODO, the session object should have a "utime command completed"
 
186
       inside itself, so be more accurate, and so this doesnt have to
 
187
       keep calling current_utime, which can be slow */
 
188
  
 
189
    uint64_t t_mark= get_microtime();
 
190
  
 
191
    // buffer to quotify the query
 
192
    unsigned char qs[255];
 
193
  
 
194
    // to avoid trying to printf %s something that is potentially NULL
 
195
    const char *dbs= (session->db) ? session->db : "";
 
196
    int dbl= 0;
 
197
    if (dbs != NULL)
 
198
      dbl= session->db_length;
 
199
  
 
200
    // todo, add hostname, listener port, and server id to this
 
201
  
 
202
    msgbuf_len=
 
203
      snprintf(msgbuf, MAX_MSG_LEN,
 
204
               "%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
 
205
               "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"",
 
206
               t_mark,
 
207
               session->thread_id,
 
208
               session->query_id,
 
209
               // dont need to quote the db name, always CSV safe
 
210
               dbl, dbs,
 
211
               // do need to quote the query
 
212
               quotify((unsigned char *)session->query,
 
213
                       session->query_length, qs, sizeof(qs)),
 
214
               // command_name is defined in drizzled/sql_parse.cc
 
215
               // dont need to quote the command name, always CSV safe
 
216
               (int)command_name[session->command].length,
 
217
               command_name[session->command].str,
 
218
               // counters are at end, to make it easier to add more
 
219
               (t_mark - session->connect_utime),
 
220
               (t_mark - session->start_utime),
 
221
               (t_mark - session->utime_after_lock),
 
222
               session->sent_row_count,
 
223
               session->examined_row_count);
 
224
  
 
225
    char job_handle[GEARMAN_JOB_HANDLE_SIZE];
 
226
  
 
227
    (void) gearman_client_do_background(&gearman_client,
 
228
                                        sysvar_logging_gearman_function,
 
229
                                        NULL,
 
230
                                        (void *) msgbuf,
 
231
                                        (size_t) msgbuf_len,
 
232
                                        job_handle);
 
233
  
 
234
    return false;
 
235
  }
 
236
};
 
237
 
 
238
static Logging_handler *handler= NULL;
 
239
 
 
240
static int logging_gearman_plugin_init(PluginRegistry &registry)
 
241
{
 
242
  gearman_return_t ret;
 
243
 
 
244
  /* TODO
 
245
     saying "return 0" means "success"
 
246
     right now, if we return an error
 
247
     this causes Drizzle to crash
 
248
     so until that is fixed,
 
249
     just return a success,
 
250
     but leave the function pointers as NULL
 
251
  */
 
252
 
 
253
  if (sysvar_logging_gearman_host == NULL)
 
254
  {
 
255
    /* no destination gearman server host was specified via system variables
 
256
       return now, dont set the callback pointers
 
257
    */
 
258
    return 0;
 
259
  }
 
260
 
 
261
  if (gearman_client_create(&gearman_client) == NULL)
 
262
  {
 
263
    errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
 
264
                  strerror(errno));
 
265
    return 0;
 
266
  }
 
267
 
 
268
  /* TODO, be able to override the port */
 
269
  ret= gearman_client_add_server(&gearman_client,
 
270
                                 sysvar_logging_gearman_host, 0);
 
271
  if (ret != GEARMAN_SUCCESS)
 
272
  {
 
273
    errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
 
274
                  gearman_client_error(&gearman_client));
 
275
    return 0;
 
276
  }
 
277
 
 
278
  handler= new LoggingGearman();
 
279
  registry.add(handler);
 
280
 
 
281
  return 0;
 
282
}
 
283
 
 
284
static int logging_gearman_plugin_deinit(PluginRegistry &registry)
 
285
{
 
286
 
 
287
  gearman_client_free(&gearman_client);
 
288
 
 
289
  registry.remove(handler);
 
290
  delete(handler);
 
291
 
 
292
  return 0;
 
293
}
 
294
 
 
295
static DRIZZLE_SYSVAR_BOOL(
 
296
                           enable,
 
297
                           sysvar_logging_gearman_enable,
 
298
                           PLUGIN_VAR_NOCMDARG,
 
299
                           N_("Enable logging to a gearman server"),
 
300
                           NULL, /* check func */
 
301
                           NULL, /* update func */
 
302
                           false /* default */);
 
303
 
 
304
static DRIZZLE_SYSVAR_STR(
 
305
                          host,
 
306
                          sysvar_logging_gearman_host,
 
307
                          PLUGIN_VAR_READONLY,
 
308
                          N_("Hostname for logging to a Gearman server"),
 
309
                          NULL, /* check func */
 
310
                          NULL, /* update func*/
 
311
                          "localhost" /* default */);
 
312
 
 
313
static DRIZZLE_SYSVAR_STR(
 
314
                          function,
 
315
                          sysvar_logging_gearman_function,
 
316
                          PLUGIN_VAR_READONLY,
 
317
                          N_("Gearman Function to send logging to"),
 
318
                          NULL, /* check func */
 
319
                          NULL, /* update func*/
 
320
                          "drizzlelog" /* default */);
 
321
 
 
322
static struct st_mysql_sys_var* logging_gearman_system_variables[]= {
 
323
  DRIZZLE_SYSVAR(enable),
 
324
  DRIZZLE_SYSVAR(host),
 
325
  DRIZZLE_SYSVAR(function),
 
326
  NULL
 
327
};
 
328
 
 
329
drizzle_declare_plugin(logging_gearman)
 
330
{
 
331
    "logging_gearman",
 
332
    "0.1",
 
333
    "Mark Atwood <mark@fallenpegasus.com>",
 
334
    N_("Log queries to a Gearman server"),
 
335
    PLUGIN_LICENSE_GPL,
 
336
    logging_gearman_plugin_init,
 
337
    logging_gearman_plugin_deinit,
 
338
    NULL,   /* status variables */
 
339
    logging_gearman_system_variables,
 
340
    NULL
 
341
}
 
342
drizzle_declare_plugin_end;