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.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 drizzled::plugin::Logging
71
: drizzled::plugin::Logging("Logging_syslog"),
72
syslog_facility(-1), syslog_priority(-1)
75
for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
77
if (strcasecmp(facilitynames[ndx].c_name, sysvar_logging_syslog_facility) == 0)
79
syslog_facility= facilitynames[ndx].c_val;
83
if (syslog_facility == -1)
85
errmsg_printf(ERRMSG_LVL_WARN,
86
_("syslog facility \"%s\" not known, using \"local0\""),
87
sysvar_logging_syslog_facility);
88
syslog_facility= LOG_LOCAL0;
91
for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
93
if (strcasecmp(prioritynames[ndx].c_name, sysvar_logging_syslog_priority) == 0)
95
syslog_priority= prioritynames[ndx].c_val;
99
if (syslog_priority == -1)
101
errmsg_printf(ERRMSG_LVL_WARN,
102
_("syslog priority \"%s\" not known, using \"info\""),
103
sysvar_logging_syslog_priority);
104
syslog_priority= LOG_INFO;
107
openlog(sysvar_logging_syslog_ident,
108
LOG_PID, syslog_facility);
116
virtual bool post (Session *session)
118
assert(session != NULL);
120
// return if not enabled or query was too fast or resultset was too small
121
if (sysvar_logging_syslog_enable == false)
123
if (session->sent_row_count < sysvar_logging_syslog_threshold_big_resultset)
125
if (session->examined_row_count < sysvar_logging_syslog_threshold_big_examined)
128
/* TODO, the session object should have a "utime command completed"
129
inside itself, so be more accurate, and so this doesnt have to
130
keep calling current_utime, which can be slow */
132
uint64_t t_mark= get_microtime();
134
if ((t_mark - session->start_utime) < sysvar_logging_syslog_threshold_slow)
137
/* to avoid trying to printf %s something that is potentially NULL */
139
const char *dbs= (session->db) ? session->db : "";
142
dbl= session->db_length;
144
const char *qys= (session->getQueryString()) ? session->getQueryString() : "";
147
qyl= session->getQueryLength();
149
syslog(syslog_priority,
150
"thread_id=%ld query_id=%ld"
154
" t_connect=%lld t_start=%lld t_lock=%lld"
155
" rows_sent=%ld rows_examined=%ld"
156
" tmp_table=%ld total_warn_count=%ld\n",
157
(unsigned long) session->thread_id,
158
(unsigned long) session->getQueryId(),
161
(int) command_name[session->command].length,
162
command_name[session->command].str,
163
(unsigned long long) (t_mark - session->getConnectMicroseconds()),
164
(unsigned long long) (t_mark - session->start_utime),
165
(unsigned long long) (t_mark - session->utime_after_lock),
166
(unsigned long) session->sent_row_count,
167
(unsigned long) session->examined_row_count,
168
(unsigned long) session->tmp_table,
169
(unsigned long) session->total_warn_count);
175
static Logging_syslog *handler= NULL;
177
static int logging_syslog_plugin_init(drizzled::plugin::Registry ®istry)
179
handler= new Logging_syslog();
180
registry.logging.add(handler);
185
static int logging_syslog_plugin_deinit(drizzled::plugin::Registry ®istry)
187
registry.logging.remove(handler);
193
static DRIZZLE_SYSVAR_BOOL(
195
sysvar_logging_syslog_enable,
197
N_("Enable logging to syslog"),
198
NULL, /* check func */
199
NULL, /* update func */
200
false /* default */);
202
static DRIZZLE_SYSVAR_STR(
204
sysvar_logging_syslog_ident,
207
NULL, /* check func */
208
NULL, /* update func*/
209
"drizzled" /* default */);
211
static DRIZZLE_SYSVAR_STR(
213
sysvar_logging_syslog_facility,
215
N_("Syslog Facility"),
216
NULL, /* check func */
217
NULL, /* update func*/
218
"local0" /* default */); // local0 is what PostGreSQL uses by default
220
static DRIZZLE_SYSVAR_STR(
222
sysvar_logging_syslog_priority,
224
N_("Syslog Priority"),
225
NULL, /* check func */
226
NULL, /* update func*/
227
"info" /* default */);
229
static DRIZZLE_SYSVAR_ULONG(
231
sysvar_logging_syslog_threshold_slow,
233
N_("Threshold for logging slow queries, in microseconds"),
234
NULL, /* check func */
235
NULL, /* update func */
241
static DRIZZLE_SYSVAR_ULONG(
242
threshold_big_resultset,
243
sysvar_logging_syslog_threshold_big_resultset,
245
N_("Threshold for logging big queries, for rows returned"),
246
NULL, /* check func */
247
NULL, /* update func */
253
static DRIZZLE_SYSVAR_ULONG(
254
threshold_big_examined,
255
sysvar_logging_syslog_threshold_big_examined,
257
N_("Threshold for logging big queries, for rows examined"),
258
NULL, /* check func */
259
NULL, /* update func */
265
static struct st_mysql_sys_var* logging_syslog_system_variables[]= {
266
DRIZZLE_SYSVAR(enable),
267
DRIZZLE_SYSVAR(ident),
268
DRIZZLE_SYSVAR(facility),
269
DRIZZLE_SYSVAR(priority),
270
DRIZZLE_SYSVAR(threshold_slow),
271
DRIZZLE_SYSVAR(threshold_big_resultset),
272
DRIZZLE_SYSVAR(threshold_big_examined),
276
drizzle_declare_plugin(logging_syslog)
280
"Mark Atwood <mark@fallenpegasus.com>",
283
logging_syslog_plugin_init,
284
logging_syslog_plugin_deinit,
285
NULL, /* status variables */
286
logging_syslog_system_variables,
289
drizzle_declare_plugin_end;