~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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
20
#include <config.h>
1929.1.40 by Monty Taylor
Replaced auto_ptr with scoped_ptr.
21
22
#include <boost/scoped_array.hpp>
23
2239.1.11 by Olaf van der Spek
Refactor includes
24
#include <drizzled/plugin.h>
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
25
#include <drizzled/plugin/logging.h>
910.7.1 by Mark Atwood
add gearman logging plugin
26
#include <drizzled/gettext.h>
27
#include <drizzled/session.h>
2269.1.11 by Olaf van der Spek
Session Times
28
#include <drizzled/session/times.h>
2227.4.18 by Olaf van der Spek
Fix include
29
#include <drizzled/sql_parse.h>
2132.1.3 by Brian Aker
Updates.
30
#include <drizzled/errmsg_print.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.
31
#include <boost/date_time.hpp>
1660.6.1 by Vijay Samuel
Merge refactored commandline for logging_gearman using boost::program_options
32
#include <boost/program_options.hpp>
33
#include <drizzled/module/option_map.h>
910.7.1 by Mark Atwood
add gearman logging plugin
34
#include <libgearman/gearman.h>
1259.7.4 by Monty Taylor
Fixed namespace and include issues in some plugins we hadn't been building.
35
#include <limits.h>
36
#include <sys/types.h>
37
#include <sys/stat.h>
38
#include <fcntl.h>
1502.3.1 by iwamatsu at nigauri
Add cstdio include to files needing it. Fixes the build on some debian
39
#include <cstdio>
1702.3.3 by LinuxJedi
Fix errors in FreeBSD build
40
#include <cerrno>
1929.1.10 by Stewart Smith
fix large stack usage in logging_gearman:
41
#include <memory>
1259.7.4 by Monty Taylor
Fixed namespace and include issues in some plugins we hadn't been building.
42
2269.1.11 by Olaf van der Spek
Session Times
43
namespace drizzle_plugin {
1964.2.18 by Monty Taylor
logging_gearman
44
1660.6.1 by Vijay Samuel
Merge refactored commandline for logging_gearman using boost::program_options
45
namespace po= boost::program_options;
910.7.1 by Mark Atwood
add gearman logging plugin
46
47
/* TODO make this dynamic as needed */
48
static const int MAX_MSG_LEN= 32*1024;
49
50
/* quote a string to be safe to include in a CSV line
51
   that means backslash quoting all commas, doublequotes, backslashes,
52
   and all the ASCII unprintable characters
53
   as long as we pass the high-bit bytes unchanged
54
   this is safe to do to a UTF8 string
55
   we dont allow overrunning the targetbuffer
56
   to avoid having a very long query overwrite memory
57
58
   TODO consider remapping the unprintables instead to "Printable
59
   Representation", the Unicode characters from the area U+2400 to
60
   U+2421 reserved for representing control characters when it is
61
   necessary to print or display them rather than have them perform
62
   their intended function.
63
64
*/
65
static unsigned char *quotify (const unsigned char *src, size_t srclen,
66
                               unsigned char *dst, size_t dstlen)
67
{
68
  static const char hexit[]= { '0', '1', '2', '3', '4', '5', '6', '7',
69
                               '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
70
  size_t dst_ndx;  /* ndx down the dst */
71
  size_t src_ndx;  /* ndx down the src */
72
73
  assert(dst);
74
  assert(dstlen > 0);
75
76
  for (dst_ndx= 0,src_ndx= 0; src_ndx < srclen; src_ndx++)
77
    {
78
79
      /* Worst case, need 5 dst bytes for the next src byte.
80
         backslash x hexit hexit null
81
         so if not enough room, just terminate the string and return
82
      */
83
      if ((dstlen - dst_ndx) < 5)
84
        {
85
          dst[dst_ndx]= (unsigned char)0x00;
86
          return dst;
87
        }
88
89
      if (src[src_ndx] > 0x7f)
90
        {
91
          // pass thru high bit characters, they are non-ASCII UTF8 Unicode
92
          dst[dst_ndx++]= src[src_ndx];
93
        }
94
      else if (src[src_ndx] == 0x00)  // null
95
        {
96
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) '0';
97
        }
98
      else if (src[src_ndx] == 0x07)  // bell
99
        {
100
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'a';
101
        }
102
      else if (src[src_ndx] == 0x08)  // backspace
103
        {
104
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'b';
105
        }
106
      else if (src[src_ndx] == 0x09)  // horiz tab
107
        {
108
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 't';
109
        }
110
      else if (src[src_ndx] == 0x0a)  // line feed
111
        {
112
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'n';
113
        }
114
      else if (src[src_ndx] == 0x0b)  // vert tab
115
        {
116
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'v';
117
        }
118
      else if (src[src_ndx] == 0x0c)  // formfeed
119
        {
120
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'f';
121
        }
122
      else if (src[src_ndx] == 0x0d)  // carrage return
123
        {
124
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'r';
125
        }
126
      else if (src[src_ndx] == 0x1b)  // escape
127
        {
128
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'e';
129
        }
130
      else if (src[src_ndx] == 0x22)  // quotation mark
131
        {
132
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x22;
133
        }
134
      else if (src[src_ndx] == 0x2C)  // comma
135
        {
136
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x2C;
137
        }
138
      else if (src[src_ndx] == 0x5C)  // backslash
139
        {
140
          dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x5C;
141
        }
142
      else if ((src[src_ndx] < 0x20) || (src[src_ndx] == 0x7F))  // other unprintable ASCII
143
        {
144
          dst[dst_ndx++]= 0x5C;
145
          dst[dst_ndx++]= (unsigned char) 'x';
146
          dst[dst_ndx++]= hexit[(src[src_ndx] >> 4) & 0x0f];
147
          dst[dst_ndx++]= hexit[src[src_ndx] & 0x0f];
148
        }
149
      else  // everything else
150
        {
151
          dst[dst_ndx++]= src[src_ndx];
152
        }
153
      dst[dst_ndx]= '\0';
154
    }
155
  return dst;
156
}
157
1964.2.18 by Monty Taylor
logging_gearman
158
class LoggingGearman :
159
  public drizzled::plugin::Logging
910.7.1 by Mark Atwood
add gearman logging plugin
160
{
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
161
1964.2.18 by Monty Taylor
logging_gearman
162
  const std::string _host;
163
  const std::string _function;
164
165
  int _gearman_client_ok;
166
  gearman_client_st _gearman_client;
167
168
  LoggingGearman();
169
  LoggingGearman(const LoggingGearman&);
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
170
968.2.23 by Monty Taylor
Fixed lack-of-NULL.
171
public:
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
172
1964.2.18 by Monty Taylor
logging_gearman
173
  LoggingGearman(const std::string &host,
174
                 const std::string &function) :
175
    drizzled::plugin::Logging("LoggingGearman"),
176
    _host(host),
177
    _function(function),
178
    _gearman_client_ok(0),
179
    _gearman_client()
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
180
  {
181
    gearman_return_t ret;
182
1964.2.18 by Monty Taylor
logging_gearman
183
184
    if (gearman_client_create(&_gearman_client) == NULL)
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
185
    {
2132.1.3 by Brian Aker
Updates.
186
      drizzled::sql_perror(_("fail gearman_client_create()"));
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
187
      return;
188
    }
189
190
    /* TODO, be able to override the port */
191
    /* TODO, be able send to multiple servers */
1964.2.18 by Monty Taylor
logging_gearman
192
    ret= gearman_client_add_server(&_gearman_client,
193
                                   host.c_str(), 0);
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
194
    if (ret != GEARMAN_SUCCESS)
195
    {
2132.1.6 by Brian Aker
Fix for gearman pieces.
196
      drizzled::errmsg_printf(drizzled::error::ERROR, _("fail gearman_client_add_server(): %s"),
1964.2.18 by Monty Taylor
logging_gearman
197
                              gearman_client_error(&_gearman_client));
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
198
      return;
199
    }
200
1964.2.18 by Monty Taylor
logging_gearman
201
    _gearman_client_ok= 1;
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
202
203
  }
204
205
  ~LoggingGearman()
206
  {
1964.2.18 by Monty Taylor
logging_gearman
207
    if (_gearman_client_ok)
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
208
    {
1964.2.18 by Monty Taylor
logging_gearman
209
      gearman_client_free(&_gearman_client);
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
210
    }
211
  }
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
212
1964.2.18 by Monty Taylor
logging_gearman
213
  virtual bool post(drizzled::Session *session)
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
214
  {
1929.1.40 by Monty Taylor
Replaced auto_ptr with scoped_ptr.
215
    boost::scoped_array<char> msgbuf(new char[MAX_MSG_LEN]);
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
216
    int msgbuf_len= 0;
217
  
218
    assert(session != NULL);
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
219
220
    /* in theory, we should return "true", meaning that the plugin isn't happy,
221
       but that crashes the server, so for now, we just lie a little bit
222
    */
223
1964.2.18 by Monty Taylor
logging_gearman
224
    if (not _gearman_client_ok)
1039.4.10 by Mark Atwood
make logging_gearman plugin be more C++'ish
225
        return false;
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
226
  
2040.4.3 by Brian Aker
Scale down the calls to universal_time().
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
    */
2269.1.11 by Olaf van der Spek
Session Times
232
    uint64_t t_mark= session->times.getCurrentTimestamp(false);
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
233
  
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.
234
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
235
    // buffer to quotify the query
236
    unsigned char qs[255];
237
  
238
    // to avoid trying to printf %s something that is potentially NULL
2269.1.7 by Olaf van der Spek
Use util::string::ptr
239
    drizzled::util::string::ptr dbs(session->schema());
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
240
  
241
    msgbuf_len=
1929.1.10 by Stewart Smith
fix large stack usage in logging_gearman:
242
      snprintf(msgbuf.get(), MAX_MSG_LEN,
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
243
               "%"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
244
               "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
971.7.8 by Eric Day
Moved port options into protocol plugin and added MySQL protocol support.
245
               "%"PRIu32",%"PRIu32",%"PRIu32",\"%s\"",
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
246
               t_mark,
247
               session->thread_id,
1120.1.2 by Diego Medina
Use accessors when available.
248
               session->getQueryId(),
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
249
               // 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
250
               (int)dbs->size(), dbs->c_str(),
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
251
               // do need to quote the query
1932.2.6 by Brian Aker
Merge in additional fixes for plugin.
252
               quotify((const unsigned char *)session->getQueryString()->c_str(), session->getQueryString()->length(), qs, sizeof(qs)),
2119.4.1 by Monty Taylor
Turns on -fvisibility=hidden by default. Symbols intended to be used by
253
               // getCommandName is defined in drizzled/sql_parse.h dont
254
               // need to quote the command name, always CSV safe
255
               (int)drizzled::getCommandName(session->command).size(),
256
               drizzled::getCommandName(session->command).c_str(),
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
257
               // counters are at end, to make it easier to add more
2269.1.11 by Olaf van der Spek
Session Times
258
               (t_mark - session->times.getConnectMicroseconds()),
259
               (session->times.getElapsedTime()),
260
               (t_mark - session->times.utime_after_lock),
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
261
               session->sent_row_count,
1039.4.2 by Mark Atwood
add more info to gearman logging
262
               session->examined_row_count,
263
               session->tmp_table,
1120.1.1 by Diego Medina
* Added hostname, listener port, and server id to the logging_query and logging_gearman plugins
264
               session->total_warn_count,
265
               session->getServerId(),
2183.1.2 by Monty Taylor
A slew of tiny meaningless changes.
266
               drizzled::getServerHostname().c_str()
1120.1.1 by Diego Medina
* Added hostname, listener port, and server id to the logging_query and logging_gearman plugins
267
               );
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
268
  
269
    char job_handle[GEARMAN_JOB_HANDLE_SIZE];
270
  
1964.2.18 by Monty Taylor
logging_gearman
271
    (void) gearman_client_do_background(&_gearman_client,
272
                                        _function.c_str(),
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
273
                                        NULL,
1929.1.10 by Stewart Smith
fix large stack usage in logging_gearman:
274
                                        (void *) msgbuf.get(),
968.2.20 by Monty Taylor
Merged Mark's gearman logging client.
275
                                        (size_t) msgbuf_len,
276
                                        job_handle);
277
  
278
    return false;
279
  }
280
};
910.7.1 by Mark Atwood
add gearman logging plugin
281
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
282
static LoggingGearman *handler= NULL;
971.1.51 by Monty Taylor
New-style plugin registration now works.
283
1964.2.18 by Monty Taylor
logging_gearman
284
static int logging_gearman_plugin_init(drizzled::module::Context &context)
910.7.1 by Mark Atwood
add gearman logging plugin
285
{
1964.2.18 by Monty Taylor
logging_gearman
286
  const drizzled::module::option_map &vm= context.getOptions();
287
288
  handler= new LoggingGearman(vm["host"].as<std::string>(),
289
                              vm["function"].as<std::string>());
1324.2.2 by Monty Taylor
Use the plugin::Context everywhere.
290
  context.add(handler);
1964.2.18 by Monty Taylor
logging_gearman
291
  context.registerVariable(new drizzled::sys_var_const_string_val("host", vm["host"].as<std::string>()));
292
  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
293
294
  return 0;
295
}
296
1660.6.1 by Vijay Samuel
Merge refactored commandline for logging_gearman using boost::program_options
297
static void init_options(drizzled::module::option_context &context)
298
{
1964.2.18 by Monty Taylor
logging_gearman
299
  context("host",
300
          po::value<std::string>()->default_value("localhost"),
2068.4.1 by Andrew Hutchings
Fix intl domain
301
          _("Hostname for logging to a Gearman server"));
1964.2.18 by Monty Taylor
logging_gearman
302
  context("function",
303
          po::value<std::string>()->default_value("drizzlelog"),
2068.4.1 by Andrew Hutchings
Fix intl domain
304
          _("Gearman Function to send logging to"));
1660.6.1 by Vijay Samuel
Merge refactored commandline for logging_gearman using boost::program_options
305
}
306
1964.2.18 by Monty Taylor
logging_gearman
307
} /* namespace drizzle_plugin */
910.7.1 by Mark Atwood
add gearman logging plugin
308
1228.1.5 by Monty Taylor
Merged in some naming things.
309
DRIZZLE_DECLARE_PLUGIN
910.7.1 by Mark Atwood
add gearman logging plugin
310
{
1241.10.2 by Monty Taylor
Added support for embedding the drizzle version number in the plugin file.
311
  DRIZZLE_VERSION_ID,
1660.6.1 by Vijay Samuel
Merge refactored commandline for logging_gearman using boost::program_options
312
    "logging-gearman",
910.7.1 by Mark Atwood
add gearman logging plugin
313
    "0.1",
314
    "Mark Atwood <mark@fallenpegasus.com>",
315
    N_("Log queries to a Gearman server"),
1964.2.18 by Monty Taylor
logging_gearman
316
    drizzled::PLUGIN_LICENSE_GPL,
317
    drizzle_plugin::logging_gearman_plugin_init,
318
    NULL,
319
    drizzle_plugin::init_options
910.7.1 by Mark Atwood
add gearman logging plugin
320
}
1228.1.5 by Monty Taylor
Merged in some naming things.
321
DRIZZLE_DECLARE_PLUGIN_END;