~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_gearman/logging_gearman.cc

Merged Mark's gearman logging client.

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