~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_syslog/logging_syslog.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-03-21 01:02:23 UTC
  • mto: (960.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 961.
  • Revision ID: osullivan.padraig@gmail.com-20090321010223-j8cph7eeyt1u3xol
Fixed function object to ensure it correctly returns a boolean type since
memcmp returns an integer. Added some more comments.

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