~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008 Mark Atwood
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/* need to define DRIZZLE_SERVER to get inside the Session */
#define DRIZZLE_SERVER 1
#include <drizzled/server_includes.h>
#include <drizzled/plugin_logging.h>
#include <drizzled/gettext.h>
#include <drizzled/session.h>

/* todo, make this dynamic as needed */
#define MAX_MSG_LEN (32*1024)

static char* logging_query_filename= NULL;
static bool logging_query_enable= true;
static ulong logging_query_enable_time= 0;
static ulong logging_query_threshold_big_resultset= 0;

static int fd= -1;

// copied from drizzled/sql_parse.cc
const LEX_STRING command_name[]={
  { C_STRING_WITH_LEN("Sleep") },
  { C_STRING_WITH_LEN("Quit") },
  { C_STRING_WITH_LEN("InitDB") },
  { C_STRING_WITH_LEN("Query") },
  { C_STRING_WITH_LEN("FieldList") },
  { C_STRING_WITH_LEN("CreateDB") },
  { C_STRING_WITH_LEN("DropDB") },
  { C_STRING_WITH_LEN("Refresh") },
  { C_STRING_WITH_LEN("Shutdown") },
  { C_STRING_WITH_LEN("Processlist") },
  { C_STRING_WITH_LEN("Connect") },
  { C_STRING_WITH_LEN("Kill") },
  { C_STRING_WITH_LEN("Ping") },
  { C_STRING_WITH_LEN("Time") },
  { C_STRING_WITH_LEN("ChangeUser") },
  { C_STRING_WITH_LEN("BinlogDump") },
  { C_STRING_WITH_LEN("ConnectOut") },
  { C_STRING_WITH_LEN("RegisterSlave") },
  { C_STRING_WITH_LEN("SetOption") },
  { C_STRING_WITH_LEN("Daemon") },
  { C_STRING_WITH_LEN("Error") }
};


/* stolen from mysys/my_getsystime 
   until the Session has a good utime "now" we can use
   will have to use this instead */

#include <sys/time.h>
static uint64_t get_microtime()
{
#if defined(HAVE_GETHRTIME)
  return gethrtime()/1000;
#else
  uint64_t newtime;
  struct timeval t;
  /*
    The following loop is here because gettimeofday may fail on some systems
  */
  while (gettimeofday(&t, NULL) != 0) {}
  newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
  return newtime;
#endif  /* defined(HAVE_GETHRTIME) */
}

/* we could just not have a pre entrypoint at all,
   and have logging_pre == NULL
   but we have this here for the sake of being an example */
bool logging_query_func_pre (Session *session __attribute__((unused)))
{
  return false;
}

bool logging_query_func_post (Session *session)
{
  char msgbuf[MAX_MSG_LEN];
  int msgbuf_len= 0;
  int wrv;

  if (fd < 0) return false;

  assert(session != NULL);

  /*
    here is some time stuff from class Session
      uint64_t connect_utime;
        todo, looks like this isnt being set
	we could store the time this plugin was loaded
	but that would just be a dumb workaround
      uint64_t start_utime;
      uint64_t utime_after_lock;
      uint64_t current_utime();
        todo, cant get to because of namemangling
  */

  /* todo, the Session should have a "utime command completed" inside
     itself, so be more accurate, and so plugins dont have to keep
     calling current_utime, which can be slow */
  uint64_t t_mark= get_microtime();

  msgbuf_len=
    snprintf(msgbuf, MAX_MSG_LEN,
             "thread_id=%ld query_id=%ld"
             " t_connect=%lld t_start=%lld t_lock=%lld"
             " command=%.*s"
             " rows_sent=%ld rows_examined=%u\n"
             " db=\"%.*s\" query=\"%.*s\"\n",
             (unsigned long) session->thread_id,
             (unsigned long) session->query_id,
             (unsigned long long)(t_mark - session->connect_utime),
             (unsigned long long)(t_mark - session->start_utime),
             (unsigned long long)(t_mark - session->utime_after_lock),
             (uint32_t)command_name[session->command].length,
             command_name[session->command].str,
             (unsigned long) session->sent_row_count,
             (uint32_t) session->examined_row_count,
             session->db_length, session->db,
             session->query_length, session->query);
  /* a single write has a OS level thread lock
     so there is no need to have mutexes guarding this write,
  */
  wrv= write(fd, msgbuf, msgbuf_len);
  assert(wrv == msgbuf_len);

  return false;
}

