~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-02-02 23:10:18 UTC
  • mfrom: (779.3.40 devel)
  • Revision ID: brian@tangent.org-20090202231018-zlp0hka6kgwy1vfy
Merge from Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
#include <drizzled/gettext.h>
23
23
#include <drizzled/session.h>
24
24
 
25
 
#include <syslog.h>
 
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
 
26
33
#include <stdarg.h>
27
34
 
28
 
// copied from drizzled/sql_parse.cc
29
 
const LEX_STRING command_name[]={
30
 
  { C_STRING_WITH_LEN("Sleep") },
31
 
  { C_STRING_WITH_LEN("Quit") },
32
 
  { C_STRING_WITH_LEN("InitDB") },
33
 
  { C_STRING_WITH_LEN("Query") },
34
 
  { C_STRING_WITH_LEN("FieldList") },
35
 
  { C_STRING_WITH_LEN("CreateDB") },
36
 
  { C_STRING_WITH_LEN("DropDB") },
37
 
  { C_STRING_WITH_LEN("Refresh") },
38
 
  { C_STRING_WITH_LEN("Shutdown") },
39
 
  { C_STRING_WITH_LEN("Processlist") },
40
 
  { C_STRING_WITH_LEN("Connect") },
41
 
  { C_STRING_WITH_LEN("Kill") },
42
 
  { C_STRING_WITH_LEN("Ping") },
43
 
  { C_STRING_WITH_LEN("Time") },
44
 
  { C_STRING_WITH_LEN("ChangeUser") },
45
 
  { C_STRING_WITH_LEN("BinlogDump") },
46
 
  { C_STRING_WITH_LEN("ConnectOut") },
47
 
  { C_STRING_WITH_LEN("RegisterSlave") },
48
 
  { C_STRING_WITH_LEN("SetOption") },
49
 
  { C_STRING_WITH_LEN("Daemon") },
50
 
  { C_STRING_WITH_LEN("Error") }
51
 
};
 
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;
52
45
 
53
46
/* stolen from mysys/my_getsystime
54
47
   until the Session has a good utime "now" we can use
73
66
{
74
67
  assert(session != NULL);
75
68
 
76
 
  /* skip returning field list, too verbose */
77
 
  if (session->command == COM_FIELD_LIST) return false;
 
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 */
78
87
 
79
88
  uint64_t t_mark= get_microtime();
80
89
 
81
 
  syslog(LOG_INFO, "thread_id=%ld query_id=%ld"
 
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\""
82
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\""
83
129
         " command=%.*s"
84
 
         " rows_sent=%ld rows_examined=%u\n"
85
 
         " db=\"%.*s\" query=\"%.*s\"\n",
 
130
         " t_connect=%lld t_start=%lld t_lock=%lld"
 
131
         " rows_sent=%ld rows_examined=%ld\n",
86
132
         (unsigned long) session->thread_id,
87
133
         (unsigned long) session->query_id,
88
 
         (unsigned long long)(t_mark - session->connect_utime),
89
 
         (unsigned long long)(t_mark - session->start_utime),
90
 
         (unsigned long long)(t_mark - session->utime_after_lock),
91
 
         (uint32_t)command_name[session->command].length,
92
 
         command_name[session->command].str,
93
 
         (unsigned long) session->sent_row_count,
94
 
         (uint32_t) session->examined_row_count,
95
134
         session->db_length, session->db,
96
 
         session->query_length, session->query);
 
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
97
146
 
98
147
  return false;
99
148
}
102
151
{
103
152
  logging_t *l= (logging_t *) p;
104
153
 
105
 
  openlog("drizzled", LOG_PID, LOG_LOCAL3);
 
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);
106
190
 
107
191
  l->logging_pre= NULL;
108
192
  l->logging_post= logging_syslog_func_post;
120
204
  return 0;
121
205
}
122
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
 
123
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),
124
287
  NULL
125
288
};
126
289
 
128
291
{
129
292
  DRIZZLE_LOGGER_PLUGIN,
130
293
  "logging_syslog",
131
 
  "0.1",
 
294
  "0.2",
132
295
  "Mark Atwood <mark@fallenpegasus.com>",
133
296
  N_("Log to syslog"),
134
297
  PLUGIN_LICENSE_GPL,