drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
logging_query.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#include <drizzled/item.h>
22#include <drizzled/plugin.h>
23#include <drizzled/plugin/logging.h>
24#include <drizzled/gettext.h>
25#include <drizzled/session.h>
26#include <drizzled/session/times.h>
27#include <drizzled/sql_parse.h>
28#include <drizzled/pcre.h>
29#include <limits.h>
30#include <sys/time.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <fcntl.h>
34#include <pthread.h>
35#include <string>
36#include <boost/format.hpp>
37#include <boost/program_options.hpp>
39#include <cstdio>
40#include <cerrno>
41
42namespace po= boost::program_options;
43using namespace drizzled;
44using namespace std;
45
46#define ESCAPE_CHAR '\\'
47#define SEPARATOR_CHAR ','
48
49namespace drizzle_plugin {
50namespace logging_query {
51
52
53static bool sysvar_logging_query_enable= false;
54/* TODO fix these to not be unsigned long once we have sensible sys_var system */
55static uint32_constraint sysvar_logging_query_threshold_slow;
56static uint32_constraint sysvar_logging_query_threshold_big_resultset;
57static uint32_constraint sysvar_logging_query_threshold_big_examined;
58
59bool updateFileName(Session *, set_var*);
60bool updatePCRE(Session *, set_var*);
61
62
63/* quote a string to be safe to include in a CSV line
64 that means backslash quoting all commas, doublequotes, backslashes,
65 and all the ASCII unprintable characters
66 as long as we pass the high-bit bytes unchanged
67 this is safe to do to a UTF8 string
68 we dont allow overrunning the targetbuffer
69 to avoid having a very long query overwrite memory
70
71 TODO consider remapping the unprintables instead to "Printable
72 Representation", the Unicode characters from the area U+2400 to
73 U+2421 reserved for representing control characters when it is
74 necessary to print or display them rather than have them perform
75 their intended function.
76
77*/
78
79static void quotify(const string &src, string &dst)
80{
81 static const char hexit[]= { '0', '1', '2', '3', '4', '5', '6', '7',
82 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
83 string::const_iterator src_iter;
84
85 for (src_iter= src.begin(); src_iter < src.end(); ++src_iter)
86 {
87 if (static_cast<unsigned char>(*src_iter) > 0x7f)
88 {
89 dst.push_back(*src_iter);
90 }
91 else if (*src_iter == 0x00) // null
92 {
93 dst.push_back(ESCAPE_CHAR); dst.push_back('0');
94 }
95 else if (*src_iter == 0x07) // bell
96 {
97 dst.push_back(ESCAPE_CHAR); dst.push_back('a');
98 }
99 else if (*src_iter == 0x08) // backspace
100 {
101 dst.push_back(ESCAPE_CHAR); dst.push_back('b');
102 }
103 else if (*src_iter == 0x09) // horiz tab
104 {
105 dst.push_back(ESCAPE_CHAR); dst.push_back('t');
106 }
107 else if (*src_iter == 0x0a) // line feed
108 {
109 dst.push_back(ESCAPE_CHAR); dst.push_back('n');
110 }
111 else if (*src_iter == 0x0b) // vert tab
112 {
113 dst.push_back(ESCAPE_CHAR); dst.push_back('v');
114 }
115 else if (*src_iter == 0x0c) // formfeed
116 {
117 dst.push_back(ESCAPE_CHAR); dst.push_back('f');
118 }
119 else if (*src_iter == 0x0d) // carrage return
120 {
121 dst.push_back(ESCAPE_CHAR); dst.push_back('r');
122 }
123 else if (*src_iter == 0x1b) // escape
124 {
125 dst.push_back(ESCAPE_CHAR); dst.push_back('e');
126 }
127 else if (*src_iter == 0x22) // quotation mark
128 {
129 dst.push_back(ESCAPE_CHAR); dst.push_back(0x22);
130 }
131 else if (*src_iter == SEPARATOR_CHAR)
132 {
133 dst.push_back(ESCAPE_CHAR); dst.push_back(SEPARATOR_CHAR);
134 }
135 else if (*src_iter == ESCAPE_CHAR)
136 {
137 dst.push_back(ESCAPE_CHAR); dst.push_back(ESCAPE_CHAR);
138 }
139 else if ((*src_iter < 0x20) || (*src_iter == 0x7F)) // other unprintable ASCII
140 {
141 dst.push_back(ESCAPE_CHAR);
142 dst.push_back('x');
143 dst.push_back(hexit[(*src_iter >> 4) & 0x0f]);
144 dst.push_back(hexit[*src_iter & 0x0f]);
145 }
146 else // everything else
147 {
148 dst.push_back(*src_iter);
149 }
150 }
151}
152
153
154class Logging_query: public drizzled::plugin::Logging
155{
156 std::string sysvar_filename;
157 std::string sysvar_pcre;
158 int fd;
159 pcre2_code *re;
160 bool re_jit_ready;
161 pthread_mutex_t pcre_lock;
162
164 boost::format formatter;
165
166 bool compilePCRE(const std::string &pattern, pcre2_code **compiled_re, bool *jit_ready)
167 {
168 int error_code;
169 PCRE2_SIZE error_offset;
170 *compiled_re= pcre2_compile(reinterpret_cast<PCRE2_SPTR>(pattern.c_str()),
171 PCRE2_ZERO_TERMINATED,
172 0,
173 &error_code,
174 &error_offset,
175 NULL);
176 if (*compiled_re == NULL)
177 return false;
178
179 *jit_ready= (pcre2_jit_compile(*compiled_re, PCRE2_JIT_COMPLETE) == 0);
180 return true;
181 }
182
183 int matchPCRE(const std::string &subject)
184 {
185 pthread_mutex_lock(&pcre_lock);
186
187 if (re == NULL)
188 {
189 pthread_mutex_unlock(&pcre_lock);
190 return 0;
191 }
192
193 pcre2_match_data *match_data= pcre2_match_data_create_from_pattern(re, NULL);
194 if (match_data == NULL)
195 {
196 pthread_mutex_unlock(&pcre_lock);
197 return -1;
198 }
199
200 int rc;
201 if (re_jit_ready)
202 {
203 rc= pcre2_jit_match(re,
204 reinterpret_cast<PCRE2_SPTR>(subject.c_str()),
205 subject.length(),
206 0,
207 0,
208 match_data,
209 NULL);
210 if (rc >= 0 || rc == PCRE2_ERROR_NOMATCH)
211 {
212 pcre2_match_data_free(match_data);
213 pthread_mutex_unlock(&pcre_lock);
214 return rc;
215 }
216
217 rc= pcre2_match(re,
218 reinterpret_cast<PCRE2_SPTR>(subject.c_str()),
219 subject.length(),
220 0,
221 PCRE2_NO_JIT,
222 match_data,
223 NULL);
224 }
225 else
226 {
227 rc= pcre2_match(re,
228 reinterpret_cast<PCRE2_SPTR>(subject.c_str()),
229 subject.length(),
230 0,
231 0,
232 match_data,
233 NULL);
234 }
235
236 pcre2_match_data_free(match_data);
237 pthread_mutex_unlock(&pcre_lock);
238 return rc;
239 }
240
241public:
242
243 Logging_query(const std::string &filename,
244 const std::string &query_pcre) :
245 drizzled::plugin::Logging("csv_query_log"),
246 sysvar_filename(filename),
247 sysvar_pcre(query_pcre),
248 fd(-1), re(NULL), re_jit_ready(false),
249 formatter("%1%,%2%,%3%,\"%4%\",\"%5%\",\"%6%\",%7%,%8%,"
250 "%9%,%10%,%11%,%12%,%13%,%14%,\"%15%\"\n")
251 {
252 pthread_mutex_init(&pcre_lock, NULL);
253
254 /* if there is no destination filename, dont bother doing anything */
255 if (sysvar_filename.empty())
256 return;
257
258 fd= open(sysvar_filename.c_str(),
259 O_WRONLY | O_APPEND | O_CREAT,
260 S_IRUSR|S_IWUSR);
261
262 if (fd < 0)
263 {
264 sql_perror( _("fail open()"), sysvar_filename);
265 return;
266 }
267
268 if (not sysvar_pcre.empty())
269 {
270 compilePCRE(sysvar_pcre, &re, &re_jit_ready);
271 /* TODO emit error messages if there is a problem */
272 }
273 }
274
275
281 bool setFileName(std::string new_filename)
282 {
283 if (new_filename.empty())
284 return false;
285
286 int tmp_fd= open(new_filename.c_str(),
287 O_WRONLY | O_APPEND | O_CREAT,
288 S_IRUSR|S_IWUSR);
289
290 if (tmp_fd < 0)
291 {
292 sql_perror( _("fail open()"), new_filename);
293 return false;
294 }
295 if(fd >= 0)
296 {
297 close(fd);
298 }
299 fd= tmp_fd;
300 sysvar_filename= new_filename;
301 return true;
302 }
303
309 bool setPCRE(std::string new_pcre)
310 {
311 if (not new_pcre.empty())
312 {
313 pcre2_code *new_re= NULL;
314 bool new_jit_ready= false;
315
316 if (! compilePCRE(new_pcre, &new_re, &new_jit_ready))
317 return false;
318
319 pthread_mutex_lock(&pcre_lock);
320 if (re != NULL)
321 pcre2_code_free(re);
322
323 re= new_re;
324 re_jit_ready= new_jit_ready;
325 pthread_mutex_unlock(&pcre_lock);
326 /* TODO emit error messages if there is a problem */
327 }
328 else
329 {
330 pthread_mutex_lock(&pcre_lock);
331 if (re != NULL)
332 {
333 pcre2_code_free(re);
334 re= NULL;
335 }
336 re_jit_ready= false;
337 pthread_mutex_unlock(&pcre_lock);
338 }
339 sysvar_pcre= new_pcre;
340 return true;
341 }
342
348 std::string& getFileName()
349 {
350 return sysvar_filename;
351 }
352
358 std::string& getPCRE()
359 {
360 return sysvar_pcre;
361 }
362
364 {
365 if (fd >= 0)
366 {
367 close(fd);
368 }
369
370 pthread_mutex_lock(&pcre_lock);
371 if (re != NULL)
372 pcre2_code_free(re);
373 re= NULL;
374 pthread_mutex_unlock(&pcre_lock);
375 pthread_mutex_destroy(&pcre_lock);
376 }
377
378 virtual bool post (Session *session)
379 {
380 size_t wrv;
381
382 assert(session != NULL);
383
384 if (fd < 0)
385 return false;
386
387 /* Yes, we know that checking sysvar_logging_query_enable,
388 sysvar_logging_query_threshold_big_resultset, and
389 sysvar_logging_query_threshold_big_examined is not threadsafe,
390 because some other thread might change these sysvars. But we
391 don't care. We might start logging a little late as it spreads
392 to other threads. Big deal. */
393
394 // return if not enabled or query was too fast or resultset was too small
395 if (sysvar_logging_query_enable == false)
396 return false;
397 if (session->sent_row_count < sysvar_logging_query_threshold_big_resultset.get())
398 return false;
399 if (session->examined_row_count < sysvar_logging_query_threshold_big_examined.get())
400 return false;
401
402 /*
403 TODO, the session object should have a "utime command completed"
404 inside itself, so be more accurate, and so this doesnt have to
405 keep calling current_utime, which can be slow.
406 */
407 uint64_t t_mark= session->times.getCurrentTimestamp(false);
408
409 if (session->times.getElapsedTime() < sysvar_logging_query_threshold_slow.get())
410 return false;
411
412 Session::QueryString query_string(session->getQueryString());
413 if (query_string == NULL)
414 {
415 return false;
416 }
417
418 int this_pcre_rc= matchPCRE(*query_string);
419 if (this_pcre_rc < 0)
420 return false;
421
422 // buffer to quotify the query
423 string qs;
424
425 // Since quotify() builds the quoted string incrementally, we can
426 // avoid some reallocating if we reserve some space up front.
427 qs.reserve(query_string->length());
428
429 quotify(*query_string, qs);
430
431 // to avoid trying to printf %s something that is potentially NULL
432 util::string::ptr schema(session->schema());
433 formatter % t_mark
434 % session->thread_id
435 % session->getQueryId()
436 % (schema ? schema->c_str() : "")
437 % qs
438 % getCommandName(session->command)
439 % (t_mark - session->times.getConnectMicroseconds())
440 % session->times.getElapsedTime()
441 % (t_mark - session->times.utime_after_lock)
442 % session->sent_row_count
443 % session->examined_row_count
444 % session->tmp_table
445 % session->total_warn_count
446 % session->getServerId()
447 % getServerHostname();
448
449 string msgbuf= formatter.str();
450
451 // a single write has a kernel thread lock, thus no need mutex guard this
452 wrv= write(fd, msgbuf.c_str(), msgbuf.length());
453 (void)(wrv);
454 assert(wrv == msgbuf.length());
455
456 return false;
457 }
458};
459
460static Logging_query *handler= NULL;
461
467bool updateFileName(Session *, set_var* var)
468{
469 if (not var->value->str_value.empty())
470 {
471 std::string new_filename(var->value->str_value.data());
472 if (handler->setFileName(new_filename))
473 return false; //success
474 else
475 return true; // error
476 }
477 errmsg_printf(error::ERROR, _("logging_query_filename cannot be NULL"));
478 return true; // error
479}
480
486bool updatePCRE(Session *, set_var* var)
487{
488 std::string new_pcre(var->value->str_value.data());
489 if (handler->setPCRE(new_pcre))
490 return false; //success
491 return true; // error
492}
493
494static int init(drizzled::module::Context &context)
495{
496 const module::option_map &vm= context.getOptions();
497
498 if (vm.count("filename"))
499 {
500 handler= new Logging_query(vm["filename"].as<string>(),
501 vm["pcre"].as<string>());
502
503 context.add(handler);
504 context.registerVariable(new sys_var_bool_ptr("enable", &sysvar_logging_query_enable));
505 context.registerVariable(new sys_var_std_string("filename", handler->getFileName(), NULL, &updateFileName));
506 context.registerVariable(new sys_var_std_string("pcre", handler->getPCRE(), NULL, &updatePCRE));
507 context.registerVariable(new sys_var_constrained_value<uint32_t>("threshold_slow", sysvar_logging_query_threshold_slow));
508 context.registerVariable(new sys_var_constrained_value<uint32_t>("threshold_big_resultset", sysvar_logging_query_threshold_big_resultset));
509 context.registerVariable(new sys_var_constrained_value<uint32_t>("threshold_big_examined", sysvar_logging_query_threshold_big_examined));
510 }
511
512 return 0;
513}
514
515static void init_options(drizzled::module::option_context &context)
516{
517 context("enable",
518 po::value<bool>(&sysvar_logging_query_enable)->default_value(false)->zero_tokens(),
519 _("Enable logging to CSV file"));
520 context("filename",
521 po::value<string>(),
522 _("File to log to"));
523 context("pcre",
524 po::value<string>()->default_value(""),
525 _("PCRE to match the query against"));
526 context("threshold-slow",
527 po::value<uint32_constraint>(&sysvar_logging_query_threshold_slow)->default_value(0),
528 _("Threshold for logging slow queries, in microseconds"));
529 context("threshold-big-resultset",
530 po::value<uint32_constraint>(&sysvar_logging_query_threshold_big_resultset)->default_value(0),
531 _("Threshold for logging big queries, for rows returned"));
532 context("threshold-big-examined",
533 po::value<uint32_constraint>(&sysvar_logging_query_threshold_big_examined)->default_value(0),
534 _("Threshold for logging big queries, for rows examined"));
535}
536
537} /* namespace logging_query */
538
539} /* namespace drizzle_plugin */
540
541DRIZZLE_DECLARE_PLUGIN
542{
543 DRIZZLE_VERSION_ID,
544 "logging_query",
545 "0.2",
546 "Mark Atwood",
547 N_("Logs queries to a CSV file"),
548 PLUGIN_LICENSE_GPL,
549 drizzle_plugin::logging_query::init,
550 NULL,
551 drizzle_plugin::logging_query::init_options
552}
553DRIZZLE_DECLARE_PLUGIN_END;
bool setFileName(std::string new_filename)
String str_value
Definition item.h:109
query_id_t getQueryId() const
Definition session.h:621
enum_server_command command
Definition session.h:318
uint32_t getServerId() const
Definition session.h:646
std::shared_ptr< const std::string > QueryString
Definition session.h:133
ha_rows examined_row_count
Definition session.h:415
ha_rows sent_row_count
Definition session.h:410
TODO: Rename this file - func.h is stupid.
STL namespace.
An Proxy Wrapper around boost::program_options::variables_map.