~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_syslog/logging_syslog.cc

Re-org'd the replication stuff into slots.

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