~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2009 Mark Atwood
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <drizzled/server_includes.h>
#include <drizzled/plugin_logging.h>
#include <drizzled/gettext.h>
#include <drizzled/session.h>

#ifdef __sun
# include <syslog.h>
# include <plugin/logging_syslog/names.h>
#else
# define SYSLOG_NAMES 1
# include <syslog.h>
#endif

#include <stdarg.h>

static bool sysvar_logging_syslog_enable= false;
static char* sysvar_logging_syslog_ident= NULL;
static char* sysvar_logging_syslog_facility= NULL;
static char* sysvar_logging_syslog_priority= NULL;
static ulong sysvar_logging_syslog_threshold_slow= 0;
static ulong sysvar_logging_syslog_threshold_big_resultset= 0;
static ulong sysvar_logging_syslog_threshold_big_examined= 0;

static int syslog_facility= -1;
static int syslog_priority= -1;

/* stolen from mysys/my_getsystime
   until the Session has a good utime "now" we can use
   will have to use this instead */

#include <sys/time.h>
static uint64_t get_microtime()
{
#if defined(HAVE_GETHRTIME)
  return gethrtime()/1000;
#else
  uint64_t newtime;
  struct timeval t;
  /* loop is because gettimeofday may fail on some systems */
  while (gettimeofday(&t, NULL) != 0) {}
  newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
  return newtime;
#endif
}

class Logging_syslog: public Logging_handler
{

  virtual bool post (Session *session)
  {
    assert(session != NULL);
  
    // return if not enabled or query was too fast or resultset was too small
    if (sysvar_logging_syslog_enable == false)
      return false;
    if (session->sent_row_count < sysvar_logging_syslog_threshold_big_resultset)
      return false;
    if (session->examined_row_count < sysvar_logging_syslog_threshold_big_examined)
      return false;
  
    /* TODO, looks like connect_utime isnt being set in the session
       object.  We could store the time this plugin was loaded, but that
       would just be a dumb workaround. */
    /* TODO, the session object should have a "utime command completed"
       inside itself, so be more accurate, and so this doesnt have to
       keep calling current_utime, which can be slow */
  
    uint64_t t_mark= get_microtime();
  
    if ((t_mark - session->start_utime) < sysvar_logging_syslog_threshold_slow)
      return false;
  
    /* to avoid trying to printf %s something that is potentially NULL */
  
    const char *dbs= (session->db) ? session->db : "";
    int dbl= 0;
    if (dbs)
      dbl= session->db_length;
  
    const char *qys= (session->query) ? session->query : "";
    int qyl= 0;
    if (qys)
      qyl= session->query_length;
    
    syslog(syslog_priority,
           "thread_id=%ld query_id=%ld"
           " db=\"%.*s\""
           " query=\"%.*s\""
           " command=\"%.*s\""
           " t_connect=%lld t_start=%lld t_lock=%lld"
           " rows_sent=%ld rows_examined=%ld\n",
           (unsigned long) session->thread_id,
           (unsigned long) session->query_id,
           dbl, dbs,
           qyl, qys,
           (int) command_name[session->command].length,
           command_name[session->command].str,
           (unsigned long long) (t_mark - session->connect_utime),
           (unsigned long long) (t_mark - session->start_utime),
           (unsigned long long) (t_mark - session->utime_after_lock),
           (unsigned long) session->sent_row_count,
           (unsigned long) session->examined_row_count);
  
#if 0
    syslog(syslog_priority,
           "thread_id=%ld query_id=%ld"
           " db=\"%.*s\""
           " query=\".*%s\""
           " command=%.*s"
           " t_connect=%lld t_start=%lld t_lock=%lld"
           " rows_sent=%ld rows_examined=%ld\n",
           (unsigned long) session->thread_id,
           (unsigned long) session->query_id,
           session->db_length, session->db,
           // dont need to quote the query, because syslog does it itself
           session->query_length, session->query,
           (int) command_name[session->command].length,
           command_name[session->command].str,
           (unsigned long long) (t_mark - session->connect_utime),
           (unsigned long long) (t_mark - session->start_utime),
           (unsigned long long) (t_mark - session->utime_after_lock),
           (unsigned long) session->sent_row_count,
           (unsigned long) session->examined_row_count);
  
#endif
  
    return false;
  }
};

