~drizzle-trunk/drizzle/development

910.7.1 by Mark Atwood
add gearman logging plugin
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008, 2009 Sun Microsystems, Inc.
910.7.1 by Mark Atwood
add gearman logging plugin
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
20
#include "config.h"
1929.1.40 by Monty Taylor
Replaced auto_ptr with scoped_ptr.
21
22
#include <boost/scoped_array.hpp>
23
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
24
#include <drizzled/plugin/logging.h>
910.7.1 by Mark Atwood
add gearman logging plugin
25
#include <drizzled/gettext.h>
26
#include <drizzled/session.h>
1878.10.1 by Billy Earney
removed my_micro_time, my_micro_time_and_time, along with my_getsystime and replaced with boost:date_time for compatibility between OS.
27
#include <boost/date_time.hpp>
1660.6.1 by Vijay Samuel
Merge refactored commandline for logging_gearman using boost::program_options
28
#include <boost/program_options.hpp>
29
#include <drizzled/module/option_map.h>
910.7.1 by Mark Atwood
add gearman logging plugin
30
#include <libgearman/gearman.h>
1259.7.4 by Monty Taylor
Fixed namespace and include issues in some plugins we hadn't been building.
31
#include <limits.h>
32
#include <sys/types.h>
33
#include <sys/stat.h>
34
#include <fcntl.h>
1502.3.1 by iwamatsu at nigauri
Add cstdio include to files needing it. Fixes the build on some debian
35
#include <cstdio>
1702.3.3 by LinuxJedi
Fix errors in FreeBSD build
36
#include <cerrno>
1929.1.10 by Stewart Smith
fix large stack usage in logging_gearman:
37
#include <memory>
1259.7.4 by Monty Taylor
Fixed namespace and include issues in some plugins we hadn't been building.
38
1964.2.18 by Monty Taylor
logging_gearman
39
40
namespace drizzle_plugin
41
{
42
1660.6.1 by Vijay Samuel
Merge refactored commandline for logging_gearman using boost::program_options
43
namespace po= boost::program_options;
910.7.1 by Mark Atwood
add gearman logging plugin
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
1964.2.18 by Monty Taylor
logging_gearman
156
class LoggingGearman :
157
  public drizzled::plugin::Logging
910.7.1 by Mark Atwood
add gearman logging plugin
158
{
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
159
1964.2.18 by Monty Taylor
logging_gearman
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&);
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
168
968.2.23 by Monty Taylor
Fixed lack-of-NULL.
169
public:
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
170
1964.2.18 by Monty Taylor
logging_gearman
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()
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
178
  {
179
    gearman_return_t ret;
180
1964.2.18 by Monty Taylor
logging_gearman
181
182
    if (gearman_client_create(&_gearman_client) == NULL)
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
183
    {
1702.3.2 by LinuxJedi
Migrate the rest of strerror to strerror_r
184
      char errmsg[STRERROR_MAX];
185
      strerror_r(errno, errmsg, sizeof(errmsg));
1964.2.18 by Monty Taylor
logging_gearman
186
      drizzled::errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
187
                              errmsg);
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
188
      return;
189
    }
190
191
    /* TODO, be able to override the port */
192
    /* TODO, be able send to multiple servers */
1964.2.18 by Monty Taylor
logging_gearman
193
    ret= gearman_client_add_server(&_gearman_client,
194
                                   host.c_str(), 0);
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
195
    if (ret != GEARMAN_SUCCESS)
196
    {
1964.2.18 by Monty Taylor
logging_gearman
197
      drizzled::errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
198
                              gearman_client_error(&_gearman_client));
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
199
      return;
200
    }
201
1964.2.18 by Monty Taylor
logging_gearman
202
    _gearman_client_ok= 1;
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
203
204
  }
205
206
  ~LoggingGearman()
