1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Mark Atwood
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.
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.
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
20
#include <drizzled/server_includes.h>
21
#include <drizzled/plugin/logging_handler.h>
22
#include <drizzled/gettext.h>
23
#include <drizzled/session.h>
27
# include <plugin/logging_syslog/names.h>
29
# define SYSLOG_NAMES 1
35
static bool sysvar_logging_syslog_enable= false;
36
static char* sysvar_logging_syslog_ident= NULL;
37
static char* sysvar_logging_syslog_facility= NULL;
38
static char* sysvar_logging_syslog_priority= NULL;
39
static ulong sysvar_logging_syslog_threshold_slow= 0;
40
static ulong sysvar_logging_syslog_threshold_big_resultset= 0;
41
static ulong sysvar_logging_syslog_threshold_big_examined= 0;
43
static int syslog_facility= -1;
44
static int syslog_priority= -1;
46
/* stolen from mysys/my_getsystime
47
until the Session has a good utime "now" we can use
48
will have to use this instead */
51
static uint64_t get_microtime()
53
#if defined(HAVE_GETHRTIME)
54
return gethrtime()/1000;
58
/* loop is because gettimeofday may fail on some systems */
59
while (gettimeofday(&t, NULL) != 0) {}
60
newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
65
class Logging_syslog: public Logging_handler
68
virtual bool post (Session *session)
70
assert(session != NULL);
72
// return if not enabled or query was too fast or resultset was too small
73
if (sysvar_logging_syslog_enable == false)
75
if (session->sent_row_count < sysvar_logging_syslog_threshold_big_resultset)
77
if (session->examined_row_count < sysvar_logging_syslog_threshold_big_examined)
80
/* TODO, looks like connect_utime isnt being set in the session
81
object. We could store the time this plugin was loaded, but that
82
would just be a dumb workaround. */
83
/* TODO, the session object should have a "utime command completed"
84
inside itself, so be more accurate, and so this doesnt have to
85
keep calling current_utime, which can be slow */
87
uint64_t t_mark= get_microtime();
89
if ((t_mark - session->start_utime) < sysvar_logging_syslog_threshold_slow)
92
/* to avoid trying to printf %s something that is potentially NULL */
94
const char *dbs= (session->db) ? session->db : "";
97
dbl= session->db_length;
99
const char *qys= (session->query) ? session->query : "";
102
qyl= session->query_length;
104
syslog(syslog_priority,
105
"thread_id=%ld query_id=%ld"
109
" t_connect=%lld t_start=%lld t_lock=%lld"
110
" rows_sent=%ld rows_examined=%ld\n",
111
(unsigned long) session->thread_id,
112
(unsigned long) session->query_id,
115
(int) command_name[session->command].length,
116
command_name[session->command].str,
117
(unsigned long long) (t_mark - session->connect_utime),
118
(unsigned long long) (t_mark - session->start_utime),
119
(unsigned long long) (t_mark - session->utime_after_lock),
120
(unsigned long) session->sent_row_count,
121
(unsigned long) session->examined_row_count);
124
syslog(syslog_priority,
125
"thread_id=%ld query_id=%ld"
129
" t_connect=%lld t_start=%lld t_lock=%lld"
130
" rows_sent=%ld rows_examined=%ld\n",
131
(unsigned long) session->thread_id,
132
(unsigned long) session->query_id,
133
session->db_length, session->db,
134
// dont need to quote the query, because syslog does it itself
135
session->query_length, session->query,
136
(int) command_name[session->command].length,
137
command_name[session->command].str,
138
(unsigned long long) (t_mark - session->connect_utime),
139
(unsigned long long) (t_mark - session->start_utime),
140
(unsigned long long) (t_mark - session->utime_after_lock),
141
(unsigned long) session->sent_row_count,
142
(unsigned long) session->examined_row_count);
150
static int logging_syslog_plugin_init(void *p)
152
Logging_syslog **handler= static_cast<Logging_syslog **>(p);
156
for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
158
if (strcasecmp(facilitynames[ndx].c_name, sysvar_logging_syslog_facility) == 0)
160
syslog_facility= facilitynames[ndx].c_val;
164
if (syslog_facility == -1)
166
errmsg_printf(ERRMSG_LVL_WARN,
167
_("syslog facility \"%s\" not known, using \"local0\""),
168
sysvar_logging_syslog_facility);
169
syslog_facility= LOG_LOCAL0;
173
for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
175
if (strcasecmp(prioritynames[ndx].c_name, sysvar_logging_syslog_priority) == 0)
177
syslog_priority= prioritynames[ndx].c_val;
181
if (syslog_priority == -1)
183
errmsg_printf(ERRMSG_LVL_WARN,
184
_("syslog priority \"%s\" not known, using \"info\""),
185
sysvar_logging_syslog_priority);
186
syslog_priority= LOG_INFO;
189
openlog(sysvar_logging_syslog_ident,
190
LOG_PID, syslog_facility);
192
*handler= new Logging_syslog();
197
static int logging_syslog_plugin_deinit(void *p)
199
Logging_syslog *handler= static_cast<Logging_syslog *>(p);
206
static DRIZZLE_SYSVAR_BOOL(
208
sysvar_logging_syslog_enable,
210
N_("Enable logging"),
211
NULL, /* check func */
212
NULL, /* update func */
213
false /* default */);
215
static DRIZZLE_SYSVAR_STR(
217
sysvar_logging_syslog_ident,
220
NULL, /* check func */
221
NULL, /* update func*/
222
"drizzled" /* default */);
224
static DRIZZLE_SYSVAR_STR(
226
sysvar_logging_syslog_facility,
228
N_("Syslog Facility"),
229
NULL, /* check func */
230
NULL, /* update func*/
231
"local0" /* default */); // local0 is what PostGreSQL uses by default
233
static DRIZZLE_SYSVAR_STR(
235
sysvar_logging_syslog_priority,
237
N_("Syslog Priority"),
238
NULL, /* check func */
239
NULL, /* update func*/
240
"info" /* default */);
242
static DRIZZLE_SYSVAR_ULONG(
244
sysvar_logging_syslog_threshold_slow,
246
N_("Threshold for logging slow queries, in microseconds"),
247
NULL, /* check func */
248
NULL, /* update func */
254
static DRIZZLE_SYSVAR_ULONG(
255
threshold_big_resultset,
256
sysvar_logging_syslog_threshold_big_resultset,
258
N_("Threshold for logging big queries, for rows returned"),
259
NULL, /* check func */
260
NULL, /* update func */
266
static DRIZZLE_SYSVAR_ULONG(
267
threshold_big_examined,
268
sysvar_logging_syslog_threshold_big_examined,
270
N_("Threshold for logging big queries, for rows examined"),
271
NULL, /* check func */
272
NULL, /* update func */
278
static struct st_mysql_sys_var* logging_syslog_system_variables[]= {
279
DRIZZLE_SYSVAR(enable),
280
DRIZZLE_SYSVAR(ident),
281
DRIZZLE_SYSVAR(facility),
282
DRIZZLE_SYSVAR(priority),
283
DRIZZLE_SYSVAR(threshold_slow),
284
DRIZZLE_SYSVAR(threshold_big_resultset),
285
DRIZZLE_SYSVAR(threshold_big_examined),
289
drizzle_declare_plugin(logging_syslog)
291
DRIZZLE_LOGGER_PLUGIN,
294
"Mark Atwood <mark@fallenpegasus.com>",
297
logging_syslog_plugin_init,
298
logging_syslog_plugin_deinit,
299
NULL, /* status variables */
300
logging_syslog_system_variables,
303
drizzle_declare_plugin_end;