Drizzled Public API Documentation

logging_gearman.cc
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, Inc.
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.h>
25 #include <drizzled/plugin/logging.h>
26 #include <drizzled/gettext.h>
27 #include <drizzled/session.h>
28 #include <drizzled/session/times.h>
29 #include <drizzled/sql_parse.h>
30 #include <drizzled/errmsg_print.h>
31 #include <boost/date_time.hpp>
32 #include <boost/program_options.hpp>
34 #include <libgearman/gearman.h>
35 #include <limits.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <cstdio>
40 #include <cerrno>
41 #include <memory>
42 
43 namespace drizzle_plugin {
44 
45 namespace po= boost::program_options;
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 
160 {
161 
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();
170 
171 public:
172 
173  LoggingGearman(const std::string &host,
174  const std::string &function) :
175  drizzled::plugin::Logging("gearman_query_log"),
176  _host(host),
177  _function(function),
178  _gearman_client_ok(0),
179  _gearman_client()
180  {
181  gearman_return_t ret;
182 
183 
184  if (gearman_client_create(&_gearman_client) == NULL)
185  {
186  drizzled::sql_perror(_("fail gearman_client_create()"));
187  return;
188  }
189 
190  /* TODO, be able to override the port */
191  /* TODO, be able send to multiple servers */
192  ret= gearman_client_add_server(&_gearman_client,
193  host.c_str(), 0);
194  if (ret != GEARMAN_SUCCESS)
195  {
196  drizzled::errmsg_printf(drizzled::error::ERROR, _("fail gearman_client_add_server(): %s"),
197  gearman_client_error(&_gearman_client));
198  return;
199  }
200 
201  _gearman_client_ok= 1;
202 
203  }
204 
205  ~LoggingGearman()
206  {
207  if (_gearman_client_ok)
208  {
209  gearman_client_free(&_gearman_client);
210  }
211  }
212 
213  virtual bool post(drizzled::Session *session)
214  {
215  boost::scoped_array<char> msgbuf(new char[MAX_MSG_LEN]);
216  int msgbuf_len= 0;
217 
218  assert(session != NULL);
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 
224  if (not _gearman_client_ok)
225  return false;
226 
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  uint64_t t_mark= session->times.getCurrentTimestamp(false);
233 
234 
235  // buffer to quotify the query
236  unsigned char qs[255];
237 
238  // to avoid trying to printf %s something that is potentially NULL
239  drizzled::util::string::ptr dbs(session->schema());
240 
241  msgbuf_len=
242  snprintf(msgbuf.get(), MAX_MSG_LEN,
243  "%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
244  "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
245  "%"PRIu32",%"PRIu32",%"PRIu32",\"%s\"",
246  t_mark,
247  session->thread_id,
248  session->getQueryId(),
249  // dont need to quote the db name, always CSV safe
250  (int)dbs->size(), dbs->c_str(),
251  // do need to quote the query
252  quotify((const unsigned char *)session->getQueryString()->c_str(), session->getQueryString()->length(), qs, sizeof(qs)),
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(),
257  // counters are at end, to make it easier to add more
258  (t_mark - session->times.getConnectMicroseconds()),
259  (session->times.getElapsedTime()),
260  (t_mark - session->times.utime_after_lock),
261  session->sent_row_count,
262  session->examined_row_count,
263  session->tmp_table,
264  session->total_warn_count,
265  session->getServerId(),
266  drizzled::getServerHostname().c_str()
267  );
268 
269  char job_handle[GEARMAN_JOB_HANDLE_SIZE];
270 
271  (void) gearman_client_do_background(&_gearman_client,
272  _function.c_str(),
273  NULL,
274  (void *) msgbuf.get(),
275  (size_t) msgbuf_len,
276  job_handle);
277 
278  return false;
279  }
280 };
281 
282 static LoggingGearman *handler= NULL;
283 
284 static int logging_gearman_plugin_init(drizzled::module::Context &context)
285 {
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>());
290  context.add(handler);
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>()));
293 
294  return 0;
295 }
296 
297 static void init_options(drizzled::module::option_context &context)
298 {
299  context("host",
300  po::value<std::string>()->default_value("localhost"),
301  _("Hostname for logging to a Gearman server"));
302  context("function",
303  po::value<std::string>()->default_value("drizzlelog"),
304  _("Gearman Function to send logging to"));
305 }
306 
307 } /* namespace drizzle_plugin */
308 
309 DRIZZLE_DECLARE_PLUGIN
310 {
311  DRIZZLE_VERSION_ID,
312  "logging_gearman",
313  "0.1",
314  "Mark Atwood",
315  N_("Logs queries to a Gearman server"),
316  drizzled::PLUGIN_LICENSE_GPL,
317  drizzle_plugin::logging_gearman_plugin_init,
318  NULL,
319  drizzle_plugin::init_options
320 }
321 DRIZZLE_DECLARE_PLUGIN_END;