~drizzle-trunk/drizzle/development

771.1.1 by Mark Atwood
add syslog based logging plugin
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1010 by Brian Aker
Replacing Sun employee copyright headers (aka... anything done by a Sun
4
 *  Copyright (C) 2009 Sun Microsystems
771.1.1 by Mark Atwood
add syslog based logging plugin
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
20
#include "config.h"
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
21
#include <drizzled/plugin/logging.h>
771.1.1 by Mark Atwood
add syslog based logging plugin
22
#include <drizzled/gettext.h>
23
#include <drizzled/session.h>
24
779.6.1 by Monty Taylor
Only use our helper file on solaris so that other platforms are happy.
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
812.1.6 by Mark Atwood
logging to syslog
32
771.1.1 by Mark Atwood
add syslog based logging plugin
33
#include <stdarg.h>
34
812.1.6 by Mark Atwood
logging to syslog
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
771.1.1 by Mark Atwood
add syslog based logging plugin
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
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
62
class Logging_syslog: public drizzled::plugin::Logging
771.1.1 by Mark Atwood
add syslog based logging plugin
63
{
1039.4.9 by Mark Atwood
make logging_syslog be more C++'ish
64
65
  int syslog_facility;
66
  int syslog_priority;
67
968.2.33 by Monty Taylor
Removed plugin_foreach from logging_handler.
68
public:
69
1130.1.1 by Monty Taylor
Merged in plugin-slot-reorg patches.
70
  Logging_syslog()
71
    : drizzled::plugin::Logging("Logging_syslog"),
72
      syslog_facility(-1), syslog_priority(-1)
1039.4.9 by Mark Atwood
make logging_syslog be more C++'ish
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
  }
958.1.1 by Monty Taylor
Made logging plugin class based.
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
  
1220.1.9 by Brian Aker
Remove char *db from session, and replaces it with std::string.
139
    const char *dbs= session->db.empty() ? "" : session->db.c_str();
958.1.1 by Monty Taylor
Made logging plugin class based.
140
  
1120.1.2 by Diego Medina
Use accessors when available.
141
    const char *qys= (session->getQueryString()) ? session->getQueryString() : "";
958.1.1 by Monty Taylor
Made logging plugin class based.
142
    int qyl= 0;
143
    if (qys)
1120.1.2 by Diego Medina
Use accessors when available.
144
      qyl= session->getQueryLength();
958.1.1 by Monty Taylor
Made logging plugin class based.
145
    
146
    syslog(syslog_priority,
147
           "thread_id=%ld query_id=%ld"
148
           " db=\"%.*s\""
149
           " query=\"%.*s\""
150
           " command=\"%.*s\""
151
           " t_connect=%lld t_start=%lld t_lock=%lld"
1039.4.3 by Mark Atwood
add new info to syslog logging
152
           " rows_sent=%ld rows_examined=%ld"
153
           " tmp_table=%ld total_warn_count=%ld\n",
958.1.1 by Monty Taylor
Made logging plugin class based.
154
           (unsigned long) session->thread_id,
1120.1.2 by Diego Medina
Use accessors when available.
155
           (unsigned long) session->getQueryId(),
1220.1.9 by Brian Aker
Remove char *db from session, and replaces it with std::string.
156
           (int)session->db.length(), dbs,
958.1.1 by Monty Taylor
Made logging plugin class based.
157
           qyl, qys,
158
           (int) command_name[session->command].length,
159
           command_name[session->command].str,
1055.3.6 by Jay Pipes
Adds public getConnectMicroseconds() call to Session object. Protects connect_microseconds member variable and sets it correctly. Corrects logging_xxx plugins to use getConnectMicroseconds() API call properly. Fixes Bug #402855
160
           (unsigned long long) (t_mark - session->getConnectMicroseconds()),
958.1.1 by Monty Taylor
Made logging plugin class based.
161
           (unsigned long long) (t_mark - session->start_utime),
162
           (unsigned long long) (t_mark - session->utime_after_lock),
163
           (unsigned long) session->sent_row_count,
1039.4.3 by Mark Atwood
add new info to syslog logging
164
           (unsigned long) session->examined_row_count,
165
           (unsigned long) session->tmp_table,
166
           (unsigned long) session->total_warn_count);
958.1.1 by Monty Taylor
Made logging plugin class based.
167
  
168
    return false;
169
  }
