1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
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
/* stolen from mysys/my_getsystime
44
until the Session has a good utime "now" we can use
45
will have to use this instead */
48
static uint64_t get_microtime()
50
#if defined(HAVE_GETHRTIME)
51
return gethrtime()/1000;
55
/* loop is because gettimeofday may fail on some systems */
56
while (gettimeofday(&t, NULL) != 0) {}
57
newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
62
class Logging_syslog: public Logging_handler
70
Logging_syslog() : Logging_handler("Logging_syslog"), syslog_facility(-1), syslog_priority(-1)
73
for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
75
if (strcasecmp(facilitynames[ndx].c_name, sysvar_logging_syslog_facility) == 0)
77
syslog_facility= facilitynames[ndx].c_val;
81
if (syslog_facility == -1)
83
errmsg_printf(ERRMSG_LVL_WARN,
84
_("syslog facility \"%s\" not known, using \"local0\""),
85
sysvar_logging_syslog_facility);
86
syslog_facility= LOG_LOCAL0;
89
for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
91
if (strcasecmp(prioritynames[ndx].c_name, sysvar_logging_syslog_priority) == 0)
93
syslog_priority= prioritynames[ndx].c_val;
97
if (syslog_priority == -1)
99
errmsg_printf(ERRMSG_LVL_WARN,
100
_("syslog priority \"%s\" not known, using \"info\""),
101
sysvar_logging_syslog_priority);
102
syslog_priority= LOG_INFO;
105
openlog(sysvar_logging_syslog_ident,
106
LOG_PID, syslog_facility);
114
virtual bool post (Session *session)
116
assert(session != NULL);
118
// return if not enabled or query was too fast or resultset was too small
119
if (sysvar_logging_syslog_enable == false)
121
if (session->sent_row_count < sysvar_logging_syslog_threshold_big_resultset)
123
if (session->examined_row_count < sysvar_logging_syslog_threshold_big_examined)
126
/* TODO, looks like connect_utime isnt being set in the session
127
object. We could store the time this plugin was loaded, but that
128
would just be a dumb workaround. */
129
/* TODO, the session object should have a "utime command completed"
130
inside itself, so be more accurate, and so this doesnt have to
131
keep calling current_utime, which can be slow */
133
uint64_t t_mark= get_microtime();
135
if ((t_mark - session->start_utime) < sysvar_logging_syslog_threshold_slow)
138
/* to avoid trying to printf %s something that is potentially NULL */
140
const char *dbs= (session->db) ? session->db : "";
143
dbl= session->db_length;
145
const char *qys= (session->query) ? session->query : "";
148
qyl= session->query_length;
150
syslog(syslog_priority,
151
"thread_id=%ld query_id=%ld"
155
" t_connect=%lld t_start=%lld t_lock=%lld"
156
" rows_sent=%ld rows_examined=%ld"
157
" tmp_table=%ld total_warn_count=%ld\n",
158
(unsigned long) session->thread_id,
159
(unsigned long) session->query_id,
162
(int) command_name[session->command].length,
163
command_name[session->command].str,
164
(unsigned long long) (t_mark - session->connect_utime),
165
(unsigned long long) (t_mark - session->start_utime),
166
(unsigned long long) (t_mark - session->utime_after_lock),
167
(unsigned long) session->sent_row_count,
168
(unsigned long) session->examined_row_count,
169
(unsigned long) session->tmp_table,
170
(unsigned long) session->total_warn_count);
176
static Logging_syslog *handler= NULL;
178
static int logging_syslog_plugin_init(PluginRegistry ®istry)
180
handler= new Logging_syslog();
181
registry.add(handler);
186
static int logging_syslog_plugin_deinit(PluginRegistry ®istry)
188
registry.remove(handler);
194
static DRIZZLE_SYSVAR_BOOL(
196
sysvar_logging_syslog_enable,
198
N_("Enable logging to syslog"),
199
NULL, /* check func */
200
NULL, /* update func */
201
false /* default */);
203
static DRIZZLE_SYSVAR_STR(
205
sysvar_logging_syslog_ident,
208
NULL, /* check func */
209
NULL, /* update func*/
210
"drizzled" /* default */);
212
static DRIZZLE_SYSVAR_STR(
214
sysvar_logging_syslog_facility,
216
N_("Syslog Facility"),
217
NULL, /* check func */
218
NULL, /* update func*/
219
"local0" /* default */); // local0 is what PostGreSQL uses by default
221
static DRIZZLE_SYSVAR_STR(
223
sysvar_logging_syslog_priority,
225
N_("Syslog Priority"),
226
NULL, /* check func */
227
NULL, /* update func*/
228
"info" /* default */);
230
static DRIZZLE_SYSVAR_ULONG(
232
sysvar_logging_syslog_threshold_slow,
234
N_("Threshold for logging slow queries, in microseconds"),
235
NULL, /* check func */
236
NULL, /* update func */
242
static DRIZZLE_SYSVAR_ULONG(
243
threshold_big_resultset,
244
sysvar_logging_syslog_threshold_big_resultset,
246
N_("Threshold for logging big queries, for rows returned"),
247
NULL, /* check func */
248
NULL, /* update func */
254
static DRIZZLE_SYSVAR_ULONG(
255
threshold_big_examined,
256
sysvar_logging_syslog_threshold_big_examined,
258
N_("Threshold for logging big queries, for rows examined"),
259
NULL, /* check func */
260
NULL, /* update func */
266
static struct st_mysql_sys_var* logging_syslog_system_variables[]= {
267
DRIZZLE_SYSVAR(enable),
268
DRIZZLE_SYSVAR(ident),
269
DRIZZLE_SYSVAR(facility),
270
DRIZZLE_SYSVAR(priority),
271
DRIZZLE_SYSVAR(threshold_slow),
272
DRIZZLE_SYSVAR(threshold_big_resultset),
273
DRIZZLE_SYSVAR(threshold_big_examined),
277
drizzle_declare_plugin(logging_syslog)
281
"Mark Atwood <mark@fallenpegasus.com>",
284
logging_syslog_plugin_init,
285
logging_syslog_plugin_deinit,
286
NULL, /* status variables */
287
logging_syslog_system_variables,
290
drizzle_declare_plugin_end;