~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_syslog/logging_syslog.cc

  • Committer: Monty Taylor
  • Date: 2009-03-24 17:44:41 UTC
  • mto: (960.5.2 mordred)
  • mto: This revision was merged to the branch mainline in revision 964.
  • Revision ID: mordred@inaugust.com-20090324174441-nmsq0gwjlgf7f0mt
Changed handlerton to StorageEngine.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2009 Mark Atwood
 
5
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 */
 
19
 
 
20
#include <drizzled/server_includes.h>
 
21
#include <drizzled/plugin/logging_handler.h>
 
22
#include <drizzled/gettext.h>
 
23
#include <drizzled/session.h>
 
24
 
 
25
#ifdef __sun
 
26
# include <syslog.h>
 
27
# include <plugin/logging_syslog/names.h>
 
28
#else
 
29
# define SYSLOG_NAMES 1
 
30
# include <syslog.h>
 
31
#endif
 
32
 
 
33
#include <stdarg.h>
 
34
 
 
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;
 
42
 
 
43
static int syslog_facility= -1;
 
44
static int syslog_priority= -1;
 
45
 
 
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 */
 
49
 
 
50
#include <sys/time.h>
 
51
static uint64_t get_microtime()
 
52
{
 
53
#if defined(HAVE_GETHRTIME)
 
54
  return gethrtime()/1000;
 
55
#else
 
56
  uint64_t newtime;
 
57
  struct timeval t;
 
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;
 
61
  return newtime;
 
62
#endif
 
63
}
 
64
 
 
65
class Logging_syslog: public Logging_handler
 
66
{
 
67
 
 
68
  virtual bool post (Session *session)
 
69
  {
 
70
    assert(session != NULL);
 
71
  
 
72
    // return if not enabled or query was too fast or resultset was too small
 
73
    if (sysvar_logging_syslog_enable == false)
 
74
      return false;
 
75
    if (session->sent_row_count < sysvar_logging_syslog_threshold_big_resultset)
 
76
      return false;
 
77
    if (session->examined_row_count < sysvar_logging_syslog_threshold_big_examined)
 
78
      return false;
 
79
  
 
80
    /* TODO, looks like connect_utime isnt being set in the session
 
81
       object.  We could store the time this plugin was loaded, but that
 
82
       would just be a dumb workaround. */
 
83
    /* TODO, the session object should have a "utime command completed"
 
84
       inside itself, so be more accurate, and so this doesnt have to
 
85
       keep calling current_utime, which can be slow */
 
86
  
 
87
    uint64_t t_mark= get_microtime();
 
88
  
 
89
    if ((t_mark - session->start_utime) < sysvar_logging_syslog_threshold_slow)
 
90
      return false;
 
91
  
 
92
    /* to avoid trying to printf %s something that is potentially NULL */
 
93
  
 
94
    const char *dbs= (session->db) ? session->db : "";
 
95
    int dbl= 0;
 
96
    if (dbs)
 
97
      dbl= session->db_length;
 
98
  
 
99
    const char *qys= (session->query) ? session->query : "";
 
100
    int qyl= 0;
 
101
    if (qys)
 
102
      qyl= session->query_length;
 
103
    
 
104
    syslog(syslog_priority,
 
105
           "thread_id=%ld query_id=%ld"
 
106
           " db=\"%.*s\""
 
107
           " query=\"%.*s\""
 
108
           " command=\"%.*s\""
 
109
           " t_connect=%lld t_start=%lld t_lock=%lld"
 
110
           " rows_sent=%ld rows_examined=%ld\n",
 
111
           (unsigned long) session->thread_id,
 
112
           (unsigned long) session->query_id,
 
113
           dbl, dbs,
 
114
           qyl, qys,
 
115
           (int) command_name[session->command].length,
 
116
           command_name[session->command].str,
 
117
           (unsigned long long) (t_mark - session->connect_utime),
 
118
           (unsigned long long) (t_mark - session->start_utime),
 
119
           (unsigned long long) (t_mark - session->utime_after_lock),
 
120
           (unsigned long) session->sent_row_count,
 
121
           (unsigned long) session->examined_row_count);
 
122
  
 
123
#if 0
 
124
    syslog(syslog_priority,
 
125
           "thread_id=%ld query_id=%ld"
 
126
           " db=\"%.*s\""
 
127
           " query=\".*%s\""
 
128
           " command=%.*s"
 
129
           " t_connect=%lld t_start=%lld t_lock=%lld"
 
130
           " rows_sent=%ld rows_examined=%ld\n",
 
131
           (unsigned long) session->thread_id,
 
132
           (unsigned long) session->query_id,
 
133
           session->db_length, session->db,
 
134
           // dont need to quote the query, because syslog does it itself
 
135
           session->query_length, session->query,
 
136
           (int) command_name[session->command].length,
 
137
           command_name[session->command].str,
 
138
           (unsigned long long) (t_mark - session->connect_utime),
 
139
           (unsigned long long) (t_mark - session->start_utime),
 
140
           (unsigned long long) (t_mark - session->utime_after_lock),
 
141
           (unsigned long) session->sent_row_count,
 
142
           (unsigned long) session->examined_row_count);
 
143
  
 
144
#endif
 
145
  
 
146
    return false;
 
147
  }
 
148
};
 
