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>
36#include <boost/format.hpp>
37#include <boost/program_options.hpp>
42namespace po= boost::program_options;
46#define ESCAPE_CHAR '\\'
47#define SEPARATOR_CHAR ','
50namespace logging_query {
53static bool sysvar_logging_query_enable=
false;
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;
59bool updateFileName(Session *, set_var*);
60bool updatePCRE(Session *, set_var*);
79static void quotify(
const string &src,
string &dst)
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;
85 for (src_iter= src.begin(); src_iter < src.end(); ++src_iter)
87 if (
static_cast<unsigned char>(*src_iter) > 0x7f)
89 dst.push_back(*src_iter);
91 else if (*src_iter == 0x00)
93 dst.push_back(ESCAPE_CHAR); dst.push_back(
'0');
95 else if (*src_iter == 0x07)
97 dst.push_back(ESCAPE_CHAR); dst.push_back(
'a');
99 else if (*src_iter == 0x08)
101 dst.push_back(ESCAPE_CHAR); dst.push_back(
'b');
103 else if (*src_iter == 0x09)
105 dst.push_back(ESCAPE_CHAR); dst.push_back(
't');
107 else if (*src_iter == 0x0a)
109 dst.push_back(ESCAPE_CHAR); dst.push_back(
'n');
111 else if (*src_iter == 0x0b)
113 dst.push_back(ESCAPE_CHAR); dst.push_back(
'v');
115 else if (*src_iter == 0x0c)
117 dst.push_back(ESCAPE_CHAR); dst.push_back(
'f');
119 else if (*src_iter == 0x0d)
121 dst.push_back(ESCAPE_CHAR); dst.push_back(
'r');
123 else if (*src_iter == 0x1b)
125 dst.push_back(ESCAPE_CHAR); dst.push_back(
'e');
127 else if (*src_iter == 0x22)
129 dst.push_back(ESCAPE_CHAR); dst.push_back(0x22);
131 else if (*src_iter == SEPARATOR_CHAR)
133 dst.push_back(ESCAPE_CHAR); dst.push_back(SEPARATOR_CHAR);
135 else if (*src_iter == ESCAPE_CHAR)
137 dst.push_back(ESCAPE_CHAR); dst.push_back(ESCAPE_CHAR);
139 else if ((*src_iter < 0x20) || (*src_iter == 0x7F))
141 dst.push_back(ESCAPE_CHAR);
143 dst.push_back(hexit[(*src_iter >> 4) & 0x0f]);
144 dst.push_back(hexit[*src_iter & 0x0f]);
148 dst.push_back(*src_iter);
156 std::string sysvar_filename;
157 std::string sysvar_pcre;
161 pthread_mutex_t pcre_lock;
166 bool compilePCRE(
const std::string &pattern, pcre2_code **compiled_re,
bool *jit_ready)
169 PCRE2_SIZE error_offset;
170 *compiled_re= pcre2_compile(
reinterpret_cast<PCRE2_SPTR
>(pattern.c_str()),
171 PCRE2_ZERO_TERMINATED,
176 if (*compiled_re == NULL)
179 *jit_ready= (pcre2_jit_compile(*compiled_re, PCRE2_JIT_COMPLETE) == 0);
183 int matchPCRE(
const std::string &subject)
185 pthread_mutex_lock(&pcre_lock);
189 pthread_mutex_unlock(&pcre_lock);
193 pcre2_match_data *match_data= pcre2_match_data_create_from_pattern(re, NULL);
194 if (match_data == NULL)
196 pthread_mutex_unlock(&pcre_lock);
203 rc= pcre2_jit_match(re,
204 reinterpret_cast<PCRE2_SPTR
>(subject.c_str()),
210 if (rc >= 0 || rc == PCRE2_ERROR_NOMATCH)
212 pcre2_match_data_free(match_data);
213 pthread_mutex_unlock(&pcre_lock);
218 reinterpret_cast<PCRE2_SPTR
>(subject.c_str()),
228 reinterpret_cast<PCRE2_SPTR
>(subject.c_str()),
236 pcre2_match_data_free(match_data);
237 pthread_mutex_unlock(&pcre_lock);
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")
252 pthread_mutex_init(&pcre_lock, NULL);
255 if (sysvar_filename.empty())
258 fd= open(sysvar_filename.c_str(),
259 O_WRONLY | O_APPEND | O_CREAT,
264 sql_perror( _(
"fail open()"), sysvar_filename);
268 if (not sysvar_pcre.empty())
270 compilePCRE(sysvar_pcre, &re, &re_jit_ready);
283 if (new_filename.empty())
286 int tmp_fd= open(new_filename.c_str(),
287 O_WRONLY | O_APPEND | O_CREAT,
292 sql_perror( _(
"fail open()"), new_filename);
300 sysvar_filename= new_filename;
311 if (not new_pcre.empty())
313 pcre2_code *new_re= NULL;
314 bool new_jit_ready=
false;
316 if (! compilePCRE(new_pcre, &new_re, &new_jit_ready))
319 pthread_mutex_lock(&pcre_lock);
324 re_jit_ready= new_jit_ready;
325 pthread_mutex_unlock(&pcre_lock);
330 pthread_mutex_lock(&pcre_lock);
337 pthread_mutex_unlock(&pcre_lock);
339 sysvar_pcre= new_pcre;
350 return sysvar_filename;
370 pthread_mutex_lock(&pcre_lock);
374 pthread_mutex_unlock(&pcre_lock);
375 pthread_mutex_destroy(&pcre_lock);
378 virtual bool post (
Session *session)
382 assert(session != NULL);
395 if (sysvar_logging_query_enable ==
false)
397 if (session->sent_row_count < sysvar_logging_query_threshold_big_resultset.get())
399 if (session->examined_row_count < sysvar_logging_query_threshold_big_examined.get())
407 uint64_t t_mark= session->times.getCurrentTimestamp(
false);
409 if (session->times.getElapsedTime() < sysvar_logging_query_threshold_slow.get())
413 if (query_string == NULL)
418 int this_pcre_rc= matchPCRE(*query_string);
419 if (this_pcre_rc < 0)
427 qs.reserve(query_string->length());
429 quotify(*query_string, qs);
432 util::string::ptr schema(session->schema());
436 % (schema ? schema->c_str() :
"")
438 % getCommandName(session->
command)
439 % (t_mark - session->times.getConnectMicroseconds())
440 % session->times.getElapsedTime()
441 % (t_mark - session->times.utime_after_lock)
445 % session->total_warn_count
447 % getServerHostname();
452 wrv= write(fd, msgbuf.c_str(), msgbuf.length());
454 assert(wrv == msgbuf.length());
467bool updateFileName(Session *, set_var* var)
471 std::string new_filename(var->value->
str_value.data());
472 if (handler->setFileName(new_filename))
477 errmsg_printf(error::ERROR, _(
"logging_query_filename cannot be NULL"));
486bool updatePCRE(Session *, set_var* var)
488 std::string new_pcre(var->value->
str_value.data());
489 if (handler->setPCRE(new_pcre))
494static int init(drizzled::module::Context &context)
496 const module::option_map &vm= context.getOptions();
498 if (vm.count(
"filename"))
501 vm[
"pcre"].as<string>());
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));
515static void init_options(drizzled::module::option_context &context)
518 po::value<bool>(&sysvar_logging_query_enable)->default_value(
false)->zero_tokens(),
519 _(
"Enable logging to CSV file"));
522 _(
"File to log to"));
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"));
541DRIZZLE_DECLARE_PLUGIN
547 N_(
"Logs queries to a CSV file"),
549 drizzle_plugin::logging_query::init,
551 drizzle_plugin::logging_query::init_options
553DRIZZLE_DECLARE_PLUGIN_END;
std::string & getFileName()
bool setPCRE(std::string new_pcre)
bool setFileName(std::string new_filename)
query_id_t getQueryId() const
enum_server_command command
uint32_t getServerId() const
std::shared_ptr< const std::string > QueryString
ha_rows examined_row_count
TODO: Rename this file - func.h is stupid.
An Proxy Wrapper around boost::program_options::variables_map.