170
};
771.1.1 by Mark Atwood
add syslog based logging plugin
171
971.1.51 by Monty Taylor
New-style plugin registration now works.
172
static Logging_syslog *handler= NULL;
173
1110.1.5 by Monty Taylor
Renamed PluginRegistry to plugin::Registry.
174
static int logging_syslog_plugin_init(drizzled::plugin::Registry &registry)
771.1.1 by Mark Atwood
add syslog based logging plugin
175
{
971.1.51 by Monty Taylor
New-style plugin registration now works.
176
  handler= new Logging_syslog();
1130.1.8 by Monty Taylor
Added polymorphic add/remove methods around slot add/remove methods.
177
  registry.add(handler);
771.1.1 by Mark Atwood
add syslog based logging plugin
178
179
  return 0;
180
}
181
1110.1.5 by Monty Taylor
Renamed PluginRegistry to plugin::Registry.
182
static int logging_syslog_plugin_deinit(drizzled::plugin::Registry &registry)
771.1.1 by Mark Atwood
add syslog based logging plugin
183
{
1130.1.8 by Monty Taylor
Added polymorphic add/remove methods around slot add/remove methods.
184
  registry.remove(handler);
958.1.1 by Monty Taylor
Made logging plugin class based.
185
  delete handler;
771.1.1 by Mark Atwood
add syslog based logging plugin
186
187
  return 0;
188
}
189
812.1.6 by Mark Atwood
logging to syslog
190
static DRIZZLE_SYSVAR_BOOL(
191
  enable,
192
  sysvar_logging_syslog_enable,
193
  PLUGIN_VAR_NOCMDARG,
1039.4.9 by Mark Atwood
make logging_syslog be more C++'ish
194
  N_("Enable logging to syslog"),
812.1.6 by Mark Atwood
logging to syslog
195
  NULL, /* check func */
196
  NULL, /* update func */
197
  false /* default */);
198
199
static DRIZZLE_SYSVAR_STR(
200
  ident,
201
  sysvar_logging_syslog_ident,
202
  PLUGIN_VAR_READONLY,
203
  N_("Syslog Ident"),
204
  NULL, /* check func */
205
  NULL, /* update func*/
206
  "drizzled" /* default */);
207
208
static DRIZZLE_SYSVAR_STR(
209
  facility,
210
  sysvar_logging_syslog_facility,
211
  PLUGIN_VAR_READONLY,
212
  N_("Syslog Facility"),
213
  NULL, /* check func */
214
  NULL, /* update func*/
215
  "local0" /* default */);  // local0 is what PostGreSQL uses by default
216
217
static DRIZZLE_SYSVAR_STR(
218
  priority,
219
  sysvar_logging_syslog_priority,
220
  PLUGIN_VAR_READONLY,
221
  N_("Syslog Priority"),
222
  NULL, /* check func */
223
  NULL, /* update func*/
224
  "info" /* default */);
225
226
static DRIZZLE_SYSVAR_ULONG(
227
  threshold_slow,
228
  sysvar_logging_syslog_threshold_slow,
229
  PLUGIN_VAR_OPCMDARG,
230
  N_("Threshold for logging slow queries, in microseconds"),
231
  NULL, /* check func */
232
  NULL, /* update func */
233
  0, /* default */
234
  0, /* min */
235
  ULONG_MAX, /* max */
236
  0 /* blksiz */);
237
238
static DRIZZLE_SYSVAR_ULONG(
239
  threshold_big_resultset,
240
  sysvar_logging_syslog_threshold_big_resultset,
241
  PLUGIN_VAR_OPCMDARG,
242
  N_("Threshold for logging big queries, for rows returned"),
243
  NULL, /* check func */
244
  NULL, /* update func */
245
  0, /* default */
246
  0, /* min */
247
  ULONG_MAX, /* max */
248
  0 /* blksiz */);
249
250
static DRIZZLE_SYSVAR_ULONG(
251
  threshold_big_examined,
252
  sysvar_logging_syslog_threshold_big_examined,
253
  PLUGIN_VAR_OPCMDARG,
254
  N_("Threshold for logging big queries, for rows examined"),
255
  NULL, /* check func */
256
  NULL, /* update func */
257
  0, /* default */
258
  0, /* min */
259
  ULONG_MAX, /* max */
260
  0 /* blksiz */);
261
1228.1.5 by Monty Taylor
Merged in some naming things.
262
static drizzle_sys_var* logging_syslog_system_variables[]= {
812.1.6 by Mark Atwood
logging to syslog
263
  DRIZZLE_SYSVAR(enable),
264
  DRIZZLE_SYSVAR(ident),
265
  DRIZZLE_SYSVAR(facility),
266
  DRIZZLE_SYSVAR(priority),
267
  DRIZZLE_SYSVAR(threshold_slow),
268
  DRIZZLE_SYSVAR(threshold_big_resultset),
269
  DRIZZLE_SYSVAR(threshold_big_examined),
771.1.1 by Mark Atwood
add syslog based logging plugin
270
  NULL
271
};
272
1228.1.5 by Monty Taylor
Merged in some naming things.
273
DRIZZLE_DECLARE_PLUGIN
771.1.1 by Mark Atwood
add syslog based logging plugin
274
{
1241.10.2 by Monty Taylor
Added support for embedding the drizzle version number in the plugin file.
275
  DRIZZLE_VERSION_ID,
771.1.1 by Mark Atwood
add syslog based logging plugin
276
  "logging_syslog",
812.1.6 by Mark Atwood
logging to syslog
277
  "0.2",
771.1.1 by Mark Atwood
add syslog based logging plugin
278
  "Mark Atwood <mark@fallenpegasus.com>",
279
  N_("Log to syslog"),
280
  PLUGIN_LICENSE_GPL,
281
  logging_syslog_plugin_init,
282
  logging_syslog_plugin_deinit,
283
  NULL,   /* status variables */
284
  logging_syslog_system_variables,
285
  NULL
286
}
1228.1.5 by Monty Taylor
Merged in some naming things.
287
DRIZZLE_DECLARE_PLUGIN_END;