~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-04-25 19:24:49 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 1003.
  • Revision ID: mordred@inaugust.com-20090425192449-0htujbt2r9jzupn5
Moved heap.

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