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
69
Logging_syslog() : Logging_handler("Logging_syslog") {}
71
virtual bool post (Session *session)
73
assert(session != NULL);
75
// return if not enabled or query was too fast or resultset was too small
76
if (sysvar_logging_syslog_enable == false)
78
if (session->sent_row_count < sysvar_logging_syslog_threshold_big_resultset)
80
if (session->examined_row_count < sysvar_logging_syslog_threshold_big_examined)
83
/* TODO, looks like connect_utime isnt being set in the session
84
object. We could store the time this plugin was loaded, but that
85
would just be a dumb workaround. */
86
/* TODO, the session object should have a "utime command completed"
87
inside itself, so be more accurate, and so this doesnt have to
88
keep calling current_utime, which can be slow */
90
uint64_t t_mark= get_microtime();
92
if ((t_mark - session->start_utime) < sysvar_logging_syslog_threshold_slow)
95
/* to avoid trying to printf %s something that is potentially NULL */
97
const char *dbs= (session->db) ? session->db : "";
100
dbl= session->db_length;
102
const char *qys= (session->query) ? session->query : "";
105
qyl= session->query_length;
107
syslog(syslog_priority,
108
"thread_id=%ld query_id=%ld"
112
" t_connect=%lld t_start=%lld t_lock=%lld"
113
" rows_sent=%ld rows_examined=%ld\n",
114
(unsigned long) session->thread_id,
115
(unsigned long) session->query_id,
118
(int) command_name[session->command].length,
119
command_name[session->command].str,
120
(unsigned long long) (t_mark - session->connect_utime),
121
(unsigned long long) (t_mark - session->start_utime),
122
(unsigned long long) (t_mark - session->utime_after_lock),
123
(unsigned long) session->sent_row_count,
124
(unsigned long) session->examined_row_count);
127
syslog(syslog_priority,
128
"thread_id=%ld query_id=%ld"
132
" t_connect=%lld t_start=%lld t_lock=%lld"
133
" rows_sent=%ld rows_examined=%ld\n",
134
(unsigned long) session->thread_id,
135
(unsigned long) session->query_id,
136
session->db_length, session->db,
137
// dont need to quote the query, because syslog does it itself
138
session->query_length, session->query,
139
(int) command_name[session->command].length,
140
command_name[session->command].str,
141
(unsigned long long) (t_mark - session->connect_utime),
142
(unsigned long long) (t_mark - session->start_utime),
143
(unsigned long long) (t_mark - session->utime_after_lock),
144
(unsigned long) session->sent_row_count,
145
(unsigned long) session->examined_row_count);
153
static Logging_syslog *handler= NULL;
155
static int logging_syslog_plugin_init(PluginRegistry ®istry)
158
for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
160
if (strcasecmp(facilitynames[ndx].c_name, sysvar_logging_syslog_facility) == 0)
162
syslog_facility= facilitynames[ndx].c_val;
166
if (syslog_facility == -1)
168
errmsg_printf(ERRMSG_LVL_WARN,
169
_("syslog facility \"%s\" not known, using \"local0\""),
170
sysvar_logging_syslog_facility);
171
syslog_facility= LOG_LOCAL0;
175
for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
177
if (strcasecmp(prioritynames[ndx].c_name, sysvar_logging_syslog_priority) == 0)
179
syslog_priority= prioritynames[ndx].c_val;
183
if (syslog_priority == -1)
185
errmsg_printf(ERRMSG_LVL_WARN,
186
_("syslog priority \"%s\" not known, using \"info\""),
187
sysvar_logging_syslog_priority);
188
syslog_priority= LOG_INFO;
191
openlog(sysvar_logging_syslog_ident,
192
LOG_PID, syslog_facility);
194
handler= new Logging_syslog();
195
registry.add(handler);
200
static int logging_syslog_plugin_deinit(PluginRegistry ®istry)
202
registry.remove(handler);
208
static DRIZZLE_SYSVAR_BOOL(
210
sysvar_logging_syslog_enable,
212
N_("Enable logging"),
213
NULL, /* check func */
214
NULL, /* update func */
215
false /* default */);
217
static DRIZZLE_SYSVAR_STR(
219
sysvar_logging_syslog_ident,
222
NULL, /* check func */
223
NULL, /* update func*/
224
"drizzled" /* default */);
226
static DRIZZLE_SYSVAR_STR(
228
sysvar_logging_syslog_facility,
230
N_("Syslog Facility"),
231
NULL, /* check func */
232
NULL, /* update func*/
233
"local0" /* default */); // local0 is what PostGreSQL uses by default
235
static DRIZZLE_SYSVAR_STR(
237
sysvar_logging_syslog_priority,
239
N_("Syslog Priority"),
240
NULL, /* check func */
241
NULL, /* update func*/
242
"info" /* default */);
244
static DRIZZLE_SYSVAR_ULONG(
246
sysvar_logging_syslog_threshold_slow,
248
N_("Threshold for logging slow queries, in microseconds"),
249
NULL, /* check func */
250
NULL, /* update func */
256
static DRIZZLE_SYSVAR_ULONG(
257
threshold_big_resultset,
258
sysvar_logging_syslog_threshold_big_resultset,
260
N_("Threshold for logging big queries, for rows returned"),
261
NULL, /* check func */
262
NULL, /* update func */
268
static DRIZZLE_SYSVAR_ULONG(
269
threshold_big_examined,
270
sysvar_logging_syslog_threshold_big_examined,
272
N_("Threshold for logging big queries, for rows examined"),
273
NULL, /* check func */
274
NULL, /* update func */
280
static struct st_mysql_sys_var* logging_syslog_system_variables[]= {
281
DRIZZLE_SYSVAR(enable),
282
DRIZZLE_SYSVAR(ident),
283
DRIZZLE_SYSVAR(facility),
284
DRIZZLE_SYSVAR(priority),
285
DRIZZLE_SYSVAR(threshold_slow),
286
DRIZZLE_SYSVAR(threshold_big_resultset),
287
DRIZZLE_SYSVAR(threshold_big_examined),
291
drizzle_declare_plugin(logging_syslog)
295
"Mark Atwood <mark@fallenpegasus.com>",
298
logging_syslog_plugin_init,
299
logging_syslog_plugin_deinit,
300
NULL, /* status variables */
301
logging_syslog_system_variables,
304
drizzle_declare_plugin_end;