207
  {
1964.2.18 by Monty Taylor
logging_gearman
208
    if (_gearman_client_ok)
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
209
    {
1964.2.18 by Monty Taylor
logging_gearman
210
      gearman_client_free(&_gearman_client);
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
211
    }
212
  }
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
213
1964.2.18 by Monty Taylor
logging_gearman
214
  virtual bool post(drizzled::Session *session)
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
215
  {
1929.1.40 by Monty Taylor
Replaced auto_ptr with scoped_ptr.
216
    boost::scoped_array<char> msgbuf(new char[MAX_MSG_LEN]);
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
217
    int msgbuf_len= 0;
218
  
219
    assert(session != NULL);
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
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
1964.2.18 by Monty Taylor
logging_gearman
225
    if (not _gearman_client_ok)
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
226
        return false;
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
227
  
2040.4.3 by Brian Aker
Scale down the calls to universal_time().
228
    /* 
229
      TODO, the session object should have a "utime command completed"
230
      inside itself, so be more accurate, and so this doesnt have to
231
      keep calling current_utime, which can be slow.
232
    */
233
    uint64_t t_mark= session->getCurrentTimestamp(false);
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
234
  
1878.10.1 by Billy Earney
removed my_micro_time, my_micro_time_and_time, along with my_getsystime and replaced with boost:date_time for compatibility between OS.
235
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
236
    // buffer to quotify the query
237
    unsigned char qs[255];
238
  
239
    // to avoid trying to printf %s something that is potentially NULL
1976.5.4 by Brian Aker
Update schema, make sure that it always ruturns a valid string (it just
240
    drizzled::util::string::const_shared_ptr dbs(session->schema());
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
241
  
242
    msgbuf_len=
1929.1.10 by Stewart Smith
fix large stack usage in logging_gearman:
243
      snprintf(msgbuf.get(), MAX_MSG_LEN,
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
244
               "%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
1120.1.1 by Diego Medina
* Added hostname, listener port, and server id to the logging_query and logging_gearman plugins
245
               "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
971.7.8 by Eric Day
Moved port options into protocol plugin and added MySQL protocol support.
246
               "%"PRIu32",%"PRIu32",%"PRIu32",\"%s\"",
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
247
               t_mark,
248
               session->thread_id,
1120.1.2 by Diego Medina
Use accessors when available.
249
               session->getQueryId(),
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
250
               // dont need to quote the db name, always CSV safe
1976.5.4 by Brian Aker
Update schema, make sure that it always ruturns a valid string (it just
251
               (int)dbs->size(), dbs->c_str(),
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
252
               // do need to quote the query
1932.2.6 by Brian Aker
Merge in additional fixes for plugin.
253
               quotify((const unsigned char *)session->getQueryString()->c_str(), session->getQueryString()->length(), qs, sizeof(qs)),
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
254
               // command_name is defined in drizzled/sql_parse.cc
255
               // dont need to quote the command name, always CSV safe
1964.2.18 by Monty Taylor
logging_gearman
256
               (int)drizzled::command_name[session->command].length,
257
               drizzled::command_name[session->command].str,
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
258
               // counters are at end, to make it easier to add more
1055.3.6 by Jay Pipes
Adds public getConnectMicroseconds() call to Session object. Protects connect_microseconds member variable and sets it correctly. Corrects logging_xxx plugins to use getConnectMicroseconds() API call properly. Fixes Bug #402855
259
               (t_mark - session->getConnectMicroseconds()),
2045.1.2 by Brian Aker
Merge in fix for icc find for not normally compiled module
260
               (session->getElapsedTime()),
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
261
               (t_mark - session->utime_after_lock),
262
               session->sent_row_count,
1039.4.2 by Mark Atwood
add more info to gearman logging
263
               session->examined_row_count,
264
               session->tmp_table,
1120.1.1 by Diego Medina
* Added hostname, listener port, and server id to the logging_query and logging_gearman plugins
265
               session->total_warn_count,
266
               session->getServerId(),
1964.2.18 by Monty Taylor
logging_gearman
267
               drizzled::glob_hostname
1120.1.1 by Diego Medina
* Added hostname, listener port, and server id to the logging_query and logging_gearman plugins
268
               );
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
269
  
270
    char job_handle[GEARMAN_JOB_HANDLE_SIZE];
271
  
1964.2.18 by Monty Taylor
logging_gearman
272
    (void) gearman_client_do_background(&_gearman_client,
273
                                        _function.c_str(),
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
274
                                        NULL,
1929.1.10 by Stewart Smith
fix large stack usage in logging_gearman:
275
                                        (void *) msgbuf.get(),
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
276
                                        (size_t) msgbuf_len,
277
                                        job_handle);
278
  
279
    return false;
280
  }
281
};
910.7.1 by Mark Atwood
add gearman logging plugin
282
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
283
static LoggingGearman *handler= NULL;
971.1.51 by Monty Taylor
New-style plugin registration now works.
284
1964.2.18 by Monty Taylor
logging_gearman
285
static int logging_gearman_plugin_init(drizzled::module::Context &context)
910.7.1 by Mark Atwood
add gearman logging plugin
286
{
1964.2.18 by Monty Taylor
logging_gearman
287
  const drizzled::module::option_map &vm= context.getOptions();
288
289
  handler= new LoggingGearman(vm["host"].as<std::string>(),
290
                              vm["function"].as<std::string>());
1324.2.2 by Monty Taylor
Use the plugin::Context everywhere.
291
  context.add(handler);
1964.2.18 by Monty Taylor
logging_gearman
292
  context.registerVariable(new drizzled::sys_var_const_string_val("host", vm["host"].as<std::string>()));
293
  context.registerVariable(new drizzled::sys_var_const_string_val("function", vm["function"].as<std::string>()));
910.7.1 by Mark Atwood
add gearman logging plugin
294
295
  return 0;
296
}
297
1660.6.1 by Vijay Samuel
Merge refactored commandline for logging_gearman using boost::program_options
298
static void init_options(drizzled::module::option_context &context)
299
{
1964.2.18 by Monty Taylor
logging_gearman
300
  context("host",
301
          po::value<std::string>()->default_value("localhost"),
2068.4.1 by Andrew Hutchings
Fix intl domain
302
          _("Hostname for logging to a Gearman server"));
1964.2.18 by Monty Taylor
logging_gearman
303
  context("function",
304
          po::value<std::string>()->default_value("drizzlelog"),
2068.4.1 by Andrew Hutchings
Fix intl domain
305
          _("Gearman Function to send logging to"));
1660.6.1 by Vijay Samuel
Merge refactored commandline for logging_gearman using boost::program_options
306
}
307
1964.2.18 by Monty Taylor
logging_gearman
308
} /* namespace drizzle_plugin */
910.7.1 by Mark Atwood
add gearman logging plugin
309
1228.1.5 by Monty Taylor
Merged in some naming things.
310
DRIZZLE_DECLARE_PLUGIN
910.7.1 by Mark Atwood
add gearman logging plugin
311
{
1241.10.2 by Monty Taylor
Added support for embedding the drizzle version number in the plugin file.
312
  DRIZZLE_VERSION_ID,
1660.6.1 by Vijay Samuel
Merge refactored commandline for logging_gearman using boost::program_options
313
    "logging-gearman",
910.7.1 by Mark Atwood
add gearman logging plugin
314
    "0.1",
315
    "Mark Atwood <mark@fallenpegasus.com>",
316
    N_("Log queries to a Gearman server"),
1964.2.18 by Monty Taylor
logging_gearman
317
    drizzled::PLUGIN_LICENSE_GPL,
318
    drizzle_plugin::logging_gearman_plugin_init,
319
    NULL,
320
    drizzle_plugin::init_options
910.7.1 by Mark Atwood
add gearman logging plugin
321
}
1228.1.5 by Monty Taylor
Merged in some naming things.
322
DRIZZLE_DECLARE_PLUGIN_END;