149
 
 
150
static int logging_syslog_plugin_init(void *p)
 
151
{
 
152
  Logging_syslog **handler= static_cast<Logging_syslog **>(p);
 
153
  *handler= NULL;
 
154
 
 
155
  syslog_facility= -1;
 
156
  for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
 
157
  {
 
158
    if (strcasecmp(facilitynames[ndx].c_name, sysvar_logging_syslog_facility) == 0)
 
159
    {
 
160
      syslog_facility= facilitynames[ndx].c_val;
 
161
      break;
 
162
    }
 
163
  }
 
164
  if (syslog_facility == -1)
 
165
  {
 
166
    errmsg_printf(ERRMSG_LVL_WARN,
 
167
                  _("syslog facility \"%s\" not known, using \"local0\""),
 
168
                  sysvar_logging_syslog_facility);
 
169
    syslog_facility= LOG_LOCAL0;
 
170
  }
 
171
 
 
172
  syslog_priority= -1;
 
173
  for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
 
174
  {
 
175
    if (strcasecmp(prioritynames[ndx].c_name, sysvar_logging_syslog_priority) == 0)
 
176
    {
 
177
      syslog_priority= prioritynames[ndx].c_val;
 
178
      break;
 
179
    }
 
180
  }
 
181
  if (syslog_priority == -1)
 
182
  {
 
183
    errmsg_printf(ERRMSG_LVL_WARN,
 
184
                  _("syslog priority \"%s\" not known, using \"info\""),
 
185
                  sysvar_logging_syslog_priority);
 
186
    syslog_priority= LOG_INFO;
 
187
  }
 
188
 
 
189
  openlog(sysvar_logging_syslog_ident,
 
190
          LOG_PID, syslog_facility);
 
191
 
 
192
  *handler= new Logging_syslog();
 
193
 
 
194
  return 0;
 
195
}
 
196
 
 
197
static int logging_syslog_plugin_deinit(void *p)
 
198
{
 
199
  Logging_syslog *handler= static_cast<Logging_syslog *>(p);
 
200
 
 
201
  delete handler;
 
202
 
 
203
  return 0;
 
204
}
 
205
 
 
206
static DRIZZLE_SYSVAR_BOOL(
 
207
  enable,
 
208
  sysvar_logging_syslog_enable,
 
209
  PLUGIN_VAR_NOCMDARG,
 
210
  N_("Enable logging"),
 
211
  NULL, /* check func */
 
212
  NULL, /* update func */
 
213
  false /* default */);
 
