00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include <boost/scoped_array.hpp>
00023
00024 #include <drizzled/plugin/logging.h>
00025 #include <drizzled/gettext.h>
00026 #include <drizzled/session.h>
00027 #include <drizzled/sql_parse.h>
00028 #include <drizzled/errmsg_print.h>
00029 #include <boost/date_time.hpp>
00030 #include <boost/program_options.hpp>
00031 #include <drizzled/module/option_map.h>
00032 #include <libgearman-1.0/gearman.h>
00033 #include <limits.h>
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036 #include <fcntl.h>
00037 #include <cstdio>
00038 #include <cerrno>
00039 #include <memory>
00040
00041
00042 namespace drizzle_plugin
00043 {
00044
00045 namespace po= boost::program_options;
00046
00047
00048 static const int MAX_MSG_LEN= 32*1024;
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 static unsigned char *quotify (const unsigned char *src, size_t srclen,
00066 unsigned char *dst, size_t dstlen)
00067 {
00068 static const char hexit[]= { '0', '1', '2', '3', '4', '5', '6', '7',
00069 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
00070 size_t dst_ndx;
00071 size_t src_ndx;
00072
00073 assert(dst);
00074 assert(dstlen > 0);
00075
00076 for (dst_ndx= 0,src_ndx= 0; src_ndx < srclen; src_ndx++)
00077 {
00078
00079
00080
00081
00082
00083 if ((dstlen - dst_ndx) < 5)
00084 {
00085 dst[dst_ndx]= (unsigned char)0x00;
00086 return dst;
00087 }
00088
00089 if (src[src_ndx] > 0x7f)
00090 {
00091
00092 dst[dst_ndx++]= src[src_ndx];
00093 }
00094 else if (src[src_ndx] == 0x00)
00095 {
00096 dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) '0';
00097 }
00098 else if (src[src_ndx] == 0x07)
00099 {
00100 dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'a';
00101 }
00102 else if (src[src_ndx] == 0x08)
00103 {
00104 dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'b';
00105 }
00106 else if (src[src_ndx] == 0x09)
00107 {
00108 dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 't';
00109 }
00110 else if (src[src_ndx] == 0x0a)
00111 {
00112 dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'n';
00113 }
00114 else if (src[src_ndx] == 0x0b)
00115 {
00116 dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'v';
00117 }
00118 else if (src[src_ndx] == 0x0c)
00119 {
00120 dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'f';
00121 }
00122 else if (src[src_ndx] == 0x0d)
00123 {
00124 dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'r';
00125 }
00126 else if (src[src_ndx] == 0x1b)
00127 {
00128 dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= (unsigned char) 'e';
00129 }
00130 else if (src[src_ndx] == 0x22)
00131 {
00132 dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x22;
00133 }
00134 else if (src[src_ndx] == 0x2C)
00135 {
00136 dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x2C;
00137 }
00138 else if (src[src_ndx] == 0x5C)
00139 {
00140 dst[dst_ndx++]= 0x5C; dst[dst_ndx++]= 0x5C;
00141 }
00142 else if ((src[src_ndx] < 0x20) || (src[src_ndx] == 0x7F))
00143 {
00144 dst[dst_ndx++]= 0x5C;
00145 dst[dst_ndx++]= (unsigned char) 'x';
00146 dst[dst_ndx++]= hexit[(src[src_ndx] >> 4) & 0x0f];
00147 dst[dst_ndx++]= hexit[src[src_ndx] & 0x0f];
00148 }
00149 else
00150 {
00151 dst[dst_ndx++]= src[src_ndx];
00152 }
00153 dst[dst_ndx]= '\0';
00154 }
00155 return dst;
00156 }
00157
00158 class LoggingGearman :
00159 public drizzled::plugin::Logging
00160 {
00161
00162 const std::string _host;
00163 const std::string _function;
00164
00165 int _gearman_client_ok;
00166 gearman_client_st _gearman_client;
00167
00168 LoggingGearman();
00169 LoggingGearman(const LoggingGearman&);
00170
00171 public:
00172
00173 LoggingGearman(const std::string &host,
00174 const std::string &function) :
00175 drizzled::plugin::Logging("LoggingGearman"),
00176 _host(host),
00177 _function(function),
00178 _gearman_client_ok(0),
00179 _gearman_client()
00180 {
00181 gearman_return_t ret;
00182
00183
00184 if (gearman_client_create(&_gearman_client) == NULL)
00185 {
00186 drizzled::sql_perror(_("fail gearman_client_create()"));
00187 return;
00188 }
00189
00190
00191
00192 ret= gearman_client_add_server(&_gearman_client,
00193 host.c_str(), 0);
00194 if (ret != GEARMAN_SUCCESS)
00195 {
00196 drizzled::errmsg_printf(drizzled::error::ERROR, _("fail gearman_client_add_server(): %s"),
00197 gearman_client_error(&_gearman_client));
00198 return;
00199 }
00200
00201 _gearman_client_ok= 1;
00202
00203 }
00204
00205 ~LoggingGearman()
00206 {
00207 if (_gearman_client_ok)
00208 {
00209 gearman_client_free(&_gearman_client);
00210 }
00211 }
00212
00213 virtual bool post(drizzled::Session *session)
00214 {
00215 boost::scoped_array<char> msgbuf(new char[MAX_MSG_LEN]);
00216 int msgbuf_len= 0;
00217
00218 assert(session != NULL);
00219
00220
00221
00222
00223
00224 if (not _gearman_client_ok)
00225 return false;
00226
00227
00228
00229
00230
00231
00232 uint64_t t_mark= session->getCurrentTimestamp(false);
00233
00234
00235
00236 unsigned char qs[255];
00237
00238
00239 drizzled::util::string::const_shared_ptr dbs(session->schema());
00240
00241 msgbuf_len=
00242 snprintf(msgbuf.get(), MAX_MSG_LEN,
00243 "%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
00244 "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
00245 "%"PRIu32",%"PRIu32",%"PRIu32",\"%s\"",
00246 t_mark,
00247 session->thread_id,
00248 session->getQueryId(),
00249
00250 (int)dbs->size(), dbs->c_str(),
00251
00252 quotify((const unsigned char *)session->getQueryString()->c_str(), session->getQueryString()->length(), qs, sizeof(qs)),
00253
00254
00255 (int)drizzled::getCommandName(session->command).size(),
00256 drizzled::getCommandName(session->command).c_str(),
00257
00258 (t_mark - session->getConnectMicroseconds()),
00259 (session->getElapsedTime()),
00260 (t_mark - session->utime_after_lock),
00261 session->sent_row_count,
00262 session->examined_row_count,
00263 session->tmp_table,
00264 session->total_warn_count,
00265 session->getServerId(),
00266 drizzled::getServerHostname().c_str()
00267 );
00268
00269 char job_handle[GEARMAN_JOB_HANDLE_SIZE];
00270
00271 (void) gearman_client_do_background(&_gearman_client,
00272 _function.c_str(),
00273 NULL,
00274 (void *) msgbuf.get(),
00275 (size_t) msgbuf_len,
00276 job_handle);
00277
00278 return false;
00279 }
00280 };
00281
00282 static LoggingGearman *handler= NULL;
00283
00284 static int logging_gearman_plugin_init(drizzled::module::Context &context)
00285 {
00286 const drizzled::module::option_map &vm= context.getOptions();
00287
00288 handler= new LoggingGearman(vm["host"].as<std::string>(),
00289 vm["function"].as<std::string>());
00290 context.add(handler);
00291 context.registerVariable(new drizzled::sys_var_const_string_val("host", vm["host"].as<std::string>()));
00292 context.registerVariable(new drizzled::sys_var_const_string_val("function", vm["function"].as<std::string>()));
00293
00294 return 0;
00295 }
00296
00297 static void init_options(drizzled::module::option_context &context)
00298 {
00299 context("host",
00300 po::value<std::string>()->default_value("localhost"),
00301 _("Hostname for logging to a Gearman server"));
00302 context("function",
00303 po::value<std::string>()->default_value("drizzlelog"),
00304 _("Gearman Function to send logging to"));
00305 }
00306
00307 }
00308
00309 DRIZZLE_DECLARE_PLUGIN
00310 {
00311 DRIZZLE_VERSION_ID,
00312 "logging-gearman",
00313 "0.1",
00314 "Mark Atwood <mark@fallenpegasus.com>",
00315 N_("Log queries to a Gearman server"),
00316 drizzled::PLUGIN_LICENSE_GPL,
00317 drizzle_plugin::logging_gearman_plugin_init,
00318 NULL,
00319 drizzle_plugin::init_options
00320 }
00321 DRIZZLE_DECLARE_PLUGIN_END;