~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_syslog/logging_syslog.cc

  • Committer: Brian Aker
  • Date: 2009-06-05 23:10:06 UTC
  • mto: This revision was merged to the branch mainline in revision 1055.
  • Revision ID: brian@gaz-20090605231006-01nyw7pfpj2z2v8p
Remove guts in parser for LOCK TABLE.

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