~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_gearman/logging_gearman.cc

  • Committer: Monty Taylor
  • Date: 2009-03-06 03:33:24 UTC
  • mfrom: (916.1.2 merge)
  • Revision ID: mordred@inaugust.com-20090306033324-dcedf80g9qzywbvu
Merged Brian's merge... re-rotate the tree.

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