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
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
36
#include <sys/types.h>
41
using namespace drizzled;
43
static bool sysvar_logging_syslog_enable= false;
44
static char* sysvar_logging_syslog_ident= NULL;
45
static char* sysvar_logging_syslog_facility= NULL;
46
static char* sysvar_logging_syslog_priority= NULL;
47
static ulong sysvar_logging_syslog_threshold_slow= 0;
48
static ulong sysvar_logging_syslog_threshold_big_resultset= 0;
49
static ulong sysvar_logging_syslog_threshold_big_examined= 0;
51
/* stolen from mysys/my_getsystime
52
until the Session has a good utime "now" we can use
53
will have to use this instead */
55
static uint64_t get_microtime()
57
#if defined(HAVE_GETHRTIME)
58
return gethrtime()/1000;
62
/* loop is because gettimeofday may fail on some systems */
63
while (gettimeofday(&t, NULL) != 0) {}
64
newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
69
class Logging_syslog: public drizzled::plugin::Logging
78
: drizzled::plugin::Logging("Logging_syslog"),
79
syslog_facility(-1), syslog_priority(-1)
82
for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
84
if (strcasecmp(facilitynames[ndx].c_name, sysvar_logging_syslog_facility) == 0)
86
syslog_facility= facilitynames[ndx].c_val;
90
if (syslog_facility == -1)
92
errmsg_printf(ERRMSG_LVL_WARN,
93
_("syslog facility \"%s\" not known, using \"local0\""),
94
sysvar_logging_syslog_facility);
95
syslog_facility= LOG_LOCAL0;
98
for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
100
if (strcasecmp(prioritynames[ndx].c_name, sysvar_logging_syslog_priority) == 0)
102
syslog_priority= prioritynames[ndx].c_val;
106
if (syslog_priority == -1)
108
errmsg_printf(ERRMSG_LVL_WARN,
109
_("syslog priority \"%s\" not known, using \"info\""),
110
sysvar_logging_syslog_priority);
111
syslog_priority= LOG_INFO;
114
openlog(sysvar_logging_syslog_ident,
115
LOG_PID, syslog_facility);
123
virtual bool post (Session *session)
125
assert(session != NULL);
127
// return if not enabled or query was too fast or resultset was too small
128
if (sysvar_logging_syslog_enable == false)
130
if (session->sent_row_count < sysvar_logging_syslog_threshold_big_resultset)
132
if (session->examined_row_count < sysvar_logging_syslog_threshold_big_examined)
135
/* TODO, the session object should have a "utime command completed"
136
inside itself, so be more accurate, and so this doesnt have to
137
keep calling current_utime, which can be slow */
139
uint64_t t_mark= get_microtime();
141
if ((t_mark - session->start_utime) < sysvar_logging_syslog_threshold_slow)
144
/* to avoid trying to printf %s something that is potentially NULL */
146
const char *dbs= session->db.empty() ? "" : session->db.c_str();
148
const char *qys= (session->getQueryString()) ? session->getQueryString() : "";
151
qyl= session->getQueryLength();
153
syslog(syslog_priority,
154
"thread_id=%ld query_id=%ld"
158
" t_connect=%lld t_start=%lld t_lock=%lld"
159
" rows_sent=%ld rows_examined=%ld"
160
" tmp_table=%ld total_warn_count=%ld\n",
161
(unsigned long) session->thread_id,
162
(unsigned long) session->getQueryId(),
163
(int)session->db.length(), dbs,
165
(int) command_name[session->command].length,
166
command_name[session->command].str,
167
(unsigned long long) (t_mark - session->getConnectMicroseconds()),
168
(unsigned long long) (t_mark - session->start_utime),
169
(unsigned long long) (t_mark - session->utime_after_lock),
170
(unsigned long) session->sent_row_count,
171
(unsigned long) session->examined_row_count,
172
(unsigned long) session->tmp_table,
173
(unsigned long) session->total_warn_count);
179
static Logging_syslog *handler= NULL;
181
static int logging_syslog_plugin_init(drizzled::plugin::Registry ®istry)
183
handler= new Logging_syslog();
184
registry.add(handler);
189
static int logging_syslog_plugin_deinit(drizzled::plugin::Registry ®istry)
191
registry.remove(handler);
197
static DRIZZLE_SYSVAR_BOOL(
199
sysvar_logging_syslog_enable,
201
N_("Enable logging to syslog"),
202
NULL, /* check func */
203
NULL, /* update func */
204
false /* default */);
206
static DRIZZLE_SYSVAR_STR(
208
sysvar_logging_syslog_ident,
211
NULL, /* check func */
212
NULL, /* update func*/
213
"drizzled" /* default */);
215
static DRIZZLE_SYSVAR_STR(
217
sysvar_logging_syslog_facility,
219
N_("Syslog Facility"),
220
NULL, /* check func */
221
NULL, /* update func*/
222
"local0" /* default */); // local0 is what PostGreSQL uses by default
224
static DRIZZLE_SYSVAR_STR(
226
sysvar_logging_syslog_priority,
228
N_("Syslog Priority"),
229
NULL, /* check func */
230
NULL, /* update func*/
231
"info" /* default */);
233
static DRIZZLE_SYSVAR_ULONG(
235
sysvar_logging_syslog_threshold_slow,
237
N_("Threshold for logging slow queries, in microseconds"),
238
NULL, /* check func */
239
NULL, /* update func */
245
static DRIZZLE_SYSVAR_ULONG(
246
threshold_big_resultset,
247
sysvar_logging_syslog_threshold_big_resultset,
249
N_("Threshold for logging big queries, for rows returned"),
250
NULL, /* check func */
251
NULL, /* update func */
257
static DRIZZLE_SYSVAR_ULONG(
258
threshold_big_examined,
259
sysvar_logging_syslog_threshold_big_examined,
261
N_("Threshold for logging big queries, for rows examined"),
262
NULL, /* check func */
263
NULL, /* update func */
269
static drizzle_sys_var* logging_syslog_system_variables[]= {
270
DRIZZLE_SYSVAR(enable),
271
DRIZZLE_SYSVAR(ident),
272
DRIZZLE_SYSVAR(facility),
273
DRIZZLE_SYSVAR(priority),
274
DRIZZLE_SYSVAR(threshold_slow),
275
DRIZZLE_SYSVAR(threshold_big_resultset),
276
DRIZZLE_SYSVAR(threshold_big_examined),
280
DRIZZLE_DECLARE_PLUGIN
285
"Mark Atwood <mark@fallenpegasus.com>",
288
logging_syslog_plugin_init,
289
logging_syslog_plugin_deinit,
290
NULL, /* status variables */
291
logging_syslog_system_variables,
294
DRIZZLE_DECLARE_PLUGIN_END;