214
 
 
215
static DRIZZLE_SYSVAR_STR(
 
216
  ident,
 
217
  sysvar_logging_syslog_ident,
 
218
  PLUGIN_VAR_READONLY,
 
219
  N_("Syslog Ident"),
 
220
  NULL, /* check func */
 
221
  NULL, /* update func*/
 
222
  "drizzled" /* default */);
 
223
 
 
224
static DRIZZLE_SYSVAR_STR(
 
225
  facility,
 
226
  sysvar_logging_syslog_facility,
 
227
  PLUGIN_VAR_READONLY,
 
228
  N_("Syslog Facility"),
 
229
  NULL, /* check func */
 
230
  NULL, /* update func*/
 
231
  "local0" /* default */);  // local0 is what PostGreSQL uses by default
 
232
 
 
233
static DRIZZLE_SYSVAR_STR(
 
234
  priority,
 
235
  sysvar_logging_syslog_priority,
 
236
  PLUGIN_VAR_READONLY,
 
237
  N_("Syslog Priority"),
 
238
  NULL, /* check func */
 
239
  NULL, /* update func*/
 
240
  "info" /* default */);
 
241
 
 
242
static DRIZZLE_SYSVAR_ULONG(
 
243
  threshold_slow,
 
244
  sysvar_logging_syslog_threshold_slow,
 
245
  PLUGIN_VAR_OPCMDARG,
 
246
  N_("Threshold for logging slow queries, in microseconds"),
 
247
  NULL, /* check func */
 
248
  NULL, /* update func */
 
249
  0, /* default */
 
250
  0, /* min */
 
251
  ULONG_MAX, /* max */
 
252
  0 /* blksiz */);
 
253
 
 
254
static DRIZZLE_SYSVAR_ULONG(
 
255
  threshold_big_resultset,
 
256
  sysvar_logging_syslog_threshold_big_resultset,
 
257
  PLUGIN_VAR_OPCMDARG,
 
258
  N_("Threshold for logging big queries, for rows returned"),
 
259
  NULL, /* check func */
 
260
  NULL, /* update func */
 
261
  0, /* default */
 
262
  0, /* min */
 
263
  ULONG_MAX, /* max */
 
264
  0 /* blksiz */);
 
265
 
 
266
static DRIZZLE_SYSVAR_ULONG(
 
267
  threshold_big_examined,
 
268
  sysvar_logging_syslog_threshold_big_examined,
 
269
  PLUGIN_VAR_OPCMDARG,
 
270
  N_("Threshold for logging big queries, for rows examined"),
 
271
  NULL, /* check func */
 
272
  NULL, /* update func */
 
273
  0, /* default */
 
274
  0, /* min */
 
275
  ULONG_MAX, /* max */
 
276
  0 /* blksiz */);
 
277
 
 
278
static struct st_mysql_sys_var* logging_syslog_system_variables[]= {
 
279
  DRIZZLE_SYSVAR(enable),
 
280
  DRIZZLE_SYSVAR(ident),
 
281
  DRIZZLE_SYSVAR(facility),
 
282
  DRIZZLE_SYSVAR(priority),
 
283
  DRIZZLE_SYSVAR(threshold_slow),
 
284
  DRIZZLE_SYSVAR(threshold_big_resultset),
 
285
  DRIZZLE_SYSVAR(threshold_big_examined),
 
286
  NULL
 
287
};
 
288
 
 
289
drizzle_declare_plugin(logging_syslog)
 
290
{
 
291
  DRIZZLE_LOGGER_PLUGIN,
 
292
  "logging_syslog",
 
293
  "0.2",
 
294
  "Mark Atwood <mark@fallenpegasus.com>",
 
295
  N_("Log to syslog"),
 
296
  PLUGIN_LICENSE_GPL,
 
297
  logging_syslog_plugin_init,
 
298
  logging_syslog_plugin_deinit,
 
299
  NULL,   /* status variables */
 
300
  logging_syslog_system_variables,
 
301
  NULL
 
302
}
 
303
drizzle_declare_plugin_end;