17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include <boost/scoped_array.hpp>
24
#include <drizzled/plugin/logging.h>
20
#include <drizzled/server_includes.h>
21
#include <drizzled/plugin/logging_handler.h>
25
22
#include <drizzled/gettext.h>
26
23
#include <drizzled/session.h>
27
#include <boost/date_time.hpp>
28
#include <boost/program_options.hpp>
29
#include <drizzled/module/option_map.h>
30
25
#include <libgearman/gearman.h>
32
#include <sys/types.h>
40
namespace drizzle_plugin
43
namespace po= boost::program_options;
45
28
/* TODO make this dynamic as needed */
46
29
static const int MAX_MSG_LEN= 32*1024;
31
static bool sysvar_logging_gearman_enable= false;
32
static char* sysvar_logging_gearman_host= NULL;
33
static char* sysvar_logging_gearman_function= NULL;
35
static gearman_client_st gearman_client;
38
/* stolen from mysys/my_getsystime
39
until the Session has a good utime "now" we can use
40
will have to use this instead */
43
static uint64_t get_microtime()
45
#if defined(HAVE_GETHRTIME)
46
return gethrtime()/1000;
51
The following loop is here because gettimeofday may fail on some systems
53
while (gettimeofday(&t, NULL) != 0) {}
54
newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
56
#endif /* defined(HAVE_GETHRTIME) */
48
59
/* quote a string to be safe to include in a CSV line
49
60
that means backslash quoting all commas, doublequotes, backslashes,
50
61
and all the ASCII unprintable characters
156
class LoggingGearman :
157
public drizzled::plugin::Logging
167
class LoggingGearman : public Logging_handler
160
const std::string _host;
161
const std::string _function;
163
int _gearman_client_ok;
164
gearman_client_st _gearman_client;
167
LoggingGearman(const LoggingGearman&);
171
LoggingGearman(const std::string &host,
172
const std::string &function) :
173
drizzled::plugin::Logging("LoggingGearman"),
176
_gearman_client_ok(0),
179
gearman_return_t ret;
182
if (gearman_client_create(&_gearman_client) == NULL)
184
char errmsg[STRERROR_MAX];
185
strerror_r(errno, errmsg, sizeof(errmsg));
186
drizzled::errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
191
/* TODO, be able to override the port */
192
/* TODO, be able send to multiple servers */
193
ret= gearman_client_add_server(&_gearman_client,
195
if (ret != GEARMAN_SUCCESS)
197
drizzled::errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
198
gearman_client_error(&_gearman_client));
202
_gearman_client_ok= 1;
208
if (_gearman_client_ok)
210
gearman_client_free(&_gearman_client);
214
virtual bool post(drizzled::Session *session)
216
boost::scoped_array<char> msgbuf(new char[MAX_MSG_LEN]);
170
LoggingGearman() : Logging_handler("LoggingGearman") {}
172
virtual bool post(Session *session)
174
char msgbuf[MAX_MSG_LEN];
217
175
int msgbuf_len= 0;
219
177
assert(session != NULL);
221
/* in theory, we should return "true", meaning that the plugin isn't happy,
222
but that crashes the server, so for now, we just lie a little bit
225
if (not _gearman_client_ok)
179
if (sysvar_logging_gearman_enable == false)
182
/* TODO, looks like connect_utime isnt being set in the session
183
object. We could store the time this plugin was loaded, but that
184
would just be a dumb workaround. */
228
185
/* TODO, the session object should have a "utime command completed"
229
186
inside itself, so be more accurate, and so this doesnt have to
230
187
keep calling current_utime, which can be slow */
232
boost::posix_time::ptime mytime(boost::posix_time::microsec_clock::local_time());
233
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
234
uint64_t t_mark= (mytime-epoch).total_microseconds();
189
uint64_t t_mark= get_microtime();
237
191
// buffer to quotify the query
238
192
unsigned char qs[255];
240
194
// to avoid trying to printf %s something that is potentially NULL
241
drizzled::util::string::const_shared_ptr dbs(session->schema());
195
const char *dbs= (session->db) ? session->db : "";
198
dbl= session->db_length;
200
// todo, add hostname, listener port, and server id to this
244
snprintf(msgbuf.get(), MAX_MSG_LEN,
203
snprintf(msgbuf, MAX_MSG_LEN,
245
204
"%"PRIu64",%"PRIu64",%"PRIu64",\"%.*s\",\"%s\",\"%.*s\","
246
"%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
247
"%"PRIu32",%"PRIu32",%"PRIu32",\"%s\"",
205
"%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"",
249
207
session->thread_id,
250
session->getQueryId(),
251
209
// dont need to quote the db name, always CSV safe
252
(int)dbs->size(), dbs->c_str(),
253
211
// do need to quote the query
254
quotify((const unsigned char *)session->getQueryString()->c_str(), session->getQueryString()->length(), qs, sizeof(qs)),
212
quotify((unsigned char *)session->query,
213
session->query_length, qs, sizeof(qs)),
255
214
// command_name is defined in drizzled/sql_parse.cc
256
215
// dont need to quote the command name, always CSV safe
257
(int)drizzled::command_name[session->command].length,
258
drizzled::command_name[session->command].str,
216
(int)command_name[session->command].length,
217
command_name[session->command].str,
259
218
// counters are at end, to make it easier to add more
260
(t_mark - session->getConnectMicroseconds()),
219
(t_mark - session->connect_utime),
261
220
(t_mark - session->start_utime),
262
221
(t_mark - session->utime_after_lock),
263
222
session->sent_row_count,
264
session->examined_row_count,
266
session->total_warn_count,
267
session->getServerId(),
268
drizzled::glob_hostname
223
session->examined_row_count);
271
225
char job_handle[GEARMAN_JOB_HANDLE_SIZE];
273
(void) gearman_client_do_background(&_gearman_client,
227
(void) gearman_client_do_background(&gearman_client,
228
sysvar_logging_gearman_function,
276
(void *) msgbuf.get(),
277
231
(size_t) msgbuf_len,
284
static LoggingGearman *handler= NULL;
286
static int logging_gearman_plugin_init(drizzled::module::Context &context)
288
const drizzled::module::option_map &vm= context.getOptions();
290
handler= new LoggingGearman(vm["host"].as<std::string>(),
291
vm["function"].as<std::string>());
292
context.add(handler);
293
context.registerVariable(new drizzled::sys_var_const_string_val("host", vm["host"].as<std::string>()));
294
context.registerVariable(new drizzled::sys_var_const_string_val("function", vm["function"].as<std::string>()));
299
static void init_options(drizzled::module::option_context &context)
302
po::value<std::string>()->default_value("localhost"),
303
N_("Hostname for logging to a Gearman server"));
305
po::value<std::string>()->default_value("drizzlelog"),
306
N_("Gearman Function to send logging to"));
309
} /* namespace drizzle_plugin */
311
DRIZZLE_DECLARE_PLUGIN
238
static Logging_handler *handler= NULL;
240
static int logging_gearman_plugin_init(PluginRegistry ®istry)
242
gearman_return_t ret;
245
saying "return 0" means "success"
246
right now, if we return an error
247
this causes Drizzle to crash
248
so until that is fixed,
249
just return a success,
250
but leave the function pointers as NULL
253
if (sysvar_logging_gearman_host == NULL)
255
/* no destination gearman server host was specified via system variables
256
return now, dont set the callback pointers
261
if (gearman_client_create(&gearman_client) == NULL)
263
errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_create(): %s"),
268
/* TODO, be able to override the port */
269
ret= gearman_client_add_server(&gearman_client,
270
sysvar_logging_gearman_host, 0);
271
if (ret != GEARMAN_SUCCESS)
273
errmsg_printf(ERRMSG_LVL_ERROR, _("fail gearman_client_add_server(): %s"),
274
gearman_client_error(&gearman_client));
278
handler= new LoggingGearman();
279
registry.add(handler);
284
static int logging_gearman_plugin_deinit(PluginRegistry ®istry)
287
gearman_client_free(&gearman_client);
289
registry.remove(handler);
295
static DRIZZLE_SYSVAR_BOOL(
297
sysvar_logging_gearman_enable,
299
N_("Enable logging to a gearman server"),
300
NULL, /* check func */
301
NULL, /* update func */
302
false /* default */);
304
static DRIZZLE_SYSVAR_STR(
306
sysvar_logging_gearman_host,
308
N_("Hostname for logging to a Gearman server"),
309
NULL, /* check func */
310
NULL, /* update func*/
311
"localhost" /* default */);
313
static DRIZZLE_SYSVAR_STR(
315
sysvar_logging_gearman_function,
317
N_("Gearman Function to send logging to"),
318
NULL, /* check func */
319
NULL, /* update func*/
320
"drizzlelog" /* default */);
322
static struct st_mysql_sys_var* logging_gearman_system_variables[]= {
323
DRIZZLE_SYSVAR(enable),
324
DRIZZLE_SYSVAR(host),
325
DRIZZLE_SYSVAR(function),
329
drizzle_declare_plugin(logging_gearman)
316
333
"Mark Atwood <mark@fallenpegasus.com>",
317
334
N_("Log queries to a Gearman server"),
318
drizzled::PLUGIN_LICENSE_GPL,
319
drizzle_plugin::logging_gearman_plugin_init,
321
drizzle_plugin::init_options
336
logging_gearman_plugin_init,
337
logging_gearman_plugin_deinit,
338
NULL, /* status variables */
339
logging_gearman_system_variables,
323
DRIZZLE_DECLARE_PLUGIN_END;
342
drizzle_declare_plugin_end;