~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_syslog/logging_syslog.cc

  • Committer: Stewart Smith
  • Date: 2009-03-04 22:49:53 UTC
  • mto: (910.4.2 sparc) (908.3.6 work)
  • mto: This revision was merged to the branch mainline in revision 912.
  • Revision ID: stewart@flamingspork.com-20090304224953-b2ow237kc1bkp0o0
for getopt, replace GET_ULONG with GET_UINT32.

Don't replace for sql variables (yet). instead just indicated the intense source of fail with GET_ULONG_IS_FAIL.

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