static int logging_query_plugin_init(void *p)
{
  logging_t *l= (logging_t *) p;

  if (logging_query_filename == NULL)
  {
    /* no destination filename was specified via system variables
       return now, dont set the callback pointers 
    */
    return 0;
  }

  fd= open(logging_query_filename, O_WRONLY | O_APPEND | O_CREAT,
           S_IRUSR|S_IWUSR);
  if (fd < 0) 
  {
    sql_print_error(_("fail open() fn=%s er=%s\n"),
		    logging_query_filename,
		    strerror(errno));

    /* todo
       we should return an error here, so the plugin doesnt load
       but this causes Drizzle to crash
       so until that is fixed,
       just return a success,
       but leave the function pointers as NULL and the fd as -1
    */
    return 0;
  }

  l->logging_pre= logging_query_func_pre;
  l->logging_post= logging_query_func_post;

  return 0;
}

static int logging_query_plugin_deinit(void *p)
{
  logging_st *l= (logging_st *) p;

  if (fd >= 0) 
  {
    close(fd);
    fd= -1;
  }

  l->logging_pre= NULL;
  l->logging_post= NULL;

  return 0;
}

static DRIZZLE_SYSVAR_STR(
  filename,
  logging_query_filename,
  PLUGIN_VAR_READONLY,
  N_("File to log to"),
  NULL, /* check func */
  NULL, /* update func*/
  NULL /* default */);

static DRIZZLE_SYSVAR_BOOL(
  enable,
  logging_query_enable,
  PLUGIN_VAR_NOCMDARG,
  N_("Enable logging"),
  NULL, /* check func */
  NULL, /* update func */
  true /* default */);

static DRIZZLE_SYSVAR_ULONG(
  enable_time,
  logging_query_enable_time,
  PLUGIN_VAR_OPCMDARG,
  N_("Disable after this many seconds. Zero for forever"),
  NULL, /* check func */
  NULL, /* update func */
  0, /* default */
  0, /* min */
  ULONG_MAX, /* max */
  0 /* blksiz */);

#ifdef NOT_YET
static DRIZZLE_SYSVAR_ULONG(
  threshhold_slow,
  logging_query_threshold_slow,
  PLUGIN_VAR_OPCMDARG,
  N_("Threshold for logging slow queries, in microseconds"),
  NULL, /* check func */
  NULL, /* update func */
  0, /* default */
  0, /* min */
  ULONG_MAX, /* max */
  0 /* blksiz */);
#endif

static DRIZZLE_SYSVAR_ULONG(
  threshold_big_resultset,
  logging_query_threshold_big_resultset,
  PLUGIN_VAR_OPCMDARG,
  N_("Threshold for logging big queries, for rows returned"),
  NULL, /* check func */
  NULL, /* update func */
  0, /* default */
  0, /* min */
  ULONG_MAX, /* max */
  0 /* blksiz */);

#ifdef NOT_YET
static DRIZZLE_SYSVAR_ULONG(
  threshhold_big_examined,
  logging_query_threshold_big_examined,
  PLUGIN_VAR_OPCMDARG,
  N_("Threshold for logging big queries, for rows examined"),
  NULL, /* check func */
  NULL, /* update func */
  0, /* default */
  0, /* min */
  ULONG_MAX, /* max */
  0 /* blksiz */);
#endif

static struct st_mysql_sys_var* logging_query_system_variables[]= {
  DRIZZLE_SYSVAR(filename),
  DRIZZLE_SYSVAR(enable),
  DRIZZLE_SYSVAR(enable_time),
  DRIZZLE_SYSVAR(threshold_big_resultset),
  NULL
};

mysql_declare_plugin(logging_query)
{
  DRIZZLE_LOGGER_PLUGIN,
  "logging_query",
  "0.1",
  "Mark Atwood <mark@fallenpegasus.com>",
  N_("Log queries to a file"),
  PLUGIN_LICENSE_GPL,
  logging_query_plugin_init,
  logging_query_plugin_deinit,
  NULL,   /* status variables */
  logging_query_system_variables,
  NULL
}
mysql_declare_plugin_end;