~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-11-16 23:47:43 UTC
  • mto: (584.1.10 devel)
  • mto: This revision was merged to the branch mainline in revision 589.
  • Revision ID: monty@inaugust.com-20081116234743-c38gmv0pa2kdefaj
BrokeĀ outĀ cached_item.

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::Context &context)
294
 
{
295
 
  handler= new LoggingGearman();
296
 
  context.add(handler);
297
 
 
298
 
  return 0;
299
 
}
300
 
 
301
 
static DRIZZLE_SYSVAR_BOOL(
302
 
                           enable,
303
 
                           sysvar_logging_gearman_enable,
304
 
                           PLUGIN_VAR_NOCMDARG,
305
 
                           N_("Enable logging to a gearman server"),
306
 
                           NULL, /* check func */
307
 
                           NULL, /* update func */
308
 
                           false /* default */);
309
 
 
310
 
static DRIZZLE_SYSVAR_STR(
311
 
                          host,
312
 
                          sysvar_logging_gearman_host,
313
 
                          PLUGIN_VAR_READONLY,
314
 
                          N_("Hostname for logging to a Gearman server"),
315
 
                          NULL, /* check func */
316
 
                          NULL, /* update func*/
317
 
                          "localhost" /* default */);
318
 
 
319
 
static DRIZZLE_SYSVAR_STR(
320
 
                          function,
321
 
                          sysvar_logging_gearman_function,
322
 
                          PLUGIN_VAR_READONLY,
323
 
                          N_("Gearman Function to send logging to"),
324
 
                          NULL, /* check func */
325
 
                          NULL, /* update func*/
326
 
                          "drizzlelog" /* default */);
327
 
 
328
 
static drizzle_sys_var* logging_gearman_system_variables[]= {
329
 
  DRIZZLE_SYSVAR(enable),
330
 
  DRIZZLE_SYSVAR(host),
331
 
  DRIZZLE_SYSVAR(function),
332
 
  NULL
333
 
};
334
 
 
335
 
DRIZZLE_DECLARE_PLUGIN
336
 
{
337
 
  DRIZZLE_VERSION_ID,
338
 
    "logging_gearman",
339
 
    "0.1",
340
 
    "Mark Atwood <mark@fallenpegasus.com>",
341
 
    N_("Log queries to a Gearman server"),
342
 
    PLUGIN_LICENSE_GPL,
343
 
    logging_gearman_plugin_init,
344
 
    logging_gearman_system_variables,
345
 
    NULL
346
 
}
347
 
DRIZZLE_DECLARE_PLUGIN_END;