static int logging_syslog_plugin_init(void *p)
{
  Logging_syslog **handler= static_cast<Logging_syslog **>(p);
  *handler= NULL;

  syslog_facility= -1;
  for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
  {
    if (strcasecmp(facilitynames[ndx].c_name, sysvar_logging_syslog_facility) == 0)
    {
      syslog_facility= facilitynames[ndx].c_val;
      break;
    }
  }
  if (syslog_facility == -1)
  {
    errmsg_printf(ERRMSG_LVL_WARN,
                  _("syslog facility \"%s\" not known, using \"local0\""),
                  sysvar_logging_syslog_facility);
    syslog_facility= LOG_LOCAL0;
  }

  syslog_priority= -1;
  for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
  {
    if (strcasecmp(prioritynames[ndx].c_name, sysvar_logging_syslog_priority) == 0)
    {
      syslog_priority= prioritynames[ndx].c_val;
      break;
    }
  }
  if (syslog_priority == -1)
  {
    errmsg_printf(ERRMSG_LVL_WARN,
                  _("syslog priority \"%s\" not known, using \"info\""),
                  sysvar_logging_syslog_priority);
    syslog_priority= LOG_INFO;
  }

  openlog(sysvar_logging_syslog_ident,
          LOG_PID, syslog_facility);

  *handler= new Logging_syslog();

  return 0;
}

static int logging_syslog_plugin_deinit(void *p)
{
  Logging_syslog *handler= static_cast<Logging_syslog *>(p);

  delete handler;

  return 0;
}

static DRIZZLE_SYSVAR_BOOL(
  enable,
  sysvar_logging_syslog_enable,
  PLUGIN_VAR_NOCMDARG,
  N_("Enable logging"),
  NULL, /* check func */
  NULL, /* update func */
  false /* default */);

static DRIZZLE_SYSVAR_STR(
  ident,
  sysvar_logging_syslog_ident,
  PLUGIN_VAR_READONLY,
  N_("Syslog Ident"),
  NULL, /* check func */
  NULL, /* update func*/
  "drizzled" /* default */);

static DRIZZLE_SYSVAR_STR(
  facility,
  sysvar_logging_syslog_facility,
  PLUGIN_VAR_READONLY,
  N_("Syslog Facility"),
  NULL, /* check func */
  NULL, /* update func*/
  "local0" /* default */);  // local0 is what PostGreSQL uses by default

static DRIZZLE_SYSVAR_STR(
  priority,
  sysvar_logging_syslog_priority,
  PLUGIN_VAR_READONLY,
  N_("Syslog Priority"),
  NULL, /* check func */
  NULL, /* update func*/
  "info" /* default */);

static DRIZZLE_SYSVAR_ULONG(
  threshold_slow,
  sysvar_logging_syslog_threshold_slow,
  PLUGIN_VAR_OPCMDARG,
  N_("Threshold for logging slow queries, in microseconds"),
  NULL, /* check func */
  NULL, /* update func */
  0, /* default */
  0, /* min */
  ULONG_MAX, /* max */
  0 /* blksiz */);

static DRIZZLE_SYSVAR_ULONG(
  threshold_big_resultset,
  sysvar_logging_syslog_threshold_big_resultset,
  PLUGIN_VAR_OPCMDARG,
  N_("Threshold for logging big queries, for rows returned"),
  NULL, /* check func */
  NULL, /* update func */
  0, /* default */
  0, /* min */
  ULONG_MAX, /* max */
  0 /* blksiz */);

static DRIZZLE_SYSVAR_ULONG(
  threshold_big_examined,
  sysvar_logging_syslog_threshold_big_examined,
  PLUGIN_VAR_OPCMDARG,
  N_("Threshold for logging big queries, for rows examined"),
  NULL, /* check func */
  NULL, /* update func */
  0, /* default */
  0, /* min */
  ULONG_MAX, /* max */
  0 /* blksiz */);

static struct st_mysql_sys_var* logging_syslog_system_variables[]= {
  DRIZZLE_SYSVAR(enable),
  DRIZZLE_SYSVAR(ident),
  DRIZZLE_SYSVAR(facility),
  DRIZZLE_SYSVAR(priority),
  DRIZZLE_SYSVAR(threshold_slow),
  DRIZZLE_SYSVAR(threshold_big_resultset),
  DRIZZLE_SYSVAR(threshold_big_examined),
  NULL
};

drizzle_declare_plugin(logging_syslog)
{
  DRIZZLE_LOGGER_PLUGIN,
  "logging_syslog",
  "0.2",
  "Mark Atwood <mark@fallenpegasus.com>",
  N_("Log to syslog"),
  PLUGIN_LICENSE_GPL,
  logging_syslog_plugin_init,
  logging_syslog_plugin_deinit,
  NULL,   /* status variables */
  logging_syslog_system_variables,
  NULL
}
drizzle_declare_plugin_end;